Beispiel #1
0
 public void setClientEnabled(Int32 clientID, bool enabled)
 {
     //            Console.WriteLine("ID " + clientID.ToString() + "--" + enabled.ToString());
     if (clients.ContainsKey(clientID))
     {
         Sclient s = clients[clientID];
         s.selected        = enabled;
         clients[clientID] = s;
     }
     //            foreach (var c in clients)
     //            {
     //                Console.WriteLine("Id " + c.Key.ToString() + "--" + c.Value.selected.ToString());
     //            }
 }
Beispiel #2
0
        public MyNetworkInterface()
        {
            IPv4AddressList = new List <System.Net.IPAddress>();
            rnd             = new Random((int)DateTime.Now.ToBinary());
            clientID        = rnd.Next();
            refreshIPv4AddressList();

            clients   = new Dictionary <Int32, Sclient>();
            udpClient = new UdpClient();
            udpClient.Client.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, udpPort));
            var from = new System.Net.IPEndPoint(0, 0);

            Task.Run(() =>
            {
                while (true)
                {
                    int pos        = 0;
                    var recvBuffer = udpClient.Receive(ref from);
                    if (recvBuffer.Length < magicHeader.Length + 4)
                    {
                        continue;
                    }
                    if (!recvBuffer.Skip(pos).Take(magicHeader.Length).SequenceEqual(magicHeader))
                    {
                        continue;
                    }
                    pos     += magicHeader.Length;
                    Int32 id = BitConverter.ToInt32(recvBuffer.Skip(pos).Take(4).ToArray(), 0);
                    pos     += 4;
                    if (clients.ContainsKey(id))
                    {
                        clients[id].set(clientTimeout);
                    }
                    if (pos < recvBuffer.Length)
                    {
                        //packet with DATA
                        if (clients.ContainsKey(id) && clients[id].selected)
                        {
                            datagram = recvBuffer.Skip(pos).ToArray();
                            Console.WriteLine("New " + datagram.Length.ToString() + " bytes DATA from " + from.Address.ToString() + ":" + from.Port.ToString());
                            newDatagram?.Invoke(null, null);
                        }
                    }
                    else
                    {
                        //empty packet
                        if (!clients.ContainsKey(id) && id != clientID)
                        {
                            Console.WriteLine("New client " + from.Address.ToString() + ":" + from.Port.ToString());
                            clients[id] = new Sclient(from.Address, clientTimeout);
                            newClient?.Invoke(null, null);
                        }
                    }
                }
            });
            var timer = new System.Windows.Forms.Timer();

            timer.Tick    += Timer_Tick;
            timer.Interval = 2000;
            timer.Start();
        }