rpc/server/rwc/socket/servlet_rwc_handlers.go
crusader 170438c0d0 ing
2018-03-24 02:32:44 +09:00

79 lines
1.6 KiB
Go

package socket
import (
"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) {
var (
buf []byte
rErr error
)
soc := conn.(server.Socket)
buf, rErr = rpc.TCPReadData(soc)
if nil != rErr {
return nil, rErr
}
return codec.NewRequest(buf)
}
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, reqCodec protocol.ServerRequestCodec, result interface{}, err error) error {
var (
buf []byte
wErr error
)
soc := conn.(server.Socket)
if nil != err {
buf, wErr = reqCodec.NewError(500, err)
} else {
buf, wErr = reqCodec.NewResponse(result)
}
if nil != wErr {
return wErr
}
wErr = rpc.TCPWriteData(soc, buf)
if nil != wErr {
return wErr
}
return nil
}
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params []interface{}) error {
var (
buf []byte
wErr error
)
soc := conn.(server.Socket)
buf, wErr = codec.NewNotification(method, params)
if nil != wErr {
return wErr
}
wErr = rpc.TCPWriteData(soc, buf)
if nil != wErr {
return wErr
}
return nil
}
func (srwch *ServletReadWriteCloseHandlers) Validate() {
srwch.ServletReadWriteCloseHandlers.Validate()
}