Beispiel #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();
        }
Beispiel #2
0
        //----------------------------------//

        internal UdpMessage(UdpConnection connection)
        {
            // reference the web client
            Connection = connection;

            _streamBuffer = new ByteBuffer(new MemoryStream(200));

            Header = new UdpHeader();

            _remainingBodyLength = -1;
        }
Beispiel #3
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);
        }
Beispiel #4
0
        /// <summary>
        /// Get a udp connection to the specified endpoint.
        /// </summary>
        public UdpConnection GetConnection(IPEndPoint remoteEndpoint)
        {
            // has the server been stopped? yes, throw
            if (_stopped)
            {
                throw new InvalidOperationException("The server is stopped.");
            }

            // try get an existing connection for the specified endpoint
            UdpConnection connection;

            if (_connections.TakeItem().TryGetValue(remoteEndpoint, out connection))
            {
                _connections.Release();
                return(connection);
            }

            try {
                // create the new connection
                connection         = new UdpConnection(this, _socket, remoteEndpoint);
                connection.OnError = new ActionSet <Exception, UdpConnection>(OnConnectionError, (Exception)null, connection);
            } catch (Exception ex) {
                Log.Error("Exception creating connection from '" + _socket.LocalEndPoint + "' to '" + remoteEndpoint + "'.", ex);
                return(null);
            }

            // add the new connection to the collection
            _connections.Item.Add(remoteEndpoint, connection);
            _connections.Release();

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

            // return the new connnection
            return(connection);
        }
Beispiel #5
0
 /// <summary>
 /// Remove a connection from the servers collection.
 /// </summary>
 internal void RemoveConnection(UdpConnection connection)
 {
     _connections.TakeItem().Remove(connection.RemoteEndPoint);
     _connections.Release();
 }