Example #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(UdpSimpleContext sender, string commandSend)
        {
            try
            {
                // Clear last error.
                ClearLastError();

                // Process the command.
                switch (commandSend.ToUpper())
                {
                case "RECEIVED":
                    try
                    {
                        // If receiver handler has been set.
                        if (_receivedHandler != null)
                        {
                            // Send to client.
                            _receivedHandler(this, sender._readBuffer.Take(sender.DataReceiverBytesRead).ToArray(), sender.Client);
                        }
                    }
                    catch { }

                    // Data has been received.
                    DataReceived();
                    break;
                }
            }
            catch (Exception ex)
            {
                SetLastError(ex);
            }
        }
Example #2
0
        /// <summary>
        /// Create a new instance of the server context type.
        /// </summary>
        private void CreateServerContext()
        {
            try
            {
                // Create a new instance of the server context type
                UdpSimpleContext context = new UdpSimpleContext(_specificAddressFamily);
                context.Socket             = _socket;
                context.ReceiveSocketFlags = _receiveSocketFlags;
                context.SendSocketFlags    = _sendSocketFlags;
                context.ReadBufferSize     = READ_BUFFER_SIZE;
                context.WriteBufferSize    = WRITE_BUFFER_SIZE;
                context.Server             = this;

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

                // Increment the count.
                IncrementCount();

                // Lock the socket receive process.
                lock (LockingSocket)
                {
                    // Start receiving data from the client.
                    _socket.BeginReceiveFrom(context._readBuffer, 0, context.ReadBufferSize,
                                             context.ReceiveSocketFlags, ref context.RemoteClient,
                                             new AsyncCallback(context.DataReceiver), context);
                }
            }
            catch (Exception ex)
            {
                SetLastError(ex);
            }
        }