2017-11-29 09:55:24 +00:00
|
|
|
package socket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.loafle.net/commons_go/rpc"
|
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
|
|
|
"git.loafle.net/commons_go/server"
|
|
|
|
)
|
|
|
|
|
2017-11-29 10:02:33 +00:00
|
|
|
func New() rpc.ServletReadWriteCloseHandler {
|
|
|
|
return &ServletReadWriteCloseHandlers{}
|
2017-11-29 09:55:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 10:02:33 +00:00
|
|
|
type ServletReadWriteCloseHandlers struct {
|
|
|
|
rpc.ServletReadWriteCloseHandlers
|
|
|
|
}
|
|
|
|
|
2018-03-23 09:45:53 +00:00
|
|
|
func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
|
2018-03-23 13:45:48 +00:00
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
rErr error
|
|
|
|
)
|
2017-11-29 09:55:24 +00:00
|
|
|
soc := conn.(server.Socket)
|
2018-03-22 13:35:08 +00:00
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
buf, rErr = rpc.TCPReadData(soc)
|
|
|
|
if nil != rErr {
|
|
|
|
return nil, rErr
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:32:44 +00:00
|
|
|
return codec.NewRequest(buf)
|
2017-11-29 09:55:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 10:02:33 +00:00
|
|
|
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, reqCodec protocol.ServerRequestCodec, result interface{}, err error) error {
|
2018-03-22 13:35:08 +00:00
|
|
|
var (
|
2018-03-23 13:45:48 +00:00
|
|
|
buf []byte
|
2018-03-22 13:35:08 +00:00
|
|
|
wErr error
|
|
|
|
)
|
2017-11-29 09:55:24 +00:00
|
|
|
soc := conn.(server.Socket)
|
|
|
|
|
|
|
|
if nil != err {
|
2018-03-23 17:32:44 +00:00
|
|
|
buf, wErr = reqCodec.NewError(500, err)
|
2017-11-29 09:55:24 +00:00
|
|
|
} else {
|
2018-03-23 17:32:44 +00:00
|
|
|
buf, wErr = reqCodec.NewResponse(result)
|
2018-03-23 13:45:48 +00:00
|
|
|
}
|
|
|
|
if nil != wErr {
|
|
|
|
return wErr
|
2017-11-29 09:55:24 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
wErr = rpc.TCPWriteData(soc, buf)
|
|
|
|
if nil != wErr {
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-11-29 09:55:24 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 06:31:54 +00:00
|
|
|
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params []interface{}) error {
|
2018-03-22 13:35:08 +00:00
|
|
|
var (
|
2018-03-23 13:45:48 +00:00
|
|
|
buf []byte
|
2018-03-22 13:35:08 +00:00
|
|
|
wErr error
|
|
|
|
)
|
2017-11-29 09:55:24 +00:00
|
|
|
soc := conn.(server.Socket)
|
|
|
|
|
2018-03-23 17:32:44 +00:00
|
|
|
buf, wErr = codec.NewNotification(method, params)
|
2018-03-23 13:45:48 +00:00
|
|
|
if nil != wErr {
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
|
|
|
|
wErr = rpc.TCPWriteData(soc, buf)
|
|
|
|
if nil != wErr {
|
|
|
|
return wErr
|
|
|
|
}
|
2017-11-29 09:55:24 +00:00
|
|
|
|
2018-03-23 13:45:48 +00:00
|
|
|
return nil
|
2017-11-29 09:55:24 +00:00
|
|
|
}
|
2017-11-30 04:59:46 +00:00
|
|
|
|
|
|
|
func (srwch *ServletReadWriteCloseHandlers) Validate() {
|
|
|
|
srwch.ServletReadWriteCloseHandlers.Validate()
|
|
|
|
}
|