Exemplo n.º 1
0
        /// <summary>
        /// On a connection error. This is the default callback for new connections created
        /// by this server.
        /// </summary>
        protected void OnConnectionError(Exception ex, UdpConnection connection)
        {
            // log the error
            Log.Error("Connection error '" + ex + "'.");

            // dispose of the connection
            connection.Dispose();
        }
Exemplo n.º 2
0
        /// <summary>
        /// On a client being accepted by the listenner.
        /// </summary>
        private void OnReceive(IPEndPoint endpoint, byte[] buffer, int count)
        {
            if (_stopped)
            {
                return;
            }

            UdpConnection connection;

            if (_connections.TakeItem().TryGetValue(endpoint, out connection))
            {
                _connections.Release();

                // pass the buffer to the connection
                connection.OnReceived(endpoint, buffer, count);

                return;
            }

            try {
                // create a new connection
                connection = new UdpConnection(this, new IPEndPoint(IPAddress.Any, 0), endpoint);
            } catch (Exception ex) {
                Log.Error("Exception creating new incoming connection.", ex);

                // dispose of the connections
                _connections.Release();
                // dispose of the new connection
                if (connection != null)
                {
                    connection.Dispose();
                }

                return;
            }

            // add the new connection
            _connections.Item.Add(connection.RemoteEndPoint, connection);
            _connections.Release();

            // run on connection callback
            _onConnection.Run(connection);

            // pass the received buffer to the connection
            connection.OnReceived(endpoint, buffer, count);
        }