From 4911fe3fd94019fcca730710e0b411e8e5a66847 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 29 Dec 2015 15:10:18 +0100 Subject: moved websocket session handling to same goroutine diff --git a/src/helsinki.at/rhimportd/ctrlWebSocket.go b/src/helsinki.at/rhimportd/ctrlWebSocket.go index 93134fd..b3f10f6 100644 --- a/src/helsinki.at/rhimportd/ctrlWebSocket.go +++ b/src/helsinki.at/rhimportd/ctrlWebSocket.go @@ -83,7 +83,31 @@ func sendWebSocketErrorResponse(ws *websocket.Conn, code int, err_str string) { } } -func webSocketSessionHandler() { +func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.Conn) { + defer ws.Close() + + select { + case reqdata := <-reqchan: + switch reqdata.Command { + case "new": + sendWebSocketErrorResponse(ws, http.StatusNotImplemented, "new session - not yet implemented") + return + case "reconnect": + if reqdata.UserName == "" { + sendWebSocketErrorResponse(ws, http.StatusBadRequest, "missing mandotary field LOGIN_NAME") + return + } + if reqdata.Id == "" { + sendWebSocketErrorResponse(ws, http.StatusBadRequest, "missing mandotary field ID") + return + } + sendWebSocketErrorResponse(ws, http.StatusNotImplemented, "reconnect session - not yet implemented") + return + default: + sendWebSocketErrorResponse(ws, http.StatusBadRequest, fmt.Sprintf("unknown command '%s'", reqdata.Command)) + return + } + } } func webSocketHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions *rhimport.SessionStoreChan, trusted bool, w http.ResponseWriter, r *http.Request) { @@ -98,6 +122,8 @@ func webSocketHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions * return } rhdl.Println("WebSocket Client", ws.RemoteAddr(), "connected") + reqchan := make(chan webSocketRequestData) + go webSocketSessionHandler(reqchan, ws) for { reqdata := newWebSocketRequestData(conf) @@ -109,26 +135,7 @@ func webSocketHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions * if trusted { reqdata.UserName = r.Header.Get("X-Forwarded-User") } - - switch reqdata.Command { - case "new": - sendWebSocketErrorResponse(ws, http.StatusNotImplemented, "new session - not yet implemented") - return - case "reconnect": - if reqdata.UserName == "" { - sendWebSocketErrorResponse(ws, http.StatusBadRequest, "missing mandotary field LOGIN_NAME") - return - } - if reqdata.Id == "" { - sendWebSocketErrorResponse(ws, http.StatusBadRequest, "missing mandotary field ID") - return - } - sendWebSocketErrorResponse(ws, http.StatusNotImplemented, "get session - not yet implemented") - return - default: - sendWebSocketErrorResponse(ws, http.StatusBadRequest, fmt.Sprintf("unknown command '%s'", reqdata.Command)) - return - } + reqchan <- *reqdata } } -- cgit v0.10.2