rpc/server/rwc/socket/servlet_rwc_handlers.go
crusader 3e82c65689 ing
2018-03-23 22:45:48 +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.NewRequestB(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.NewErrorB(500, err)
} else {
buf, wErr = reqCodec.NewResponseB(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.NewNotificationB(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()
}