From ff7efe9f3bfc98f3f452727f7b6350a2bf2007fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 25 Nov 2020 18:36:33 +0100 Subject: [PATCH] socket -> websocket_peer. --- internal/session/session.go | 30 ++++++++--------- internal/types/session.go | 2 +- internal/types/websocket.go | 2 +- internal/websocket/handler/handler.go | 2 +- internal/websocket/manager.go | 6 ++-- internal/websocket/peer.go | 47 +++++++++++++++++++++++++++ internal/websocket/websocket.go | 47 --------------------------- 7 files changed, 68 insertions(+), 68 deletions(-) create mode 100644 internal/websocket/peer.go delete mode 100644 internal/websocket/websocket.go diff --git a/internal/session/session.go b/internal/session/session.go index e1e41a74..abf3f4f3 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -9,14 +9,14 @@ import ( ) type SessionCtx struct { - logger zerolog.Logger - id string - name string - admin bool - connected bool - manager *SessionManagerCtx - socket types.WebSocket - peer types.Peer + logger zerolog.Logger + id string + name string + admin bool + connected bool + manager *SessionManagerCtx + websocket_peer types.WebSocketPeer + peer types.Peer } func (session *SessionCtx) ID() string { @@ -43,8 +43,8 @@ func (session *SessionCtx) SetName(name string) { session.name = name } -func (session *SessionCtx) SetSocket(socket types.WebSocket) { - session.socket = socket +func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) { + session.websocket_peer = websocket_peer session.manager.emmiter.Emit("created", session) } @@ -59,7 +59,7 @@ func (session *SessionCtx) SetConnected(connected bool) { session.manager.emmiter.Emit("connected", session) } else { session.manager.emmiter.Emit("disconnected", session) - session.socket = nil + session.websocket_peer = nil // TODO: Refactor. _ = session.manager.Destroy(session.id) @@ -77,11 +77,11 @@ func (session *SessionCtx) Disconnect(reason string) error { } func (session *SessionCtx) Send(v interface{}) error { - if session.socket == nil { + if session.websocket_peer == nil { return nil } - return session.socket.Send(v) + return session.websocket_peer.Send(v) } func (session *SessionCtx) SignalAnswer(sdp string) error { @@ -93,8 +93,8 @@ func (session *SessionCtx) SignalAnswer(sdp string) error { } func (session *SessionCtx) destroy() error { - if session.socket != nil { - if err := session.socket.Destroy(); err != nil { + if session.websocket_peer != nil { + if err := session.websocket_peer.Destroy(); err != nil { return err } } diff --git a/internal/types/session.go b/internal/types/session.go index 3e3a0bc4..be775442 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -9,7 +9,7 @@ type Session interface { IsHost() bool Connected() bool SetName(name string) - SetSocket(socket WebSocket) + SetWebSocketPeer(websocket_peer WebSocketPeer) SetPeer(peer Peer) SetConnected(connected bool) Disconnect(reason string) error diff --git a/internal/types/websocket.go b/internal/types/websocket.go index 630c5d65..a55a9ee5 100644 --- a/internal/types/websocket.go +++ b/internal/types/websocket.go @@ -2,7 +2,7 @@ package types import "net/http" -type WebSocket interface { +type WebSocketPeer interface { Send(v interface{}) error Destroy() error } diff --git a/internal/websocket/handler/handler.go b/internal/websocket/handler/handler.go index 77bfb699..3f3abb61 100644 --- a/internal/websocket/handler/handler.go +++ b/internal/websocket/handler/handler.go @@ -40,7 +40,7 @@ type MessageHandlerCtx struct { locked bool } -func (h *MessageHandlerCtx) Connected(session types.Session, socket types.WebSocket) (bool, string) { +func (h *MessageHandlerCtx) Connected(session types.Session, websocket_peer types.WebSocketPeer) (bool, string) { if h.locked && !session.Admin(){ h.logger.Debug().Msg("server locked") return false, "locked" diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index a9b5ee6c..7b583173 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -142,13 +142,13 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request) e return connection.Close() } - socket := &WebSocketCtx{ + websocket_peer := &WebSocketPeerCtx{ session: session, ws: ws, connection: connection, } - ok, reason := ws.handler.Connected(session, socket) + ok, reason := ws.handler.Connected(session, websocket_peer) if !ok { // TODO: Refactor if err = connection.WriteJSON(message.Disconnect{ @@ -161,7 +161,7 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request) e return connection.Close() } - session.SetSocket(socket) + session.SetWebSocketPeer(websocket_peer) ws.logger. Debug(). diff --git a/internal/websocket/peer.go b/internal/websocket/peer.go new file mode 100644 index 00000000..98a6e607 --- /dev/null +++ b/internal/websocket/peer.go @@ -0,0 +1,47 @@ +package websocket + +import ( + "encoding/json" + "sync" + + "github.com/gorilla/websocket" + + "demodesk/neko/internal/types" +) + +type WebSocketPeerCtx struct { + session types.Session + ws *WebSocketManagerCtx + connection *websocket.Conn + mu sync.Mutex +} + +func (websocket_peer *WebSocketPeerCtx) Send(v interface{}) error { + websocket_peer.mu.Lock() + defer websocket_peer.mu.Unlock() + + if websocket_peer.connection == nil { + return nil + } + + raw, err := json.Marshal(v) + if err != nil { + return err + } + + websocket_peer.ws.logger.Debug(). + Str("session", websocket_peer.session.ID()). + Str("address", websocket_peer.connection.RemoteAddr().String()). + Str("raw", string(raw)). + Msg("sending message to client") + + return websocket_peer.connection.WriteMessage(websocket.TextMessage, raw) +} + +func (websocket_peer *WebSocketPeerCtx) Destroy() error { + if websocket_peer.connection == nil { + return nil + } + + return websocket_peer.connection.Close() +} diff --git a/internal/websocket/websocket.go b/internal/websocket/websocket.go deleted file mode 100644 index 53dcb43e..00000000 --- a/internal/websocket/websocket.go +++ /dev/null @@ -1,47 +0,0 @@ -package websocket - -import ( - "encoding/json" - "sync" - - "github.com/gorilla/websocket" - - "demodesk/neko/internal/types" -) - -type WebSocketCtx struct { - session types.Session - ws *WebSocketManagerCtx - connection *websocket.Conn - mu sync.Mutex -} - -func (socket *WebSocketCtx) Send(v interface{}) error { - socket.mu.Lock() - defer socket.mu.Unlock() - - if socket.connection == nil { - return nil - } - - raw, err := json.Marshal(v) - if err != nil { - return err - } - - socket.ws.logger.Debug(). - Str("session", socket.session.ID()). - Str("address", socket.connection.RemoteAddr().String()). - Str("raw", string(raw)). - Msg("sending message to client") - - return socket.connection.WriteMessage(websocket.TextMessage, raw) -} - -func (socket *WebSocketCtx) Destroy() error { - if socket.connection == nil { - return nil - } - - return socket.connection.Close() -}