From e31a48c4cd22aa2d8cfe352a83a5e2712f0f0e8e Mon Sep 17 00:00:00 2001 From: lc-official Date: Wed, 16 Jul 2025 09:35:17 +0800 Subject: [PATCH] new packet: ServerTotalPacket --- .idea/coreapp.iml | 7 +- proto/server.proto | 5 + router/minecraft/server.pb.go | 510 ---------------------------------- router/minecraft/servers.go | 38 ++- 4 files changed, 42 insertions(+), 518 deletions(-) delete mode 100644 router/minecraft/server.pb.go diff --git a/.idea/coreapp.iml b/.idea/coreapp.iml index 5e764c4..7ee078d 100644 --- a/.idea/coreapp.iml +++ b/.idea/coreapp.iml @@ -1,9 +1,4 @@ - + - - - - - \ No newline at end of file diff --git a/proto/server.proto b/proto/server.proto index f8e6373..a724536 100644 --- a/proto/server.proto +++ b/proto/server.proto @@ -12,6 +12,7 @@ enum ServerPacketType { JOIN = 5; LEAVE = 6; LIST = 7; + TOTAL = 8; } message ServerPacket { @@ -44,4 +45,8 @@ message ServerPlayerPacket { message ServerListPacket { string serverId = 1; repeated string players = 2; +} + +message ServerTotalPacket { + int32 total = 1; } \ No newline at end of file diff --git a/router/minecraft/server.pb.go b/router/minecraft/server.pb.go deleted file mode 100644 index d866104..0000000 --- a/router/minecraft/server.pb.go +++ /dev/null @@ -1,510 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.36.6 -// protoc v6.31.1 -// source: server.proto - -package minecraft - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" - unsafe "unsafe" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type ServerPacketType int32 - -const ( - ServerPacketType_PING ServerPacketType = 0 - ServerPacketType_PONG ServerPacketType = 1 - ServerPacketType_NEW ServerPacketType = 2 - ServerPacketType_DELETE ServerPacketType = 3 - ServerPacketType_CONNECT ServerPacketType = 4 - ServerPacketType_JOIN ServerPacketType = 5 - ServerPacketType_LEAVE ServerPacketType = 6 - ServerPacketType_LIST ServerPacketType = 7 -) - -// Enum value maps for ServerPacketType. -var ( - ServerPacketType_name = map[int32]string{ - 0: "PING", - 1: "PONG", - 2: "NEW", - 3: "DELETE", - 4: "CONNECT", - 5: "JOIN", - 6: "LEAVE", - 7: "LIST", - } - ServerPacketType_value = map[string]int32{ - "PING": 0, - "PONG": 1, - "NEW": 2, - "DELETE": 3, - "CONNECT": 4, - "JOIN": 5, - "LEAVE": 6, - "LIST": 7, - } -) - -func (x ServerPacketType) Enum() *ServerPacketType { - p := new(ServerPacketType) - *p = x - return p -} - -func (x ServerPacketType) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (ServerPacketType) Descriptor() protoreflect.EnumDescriptor { - return file_server_proto_enumTypes[0].Descriptor() -} - -func (ServerPacketType) Type() protoreflect.EnumType { - return &file_server_proto_enumTypes[0] -} - -func (x ServerPacketType) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use ServerPacketType.Descriptor instead. -func (ServerPacketType) EnumDescriptor() ([]byte, []int) { - return file_server_proto_rawDescGZIP(), []int{0} -} - -type ServerPacket struct { - state protoimpl.MessageState `protogen:"open.v1"` - Typ ServerPacketType `protobuf:"varint,1,opt,name=typ,proto3,enum=ServerPacketType" json:"typ,omitempty"` - Payload []byte `protobuf:"bytes,2,opt,name=payload,proto3,oneof" json:"payload,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ServerPacket) Reset() { - *x = ServerPacket{} - mi := &file_server_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ServerPacket) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ServerPacket) ProtoMessage() {} - -func (x *ServerPacket) ProtoReflect() protoreflect.Message { - mi := &file_server_proto_msgTypes[0] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ServerPacket.ProtoReflect.Descriptor instead. -func (*ServerPacket) Descriptor() ([]byte, []int) { - return file_server_proto_rawDescGZIP(), []int{0} -} - -func (x *ServerPacket) GetTyp() ServerPacketType { - if x != nil { - return x.Typ - } - return ServerPacketType_PING -} - -func (x *ServerPacket) GetPayload() []byte { - if x != nil { - return x.Payload - } - return nil -} - -type ServerNewPacket struct { - state protoimpl.MessageState `protogen:"open.v1"` - ServerId string `protobuf:"bytes,1,opt,name=serverId,proto3" json:"serverId,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` - Host string `protobuf:"bytes,3,opt,name=host,proto3" json:"host,omitempty"` - Port int32 `protobuf:"varint,4,opt,name=port,proto3" json:"port,omitempty"` - Motd string `protobuf:"bytes,5,opt,name=motd,proto3" json:"motd,omitempty"` - Group string `protobuf:"bytes,6,opt,name=group,proto3" json:"group,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ServerNewPacket) Reset() { - *x = ServerNewPacket{} - mi := &file_server_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ServerNewPacket) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ServerNewPacket) ProtoMessage() {} - -func (x *ServerNewPacket) ProtoReflect() protoreflect.Message { - mi := &file_server_proto_msgTypes[1] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ServerNewPacket.ProtoReflect.Descriptor instead. -func (*ServerNewPacket) Descriptor() ([]byte, []int) { - return file_server_proto_rawDescGZIP(), []int{1} -} - -func (x *ServerNewPacket) GetServerId() string { - if x != nil { - return x.ServerId - } - return "" -} - -func (x *ServerNewPacket) GetName() string { - if x != nil { - return x.Name - } - return "" -} - -func (x *ServerNewPacket) GetHost() string { - if x != nil { - return x.Host - } - return "" -} - -func (x *ServerNewPacket) GetPort() int32 { - if x != nil { - return x.Port - } - return 0 -} - -func (x *ServerNewPacket) GetMotd() string { - if x != nil { - return x.Motd - } - return "" -} - -func (x *ServerNewPacket) GetGroup() string { - if x != nil { - return x.Group - } - return "" -} - -type ServerDeletePacket struct { - state protoimpl.MessageState `protogen:"open.v1"` - ServerId string `protobuf:"bytes,1,opt,name=serverId,proto3" json:"serverId,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ServerDeletePacket) Reset() { - *x = ServerDeletePacket{} - mi := &file_server_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ServerDeletePacket) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ServerDeletePacket) ProtoMessage() {} - -func (x *ServerDeletePacket) ProtoReflect() protoreflect.Message { - mi := &file_server_proto_msgTypes[2] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ServerDeletePacket.ProtoReflect.Descriptor instead. -func (*ServerDeletePacket) Descriptor() ([]byte, []int) { - return file_server_proto_rawDescGZIP(), []int{2} -} - -func (x *ServerDeletePacket) GetServerId() string { - if x != nil { - return x.ServerId - } - return "" -} - -type ServerConnectPacket struct { - state protoimpl.MessageState `protogen:"open.v1"` - ServerId string `protobuf:"bytes,1,opt,name=serverId,proto3" json:"serverId,omitempty"` - Player string `protobuf:"bytes,2,opt,name=player,proto3" json:"player,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ServerConnectPacket) Reset() { - *x = ServerConnectPacket{} - mi := &file_server_proto_msgTypes[3] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ServerConnectPacket) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ServerConnectPacket) ProtoMessage() {} - -func (x *ServerConnectPacket) ProtoReflect() protoreflect.Message { - mi := &file_server_proto_msgTypes[3] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ServerConnectPacket.ProtoReflect.Descriptor instead. -func (*ServerConnectPacket) Descriptor() ([]byte, []int) { - return file_server_proto_rawDescGZIP(), []int{3} -} - -func (x *ServerConnectPacket) GetServerId() string { - if x != nil { - return x.ServerId - } - return "" -} - -func (x *ServerConnectPacket) GetPlayer() string { - if x != nil { - return x.Player - } - return "" -} - -type ServerPlayerPacket struct { - state protoimpl.MessageState `protogen:"open.v1"` - Player string `protobuf:"bytes,1,opt,name=player,proto3" json:"player,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ServerPlayerPacket) Reset() { - *x = ServerPlayerPacket{} - mi := &file_server_proto_msgTypes[4] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ServerPlayerPacket) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ServerPlayerPacket) ProtoMessage() {} - -func (x *ServerPlayerPacket) ProtoReflect() protoreflect.Message { - mi := &file_server_proto_msgTypes[4] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ServerPlayerPacket.ProtoReflect.Descriptor instead. -func (*ServerPlayerPacket) Descriptor() ([]byte, []int) { - return file_server_proto_rawDescGZIP(), []int{4} -} - -func (x *ServerPlayerPacket) GetPlayer() string { - if x != nil { - return x.Player - } - return "" -} - -type ServerListPacket struct { - state protoimpl.MessageState `protogen:"open.v1"` - ServerId string `protobuf:"bytes,1,opt,name=serverId,proto3" json:"serverId,omitempty"` - Players []string `protobuf:"bytes,2,rep,name=players,proto3" json:"players,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ServerListPacket) Reset() { - *x = ServerListPacket{} - mi := &file_server_proto_msgTypes[5] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ServerListPacket) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ServerListPacket) ProtoMessage() {} - -func (x *ServerListPacket) ProtoReflect() protoreflect.Message { - mi := &file_server_proto_msgTypes[5] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ServerListPacket.ProtoReflect.Descriptor instead. -func (*ServerListPacket) Descriptor() ([]byte, []int) { - return file_server_proto_rawDescGZIP(), []int{5} -} - -func (x *ServerListPacket) GetServerId() string { - if x != nil { - return x.ServerId - } - return "" -} - -func (x *ServerListPacket) GetPlayers() []string { - if x != nil { - return x.Players - } - return nil -} - -var File_server_proto protoreflect.FileDescriptor - -const file_server_proto_rawDesc = "" + - "\n" + - "\fserver.proto\"^\n" + - "\fServerPacket\x12#\n" + - "\x03typ\x18\x01 \x01(\x0e2\x11.ServerPacketTypeR\x03typ\x12\x1d\n" + - "\apayload\x18\x02 \x01(\fH\x00R\apayload\x88\x01\x01B\n" + - "\n" + - "\b_payload\"\x93\x01\n" + - "\x0fServerNewPacket\x12\x1a\n" + - "\bserverId\x18\x01 \x01(\tR\bserverId\x12\x12\n" + - "\x04name\x18\x02 \x01(\tR\x04name\x12\x12\n" + - "\x04host\x18\x03 \x01(\tR\x04host\x12\x12\n" + - "\x04port\x18\x04 \x01(\x05R\x04port\x12\x12\n" + - "\x04motd\x18\x05 \x01(\tR\x04motd\x12\x14\n" + - "\x05group\x18\x06 \x01(\tR\x05group\"0\n" + - "\x12ServerDeletePacket\x12\x1a\n" + - "\bserverId\x18\x01 \x01(\tR\bserverId\"I\n" + - "\x13ServerConnectPacket\x12\x1a\n" + - "\bserverId\x18\x01 \x01(\tR\bserverId\x12\x16\n" + - "\x06player\x18\x02 \x01(\tR\x06player\",\n" + - "\x12ServerPlayerPacket\x12\x16\n" + - "\x06player\x18\x01 \x01(\tR\x06player\"H\n" + - "\x10ServerListPacket\x12\x1a\n" + - "\bserverId\x18\x01 \x01(\tR\bserverId\x12\x18\n" + - "\aplayers\x18\x02 \x03(\tR\aplayers*g\n" + - "\x10ServerPacketType\x12\b\n" + - "\x04PING\x10\x00\x12\b\n" + - "\x04PONG\x10\x01\x12\a\n" + - "\x03NEW\x10\x02\x12\n" + - "\n" + - "\x06DELETE\x10\x03\x12\v\n" + - "\aCONNECT\x10\x04\x12\b\n" + - "\x04JOIN\x10\x05\x12\t\n" + - "\x05LEAVE\x10\x06\x12\b\n" + - "\x04LIST\x10\aB\x12Z\x10router/minecraftb\x06proto3" - -var ( - file_server_proto_rawDescOnce sync.Once - file_server_proto_rawDescData []byte -) - -func file_server_proto_rawDescGZIP() []byte { - file_server_proto_rawDescOnce.Do(func() { - file_server_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_server_proto_rawDesc), len(file_server_proto_rawDesc))) - }) - return file_server_proto_rawDescData -} - -var file_server_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_server_proto_msgTypes = make([]protoimpl.MessageInfo, 6) -var file_server_proto_goTypes = []any{ - (ServerPacketType)(0), // 0: ServerPacketType - (*ServerPacket)(nil), // 1: ServerPacket - (*ServerNewPacket)(nil), // 2: ServerNewPacket - (*ServerDeletePacket)(nil), // 3: ServerDeletePacket - (*ServerConnectPacket)(nil), // 4: ServerConnectPacket - (*ServerPlayerPacket)(nil), // 5: ServerPlayerPacket - (*ServerListPacket)(nil), // 6: ServerListPacket -} -var file_server_proto_depIdxs = []int32{ - 0, // 0: ServerPacket.typ:type_name -> ServerPacketType - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name -} - -func init() { file_server_proto_init() } -func file_server_proto_init() { - if File_server_proto != nil { - return - } - file_server_proto_msgTypes[0].OneofWrappers = []any{} - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: unsafe.Slice(unsafe.StringData(file_server_proto_rawDesc), len(file_server_proto_rawDesc)), - NumEnums: 1, - NumMessages: 6, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_server_proto_goTypes, - DependencyIndexes: file_server_proto_depIdxs, - EnumInfos: file_server_proto_enumTypes, - MessageInfos: file_server_proto_msgTypes, - }.Build() - File_server_proto = out.File - file_server_proto_goTypes = nil - file_server_proto_depIdxs = nil -} diff --git a/router/minecraft/servers.go b/router/minecraft/servers.go index bc515a3..223182c 100644 --- a/router/minecraft/servers.go +++ b/router/minecraft/servers.go @@ -257,7 +257,10 @@ func serverReadLoop(conn *ws.Conn, serverId string) { // 使用 Load 读取,避免并发问题 serverVal, ok := servers.Load(serverId) if !ok { - conn.Close() + err := conn.Close() + if err != nil { + return + } return } server := serverVal.(*Server) @@ -323,9 +326,37 @@ func serverReadLoop(conn *ws.Conn, serverId string) { if err != nil { panic(err) } + buf, err = proto.Marshal(&ServerPacket{ + Typ: ServerPacketType_LIST, + Payload: buf, + }) + if err != nil { + panic(err) + } conn.WriteChan <- ws.Binary(buf) return } + case ServerPacketType_TOTAL: + total := 0 + servers.Range(func(_, value any) bool { + total += len(value.(*Server).Players) + return true + }) + reply := &ServerTotalPacket{ + Total: int32(total), + } + buf, err := proto.Marshal(reply) + if err != nil { + panic(err) + } + buf, err = proto.Marshal(&ServerPacket{ + Typ: ServerPacketType_TOTAL, + Payload: buf, + }) + if err != nil { + panic(err) + } + conn.WriteChan <- ws.Binary(buf) } default: if conn.IsClosed() { @@ -397,7 +428,10 @@ func init() { return true // 继续遍历 }) if time.Since(serverProxyLastHB) > time.Second*10 && serverProxyConn != nil { - serverProxyConn.Close() + err := serverProxyConn.Close() + if err != nil { + continue + } } } }()