private static void UDPReceiveCallback(IAsyncResult result) { try { IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] data = udpListener.EndReceive(result, ref clientEndPoint); udpListener.BeginReceive(UDPReceiveCallback, null); if (data.Length < 4) { return; } using (Packet packet = new Packet(data)) { int clientId = packet.ReadInt(); if (clientId == 0) { return; } if (clients[clientId].udp.endPoint == null) { clients[clientId].udp.Connect(clientEndPoint); return; } if (clients[clientId].udp.endPoint.ToString() == clientEndPoint.ToString()) { clients[clientId].udp.HandleData(packet); } } } catch (Exception ex) { Console.WriteLine($"Error receiving UDP data: {ex}"); } }
private static void OnUDPReceive(IAsyncResult result) { try { IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] data = UDPlistener.EndReceive(result, ref clientEndPoint); UDPlistener.BeginReceive(OnUDPReceive, null); if (data.Length < 4) { // todo this could potentially could cause problems under high traffic conditions return; } using (Packet packet = new Packet(data)) { int clientId = packet.ReadInt(); if (clientId == 0) { return; } // if this connection is new, start session if (clients[clientId].udp.endPoint == null) { clients[clientId].udp.Connect(clientEndPoint); return; } // todo additional security might be needed to prevent ip spoofing attack if (clients[clientId].udp.endPoint.ToString() == clientEndPoint.ToString()) { clients[clientId].udp.HandleData(packet); } } } catch (Exception e) { Console.WriteLine($"Error receiving UDP data: {e}"); } }
private static void UdpReceiveCallback(IAsyncResult ar) { try { IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] data = udpListener.EndReceive(ar, ref clientEndPoint); udpListener.BeginReceive(UdpReceiveCallback, null); if (data.Length >= 4) { using (Packet p = new Packet(data)) { int clientId = p.ReadInt(); if (!clients.ContainsKey(clientId)) { return; } if (clients[clientId].udp.endPoint == null) { clients[clientId].udp.Connect(clientEndPoint); return; } if (clients[clientId].udp.endPoint.ToString() == clientEndPoint.ToString()) { clients[clientId].udp.HandleData(p); } } } } catch (Exception ex) { Console.WriteLine($"Error recieving data via udp: {ex}"); } }
/// <summary>Receives incoming UDP data.</summary> public static void UDPReceiveCallback(IAsyncResult _result) { try { IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] data = udpListener.EndReceive(_result, ref clientEndPoint); udpListener.BeginReceive(UDPReceiveCallback, null); if (data.Length < 4) { return; } //Handle received Data using (Packet _packet = new Packet(data)) { int clientId = _packet.ReadInt(); //// If this is a new connection if (clientId == 0) { for (int i = 1; i < users.Count; i++) { if (users[i].udp.endPoint == null) { users[i].udp.Connect(clientEndPoint); users[i].name = _packet.ReadString(); clientId = i; break; } } //using (Packet _pack = new Packet((int)ServerPackets.welcome)) //{ // //you have to catch these info at client on the same order // _pack.Write(clientId); // _pack.Write("Welcome"); // _pack.WriteLength(); // UDPServer.SendUDPData(clientEndPoint, _pack); //} //sent to own client a Welcome message ServerSend.Welcome(clientId); return; } if (users[clientId].udp.endPoint.ToString() == clientEndPoint.ToString()) { users[clientId].udp.HandleData(_packet); } } } catch (Exception ex) { Console.WriteLine($"Error receiving UDP data: {ex}"); } }
private void ReceiveCallback(IAsyncResult result) { IPEndPoint endPoint = null; byte[] data = socket.EndReceive(result, ref endPoint); if (data?.Length != 0) { int nextIndex = 0; ConnectionState packetState = (ConnectionState)BitConverter.ToInt16(data, nextIndex); nextIndex += 2; int nameLength = BitConverter.ToInt32(data, nextIndex); nextIndex += 4; string name = Encoding.ASCII.GetString(data, nextIndex, nameLength); nextIndex += nameLength; // Client verification ClientInfo client = GetClient(endPoint); if (client == null) { AddClient(endPoint, name); SendVerificationPacket(endPoint); Console.WriteLine($"Sending verification to: {endPoint}."); } else if (packetState == ConnectionState.Verification && client.State == ConnectionState.Connecting) { Console.WriteLine($"Received verification from: {endPoint}, client is now connected."); client.State = ConnectionState.Connected; } else if (packetState == ConnectionState.Connected && client.State == ConnectionState.Connected) { Request request = (Request)BitConverter.ToUInt16(data, nextIndex); nextIndex += 2; if (request == Request.None) { } else if (request == Request.JoinGame) { JoinGame(client); } else if (request == Request.JoinSpectators) { JoinSpectators(client); } else if (request == Request.Disconnect) { RemoveClient(client); } else if (request == Request.MovePosition) { if (client.playerNumber > 0) { bool moveUp = BitConverter.ToBoolean(data, nextIndex); nextIndex++; bool moveDown = BitConverter.ToBoolean(data, nextIndex); nextIndex++; MovePlayerPosition(client.playerNumber, moveUp, moveDown); } } } } socket.BeginReceive(ReceiveCallback, null); }