Exemplo n.º 1
0
        private void BeginSend()
        {
            if (_running == 0)
            {
                return;
            }

            RawData raw;

            if (!_sendingQueue.TryDequeue(out raw))
            {
                System.Threading.Interlocked.Exchange(ref _writing, 0);
                return;
            }

            UDPSocket  socket   = _socket;
            IPEndPoint remoteEP = (IPEndPoint)raw.EndPoint;

            if (remoteEP.AddressFamily == AddressFamily.InterNetwork)
            {
                if (_socketBackup != null)
                {
                    // use the separated socket of IPv4 to deal with IPv4 conversions.
                    socket = _socketBackup;
                }
                else if (_socket.Socket.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    remoteEP = new IPEndPoint(IPAddressExtensions.MapToIPv6(remoteEP.Address), remoteEP.Port);
                }
            }

            BeginSend(socket, raw.Data, remoteEP);
        }
Exemplo n.º 2
0
        private void EndReceive(UDPSocket socket, Byte[] buffer, Int32 offset, Int32 count, System.Net.EndPoint ep)
        {
            if (count > 0)
            {
                Byte[] bytes = new Byte[count];
                Buffer.BlockCopy(buffer, 0, bytes, 0, count);

                if (ep.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    IPEndPoint ipep = (IPEndPoint)ep;
                    if (IPAddressExtensions.IsIPv4MappedToIPv6(ipep.Address))
                    {
                        ep = new IPEndPoint(IPAddressExtensions.MapToIPv4(ipep.Address), ipep.Port);
                    }
                }

                try {
                    DateTimeOffset start = DateTimeOffset.Now;
                    log.Info($"UDP-FireDataReceived START");
                    FireDataReceived(bytes, ep);
                    log.Info($"UDP-FireDataReceived END ({DateTimeOffset.Now - start})");
                }
                catch (Exception e) {
                    log.Error($"FireDataReceived error occurred: {e.ToString()}", e);
                }
            }

            BeginReceive(socket);
        }