2017-11-26 10:15:51 +00:00
|
|
|
package socket
|
|
|
|
|
|
|
|
import (
|
2017-11-28 09:33:52 +00:00
|
|
|
"net"
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
"git.loafle.net/commons_go/rpc"
|
2017-11-26 10:15:51 +00:00
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServletHandlers struct {
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
func (sh *ServletHandlers) GetRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
|
|
|
|
nConn := conn.(net.Conn)
|
|
|
|
requestCodec, err := codec.NewRequest(nConn)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
|
|
|
return requestCodec, err
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
func (sh *ServletHandlers) SendResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
|
|
|
|
nConn := conn.(net.Conn)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
|
|
|
if nil != err {
|
2017-11-28 09:33:52 +00:00
|
|
|
if wErr := requestCodec.WriteError(nConn, 500, err); nil != wErr {
|
|
|
|
return wErr
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-28 09:33:52 +00:00
|
|
|
if wErr := requestCodec.WriteResponse(nConn, result); nil != wErr {
|
|
|
|
return wErr
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
func (sh *ServletHandlers) SendNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, args ...interface{}) error {
|
|
|
|
nConn := conn.(net.Conn)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
if wErr := codec.WriteNotification(nConn, method, args); nil != wErr {
|
|
|
|
return wErr
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|