You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ngs/cluster/acceptor.go

93 lines
2.1 KiB
Go

package cluster
import (
"context"
"git.noahlan.cn/northlan/ngs/cluster/clusterpb"
"git.noahlan.cn/northlan/ngs/internal/message"
"git.noahlan.cn/northlan/ngs/mock"
"git.noahlan.cn/northlan/ngs/session"
"net"
)
type acceptor struct {
sid int64
gateClient clusterpb.MemberClient
session *session.Session
lastMid uint64
rpcHandler rpcHandler
gateAddr string
}
// Push implements the session.NetworkEntity interface
func (a *acceptor) Push(route string, v interface{}) error {
// TODO: buffer
data, err := message.Serialize(v)
if err != nil {
return err
}
request := &clusterpb.PushMessage{
SessionId: a.sid,
Route: route,
Data: data,
}
_, err = a.gateClient.HandlePush(context.Background(), request)
return err
}
// RPC implements the session.NetworkEntity interface
func (a *acceptor) RPC(route string, v interface{}) error {
// TODO: buffer
data, err := message.Serialize(v)
if err != nil {
return err
}
msg := &message.Message{
Type: message.Notify,
Route: route,
Data: data,
}
a.rpcHandler(a.session, msg, true)
return nil
}
// LastMid implements the session.NetworkEntity interface
func (a *acceptor) LastMid() uint64 {
return a.lastMid
}
// Response implements the session.NetworkEntity interface
func (a *acceptor) Response(v interface{}) error {
return a.ResponseMid(a.lastMid, v)
}
// ResponseMid implements the session.NetworkEntity interface
func (a *acceptor) ResponseMid(mid uint64, v interface{}) error {
// TODO: buffer
data, err := message.Serialize(v)
if err != nil {
return err
}
request := &clusterpb.ResponseMessage{
SessionId: a.sid,
Id: mid,
Data: data,
}
_, err = a.gateClient.HandleResponse(context.Background(), request)
return err
}
// Close implements the session.NetworkEntity interface
func (a *acceptor) Close() error {
// TODO: buffer
request := &clusterpb.CloseSessionRequest{
SessionId: a.sid,
}
_, err := a.gateClient.CloseSession(context.Background(), request)
return err
}
// RemoteAddr implements the session.NetworkEntity interface
func (*acceptor) RemoteAddr() net.Addr {
return mock.NetAddr{}
}