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

@@ -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;
}

View File

@@ -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,
},

View File

@@ -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{