private void ClientSocket_ConnectCompleted(object sender, TcpCompletedEventArgs e) { if (!e.Error) { Console.WriteLine($"Connected to {(IPEndPoint)e.Data}"); PacketProtocol = new PacketProtocol(ClientSocket); PacketProtocol.PacketArrived += PacketProtocol_PacketArrived; PacketProtocol.Start(); Console.WriteLine("Sending client info..."); PacketProtocol.SendPacket(ClientSocket, new Packet { Content = World.LocalPlayer, Type = PacketType.PLAYER_INFO }); World.AddPlayer(World.LocalPlayer); ConnectedToServer?.Invoke(ClientSocket.RemoteEndPoint.ToString(), World.LocalPlayer.Name); _connected = true; } else { CriticalErrorOccured($"Произошла критическая ошибка!\n{((Exception)e.Data).Message}"); } }
private void ClientSocket_ShutdownCompleted(object sender, TcpCompletedEventArgs e) { Console.WriteLine("Disconnected from server."); PacketProtocol.PacketArrived -= PacketProtocol_PacketArrived; ClientSocket.ConnectCompleted -= ClientSocket_ConnectCompleted; ClientSocket.ShutdownCompleted -= ClientSocket_ShutdownCompleted; ClientSocket.Close(); ClientSocket = null; PacketProtocol = null; _connected = false; this.DisconnectedFromServer(); }
private void ServerSocket_AcceptCompleted(object sender, TcpCompletedEventArgs e) { ServerSocket.ClientAcceptAsync(); var client = (GameTcpServerConnection)e.Data; var protocol = new PacketProtocol(client); var context = new ClientContext(); context.PacketProtocol = protocol; context.Connected = true; clients.Add(client, context); protocol.PacketArrived += Protocol_PacketArrived; //client.SendCompleted += Client_SendCompleted; protocol.Start(); Console.WriteLine($"Client {client.RemoteEndPoint} connected to server."); }
private void Protocol_PacketArrived(object sender, TcpCompletedEventArgs e) { var clientSocket = (GameTcpServerConnection)sender; if (e.Error) { Console.WriteLine($"Server closed connection with client during error: {e.Data}"); CloseConnectionWithClient(clientSocket); } else if (e.Data == null) { Console.WriteLine($"Client {clientSocket.RemoteEndPoint} disconnected."); CloseConnectionWithClient(clientSocket); } else { var bytePacket = Packet.Deserialize((byte[])e.Data); Packet packet = bytePacket as Packet; if (packet != null) { var context = clients.Where(x => x.Key == clientSocket).FirstOrDefault().Value; switch (packet.Type) { case PacketType.PLAYER_INFO: context.Player = new PlayerMp((PlayerShared)packet.Content); PacketProtocol.SendPacket(clientSocket, new Packet { Content = context.Player, Type = PacketType.PLAYER_INFO }); BroadcastPacket(new Packet { Type = PacketType.MESSAGE_SYSTEM, Content = $"connected>{context.Player.Name}" }, context.Player.Guid); _tickingEntities.Add(context.Player); break; case PacketType.MESSAGE_CHAT: BroadcastPacket(packet, Guid.Empty); break; case PacketType.PLAYER_MOVE: context.Player.AddPos((Vector2)packet.Content); break; case PacketType.BULLET_SHOOT: var bullet = new BulletMp((Vector2)packet.Content, context.Player.Position, context.Player.Guid); _tickingEntities.Add(bullet); break; case PacketType.PLAYER_REVIVE: context.Player.Spawn(); break; default: break; } } } }
private void PacketProtocol_PacketArrived(object sender, TcpCompletedEventArgs e) { if (!e.Error) { var bytePacket = Packet.Deserialize((byte[])e.Data); var packet = bytePacket as Packet; if (packet != null) { var player = packet.Content as PlayerShared; var bullet = packet.Content as BulletShared; PlayerSp playerToUpdate = null; BulletSp bulletToUpdate = null; if (player != null) { playerToUpdate = World.Players.Where(x => x.Guid == player.Guid).FirstOrDefault(); } if (bullet != null) { bulletToUpdate = World.Bullets.Where(x => x.Guid == bullet.Guid).FirstOrDefault(); } switch (packet.Type) { case PacketType.PLAYER_INFO: World.LocalPlayer.UpdatePlayer(player); break; case PacketType.PLAYER_STATE: if (playerToUpdate != null) { playerToUpdate.UpdatePlayer(player); } else { World.Players.Add(new PlayerSp(player)); } break; case PacketType.PLAYER_DISCONNECTED: if (playerToUpdate != null) { World.MarkRemoved(playerToUpdate); ChatMessageArrived?.Invoke($"{playerToUpdate.Name} left server!\n"); } break; case PacketType.BULLET_STATE: if (bulletToUpdate != null) { bulletToUpdate.Update(bullet); } else if (bullet != null) { World.Bullets.Add(new BulletSp(bullet)); } break; case PacketType.BULLET_REMOVE: if (bulletToUpdate != null) { World.MarkRemoved(bulletToUpdate); } break; case PacketType.PLAYER_HIT: break; case PacketType.PLAYER_DEAD: ChatMessageArrived?.Invoke($"{(string)packet.Content} умирает\n"); break; case PacketType.MESSAGE_CHAT: ChatMessageArrived?.Invoke((string)packet.Content); break; case PacketType.MESSAGE_SYSTEM: var sysMsg = ((string)packet.Content).Split('>'); switch (sysMsg[0].ToLower()) { case "connected": ChatMessageArrived?.Invoke($"{sysMsg[1]} connected to server!\n"); break; case "msg": ChatMessageArrived?.Invoke($"{sysMsg[1]}\n"); break; default: ChatMessageArrived?.Invoke($"(!) UNHANDLED SYSTEM MESSAGE (!)\n"); break; } break; default: Console.WriteLine("(!) GOT UNHANDLED PACKET TYPE (!)"); break; } } else { Console.WriteLine($"Server send corrupted packet!"); } } else { CriticalErrorOccured($"Произошла критическая ошибка!\n{((Exception)e.Data).Message}"); } }