Server now provides clientid on connection. Client expects this before continuing.

This commit is contained in:
2024-12-10 22:24:45 -05:00
parent c439c42b2f
commit 9345b49f57
6 changed files with 215 additions and 30 deletions

View File

@@ -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)) { func (c *Client) ReadData(callback func(*pb.AllClients)) {
/* /*

View File

@@ -27,6 +27,7 @@ func init() {
} }
type ClientData struct { type ClientData struct {
Id int
Address string Address string
Name string Name string
Position gamedata.Coordinates Position gamedata.Coordinates
@@ -35,6 +36,7 @@ type ClientData struct {
type Game struct { type Game struct {
name string name string
blocky *elements.Block blocky *elements.Block
gameId int
//players map[client.Identity] //players map[client.Identity]
@@ -56,6 +58,8 @@ func NewGame() *Game {
name: namelist[rand.Intn(len(namelist))], name: namelist[rand.Intn(len(namelist))],
} }
g.clients = make(map[string]ClientData) g.clients = make(map[string]ClientData)
g.gameId = g.gameclient.GetIdentity()
go g.gameclient.ReadData(g.HandleServerData) go g.gameclient.ReadData(g.HandleServerData)
return g return g
} }
@@ -145,8 +149,8 @@ func (g *Game) HandleServerData(allclients *pb.AllClients) {
for _, client := range allclients.Clients { for _, client := range allclients.Clients {
//fmt.Println(client.Coordinates.X) //fmt.Println(client.Coordinates.X)
localaddr := g.gameclient.GetLocalAddr() //localaddr := g.gameclient.GetLocalAddr()
if client.Address != localaddr { if client.Id != int32(g.gameId) {
update := ClientData{ update := ClientData{
Address: client.Address, Address: client.Address,
Name: client.Name, Name: client.Name,

View File

@@ -78,9 +78,10 @@ type ClientData struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Address string `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty"` Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=Name,proto3" json:"Name,omitempty"` Address string `protobuf:"bytes,2,opt,name=Address,proto3" json:"Address,omitempty"`
Coordinates *Coordinates `protobuf:"bytes,3,opt,name=coordinates,proto3" json:"coordinates,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() { func (x *ClientData) Reset() {
@@ -113,6 +114,13 @@ func (*ClientData) Descriptor() ([]byte, []int) {
return file_clientdata_proto_rawDescGZIP(), []int{1} 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 { func (x *ClientData) GetAddress() string {
if x != nil { if x != nil {
return x.Address return x.Address
@@ -232,6 +240,51 @@ func (x *AllClients) GetClients() []*ClientData {
return nil 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 protoreflect.FileDescriptor
var file_clientdata_proto_rawDesc = []byte{ 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, 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, 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, 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, 0x52, 0x01, 0x59, 0x22, 0x7f, 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, 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, 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, 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, 0x03, 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, 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, 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, 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, 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, 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, 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, 0x61, 0x74, 0x61, 0x52, 0x07, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x20, 0x0a, 0x0e,
0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 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 ( var (
@@ -271,12 +327,13 @@ func file_clientdata_proto_rawDescGZIP() []byte {
return file_clientdata_proto_rawDescData 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{ var file_clientdata_proto_goTypes = []any{
(*Coordinates)(nil), // 0: main.Coordinates (*Coordinates)(nil), // 0: main.Coordinates
(*ClientData)(nil), // 1: main.ClientData (*ClientData)(nil), // 1: main.ClientData
(*ClientCoordinates)(nil), // 2: main.ClientCoordinates (*ClientCoordinates)(nil), // 2: main.ClientCoordinates
(*AllClients)(nil), // 3: main.AllClients (*AllClients)(nil), // 3: main.AllClients
(*ClientIdentity)(nil), // 4: main.ClientIdentity
} }
var file_clientdata_proto_depIdxs = []int32{ var file_clientdata_proto_depIdxs = []int32{
0, // 0: main.ClientData.coordinates:type_name -> main.Coordinates 0, // 0: main.ClientData.coordinates:type_name -> main.Coordinates
@@ -300,7 +357,7 @@ func file_clientdata_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_clientdata_proto_rawDesc, RawDescriptor: file_clientdata_proto_rawDesc,
NumEnums: 0, NumEnums: 0,
NumMessages: 4, NumMessages: 5,
NumExtensions: 0, NumExtensions: 0,
NumServices: 0, NumServices: 0,
}, },

View File

@@ -10,9 +10,10 @@ message Coordinates {
} }
message ClientData { message ClientData {
string Address = 1; int32 Id = 1;
string Name = 2; string Address = 2;
Coordinates coordinates = 3; string Name = 3;
Coordinates coordinates = 4;
} }
message ClientCoordinates { message ClientCoordinates {
@@ -23,3 +24,7 @@ message ClientCoordinates {
message AllClients{ message AllClients{
repeated ClientData Clients = 1; repeated ClientData Clients = 1;
} }
message ClientIdentity{
int32 Id = 1;
}

View File

@@ -78,9 +78,10 @@ type ClientData struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Address string `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty"` Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=Name,proto3" json:"Name,omitempty"` Address string `protobuf:"bytes,2,opt,name=Address,proto3" json:"Address,omitempty"`
Coordinates *Coordinates `protobuf:"bytes,3,opt,name=coordinates,proto3" json:"coordinates,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() { func (x *ClientData) Reset() {
@@ -113,6 +114,13 @@ func (*ClientData) Descriptor() ([]byte, []int) {
return file_clientdata_proto_rawDescGZIP(), []int{1} 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 { func (x *ClientData) GetAddress() string {
if x != nil { if x != nil {
return x.Address return x.Address
@@ -232,6 +240,51 @@ func (x *AllClients) GetClients() []*ClientData {
return nil 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 protoreflect.FileDescriptor
var file_clientdata_proto_rawDesc = []byte{ 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, 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, 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, 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, 0x52, 0x01, 0x59, 0x22, 0x7f, 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, 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, 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, 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, 0x03, 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, 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, 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, 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, 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, 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, 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, 0x61, 0x74, 0x61, 0x52, 0x07, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x22, 0x20, 0x0a, 0x0e,
0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 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 ( var (
@@ -271,12 +327,13 @@ func file_clientdata_proto_rawDescGZIP() []byte {
return file_clientdata_proto_rawDescData 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{ var file_clientdata_proto_goTypes = []any{
(*Coordinates)(nil), // 0: main.Coordinates (*Coordinates)(nil), // 0: main.Coordinates
(*ClientData)(nil), // 1: main.ClientData (*ClientData)(nil), // 1: main.ClientData
(*ClientCoordinates)(nil), // 2: main.ClientCoordinates (*ClientCoordinates)(nil), // 2: main.ClientCoordinates
(*AllClients)(nil), // 3: main.AllClients (*AllClients)(nil), // 3: main.AllClients
(*ClientIdentity)(nil), // 4: main.ClientIdentity
} }
var file_clientdata_proto_depIdxs = []int32{ var file_clientdata_proto_depIdxs = []int32{
0, // 0: main.ClientData.coordinates:type_name -> main.Coordinates 0, // 0: main.ClientData.coordinates:type_name -> main.Coordinates
@@ -300,7 +357,7 @@ func file_clientdata_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_clientdata_proto_rawDesc, RawDescriptor: file_clientdata_proto_rawDesc,
NumEnums: 0, NumEnums: 0,
NumMessages: 4, NumMessages: 5,
NumExtensions: 0, NumExtensions: 0,
NumServices: 0, NumServices: 0,
}, },

View File

@@ -15,12 +15,14 @@ import (
) )
type ClientData struct { type ClientData struct {
Id int
Address string Address string
Name string Name string
Position gamedata.Coordinates Position gamedata.Coordinates
} }
type Server struct { type Server struct {
generatorId int
//clientlist map[net.Conn]string //clientlist map[net.Conn]string
clientlist map[net.Conn]ClientData clientlist map[net.Conn]ClientData
mu sync.Mutex mu sync.Mutex
@@ -51,11 +53,15 @@ func (s *Server) Start(port int) {
continue continue
} }
log.Println("New connection " + conn.RemoteAddr().String()) 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 //remove client from list upon disconnection
defer func() { defer func() {
@@ -66,6 +72,32 @@ func (s *Server) HandleClient(conn net.Conn) {
conn.Close() 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 //create the outline of client data, so far we know only
//their addr, then add it to our client list //their addr, then add it to our client list
clientdata := ClientData{ clientdata := ClientData{
@@ -105,9 +137,10 @@ func (s *Server) HandleClient(conn net.Conn) {
return return
} }
fmt.Println(coords.Name) //fmt.Println(coords.Name)
cd := ClientData{ cd := ClientData{
Id: id,
Address: conn.RemoteAddr().String(), Address: conn.RemoteAddr().String(),
Name: coords.Name, Name: coords.Name,
Position: gamedata.Coordinates{X: coords.Coordinates.X, Y: coords.Coordinates.Y}, 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 { for _, data := range s.clientlist {
clientdata := &pb.ClientData{ clientdata := &pb.ClientData{
Id: int32(data.Id),
Address: data.Address, Address: data.Address,
Name: data.Name, Name: data.Name,
Coordinates: &pb.Coordinates{ Coordinates: &pb.Coordinates{