From f3f3042c816bbb3c846ce4cc7f59f57021bd9058 Mon Sep 17 00:00:00 2001 From: Qian Wang Date: Mon, 31 Dec 2018 11:30:39 +0000 Subject: [PATCH] Remove redundant functions --- internal/multiplex/frameSorter.go | 1 + internal/multiplex/session.go | 35 +++++------------------------ internal/multiplex/switchboard.go | 6 ++--- internal/server/usermanager/user.go | 6 ----- 4 files changed, 9 insertions(+), 39 deletions(-) diff --git a/internal/multiplex/frameSorter.go b/internal/multiplex/frameSorter.go index c8124c6..8706b3d 100644 --- a/internal/multiplex/frameSorter.go +++ b/internal/multiplex/frameSorter.go @@ -114,6 +114,7 @@ func (s *Stream) recvNewFrame() { func (s *Stream) pushFrame(f *Frame) { if f.Closing == 1 { + // empty data indicates closing signal s.sortedBufCh <- []byte{} return } diff --git a/internal/multiplex/session.go b/internal/multiplex/session.go index 929c83b..649c7b8 100644 --- a/internal/multiplex/session.go +++ b/internal/multiplex/session.go @@ -94,16 +94,8 @@ func (sesh *Session) delStream(id uint32) { sesh.streamsM.Unlock() } -func (sesh *Session) isStream(id uint32) bool { - sesh.streamsM.RLock() - _, ok := sesh.streams[id] - sesh.streamsM.RUnlock() - return ok -} - -// If the stream has been closed and the triggering frame is a closing frame, -// we return nil -func (sesh *Session) getOrAddStream(id uint32, closingFrame bool) *Stream { +// either fetch an existing stream or instantiate a new stream and put it in the dict, and return it +func (sesh *Session) getStream(id uint32, closingFrame bool) *Stream { // it would have been neater to use defer Unlock(), however it gives // non-negligable overhead and this function is performance critical sesh.streamsM.Lock() @@ -113,6 +105,8 @@ func (sesh *Session) getOrAddStream(id uint32, closingFrame bool) *Stream { return stream } else { if closingFrame { + // If the stream has been closed and the current frame is a closing frame, + // we return nil sesh.streamsM.Unlock() return nil } else { @@ -126,24 +120,6 @@ func (sesh *Session) getOrAddStream(id uint32, closingFrame bool) *Stream { } } -func (sesh *Session) getStream(id uint32) *Stream { - sesh.streamsM.RLock() - ret := sesh.streams[id] - sesh.streamsM.RUnlock() - return ret -} - -// addStream is used when the remote opened a new stream and we got notified -func (sesh *Session) addStream(id uint32) *Stream { - stream := makeStream(id, sesh) - sesh.streamsM.Lock() - sesh.streams[id] = stream - sesh.streamsM.Unlock() - sesh.acceptCh <- stream - log.Printf("Adding stream %v\n", id) - return stream -} - func (sesh *Session) Close() error { // Because closing a closed channel causes panic sesh.suicide.Do(func() { close(sesh.die) }) @@ -153,13 +129,12 @@ func (sesh *Session) Close() error { // because stream.Close calls sesh.delStream, which locks the mutex. // so we need to implement a method of stream that closes the stream without calling // sesh.delStream - // This can also be seen in smux go stream.closeNoDelMap() delete(sesh.streams, id) } sesh.streamsM.Unlock() - sesh.sb.shutdown() + sesh.sb.closeAll() return nil } diff --git a/internal/multiplex/switchboard.go b/internal/multiplex/switchboard.go index 5732dd4..4f3bab1 100644 --- a/internal/multiplex/switchboard.go +++ b/internal/multiplex/switchboard.go @@ -129,14 +129,14 @@ func (sb *switchboard) removeConn(closing *connEnclave) { } // actively triggered by session.Close() -func (sb *switchboard) shutdown() { +func (sb *switchboard) closeAll() { for _, ce := range sb.ces { ce.remoteConn.Close() } } // deplex function costantly reads from a TCP connection, call deobfs and distribute it -// to the corresponding frame +// to the corresponding stream func (sb *switchboard) deplex(ce *connEnclave) { buf := make([]byte, 20480) for { @@ -170,7 +170,7 @@ func (sb *switchboard) deplex(ce *connEnclave) { // of the library. Maybe there's a race somewhere? I may eventually use another // method to encrypt the headers. xxHash isn't cryptographic afterall. - stream := sb.session.getOrAddStream(frame.StreamID, frame.Closing == 1) + stream := sb.session.getStream(frame.StreamID, frame.Closing == 1) // if the frame is telling us to close a closed stream // (this happens when ss-server and ss-local closes the stream // simutaneously), we don't do anything diff --git a/internal/server/usermanager/user.go b/internal/server/usermanager/user.go index 06efa02..ea3018a 100644 --- a/internal/server/usermanager/user.go +++ b/internal/server/usermanager/user.go @@ -67,12 +67,6 @@ func (u *User) updateInfo(uinfo UserInfo) { u.setExpiryTime(uinfo.ExpiryTime) } -func (u *User) PutSession(sessionID uint32, sesh *mux.Session) { - u.sessionsM.Lock() - u.sessions[sessionID] = sesh - u.sessionsM.Unlock() -} - func (u *User) DelSession(sessionID uint32) { u.sessionsM.Lock() delete(u.sessions, sessionID)