Client list no longer dependent on cleanup; server will alert clients upon connection/disconnection.

This commit is contained in:
2024-12-11 18:18:04 -05:00
parent efbc59ce2d
commit ad6a061e5a
5 changed files with 295 additions and 136 deletions

View File

@@ -34,23 +34,6 @@ func NewClient() *Client {
return c
}
func (c *Client) SendProtoData(coords *pb.ClientCoordinates) {
data, err := proto.Marshal(coords)
if err != nil {
fmt.Println("Couldn't serialize. ", err)
return
}
// Add length prefix
length := uint32(len(data))
buf := make([]byte, 4+len(data))
binary.BigEndian.PutUint32(buf[:4], length)
copy(buf[4:], data)
c.SendBuffer(c.conn, buf)
}
func (c *Client) SendBuffer(conn net.Conn, buf []byte) {
// Ensure complete write
totalWritten := 0

View File

@@ -17,8 +17,9 @@ import (
)
var (
screenWidth = 640
screenHeight = 480
screenWidth = 640
screenHeight = 480
movementLimit = 5
namelist = []string{"slappy", "mick", "rodney", "george", "ringo", "robin", "temitry "}
)
@@ -36,21 +37,14 @@ type ClientData struct {
}
type Game struct {
name string
blocky *elements.Block
hitblocky *elements.Block
gameId int
//players map[client.Identity]
clients map[string]ClientData
gameclient *client.Client
cycle int
position gamedata.Coordinates
mu sync.Mutex
name string
blocky *elements.Block
hitblocky *elements.Block
gameId int
realclients map[int]ClientData
gameclient *client.Client
cycle int
mu sync.Mutex
}
func NewGame() *Game {
@@ -64,7 +58,9 @@ func NewGame() *Game {
g.hitblocky.SetHit(true)
g.clients = make(map[string]ClientData)
g.blocky.SetPosition(gamedata.Coordinates{X: float64(screenWidth) / 2, Y: float64(screenHeight) / 2})
g.realclients = make(map[int]ClientData)
//g.gameId = g.gameclient.GetIdentity()
go g.gameclient.ReadData(g.HandleServerData)
@@ -73,47 +69,29 @@ func NewGame() *Game {
func (g *Game) Update() error {
x, y := ebiten.CursorPosition()
g.position.X = float64(x)
g.position.Y = float64(y)
g.blocky.SetPosition(g.position)
g.HandleInput()
//broadcast our position
if g.cycle%2 == 0 {
if g.gameclient.IsConnected() {
//g.gameclient.SendData(fmt.Sprintf("%s,%.0f,%.0f\n", g.name, g.position.X, g.position.Y))
cd := &pb.ClientCoordinates{
Name: g.name,
Coordinates: &pb.Coordinates{
X: g.position.X,
Y: g.position.Y,
},
}
envelope := &pb.ClientEnvelope{
Payload: &pb.ClientEnvelope_Coordinates{
Coordinates: cd,
},
}
g.gameclient.SendMessage(envelope)
//g.gameclient.SendProtoData(cd)
/*
cd := *client.ClientData{
Name: g.name,
Address: g.
}*/
//if g.cycle%2 == 0 {
if g.gameclient.IsConnected() {
cd := &pb.ClientCoordinates{
Name: g.name,
Coordinates: &pb.Coordinates{
X: g.blocky.GetPosition().X, //g.position.X,
Y: g.blocky.GetPosition().Y, //g.position.Y,
},
}
}
//cleanup client list every 2 seconds
if g.cycle%120 == 0 {
go g.CleanupClients()
envelope := &pb.ClientEnvelope{
Payload: &pb.ClientEnvelope_Coordinates{
Coordinates: cd,
},
}
g.gameclient.SendMessage(envelope)
}
//}
g.cycle++
return nil
@@ -138,7 +116,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
text.Draw(screen, "you ("+g.name+")", f2, top)
g.mu.Lock()
clientcopy := maps.Clone(g.clients)
clientcopy := maps.Clone(g.realclients)
g.mu.Unlock()
for _, client := range clientcopy {
op := &ebiten.DrawImageOptions{}
@@ -185,21 +163,46 @@ func (g *Game) HandleServerData(envelope *pb.ServerEnvelope) {
}
g.mu.Lock()
g.clients[update.Address] = update
g.realclients[int(client.Id)] = update
g.mu.Unlock()
} else {
g.blocky.SetHit(client.Hit)
}
}
case *pb.ServerEnvelope_Event:
//add or remove client from client list
if payload.Event.Connected && payload.Event.Id != int32(g.gameId) {
realclient := ClientData{
Id: int(payload.Event.Id),
}
g.realclients[int(payload.Event.Id)] = realclient
} else {
delete(g.realclients, int(payload.Event.Id))
}
case *pb.ServerEnvelope_Identity:
fmt.Println("Server is trying to give us our id.")
g.gameId = int(payload.Identity.Id)
}
}
func (g *Game) CleanupClients() {
g.mu.Lock()
for k := range g.clients {
delete(g.clients, k)
func (g *Game) HandleInput() {
dx := 0
dy := 0
if ebiten.IsKeyPressed(ebiten.KeyW) {
dy = -movementLimit
}
g.mu.Unlock()
if ebiten.IsKeyPressed(ebiten.KeyS) {
dy = +movementLimit
}
if ebiten.IsKeyPressed(ebiten.KeyA) {
dx = -movementLimit
}
if ebiten.IsKeyPressed(ebiten.KeyD) {
dx = +movementLimit
}
cpos := g.blocky.GetPosition()
cpos.X += float64(dx)
cpos.Y += float64(dy)
g.blocky.SetPosition(cpos)
}

View File

@@ -407,6 +407,59 @@ type ClientEnvelope_Coordinates struct {
func (*ClientEnvelope_Coordinates) isClientEnvelope_Payload() {}
type ClientEvent struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"`
Connected bool `protobuf:"varint,2,opt,name=Connected,proto3" json:"Connected,omitempty"`
}
func (x *ClientEvent) Reset() {
*x = ClientEvent{}
mi := &file_clientdata_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *ClientEvent) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ClientEvent) ProtoMessage() {}
func (x *ClientEvent) ProtoReflect() protoreflect.Message {
mi := &file_clientdata_proto_msgTypes[6]
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 ClientEvent.ProtoReflect.Descriptor instead.
func (*ClientEvent) Descriptor() ([]byte, []int) {
return file_clientdata_proto_rawDescGZIP(), []int{6}
}
func (x *ClientEvent) GetId() int32 {
if x != nil {
return x.Id
}
return 0
}
func (x *ClientEvent) GetConnected() bool {
if x != nil {
return x.Connected
}
return false
}
type ServerEnvelope struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -416,12 +469,13 @@ type ServerEnvelope struct {
//
// *ServerEnvelope_Identity
// *ServerEnvelope_Broadcast
// *ServerEnvelope_Event
Payload isServerEnvelope_Payload `protobuf_oneof:"Payload"`
}
func (x *ServerEnvelope) Reset() {
*x = ServerEnvelope{}
mi := &file_clientdata_proto_msgTypes[6]
mi := &file_clientdata_proto_msgTypes[7]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -433,7 +487,7 @@ func (x *ServerEnvelope) String() string {
func (*ServerEnvelope) ProtoMessage() {}
func (x *ServerEnvelope) ProtoReflect() protoreflect.Message {
mi := &file_clientdata_proto_msgTypes[6]
mi := &file_clientdata_proto_msgTypes[7]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -446,7 +500,7 @@ func (x *ServerEnvelope) ProtoReflect() protoreflect.Message {
// Deprecated: Use ServerEnvelope.ProtoReflect.Descriptor instead.
func (*ServerEnvelope) Descriptor() ([]byte, []int) {
return file_clientdata_proto_rawDescGZIP(), []int{6}
return file_clientdata_proto_rawDescGZIP(), []int{7}
}
func (m *ServerEnvelope) GetPayload() isServerEnvelope_Payload {
@@ -470,6 +524,13 @@ func (x *ServerEnvelope) GetBroadcast() *AllClients {
return nil
}
func (x *ServerEnvelope) GetEvent() *ClientEvent {
if x, ok := x.GetPayload().(*ServerEnvelope_Event); ok {
return x.Event
}
return nil
}
type isServerEnvelope_Payload interface {
isServerEnvelope_Payload()
}
@@ -482,10 +543,16 @@ type ServerEnvelope_Broadcast struct {
Broadcast *AllClients `protobuf:"bytes,2,opt,name=broadcast,proto3,oneof"`
}
type ServerEnvelope_Event struct {
Event *ClientEvent `protobuf:"bytes,3,opt,name=event,proto3,oneof"`
}
func (*ServerEnvelope_Identity) isServerEnvelope_Payload() {}
func (*ServerEnvelope_Broadcast) isServerEnvelope_Payload() {}
func (*ServerEnvelope_Event) isServerEnvelope_Payload() {}
var File_clientdata_proto protoreflect.FileDescriptor
var file_clientdata_proto_rawDesc = []byte{
@@ -519,22 +586,28 @@ var file_clientdata_proto_rawDesc = []byte{
0x65, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e,
0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65,
0x73, 0x48, 0x00, 0x52, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73,
0x42, 0x09, 0x0a, 0x07, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x81, 0x01, 0x0a, 0x0e,
0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x45, 0x6e, 0x76, 0x65, 0x6c, 0x6f, 0x70, 0x65, 0x12, 0x32,
0x0a, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x14, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64,
0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x48, 0x00, 0x52, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69,
0x74, 0x79, 0x12, 0x30, 0x0a, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x18,
0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x41, 0x6c, 0x6c,
0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x48, 0x00, 0x52, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64,
0x63, 0x61, 0x73, 0x74, 0x42, 0x09, 0x0a, 0x07, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x2a,
0x56, 0x0a, 0x0b, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x16,
0x0a, 0x12, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x42, 0x52, 0x4f, 0x41, 0x44,
0x43, 0x41, 0x53, 0x54, 0x10, 0x00, 0x12, 0x15, 0x0a, 0x11, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59,
0x50, 0x45, 0x5f, 0x49, 0x44, 0x45, 0x4e, 0x54, 0x49, 0x54, 0x59, 0x10, 0x01, 0x12, 0x18, 0x0a,
0x14, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x49,
0x4e, 0x41, 0x54, 0x45, 0x53, 0x10, 0x02, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x42, 0x09, 0x0a, 0x07, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x3b, 0x0a, 0x0b, 0x43,
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x43, 0x6f,
0x6e, 0x6e, 0x65, 0x63, 0x74, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x43,
0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x65, 0x64, 0x22, 0xac, 0x01, 0x0a, 0x0e, 0x53, 0x65, 0x72,
0x76, 0x65, 0x72, 0x45, 0x6e, 0x76, 0x65, 0x6c, 0x6f, 0x70, 0x65, 0x12, 0x32, 0x0a, 0x08, 0x69,
0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e,
0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x65, 0x6e, 0x74,
0x69, 0x74, 0x79, 0x48, 0x00, 0x52, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12,
0x30, 0x0a, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01,
0x28, 0x0b, 0x32, 0x10, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x41, 0x6c, 0x6c, 0x43, 0x6c, 0x69,
0x65, 0x6e, 0x74, 0x73, 0x48, 0x00, 0x52, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73,
0x74, 0x12, 0x29, 0x0a, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x11, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45, 0x76,
0x65, 0x6e, 0x74, 0x48, 0x00, 0x52, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x42, 0x09, 0x0a, 0x07,
0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x2a, 0x56, 0x0a, 0x0b, 0x4d, 0x65, 0x73, 0x73, 0x61,
0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x12, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59,
0x50, 0x45, 0x5f, 0x42, 0x52, 0x4f, 0x41, 0x44, 0x43, 0x41, 0x53, 0x54, 0x10, 0x00, 0x12, 0x15,
0x0a, 0x11, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x49, 0x44, 0x45, 0x4e, 0x54,
0x49, 0x54, 0x59, 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50,
0x45, 0x5f, 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x49, 0x4e, 0x41, 0x54, 0x45, 0x53, 0x10, 0x02, 0x42,
0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -550,7 +623,7 @@ func file_clientdata_proto_rawDescGZIP() []byte {
}
var file_clientdata_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_clientdata_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
var file_clientdata_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
var file_clientdata_proto_goTypes = []any{
(MessageType)(0), // 0: main.MessageType
(*Coordinates)(nil), // 1: main.Coordinates
@@ -559,7 +632,8 @@ var file_clientdata_proto_goTypes = []any{
(*AllClients)(nil), // 4: main.AllClients
(*ClientIdentity)(nil), // 5: main.ClientIdentity
(*ClientEnvelope)(nil), // 6: main.ClientEnvelope
(*ServerEnvelope)(nil), // 7: main.ServerEnvelope
(*ClientEvent)(nil), // 7: main.ClientEvent
(*ServerEnvelope)(nil), // 8: main.ServerEnvelope
}
var file_clientdata_proto_depIdxs = []int32{
1, // 0: main.ClientData.coordinates:type_name -> main.Coordinates
@@ -568,11 +642,12 @@ var file_clientdata_proto_depIdxs = []int32{
3, // 3: main.ClientEnvelope.coordinates:type_name -> main.ClientCoordinates
5, // 4: main.ServerEnvelope.identity:type_name -> main.ClientIdentity
4, // 5: main.ServerEnvelope.broadcast:type_name -> main.AllClients
6, // [6:6] is the sub-list for method output_type
6, // [6:6] is the sub-list for method input_type
6, // [6:6] is the sub-list for extension type_name
6, // [6:6] is the sub-list for extension extendee
0, // [0:6] is the sub-list for field type_name
7, // 6: main.ServerEnvelope.event:type_name -> main.ClientEvent
7, // [7:7] is the sub-list for method output_type
7, // [7:7] is the sub-list for method input_type
7, // [7:7] is the sub-list for extension type_name
7, // [7:7] is the sub-list for extension extendee
0, // [0:7] is the sub-list for field type_name
}
func init() { file_clientdata_proto_init() }
@@ -583,9 +658,10 @@ func file_clientdata_proto_init() {
file_clientdata_proto_msgTypes[5].OneofWrappers = []any{
(*ClientEnvelope_Coordinates)(nil),
}
file_clientdata_proto_msgTypes[6].OneofWrappers = []any{
file_clientdata_proto_msgTypes[7].OneofWrappers = []any{
(*ServerEnvelope_Identity)(nil),
(*ServerEnvelope_Broadcast)(nil),
(*ServerEnvelope_Event)(nil),
}
type x struct{}
out := protoimpl.TypeBuilder{
@@ -593,7 +669,7 @@ func file_clientdata_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_clientdata_proto_rawDesc,
NumEnums: 1,
NumMessages: 7,
NumMessages: 8,
NumExtensions: 0,
NumServices: 0,
},

View File

@@ -407,6 +407,59 @@ type ClientEnvelope_Coordinates struct {
func (*ClientEnvelope_Coordinates) isClientEnvelope_Payload() {}
type ClientEvent struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"`
Connected bool `protobuf:"varint,2,opt,name=Connected,proto3" json:"Connected,omitempty"`
}
func (x *ClientEvent) Reset() {
*x = ClientEvent{}
mi := &file_clientdata_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *ClientEvent) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ClientEvent) ProtoMessage() {}
func (x *ClientEvent) ProtoReflect() protoreflect.Message {
mi := &file_clientdata_proto_msgTypes[6]
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 ClientEvent.ProtoReflect.Descriptor instead.
func (*ClientEvent) Descriptor() ([]byte, []int) {
return file_clientdata_proto_rawDescGZIP(), []int{6}
}
func (x *ClientEvent) GetId() int32 {
if x != nil {
return x.Id
}
return 0
}
func (x *ClientEvent) GetConnected() bool {
if x != nil {
return x.Connected
}
return false
}
type ServerEnvelope struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -416,12 +469,13 @@ type ServerEnvelope struct {
//
// *ServerEnvelope_Identity
// *ServerEnvelope_Broadcast
// *ServerEnvelope_Event
Payload isServerEnvelope_Payload `protobuf_oneof:"Payload"`
}
func (x *ServerEnvelope) Reset() {
*x = ServerEnvelope{}
mi := &file_clientdata_proto_msgTypes[6]
mi := &file_clientdata_proto_msgTypes[7]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -433,7 +487,7 @@ func (x *ServerEnvelope) String() string {
func (*ServerEnvelope) ProtoMessage() {}
func (x *ServerEnvelope) ProtoReflect() protoreflect.Message {
mi := &file_clientdata_proto_msgTypes[6]
mi := &file_clientdata_proto_msgTypes[7]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -446,7 +500,7 @@ func (x *ServerEnvelope) ProtoReflect() protoreflect.Message {
// Deprecated: Use ServerEnvelope.ProtoReflect.Descriptor instead.
func (*ServerEnvelope) Descriptor() ([]byte, []int) {
return file_clientdata_proto_rawDescGZIP(), []int{6}
return file_clientdata_proto_rawDescGZIP(), []int{7}
}
func (m *ServerEnvelope) GetPayload() isServerEnvelope_Payload {
@@ -470,6 +524,13 @@ func (x *ServerEnvelope) GetBroadcast() *AllClients {
return nil
}
func (x *ServerEnvelope) GetEvent() *ClientEvent {
if x, ok := x.GetPayload().(*ServerEnvelope_Event); ok {
return x.Event
}
return nil
}
type isServerEnvelope_Payload interface {
isServerEnvelope_Payload()
}
@@ -482,10 +543,16 @@ type ServerEnvelope_Broadcast struct {
Broadcast *AllClients `protobuf:"bytes,2,opt,name=broadcast,proto3,oneof"`
}
type ServerEnvelope_Event struct {
Event *ClientEvent `protobuf:"bytes,3,opt,name=event,proto3,oneof"`
}
func (*ServerEnvelope_Identity) isServerEnvelope_Payload() {}
func (*ServerEnvelope_Broadcast) isServerEnvelope_Payload() {}
func (*ServerEnvelope_Event) isServerEnvelope_Payload() {}
var File_clientdata_proto protoreflect.FileDescriptor
var file_clientdata_proto_rawDesc = []byte{
@@ -519,22 +586,28 @@ var file_clientdata_proto_rawDesc = []byte{
0x65, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e,
0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65,
0x73, 0x48, 0x00, 0x52, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73,
0x42, 0x09, 0x0a, 0x07, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x81, 0x01, 0x0a, 0x0e,
0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x45, 0x6e, 0x76, 0x65, 0x6c, 0x6f, 0x70, 0x65, 0x12, 0x32,
0x0a, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x14, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64,
0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x48, 0x00, 0x52, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69,
0x74, 0x79, 0x12, 0x30, 0x0a, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x18,
0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x41, 0x6c, 0x6c,
0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x73, 0x48, 0x00, 0x52, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64,
0x63, 0x61, 0x73, 0x74, 0x42, 0x09, 0x0a, 0x07, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x2a,
0x56, 0x0a, 0x0b, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x16,
0x0a, 0x12, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x42, 0x52, 0x4f, 0x41, 0x44,
0x43, 0x41, 0x53, 0x54, 0x10, 0x00, 0x12, 0x15, 0x0a, 0x11, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59,
0x50, 0x45, 0x5f, 0x49, 0x44, 0x45, 0x4e, 0x54, 0x49, 0x54, 0x59, 0x10, 0x01, 0x12, 0x18, 0x0a,
0x14, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x49,
0x4e, 0x41, 0x54, 0x45, 0x53, 0x10, 0x02, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x42, 0x09, 0x0a, 0x07, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0x3b, 0x0a, 0x0b, 0x43,
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x43, 0x6f,
0x6e, 0x6e, 0x65, 0x63, 0x74, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x43,
0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x65, 0x64, 0x22, 0xac, 0x01, 0x0a, 0x0e, 0x53, 0x65, 0x72,
0x76, 0x65, 0x72, 0x45, 0x6e, 0x76, 0x65, 0x6c, 0x6f, 0x70, 0x65, 0x12, 0x32, 0x0a, 0x08, 0x69,
0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e,
0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x65, 0x6e, 0x74,
0x69, 0x74, 0x79, 0x48, 0x00, 0x52, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12,
0x30, 0x0a, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01,
0x28, 0x0b, 0x32, 0x10, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x41, 0x6c, 0x6c, 0x43, 0x6c, 0x69,
0x65, 0x6e, 0x74, 0x73, 0x48, 0x00, 0x52, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73,
0x74, 0x12, 0x29, 0x0a, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x11, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45, 0x76,
0x65, 0x6e, 0x74, 0x48, 0x00, 0x52, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x42, 0x09, 0x0a, 0x07,
0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x2a, 0x56, 0x0a, 0x0b, 0x4d, 0x65, 0x73, 0x73, 0x61,
0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x12, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59,
0x50, 0x45, 0x5f, 0x42, 0x52, 0x4f, 0x41, 0x44, 0x43, 0x41, 0x53, 0x54, 0x10, 0x00, 0x12, 0x15,
0x0a, 0x11, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x49, 0x44, 0x45, 0x4e, 0x54,
0x49, 0x54, 0x59, 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50,
0x45, 0x5f, 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x49, 0x4e, 0x41, 0x54, 0x45, 0x53, 0x10, 0x02, 0x42,
0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -550,7 +623,7 @@ func file_clientdata_proto_rawDescGZIP() []byte {
}
var file_clientdata_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_clientdata_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
var file_clientdata_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
var file_clientdata_proto_goTypes = []any{
(MessageType)(0), // 0: main.MessageType
(*Coordinates)(nil), // 1: main.Coordinates
@@ -559,7 +632,8 @@ var file_clientdata_proto_goTypes = []any{
(*AllClients)(nil), // 4: main.AllClients
(*ClientIdentity)(nil), // 5: main.ClientIdentity
(*ClientEnvelope)(nil), // 6: main.ClientEnvelope
(*ServerEnvelope)(nil), // 7: main.ServerEnvelope
(*ClientEvent)(nil), // 7: main.ClientEvent
(*ServerEnvelope)(nil), // 8: main.ServerEnvelope
}
var file_clientdata_proto_depIdxs = []int32{
1, // 0: main.ClientData.coordinates:type_name -> main.Coordinates
@@ -568,11 +642,12 @@ var file_clientdata_proto_depIdxs = []int32{
3, // 3: main.ClientEnvelope.coordinates:type_name -> main.ClientCoordinates
5, // 4: main.ServerEnvelope.identity:type_name -> main.ClientIdentity
4, // 5: main.ServerEnvelope.broadcast:type_name -> main.AllClients
6, // [6:6] is the sub-list for method output_type
6, // [6:6] is the sub-list for method input_type
6, // [6:6] is the sub-list for extension type_name
6, // [6:6] is the sub-list for extension extendee
0, // [0:6] is the sub-list for field type_name
7, // 6: main.ServerEnvelope.event:type_name -> main.ClientEvent
7, // [7:7] is the sub-list for method output_type
7, // [7:7] is the sub-list for method input_type
7, // [7:7] is the sub-list for extension type_name
7, // [7:7] is the sub-list for extension extendee
0, // [0:7] is the sub-list for field type_name
}
func init() { file_clientdata_proto_init() }
@@ -583,9 +658,10 @@ func file_clientdata_proto_init() {
file_clientdata_proto_msgTypes[5].OneofWrappers = []any{
(*ClientEnvelope_Coordinates)(nil),
}
file_clientdata_proto_msgTypes[6].OneofWrappers = []any{
file_clientdata_proto_msgTypes[7].OneofWrappers = []any{
(*ServerEnvelope_Identity)(nil),
(*ServerEnvelope_Broadcast)(nil),
(*ServerEnvelope_Event)(nil),
}
type x struct{}
out := protoimpl.TypeBuilder{
@@ -593,7 +669,7 @@ func file_clientdata_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_clientdata_proto_rawDesc,
NumEnums: 1,
NumMessages: 7,
NumMessages: 8,
NumExtensions: 0,
NumServices: 0,
},

View File

@@ -75,11 +75,13 @@ func (s *Server) HandleClient(conn net.Conn, id int) {
s.mu.Lock()
delete(s.clientlist, conn)
s.mu.Unlock()
s.SendClientEvent(conn, id, false)
conn.Close()
}()
//before we add the client to the clientlist, let's send them their unique id first
s.SendClientIdentity(conn, id)
s.SendClientEvent(conn, id, true)
for {
// Read data from the client
@@ -178,7 +180,7 @@ func (s *Server) ManageBroadcast() {
}
s.mu.Unlock()
time.Sleep(time.Millisecond * 30)
time.Sleep(time.Millisecond * 15)
}
}
@@ -252,3 +254,22 @@ func (s *Server) SendClientIdentity(conn net.Conn, id int) {
s.SendMessage(conn, envelope)
}
func (s *Server) SendClientEvent(conn net.Conn, id int, connected bool) {
event := &pb.ClientEvent{
Id: int32(id),
Connected: connected,
}
envelope := &pb.ServerEnvelope{
Payload: &pb.ServerEnvelope_Event{
Event: event,
},
}
s.mu.Lock()
for conn := range s.clientlist {
s.SendMessage(conn, envelope)
}
s.mu.Unlock()
}