コード例 #1
0
        /// <summary>
        /// Processes all in-comming client command requests.
        /// </summary>
        /// <param name="sender">The current server to client connection channel.</param>
        /// <param name="commandSend">The command send by the client context.</param>
        private void SocketServer_OnCommandSend(UdpServerContext sender, string commandSend)
        {
            try
            {
                // Clear last error.
                ClearLastError();

                // Process the command.
                switch (commandSend.ToUpper())
                {
                case "DISCONNECT":
                    // End the client connection.
                    EndConnection(sender);
                    break;

                case "RECEIVED":
                    // Server has received data from client.
                    // Signal to the blocking handler
                    // to un-block.
                    lock (_lockingObject)
                    {
                        _receivedData.Set();
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                SetLastError(ex);
            }
        }
コード例 #2
0
        /// <summary>
        /// Ends the current client connection.
        /// </summary>
        /// <param name="sender">The current server to client connection channel.</param>
        private void EndConnection(UdpServerContext sender)
        {
            // Set the current client count.
            DecrementCount();

            // Signal to the blocking handler
            // to un-block.
            lock (_lockingObject)
                _connAvailable.Set();

            try
            {
                lock (_lockingObject)
                {
                    // Send the new disconnection to the caller.
                    if (OnClientDisconnected != null)
                    {
                        OnClientDisconnected(this, sender);
                    }
                }
            }
            catch { }

            try
            {
                lock (_lockingObject)
                {
                    // Attempt to release all the resources of the curretn server context.
                    sender.Dispose();
                    sender = null;
                }
            }
            catch { }
        }
コード例 #3
0
        /// <summary>
        /// Create a new instance of the server context type.
        /// </summary>
        private void CreateServerContext()
        {
            UdpServerContext socketServerContext = null;

            try
            {
                // Clear last error.
                ClearLastError();

                // Create a new instance of the server context type
                socketServerContext                         = new UdpServerContext(_specificAddressFamily);
                socketServerContext.Socket                  = _socket;
                socketServerContext.Name                    = _serverName;
                socketServerContext.ReceiveSocketFlags      = _receiveSocketFlags;
                socketServerContext.SendSocketFlags         = _sendSocketFlags;
                socketServerContext.SendToServerHandler     = _sendToServerHandler;
                socketServerContext.SendToServerInfoHandler = _sendToServerInfoHandler;
                socketServerContext.ReadBufferSize          = READ_BUFFER_SIZE;
                socketServerContext.WriteBufferSize         = WRITE_BUFFER_SIZE;
                socketServerContext.ServerRef               = this;
                socketServerContext.Server                  = this;

                // If a time out has been set.
                if (_timeOut > 0)
                {
                    socketServerContext.TimeOut = _timeOut;
                }

                // Create a new client data receive handler, this event
                // handles commands from the current client.
                socketServerContext.OnCommandSend +=
                    new UdpServerContextHandler(SocketServer_OnCommandSend);

                // Initalise the context.
                socketServerContext.Initialise();

                // Increment the count.
                IncrementCount();

                try
                {
                    // Send the new connection to the caller.
                    if (OnClientConnected != null)
                    {
                        OnClientConnected(this, socketServerContext);
                    }
                }
                catch { }

                // Lock the socket receive process.
                lock (LockingSocket)
                {
                    // Start receiving data from the client.
                    _socket.BeginReceiveFrom(socketServerContext._readBuffer, 0, socketServerContext.ReadBufferSize,
                                             socketServerContext.ReceiveSocketFlags, ref socketServerContext.RemoteClient,
                                             new AsyncCallback(socketServerContext.DataReceiver), socketServerContext);
                }
            }
            catch (Exception ex)
            {
                // Clean-up if error.
                if (socketServerContext != null)
                {
                    socketServerContext.Dispose();
                }

                socketServerContext = null;
                SetLastError(ex);
            }
        }