rpc/server/rwc/socket/servlet_rwc_handlers.go

60 lines
1.5 KiB
Go
Raw Normal View History

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
}
func (srwch *ServletReadWriteCloseHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
2017-11-29 09:55:24 +00:00
soc := conn.(server.Socket)
2018-03-22 13:35:08 +00:00
srwch.ReadMtx.RLock()
2017-11-29 09:55:24 +00:00
reqCodec, err := codec.NewRequest(soc)
2018-03-22 13:35:08 +00:00
srwch.ReadMtx.RUnlock()
2017-11-29 09:55:24 +00:00
return reqCodec, err
}
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 (
wErr error
)
2017-11-29 09:55:24 +00:00
soc := conn.(server.Socket)
2018-03-22 13:35:08 +00:00
srwch.WriteMtx.RLock()
2017-11-29 09:55:24 +00:00
if nil != err {
2018-03-22 13:35:08 +00:00
wErr = reqCodec.WriteError(soc, 500, err)
2017-11-29 09:55:24 +00:00
} else {
2018-03-22 13:35:08 +00:00
wErr = reqCodec.WriteResponse(soc, result)
2017-11-29 09:55:24 +00:00
}
2018-03-22 13:35:08 +00:00
srwch.WriteMtx.RUnlock()
2017-11-29 09:55:24 +00:00
2018-03-22 13:35:08 +00:00
return wErr
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 (
wErr error
)
2017-11-29 09:55:24 +00:00
soc := conn.(server.Socket)
2018-03-22 13:35:08 +00:00
srwch.WriteMtx.RLock()
wErr = codec.WriteNotification(soc, method, params)
srwch.WriteMtx.RUnlock()
2017-11-29 09:55:24 +00:00
2018-03-22 13:35:08 +00:00
return wErr
2017-11-29 09:55:24 +00:00
}
2017-11-30 04:59:46 +00:00
func (srwch *ServletReadWriteCloseHandlers) Validate() {
srwch.ServletReadWriteCloseHandlers.Validate()
}