63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package socket
|
|
|
|
import (
|
|
"io"
|
|
|
|
"git.loafle.net/commons_go/rpc"
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
|
"git.loafle.net/commons_go/server"
|
|
)
|
|
|
|
func New() rpc.ServletReadWriteCloseHandler {
|
|
return &ServletReadWriteCloseHandlers{}
|
|
}
|
|
|
|
type ServletReadWriteCloseHandlers struct {
|
|
rpc.ServletReadWriteCloseHandlers
|
|
}
|
|
|
|
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)
|
|
reqCodec, err := codec.NewRequest(soc)
|
|
|
|
return reqCodec, err
|
|
}
|
|
|
|
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)
|
|
|
|
if nil != err {
|
|
if wErr := reqCodec.WriteError(soc, 500, err); nil != wErr {
|
|
return wErr
|
|
}
|
|
} else {
|
|
if wErr := reqCodec.WriteResponse(soc, result); nil != wErr {
|
|
return wErr
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
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)
|
|
|
|
if wErr := codec.WriteNotification(soc, method, params); nil != wErr {
|
|
return wErr
|
|
}
|
|
|
|
return nil
|
|
}
|