You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.7 KiB
110 lines
2.7 KiB
package logic |
|
|
|
import ( |
|
"context" |
|
clientv3 "go.etcd.io/etcd/client/v3" |
|
"google.golang.org/grpc" |
|
"google.golang.org/grpc/reflection" |
|
"google.golang.org/protobuf/types/known/emptypb" |
|
"net" |
|
"sonet/api/gen/chat" |
|
"sonet/pkg/config" |
|
"sonet/pkg/grpc/discovery" |
|
"sonet/pkg/grpc/interceptor" |
|
"sonet/pkg/protocol/deliver" |
|
"sonet/pkg/protocol/session" |
|
"sonet/pkg/utils/logger" |
|
) |
|
|
|
type ChatServer struct { |
|
chat.UnimplementedChatServer |
|
deliver *deliver.Deliver |
|
} |
|
|
|
func NewChatServer(deliver *deliver.Deliver) *ChatServer { |
|
return &ChatServer{ |
|
deliver: deliver, |
|
} |
|
} |
|
|
|
func (s *ChatServer) Run(conf config.GrpcConfig, etcd *clientv3.Client) (err error) { |
|
server := grpc.NewServer( |
|
config.GetGrpcOptions( |
|
conf, |
|
grpc.UnaryInterceptor(interceptor.RecoverInterceptor), |
|
)..., |
|
) |
|
if !conf.NoReflection { |
|
// 注册反射服务 |
|
reflection.Register(server) |
|
} |
|
chat.RegisterChatServer(server, s) |
|
listen, err := net.Listen("tcp", conf.Address) |
|
if err != nil { |
|
return |
|
} |
|
|
|
// registry discovery |
|
register := &(conf.Register) |
|
if register.Name == "" { |
|
register.Name = chat.Chat_ServiceDesc.ServiceName |
|
} |
|
if register.Addr == "" { |
|
register.Addr = conf.Address |
|
} |
|
err = discovery.EtcdRegistry(etcd, register) |
|
if err != nil { |
|
panic(err) |
|
} |
|
|
|
// run serve |
|
logger.Infof("%s grpc server running %s\n", register.Name, listen.Addr().String()) |
|
err = server.Serve(listen) |
|
return |
|
} |
|
|
|
func (s *ChatServer) Send(ctx context.Context, req *chat.ReqSend) (*chat.ResSend, error) { |
|
subject, err := session.GetSubject(ctx) |
|
if err != nil { |
|
return nil, err |
|
} |
|
|
|
// TODO 存储消息,ack 队列重发(可异步发送),asc超时第二次发送时同步判断是否不在线? |
|
|
|
// 投递消息 |
|
message := &chat.ChatMessage{Sender: subject.Uid, Content: req.Content} |
|
_, err = s.deliver.Deliver(ctx, message, req.Receiver) |
|
if err != nil { |
|
return nil, err |
|
} |
|
|
|
return &chat.ResSend{}, nil |
|
} |
|
|
|
func (s *ChatServer) RoomSend(ctx context.Context, send *chat.ReqRoomSend) (*chat.ResSend, error) { |
|
return nil, nil |
|
} |
|
|
|
func (s *ChatServer) RoomCreate(ctx context.Context, create *chat.ReqRoomCreate) (*chat.ResRoomCreate, error) { |
|
return nil, nil |
|
} |
|
|
|
func (s *ChatServer) RoomJoin(ctx context.Context, join *chat.ReqRoomJoin) (*emptypb.Empty, error) { |
|
return nil, nil |
|
} |
|
|
|
func (s *ChatServer) RoomLeave(ctx context.Context, leave *chat.ReqRoomLeave) (*emptypb.Empty, error) { |
|
return nil, nil |
|
} |
|
|
|
func (s *ChatServer) RoomKickOut(ctx context.Context, out *chat.ReqRoomKickOut) (*emptypb.Empty, error) { |
|
return nil, nil |
|
} |
|
|
|
func (s *ChatServer) RoomInfo(ctx context.Context, info *chat.ReqRoomInfo) (*chat.ResRoomInfo, error) { |
|
return nil, nil |
|
} |
|
|
|
func (s *ChatServer) RoomList(ctx context.Context, list *chat.ReqRoomList) (*chat.ResRoomList, error) { |
|
return nil, nil |
|
}
|
|
|