/// <summary> /// Send a packet to only one player /// </summary> private void SendToOneClient(int playerId, NetworkCommand cmd, int int1, int int2, string message) { try { // Send back to all clients, but only the playerId will process it (TODO: may be improved) NetworkData backData = new NetworkData(playerId, cmd, int1, int2, message); byte[] d = backData.ToByte(); multicaster.Send(d, d.Length, remoteIpEP); } catch { } }
/// <summary> /// Send a packet to all players /// </summary> private void SendMulticast(NetworkCommand cmd, int int1, int int2, string message) { try { // Send back to all clients NetworkData backData = new NetworkData(0, cmd, int1, int2, message); byte[] d = backData.ToByte(); multicaster.Send(d, d.Length, remoteIpEP); } catch { } }
/// <summary> /// Active waiting listening for incoming packet /// </summary> private void Listening() { UdpClient listener = null; // Listening socket IPEndPoint localIpEP = null; // Listening IP // Secure creation of the socket try { listener = new UdpClient(); listener.ExclusiveAddressUse = false; localIpEP = new IPEndPoint(IPAddress.Any, portListener); listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); listener.ExclusiveAddressUse = false; listener.Client.Bind(localIpEP); IPAddress multicastAddress = IPAddress.Parse("239.0.0.222"); listener.JoinMulticastGroup(multicastAddress); } catch { Console.WriteLine("[ERROR] Can't connect to UDP port" + portListener + ". Check your network settings."); return; } listener.Client.ReceiveTimeout = 1000; // Perpetual waiting for packet while (isRunning) { try { byte[] packet = listener.Receive(ref localIpEP); NetworkData data = new NetworkData(localIpEP, packet); if (data.PlayerId == 0 || data.PlayerId == LocalPlayerId || LocalPlayerId < 0) // Block if not wanted new Thread(new ParameterizedThreadStart(TreatPacket)).Start(data); } catch { } } listener.Close(); }
/// <summary> /// Méthode qui écoute le réseau en permance en quête d'un message UDP sur le port 1523. /// </summary> private void Listening() { // Server socket creation UdpClient server = null; bool error = false; int attempts = 0; // Try 3 times before throwing error do { try { server = new UdpClient(portServer); } catch { error = true; attempts++; Thread.Sleep(400); } } while (error && attempts < 4); // Impossible to connect if (server == null) { End(); Console.WriteLine("[ERROR] Can't start server on port " + portServer + ". Check your network settings."); return; } server.Client.ReceiveTimeout = 1000; // Infinite loop waiting for packet while (isRunning) { try { IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0); byte[] packet = server.Receive(ref ip); NetworkData data = new NetworkData(ip, packet); new Thread(new ParameterizedThreadStart(TreatPacket)).Start(data); } catch (SocketException e) { } } server.Close(); }
/// <summary> /// Send a packet to the server /// </summary> private void SendPacket(NetworkCommand cmd, int int1, int int2, string message) { NetworkData data = new NetworkData(LocalPlayerId, cmd, int1, int2, message); byte[] packet = data.ToByte(); client.Send(packet, packet.Length); }