86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package fasthttp
|
|
|
|
import (
|
|
"io"
|
|
|
|
"git.loafle.net/commons_go/rpc"
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
|
cwf "git.loafle.net/commons_go/websocket_fasthttp"
|
|
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
|
|
)
|
|
|
|
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.(cwf.Socket)
|
|
|
|
srwch.ReadMtx.Lock()
|
|
_, r, err := soc.NextReader()
|
|
requestCodec, err := codec.NewRequest(r)
|
|
srwch.ReadMtx.Unlock()
|
|
|
|
return requestCodec, err
|
|
}
|
|
|
|
func (srwch *ServletReadWriteCloseHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
|
|
var (
|
|
wc io.WriteCloser
|
|
wErr error
|
|
)
|
|
soc := conn.(cwf.Socket)
|
|
|
|
srwch.WriteMtx.Lock()
|
|
wc, wErr = soc.NextWriter(websocket.TextMessage)
|
|
if nil != wErr {
|
|
srwch.WriteMtx.Unlock()
|
|
return wErr
|
|
}
|
|
|
|
defer func() {
|
|
wc.Close()
|
|
}()
|
|
|
|
if nil != err {
|
|
wErr = requestCodec.WriteError(wc, 500, err)
|
|
} else {
|
|
wErr = requestCodec.WriteResponse(wc, result)
|
|
}
|
|
srwch.WriteMtx.Unlock()
|
|
|
|
return wErr
|
|
}
|
|
|
|
func (srwch *ServletReadWriteCloseHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, params []interface{}) error {
|
|
var (
|
|
wc io.WriteCloser
|
|
wErr error
|
|
)
|
|
soc := conn.(cwf.Socket)
|
|
|
|
srwch.WriteMtx.Lock()
|
|
wc, wErr = soc.NextWriter(websocket.TextMessage)
|
|
if nil != wErr {
|
|
srwch.WriteMtx.Unlock()
|
|
return wErr
|
|
}
|
|
|
|
defer func() {
|
|
wc.Close()
|
|
}()
|
|
|
|
wErr = codec.WriteNotification(wc, method, params)
|
|
srwch.WriteMtx.Unlock()
|
|
|
|
return wErr
|
|
}
|
|
|
|
func (srwch *ServletReadWriteCloseHandlers) Validate() {
|
|
srwch.ServletReadWriteCloseHandlers.Validate()
|
|
}
|