From 9345b49f57345047c44ff3eb237de91afca23a58 Mon Sep 17 00:00:00 2001 From: iegod Date: Tue, 10 Dec 2024 22:24:45 -0500 Subject: [PATCH] Server now provides clientid on connection. Client expects this before continuing. --- client/client/client.go | 28 ++++++++++++++ client/game/game.go | 8 +++- client/pb/clientdata.pb.go | 79 ++++++++++++++++++++++++++++++++------ server/clientdata.proto | 11 ++++-- server/pb/clientdata.pb.go | 79 ++++++++++++++++++++++++++++++++------ server/server/server.go | 40 +++++++++++++++++-- 6 files changed, 215 insertions(+), 30 deletions(-) diff --git a/client/client/client.go b/client/client/client.go index ddb0c88..9918186 100644 --- a/client/client/client.go +++ b/client/client/client.go @@ -70,6 +70,34 @@ func (c *Client) SendData(msg string) { } } +func (c *Client) GetIdentity() int { + lengthBuf := make([]byte, 4) + _, err := io.ReadFull(c.conn, lengthBuf) + if err != nil { + fmt.Println("failed to read length prefix: ", err) + return 0 + } + + length := binary.BigEndian.Uint32(lengthBuf) + + data := make([]byte, length) + _, err = io.ReadFull(c.conn, data) + if err != nil { + fmt.Println("failed to read data: ", err) + return 0 + } + + var identity pb.ClientIdentity + err = proto.Unmarshal(data, &identity) + if err != nil { + fmt.Println("Error deserializing: ", err) + return 0 + } + + return int(identity.Id) + +} + func (c *Client) ReadData(callback func(*pb.AllClients)) { /* diff --git a/client/game/game.go b/client/game/game.go index 93083bc..81b30e1 100644 --- a/client/game/game.go +++ b/client/game/game.go @@ -27,6 +27,7 @@ func init() { } type ClientData struct { + Id int Address string Name string Position gamedata.Coordinates @@ -35,6 +36,7 @@ type ClientData struct { type Game struct { name string blocky *elements.Block + gameId int //players map[client.Identity] @@ -56,6 +58,8 @@ func NewGame() *Game { name: namelist[rand.Intn(len(namelist))], } g.clients = make(map[string]ClientData) + + g.gameId = g.gameclient.GetIdentity() go g.gameclient.ReadData(g.HandleServerData) return g } @@ -145,8 +149,8 @@ func (g *Game) HandleServerData(allclients *pb.AllClients) { for _, client := range allclients.Clients { //fmt.Println(client.Coordinates.X) - localaddr := g.gameclient.GetLocalAddr() - if client.Address != localaddr { + //localaddr := g.gameclient.GetLocalAddr() + if client.Id != int32(g.gameId) { update := ClientData{ Address: client.Address, Name: client.Name, diff --git a/client/pb/clientdata.pb.go b/client/pb/clientdata.pb.go index bbbc78f..c7abd06 100644 --- a/client/pb/clientdata.pb.go +++ b/client/pb/clientdata.pb.go @@ -78,9 +78,10 @@ type ClientData struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Address string `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty"` - Name string `protobuf:"bytes,2,opt,name=Name,proto3" json:"Name,omitempty"` - Coordinates *Coordinates `protobuf:"bytes,3,opt,name=coordinates,proto3" json:"coordinates,omitempty"` + Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"` + Address string `protobuf:"bytes,2,opt,name=Address,proto3" json:"Address,omitempty"` + Name string `protobuf:"bytes,3,opt,name=Name,proto3" json:"Name,omitempty"` + Coordinates *Coordinates `protobuf:"bytes,4,opt,name=coordinates,proto3" json:"coordinates,omitempty"` } func (x *ClientData) Reset() { @@ -113,6 +114,13 @@ func (*ClientData) Descriptor() ([]byte, []int) { return file_clientdata_proto_rawDescGZIP(), []int{1} } +func (x *ClientData) GetId() int32 { + if x != nil { + return x.Id + } + return 0 +} + func (x *ClientData) GetAddress() string { if x != nil { return x.Address @@ -232,6 +240,51 @@ func (x *AllClients) GetClients() []*ClientData { return nil } +type ClientIdentity struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"` +} + +func (x *ClientIdentity) Reset() { + *x = ClientIdentity{} + mi := &file_clientdata_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ClientIdentity) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ClientIdentity) ProtoMessage() {} + +func (x *ClientIdentity) ProtoReflect() protoreflect.Message { + mi := &file_clientdata_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 ClientIdentity.ProtoReflect.Descriptor instead. +func (*ClientIdentity) Descriptor() ([]byte, []int) { + return file_clientdata_proto_rawDescGZIP(), []int{4} +} + +func (x *ClientIdentity) GetId() int32 { + if x != nil { + return x.Id + } + return 0 +} + var File_clientdata_proto protoreflect.FileDescriptor var file_clientdata_proto_rawDesc = []byte{ @@ -239,11 +292,12 @@ var file_clientdata_proto_rawDesc = []byte{ 0x74, 0x6f, 0x12, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x22, 0x29, 0x0a, 0x0b, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x12, 0x0c, 0x0a, 0x01, 0x58, 0x18, 0x01, 0x20, 0x01, 0x28, 0x01, 0x52, 0x01, 0x58, 0x12, 0x0c, 0x0a, 0x01, 0x59, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, - 0x52, 0x01, 0x59, 0x22, 0x6f, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x61, 0x74, - 0x61, 0x12, 0x18, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, + 0x52, 0x01, 0x59, 0x22, 0x7f, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x61, 0x74, + 0x61, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, + 0x64, 0x12, 0x18, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x4e, - 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, - 0x33, 0x0a, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x18, 0x03, + 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, + 0x33, 0x0a, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x52, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x22, 0x5c, 0x0a, 0x11, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, @@ -255,8 +309,10 @@ var file_clientdata_proto_rawDesc = []byte{ 0x65, 0x73, 0x22, 0x38, 0x0a, 0x0a, 0x41, 0x6c, 0x6c, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x2a, 0x0a, 0x07, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, - 0x61, 0x74, 0x61, 0x52, 0x07, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x42, 0x06, 0x5a, 0x04, - 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x61, 0x74, 0x61, 0x52, 0x07, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x20, 0x0a, 0x0e, + 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x0e, + 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, 0x64, 0x42, 0x06, + 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -271,12 +327,13 @@ func file_clientdata_proto_rawDescGZIP() []byte { return file_clientdata_proto_rawDescData } -var file_clientdata_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_clientdata_proto_msgTypes = make([]protoimpl.MessageInfo, 5) var file_clientdata_proto_goTypes = []any{ (*Coordinates)(nil), // 0: main.Coordinates (*ClientData)(nil), // 1: main.ClientData (*ClientCoordinates)(nil), // 2: main.ClientCoordinates (*AllClients)(nil), // 3: main.AllClients + (*ClientIdentity)(nil), // 4: main.ClientIdentity } var file_clientdata_proto_depIdxs = []int32{ 0, // 0: main.ClientData.coordinates:type_name -> main.Coordinates @@ -300,7 +357,7 @@ func file_clientdata_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_clientdata_proto_rawDesc, NumEnums: 0, - NumMessages: 4, + NumMessages: 5, NumExtensions: 0, NumServices: 0, }, diff --git a/server/clientdata.proto b/server/clientdata.proto index 889a4c8..c600e6c 100644 --- a/server/clientdata.proto +++ b/server/clientdata.proto @@ -10,9 +10,10 @@ message Coordinates { } message ClientData { - string Address = 1; - string Name = 2; - Coordinates coordinates = 3; + int32 Id = 1; + string Address = 2; + string Name = 3; + Coordinates coordinates = 4; } message ClientCoordinates { @@ -22,4 +23,8 @@ message ClientCoordinates { message AllClients{ repeated ClientData Clients = 1; +} + +message ClientIdentity{ + int32 Id = 1; } \ No newline at end of file diff --git a/server/pb/clientdata.pb.go b/server/pb/clientdata.pb.go index bbbc78f..c7abd06 100644 --- a/server/pb/clientdata.pb.go +++ b/server/pb/clientdata.pb.go @@ -78,9 +78,10 @@ type ClientData struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Address string `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty"` - Name string `protobuf:"bytes,2,opt,name=Name,proto3" json:"Name,omitempty"` - Coordinates *Coordinates `protobuf:"bytes,3,opt,name=coordinates,proto3" json:"coordinates,omitempty"` + Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"` + Address string `protobuf:"bytes,2,opt,name=Address,proto3" json:"Address,omitempty"` + Name string `protobuf:"bytes,3,opt,name=Name,proto3" json:"Name,omitempty"` + Coordinates *Coordinates `protobuf:"bytes,4,opt,name=coordinates,proto3" json:"coordinates,omitempty"` } func (x *ClientData) Reset() { @@ -113,6 +114,13 @@ func (*ClientData) Descriptor() ([]byte, []int) { return file_clientdata_proto_rawDescGZIP(), []int{1} } +func (x *ClientData) GetId() int32 { + if x != nil { + return x.Id + } + return 0 +} + func (x *ClientData) GetAddress() string { if x != nil { return x.Address @@ -232,6 +240,51 @@ func (x *AllClients) GetClients() []*ClientData { return nil } +type ClientIdentity struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"` +} + +func (x *ClientIdentity) Reset() { + *x = ClientIdentity{} + mi := &file_clientdata_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ClientIdentity) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ClientIdentity) ProtoMessage() {} + +func (x *ClientIdentity) ProtoReflect() protoreflect.Message { + mi := &file_clientdata_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 ClientIdentity.ProtoReflect.Descriptor instead. +func (*ClientIdentity) Descriptor() ([]byte, []int) { + return file_clientdata_proto_rawDescGZIP(), []int{4} +} + +func (x *ClientIdentity) GetId() int32 { + if x != nil { + return x.Id + } + return 0 +} + var File_clientdata_proto protoreflect.FileDescriptor var file_clientdata_proto_rawDesc = []byte{ @@ -239,11 +292,12 @@ var file_clientdata_proto_rawDesc = []byte{ 0x74, 0x6f, 0x12, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x22, 0x29, 0x0a, 0x0b, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x12, 0x0c, 0x0a, 0x01, 0x58, 0x18, 0x01, 0x20, 0x01, 0x28, 0x01, 0x52, 0x01, 0x58, 0x12, 0x0c, 0x0a, 0x01, 0x59, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, - 0x52, 0x01, 0x59, 0x22, 0x6f, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x61, 0x74, - 0x61, 0x12, 0x18, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, + 0x52, 0x01, 0x59, 0x22, 0x7f, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x61, 0x74, + 0x61, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, + 0x64, 0x12, 0x18, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x4e, - 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, - 0x33, 0x0a, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x18, 0x03, + 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, + 0x33, 0x0a, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x52, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x22, 0x5c, 0x0a, 0x11, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, @@ -255,8 +309,10 @@ var file_clientdata_proto_rawDesc = []byte{ 0x65, 0x73, 0x22, 0x38, 0x0a, 0x0a, 0x41, 0x6c, 0x6c, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x2a, 0x0a, 0x07, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, - 0x61, 0x74, 0x61, 0x52, 0x07, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x42, 0x06, 0x5a, 0x04, - 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x61, 0x74, 0x61, 0x52, 0x07, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x20, 0x0a, 0x0e, + 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x0e, + 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, 0x64, 0x42, 0x06, + 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -271,12 +327,13 @@ func file_clientdata_proto_rawDescGZIP() []byte { return file_clientdata_proto_rawDescData } -var file_clientdata_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_clientdata_proto_msgTypes = make([]protoimpl.MessageInfo, 5) var file_clientdata_proto_goTypes = []any{ (*Coordinates)(nil), // 0: main.Coordinates (*ClientData)(nil), // 1: main.ClientData (*ClientCoordinates)(nil), // 2: main.ClientCoordinates (*AllClients)(nil), // 3: main.AllClients + (*ClientIdentity)(nil), // 4: main.ClientIdentity } var file_clientdata_proto_depIdxs = []int32{ 0, // 0: main.ClientData.coordinates:type_name -> main.Coordinates @@ -300,7 +357,7 @@ func file_clientdata_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_clientdata_proto_rawDesc, NumEnums: 0, - NumMessages: 4, + NumMessages: 5, NumExtensions: 0, NumServices: 0, }, diff --git a/server/server/server.go b/server/server/server.go index fcb05a2..0641a7f 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -15,12 +15,14 @@ import ( ) type ClientData struct { + Id int Address string Name string Position gamedata.Coordinates } type Server struct { + generatorId int //clientlist map[net.Conn]string clientlist map[net.Conn]ClientData mu sync.Mutex @@ -51,11 +53,15 @@ func (s *Server) Start(port int) { continue } log.Println("New connection " + conn.RemoteAddr().String()) - go s.HandleClient(conn) + + //increment the client count by one + s.generatorId++ + + go s.HandleClient(conn, s.generatorId) } } -func (s *Server) HandleClient(conn net.Conn) { +func (s *Server) HandleClient(conn net.Conn, id int) { //remove client from list upon disconnection defer func() { @@ -66,6 +72,32 @@ func (s *Server) HandleClient(conn net.Conn) { conn.Close() }() + //before we add the client to the clientlist, let's send them their unique id first + identifier := &pb.ClientIdentity{ + Id: int32(id), + } + + data, err := proto.Marshal(identifier) + if err != nil { + fmt.Println("serialization of id failed: ", err) + return + } + + length := len(data) + buf := make([]byte, 4+length) + binary.BigEndian.PutUint32(buf[:4], uint32(length)) + copy(buf[4:], data) + + totalWritten := 0 + for totalWritten < len(buf) { + n, err := conn.Write(buf[totalWritten:]) + if err != nil { + fmt.Println("Error assigning id: ", err) + return + } + totalWritten += n + } + //create the outline of client data, so far we know only //their addr, then add it to our client list clientdata := ClientData{ @@ -105,9 +137,10 @@ func (s *Server) HandleClient(conn net.Conn) { return } - fmt.Println(coords.Name) + //fmt.Println(coords.Name) cd := ClientData{ + Id: id, Address: conn.RemoteAddr().String(), Name: coords.Name, Position: gamedata.Coordinates{X: coords.Coordinates.X, Y: coords.Coordinates.Y}, @@ -226,6 +259,7 @@ func (s *Server) BuildBroadcastMessage() *pb.AllClients { for _, data := range s.clientlist { clientdata := &pb.ClientData{ + Id: int32(data.Id), Address: data.Address, Name: data.Name, Coordinates: &pb.Coordinates{