60 lines
1.5 KiB
Go
60 lines
1.5 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) {
|
|
soc := conn.(server.Socket)
|
|
|
|
srwch.ReadMtx.RLock()
|
|
reqCodec, err := codec.NewRequest(soc)
|
|
srwch.ReadMtx.RUnlock()
|
|
|
|
return reqCodec, err
|
|
}
|
|
|
|
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, reqCodec protocol.ServerRequestCodec, result interface{}, err error) error {
|
|
var (
|
|
wErr error
|
|
)
|
|
soc := conn.(server.Socket)
|
|
|
|
srwch.WriteMtx.RLock()
|
|
if nil != err {
|
|
wErr = reqCodec.WriteError(soc, 500, err)
|
|
} else {
|
|
wErr = reqCodec.WriteResponse(soc, result)
|
|
}
|
|
srwch.WriteMtx.RUnlock()
|
|
|
|
return wErr
|
|
}
|
|
|
|
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params []interface{}) error {
|
|
var (
|
|
wErr error
|
|
)
|
|
soc := conn.(server.Socket)
|
|
|
|
srwch.WriteMtx.RLock()
|
|
wErr = codec.WriteNotification(soc, method, params)
|
|
srwch.WriteMtx.RUnlock()
|
|
|
|
return wErr
|
|
}
|
|
|
|
func (srwch *ServletReadWriteCloseHandlers) Validate() {
|
|
srwch.ServletReadWriteCloseHandlers.Validate()
|
|
}
|