protected virtual async ValueTask ProcessData(UdpReceiveResult data)
        {
            // Get client from active clients
            if (!_allConnections.TryGetValue(data.RemoteEndPoint, out UdpServerConnection client))
            {
                // Check for malformed connection attempts
                if (data.Buffer[0] != (byte)UdpSendOption.Hello)
                {
                    return;
                }

                // Check rateLimit.
                if (!_connectionRateLimit.IsAllowed(data.RemoteEndPoint.Address))
                {
                    Logger.Warning("Ratelimited connection attempt from {0}.", data.RemoteEndPoint);
                    return;
                }

                // Create new client
                client = new UdpServerConnection(this, data.RemoteEndPoint, IPMode, _readerPool);

                // Store the client
                if (!_allConnections.TryAdd(data.RemoteEndPoint, client))
                {
                    throw new HazelException("Failed to add a connection. This should never happen.");
                }

                // Activate the reader loop of the client
                await client.StartAsync();
            }

            // Write to client.
            await client.Pipeline.Writer.WriteAsync(data.Buffer);
        }
        private async void ManageReliablePackets(object state)
        {
            foreach (KeyValuePair <EndPoint, UdpServerConnection> kvp in this._allConnections)
            {
                UdpServerConnection sock = kvp.Value;
                await sock.ManageReliablePackets();
            }

            try
            {
                this._reliablePacketTimer.Change(100, Timeout.Infinite);
            }
            catch { }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Instructs the listener to begin listening.
        /// </summary>
        private async Task ListenAsync()
        {
            try
            {
                while (!_stoppingCts.IsCancellationRequested)
                {
                    UdpReceiveResult data;

                    try
                    {
                        data = await _socket.ReceiveAsync();

                        if (data.Buffer.Length == 0)
                        {
                            Logger.Fatal("Hazel read 0 bytes from UDP server socket.");
                            continue;
                        }
                    }
                    catch (SocketException)
                    {
                        // Client no longer reachable, pretend it didn't happen
                        continue;
                    }
                    catch (ObjectDisposedException)
                    {
                        // Socket was disposed, don't care.
                        return;
                    }

                    // Get client from active clients
                    if (!_allConnections.TryGetValue(data.RemoteEndPoint, out var client))
                    {
                        // Check for malformed connection attempts
                        if (data.Buffer[0] != (byte)UdpSendOption.Hello)
                        {
                            continue;
                        }

                        // Check rateLimit.
                        if (!_connectionRateLimit.IsAllowed(data.RemoteEndPoint.Address))
                        {
                            Logger.Warning("Ratelimited connection attempt from {0}.", data.RemoteEndPoint);
                            continue;
                        }

                        // Create new client
                        client = new UdpServerConnection(this, data.RemoteEndPoint, IPMode, _readerPool);

                        // Store the client
                        if (!_allConnections.TryAdd(data.RemoteEndPoint, client))
                        {
                            throw new HazelException("Failed to add a connection. This should never happen.");
                        }

                        // Activate the reader loop of the client
                        await client.StartAsync();
                    }

                    // Write to client.
                    await client.Pipeline.Writer.WriteAsync(data.Buffer);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "Listen loop error");
            }
        }