Example #1
0
        private void OnDataReceived(IAsyncResult ar)
        {
            bool doBeginReceive = true;

            try {
                SocketFlags         flags = SocketFlags.None;
                IPPacketInformation packetInfo;
                int bytesRead = socket.EndReceiveMessageFrom(ar, ref flags, ref receiveEndpoint, out packetInfo);

                IPEndPoint fromEndpoint = receiveEndpoint as IPEndPoint;
                IPAddress  destAddress  = packetInfo.Address;

                SocketDataReceivedEventArgs e = new SocketDataReceivedEventArgs(fromEndpoint, new IPEndPoint(destAddress, port), buffer, bytesRead);

                lock (addressEntries) {
                    foreach (AddressEntry entry in addressEntries)
                    {
                        if (CompareSourceIP(entry.sourceAddress, fromEndpoint.Address) && CompareDestIP(entry.destAddress, destAddress))
                        {
                            entry.callback(e);
                        }
                    }
                }
            }
            catch (ObjectDisposedException) {
                doBeginReceive = false;
            }

            if (doBeginReceive)
            {
                int tryCount = 0;
                while (tryCount < 3)
                {
                    try {
                        receiveEndpoint = new IPEndPoint(IPAddress.Any, 0);
                        socket.BeginReceiveMessageFrom(buffer, 0, buffer.Length, SocketFlags.None, ref receiveEndpoint, OnDataReceived, null);
                        break;
                    }
                    catch (Exception) {
                    }
                    tryCount++;
                }
            }
        }
        private void OnDataReceived(IAsyncResult ar)
        {
            bool doBeginReceive = true;
            try {
                SocketFlags flags = SocketFlags.None;
                IPPacketInformation packetInfo;
                int bytesRead = socket.EndReceiveMessageFrom(ar, ref flags, ref receiveEndpoint, out packetInfo);

                IPEndPoint fromEndpoint = receiveEndpoint as IPEndPoint;
                IPAddress destAddress = packetInfo.Address;

                SocketDataReceivedEventArgs e = new SocketDataReceivedEventArgs(fromEndpoint, new IPEndPoint(destAddress, port), buffer, bytesRead);

                lock (addressEntries) {
                    foreach (AddressEntry entry in addressEntries) {
                        if (CompareSourceIP(entry.sourceAddress, fromEndpoint.Address) && CompareDestIP(entry.destAddress, destAddress)) {
                            entry.callback(e);
                        }
                    }
                }
            }
            catch (ObjectDisposedException) {
                doBeginReceive = false;
            }

            if (doBeginReceive) {
                int tryCount = 0;
                while (tryCount < 3) {
                    try {
                        receiveEndpoint = new IPEndPoint(IPAddress.Any, 0);
                        socket.BeginReceiveMessageFrom(buffer, 0, buffer.Length, SocketFlags.None, ref receiveEndpoint, OnDataReceived, null);
                        break;
                    }
                    catch (Exception) {
                    }
                    tryCount++;
                }
            }
        }