diff options
| author | Shulhan <ms@kilabit.info> | 2019-03-12 02:43:33 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-03-12 02:43:33 +0700 |
| commit | 085f158b6561cf511d6ad125dd01cebcfbf7d96f (patch) | |
| tree | 9819719025206d9655bcab71d9bd0083d56448ca | |
| parent | 23f280d1adeb70cf4434e3c58859d466bc76239c (diff) | |
| download | pakakeh.go-085f158b6561cf511d6ad125dd01cebcfbf7d96f.tar.xz | |
websocket: unexport internal methods and remove unused methods
Some of the methods in ClientManager and Frames should be only accessed
by internal server, not by implementor.
| -rw-r--r-- | lib/websocket/client_test.go | 12 | ||||
| -rw-r--r-- | lib/websocket/clientmanager.go | 20 | ||||
| -rw-r--r-- | lib/websocket/frames.go | 25 | ||||
| -rw-r--r-- | lib/websocket/frames_test.go | 57 | ||||
| -rw-r--r-- | lib/websocket/server.go | 24 |
5 files changed, 38 insertions, 100 deletions
diff --git a/lib/websocket/client_test.go b/lib/websocket/client_test.go index 76bc9881..dd319a12 100644 --- a/lib/websocket/client_test.go +++ b/lib/websocket/client_test.go @@ -102,7 +102,7 @@ func TestClientPing(t *testing.T) { test.Assert(t, "resp", exp, resp, true) frames := Unpack(resp) - if frames.IsClosed() { + if frames.isClosed() { testClient.SendClose(false) } @@ -180,11 +180,11 @@ func TestClientText(t *testing.T) { } frames := Unpack(resp) - if frames.IsClosed() { + if frames.isClosed() { t.Log("sending close...") testClient.SendClose(false) } else { - got := frames.Payload() + got := frames.payload() test.Assert(t, "TestClientText len", len(exp), len(got), true) test.Assert(t, "TestClientText", exp, got, true) } @@ -306,7 +306,7 @@ func TestClientFragmentation(t *testing.T) { test.Assert(t, "res", c.exp, res, true) frames := Unpack(res) - if frames.IsClosed() { + if frames.isClosed() { testClient.SendClose(false) break } @@ -447,7 +447,7 @@ func TestClientSendBin(t *testing.T) { test.Assert(t, "SendBin response", exp, frames.v[0], true) - if frames.IsClosed() { + if frames.isClosed() { testClient.SendClose(false) } @@ -492,7 +492,7 @@ func TestClientSendPing(t *testing.T) { test.Assert(t, "SendPing response", exp, frames.v[0], true) - if frames.IsClosed() { + if frames.isClosed() { t.Log("TestClientSendPing closing ...") testClient.SendClose(false) } diff --git a/lib/websocket/clientmanager.go b/lib/websocket/clientmanager.go index 7e3f9961..129a98cf 100644 --- a/lib/websocket/clientmanager.go +++ b/lib/websocket/clientmanager.go @@ -57,9 +57,9 @@ func newClientManager() *ClientManager { } // -// AddFrame add a frame as part of continuous frame on a client connection. +// addFrame add a frame as part of continuous frame on a client connection. // -func (cls *ClientManager) AddFrame(conn int, frame *Frame) { +func (cls *ClientManager) addFrame(conn int, frame *Frame) { cls.Lock() frames, ok := cls.frames[conn] if !ok { @@ -145,9 +145,9 @@ func (cls *ClientManager) Context(conn int) (ctx context.Context) { } // -// Frame return an active frame on a client connection. +// getFrame return an active frame on a client connection. // -func (cls *ClientManager) Frame(conn int) (frame *Frame, ok bool) { +func (cls *ClientManager) getFrame(conn int) (frame *Frame, ok bool) { cls.Lock() frame, ok = cls.frame[conn] cls.Unlock() @@ -155,9 +155,9 @@ func (cls *ClientManager) Frame(conn int) (frame *Frame, ok bool) { } // -// Frames return continuous frames on behalf of connection. +// getFrames return continuous frames on behalf of connection. // -func (cls *ClientManager) Frames(conn int) (frames *Frames, ok bool) { +func (cls *ClientManager) getFrames(conn int) (frames *Frames, ok bool) { cls.Lock() frames, ok = cls.frames[conn] cls.Unlock() @@ -165,10 +165,10 @@ func (cls *ClientManager) Frames(conn int) (frames *Frames, ok bool) { } // -// SetFrame set the active, chopped frame on client connection. If frame is +// setFrame set the active, chopped frame on client connection. If frame is // nil, it will delete the stored frame in connection. // -func (cls *ClientManager) SetFrame(conn int, frame *Frame) { +func (cls *ClientManager) setFrame(conn int, frame *Frame) { cls.Lock() if frame == nil { delete(cls.frame, conn) @@ -179,10 +179,10 @@ func (cls *ClientManager) SetFrame(conn int, frame *Frame) { } // -// SetFrames set continuous frames on client connection. If frames is nil it +// setFrames set continuous frames on client connection. If frames is nil it // will clear the stored frames. // -func (cls *ClientManager) SetFrames(conn int, frames *Frames) { +func (cls *ClientManager) setFrames(conn int, frames *Frames) { cls.Lock() if frames == nil { delete(cls.frames, conn) diff --git a/lib/websocket/frames.go b/lib/websocket/frames.go index 1fa40df2..b54346b9 100644 --- a/lib/websocket/frames.go +++ b/lib/websocket/frames.go @@ -63,19 +63,9 @@ func (frames *Frames) Append(f *Frame) { } // -// Get frame at specific index or nil if index out of range. +// isClosed will return true if one of the frame is control CLOSE frame. // -func (frames *Frames) Get(x int) *Frame { - if x < 0 || x >= len(frames.v) { - return nil - } - return frames.v[x] -} - -// -// IsClosed will return true if one of the frame is control CLOSE frame. -// -func (frames *Frames) IsClosed() bool { +func (frames *Frames) isClosed() bool { if len(frames.v) == 0 { return false } @@ -88,13 +78,6 @@ func (frames *Frames) IsClosed() bool { } // -// Len return the number of frame. -// -func (frames *Frames) Len() int { - return len(frames.v) -} - -// // Opcode return the operation code of the first frame. // func (frames *Frames) Opcode() opcode { @@ -105,7 +88,7 @@ func (frames *Frames) Opcode() opcode { } // -// Payload return the concatenation of continuous data frame's payload. +// payload return the concatenation of continuous data frame's payload. // // The first frame must be a data frame, either text or binary, otherwise it // will be considered empty payload, even if frames list is not empty. @@ -113,7 +96,7 @@ func (frames *Frames) Opcode() opcode { // Any control CLOSE frame of frame with fin set will considered the last // frame. // -func (frames *Frames) Payload() (payload []byte) { +func (frames *Frames) payload() (payload []byte) { if len(frames.v) == 0 { return } diff --git a/lib/websocket/frames_test.go b/lib/websocket/frames_test.go index 3934e2ae..0912a491 100644 --- a/lib/websocket/frames_test.go +++ b/lib/websocket/frames_test.go @@ -159,7 +159,7 @@ func TestFrameUnpack(t *testing.T) { gots := Unpack(c.in) - if gots != nil && gots.Len() > 0 { + if gots != nil && len(gots.v) > 0 { test.Assert(t, "", c.exp, gots.v[0], true) } } @@ -192,54 +192,9 @@ func TestFramesAppend(t *testing.T) { frames.Append(c.f) - test.Assert(t, "Frames.Len", c.expLen, frames.Len(), true) + test.Assert(t, "Frames.Len", c.expLen, len(frames.v), true) test.Assert(t, "Frames.payload", c.expPayload, - string(frames.Payload()), true) - } -} - -func TestFramesGet(t *testing.T) { - frames := &Frames{} - - f0 := &Frame{ - opcode: opcodeText, - payload: []byte("A"), - } - f1 := &Frame{ - opcode: opcodeText, - payload: []byte("B"), - } - f2 := &Frame{ - opcode: opcodeText, - payload: []byte("C"), - } - - frames.Append(f0) - frames.Append(f1) - frames.Append(f2) - - cases := []struct { - desc string - x int - exp *Frame - }{{ - desc: "With negative index", - x: -1, - }, { - desc: "With out of range index", - x: frames.Len(), - }, { - desc: "With valid index", - x: 0, - exp: f0, - }} - - for _, c := range cases { - t.Log(c.desc) - - got := frames.Get(c.x) - - test.Assert(t, "Frames.Get", c.exp, got, true) + string(frames.payload()), true) } } @@ -274,8 +229,8 @@ func TestFramesIsClosed(t *testing.T) { for _, c := range cases { t.Log(c.desc) - got := c.frames.IsClosed() - test.Assert(t, "Frames.IsClosed", c.exp, got, true) + got := c.frames.isClosed() + test.Assert(t, "Frames.isClosed", c.exp, got, true) } } @@ -355,7 +310,7 @@ func TestFramesPayload(t *testing.T) { for _, c := range cases { t.Log(c.desc) - got := c.fs.Payload() + got := c.fs.payload() test.Assert(t, "Frames.payload", c.exp, string(got), true) } diff --git a/lib/websocket/server.go b/lib/websocket/server.go index f50571fa..e4f9434b 100644 --- a/lib/websocket/server.go +++ b/lib/websocket/server.go @@ -331,8 +331,8 @@ func (serv *Server) upgrader() { // CLOSE frame or contains invalid UTF-8 text on data TEXT frame. // func (serv *Server) handleChopped(x, conn int, packet []byte) (rest []byte, isClosing bool) { - frame, _ := serv.Clients.Frame(conn) - frames, _ := serv.Clients.Frames(conn) + frame, _ := serv.Clients.getFrame(conn) + frames, _ := serv.Clients.getFrames(conn) if frame == nil { return packet, false @@ -342,7 +342,7 @@ func (serv *Server) handleChopped(x, conn int, packet []byte) (rest []byte, isCl if len(frame.chopped) > 0 { packet = frame.continueUnpack(packet) if len(packet) == 0 { - serv.Clients.SetFrame(conn, frame) + serv.Clients.setFrame(conn, frame) serv.epollRegisterRead(x, conn) return nil, false } @@ -369,7 +369,7 @@ func (serv *Server) handleChopped(x, conn int, packet []byte) (rest []byte, isCl frame.payload = append(frame.payload, packet...) if uint64(len(frame.payload)) < frame.len { // We got frame with unfinished payload. - serv.Clients.SetFrame(conn, frame) + serv.Clients.setFrame(conn, frame) serv.epollRegisterRead(x, conn) return rest, false } @@ -383,7 +383,7 @@ func (serv *Server) handleChopped(x, conn int, packet []byte) (rest []byte, isCl } // We got frame with finished payload but with unfinished // frames. - serv.Clients.AddFrame(conn, frame) + serv.Clients.addFrame(conn, frame) serv.epollRegisterRead(x, conn) return rest, false } @@ -401,7 +401,7 @@ func (serv *Server) handleChopped(x, conn int, packet []byte) (rest []byte, isCl frame = serv.Clients.finFrames(conn, frame) } else { - serv.Clients.SetFrame(conn, nil) + serv.Clients.setFrame(conn, nil) } if frame.masked != frameIsMasked { @@ -488,7 +488,7 @@ func (serv *Server) handleChopped(x, conn int, packet []byte) (rest []byte, isCl // (RFC 6455 Section 5.4 Page 34) // func (serv *Server) handleFragment(conn int, req *Frame) (isInvalid bool) { - frames, ok := serv.Clients.Frames(conn) + frames, ok := serv.Clients.getFrames(conn) if debug.Value >= 3 { log.Printf("websocket: Server.handleFragment: frame: {fin:%d opcode:%d masked:%d len:%d, payload.len:%d}\n", @@ -514,18 +514,18 @@ func (serv *Server) handleFragment(conn int, req *Frame) (isInvalid bool) { if req.fin == 0 { if uint64(len(req.payload)) < req.len { - serv.Clients.SetFrame(conn, req) + serv.Clients.setFrame(conn, req) } else { frames.Append(req) - serv.Clients.SetFrame(conn, nil) - serv.Clients.SetFrames(conn, frames) + serv.Clients.setFrame(conn, nil) + serv.Clients.setFrames(conn, frames) } return false } if !ok && uint64(len(req.payload)) < req.len { // Finished frame with unfinished payload. - serv.Clients.SetFrame(conn, req) + serv.Clients.setFrame(conn, req) return false } @@ -795,7 +795,7 @@ func (serv *Server) reader() { isClosing = false for _, frame := range frames.v { if len(frame.chopped) > 0 { - serv.Clients.SetFrame(conn, frame) + serv.Clients.setFrame(conn, frame) continue } |
