This commit is contained in:
crusader 2017-11-29 20:17:01 +09:00
parent d3a86a8a34
commit 74d87608c6
6 changed files with 68 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package socket
import ( import (
"fmt" "fmt"
"io"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/rpc/client" "git.loafle.net/commons_go/rpc/client"
@ -26,6 +27,10 @@ func (crwch *ClientReadWriteCloseHandlers) Connect() (interface{}, error) {
} }
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) { func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
if nil == conn {
return nil, io.EOF
}
soc := conn.(server.Socket) soc := conn.(server.Socket)
resCodec, err := codec.NewResponse(soc) resCodec, err := codec.NewResponse(soc)
@ -33,6 +38,10 @@ func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX ClientContext,
} }
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params interface{}, id interface{}) error { func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params interface{}, id interface{}) error {
if nil == conn {
return io.EOF
}
soc := conn.(server.Socket) soc := conn.(server.Socket)
if wErr := codec.WriteRequest(soc, method, params); nil != wErr { if wErr := codec.WriteRequest(soc, method, params); nil != wErr {
@ -43,6 +52,10 @@ func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX ClientContext,
} }
func (crwch *ClientReadWriteCloseHandlers) Disconnect(conn interface{}) { func (crwch *ClientReadWriteCloseHandlers) Disconnect(conn interface{}) {
if nil == conn {
return
}
soc := conn.(server.Socket) soc := conn.(server.Socket)
soc.Close() soc.Close()
} }

View File

@ -2,6 +2,7 @@ package fasthttp
import ( import (
"fmt" "fmt"
"io"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -23,6 +24,10 @@ func (crwch *ClientReadWriteCloseHandlers) Connect() (interface{}, error) {
} }
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) { func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
if nil == conn {
return nil, io.EOF
}
soc := conn.(cwf.Socket) soc := conn.(cwf.Socket)
_, r, err := soc.NextReader() _, r, err := soc.NextReader()
@ -32,12 +37,19 @@ func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX ClientContext,
} }
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params interface{}, id interface{}) error { func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params interface{}, id interface{}) error {
if nil == conn {
return io.EOF
}
soc := conn.(cwf.Socket) soc := conn.(cwf.Socket)
wc, wErr := soc.NextWriter(websocket.TextMessage) wc, wErr := soc.NextWriter(websocket.TextMessage)
if nil != wErr { if nil != wErr {
return wErr return wErr
} }
defer func() {
wc.Close()
}()
if wErr := codec.WriteRequest(wc, method, params); nil != wErr { if wErr := codec.WriteRequest(wc, method, params); nil != wErr {
return wErr return wErr
@ -47,6 +59,10 @@ func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX ClientContext,
} }
func (crwch *ClientReadWriteCloseHandlers) Disconnect(conn interface{}) { func (crwch *ClientReadWriteCloseHandlers) Disconnect(conn interface{}) {
if nil == conn {
return
}
soc := conn.(cwf.Socket) soc := conn.(cwf.Socket)
soc.Close() soc.Close()
} }

View File

@ -65,8 +65,13 @@ func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec
// newClientMessageCodec returns a new ClientMessageCodec. // newClientMessageCodec returns a new ClientMessageCodec.
func newClientResponseCodec(r io.Reader, codec codec.Codec) (protocol.ClientResponseCodec, error) { func newClientResponseCodec(r io.Reader, codec codec.Codec) (protocol.ClientResponseCodec, error) {
decoder := json.NewDecoder(r)
if nil == r {
return nil, io.EOF
}
res := &clientResponse{} res := &clientResponse{}
err := json.NewDecoder(r).Decode(res) err := decoder.Decode(res)
if err != nil { if err != nil {
if err == io.ErrUnexpectedEOF || err == io.EOF { if err == io.ErrUnexpectedEOF || err == io.EOF {
return nil, err return nil, err

View File

@ -36,8 +36,13 @@ type serverRequest struct {
// newRequestCodec returns a new ServerRequestCodec. // newRequestCodec returns a new ServerRequestCodec.
func newServerRequestCodec(r io.Reader, codec codec.Codec) (protocol.ServerRequestCodec, error) { func newServerRequestCodec(r io.Reader, codec codec.Codec) (protocol.ServerRequestCodec, error) {
// Decode the request body and check if RPC method is valid. // Decode the request body and check if RPC method is valid.
decoder := json.NewDecoder(r)
if nil == r {
return nil, io.EOF
}
req := &serverRequest{} req := &serverRequest{}
err := json.NewDecoder(r).Decode(req) err := decoder.Decode(req)
if err == io.ErrUnexpectedEOF || err == io.EOF { if err == io.ErrUnexpectedEOF || err == io.EOF {
return nil, err return nil, err
} }

View File

@ -1,6 +1,8 @@
package socket package socket
import ( import (
"io"
"git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/protocol" "git.loafle.net/commons_go/rpc/protocol"
"git.loafle.net/commons_go/server" "git.loafle.net/commons_go/server"
@ -15,6 +17,10 @@ type ServletReadWriteCloseHandlers struct {
} }
func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) { func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
if nil == conn {
return nil, io.EOF
}
soc := conn.(server.Socket) soc := conn.(server.Socket)
reqCodec, err := codec.NewRequest(soc) reqCodec, err := codec.NewRequest(soc)
@ -22,6 +28,10 @@ func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletCo
} }
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, reqCodec protocol.ServerRequestCodec, result interface{}, err error) error { func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, reqCodec protocol.ServerRequestCodec, result interface{}, err error) error {
if nil == conn {
return io.EOF
}
soc := conn.(server.Socket) soc := conn.(server.Socket)
if nil != err { if nil != err {
@ -38,6 +48,10 @@ func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.Servlet
} }
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params interface{}) error { func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params interface{}) error {
if nil == conn {
return io.EOF
}
soc := conn.(server.Socket) soc := conn.(server.Socket)
if wErr := codec.WriteNotification(soc, method, params); nil != wErr { if wErr := codec.WriteNotification(soc, method, params); nil != wErr {

View File

@ -1,9 +1,8 @@
package fasthttp package fasthttp
import ( import (
"fmt" "io"
"git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/protocol" "git.loafle.net/commons_go/rpc/protocol"
cwf "git.loafle.net/commons_go/websocket_fasthttp" cwf "git.loafle.net/commons_go/websocket_fasthttp"
@ -19,7 +18,10 @@ type ServletReadWriteCloseHandlers struct {
} }
func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) { func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
logging.Logger().Debug(fmt.Sprintf("ReadRequest: conn %v", conn)) if nil == conn {
return nil, io.EOF
}
soc := conn.(cwf.Socket) soc := conn.(cwf.Socket)
_, r, err := soc.NextReader() _, r, err := soc.NextReader()
@ -29,7 +31,10 @@ func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletCo
} }
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error { func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
logging.Logger().Debug(fmt.Sprintf("WriteResponse: conn %v", conn)) if nil == conn {
return io.EOF
}
soc := conn.(cwf.Socket) soc := conn.(cwf.Socket)
wc, wErr := soc.NextWriter(websocket.TextMessage) wc, wErr := soc.NextWriter(websocket.TextMessage)
@ -55,7 +60,10 @@ func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.Servlet
} }
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params interface{}) error { func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params interface{}) error {
logging.Logger().Debug(fmt.Sprintf("WriteNotification: conn %v", conn)) if nil == conn {
return io.EOF
}
soc := conn.(cwf.Socket) soc := conn.(cwf.Socket)
wc, wErr := soc.NextWriter(websocket.TextMessage) wc, wErr := soc.NextWriter(websocket.TextMessage)