예제 #1
0
        /// <summary>
        /// We'll get an endpoint invokcation from the web server
        /// so we can execute the endpoint action and response based on its supplied arguments
        /// in a seperate thread, hence the event. we'll set the event return string
        /// so the web server can know how to respond back to the ui in a seperate thread
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private static void EndPointHandler(object source, EndPointEventArgs e)
        {
            var misc = new EndPointActionArguments
            {
                Connection = e.Connection
            };

            e.ReturnString = e.Command.Execute(misc);

            // we can override the manual use of the socket if we returned a value other than null
            if (e.ReturnString != null && e.Command.UsesManualSocket)
            {
                e.ManualSent = false;
            }
            else
            {
                e.ManualSent = e.Command.UsesManualSocket;
            }
        }
예제 #2
0
        /// <summary>
        /// Starts the server.
        /// </summary>
        private void StartServer()
        {
            using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                server.Bind(new IPEndPoint(IPAddress.Any, Port));

                server.Listen(1);

                while (!_cancel)
                {
                    var connection = server.Accept();
                    if (connection.Poll(-1, SelectMode.SelectRead))
                    {
                        // Create buffer and receive raw bytes.
                        var bytes = new byte[connection.Available];

                        connection.Receive(bytes);

                        // Convert to string, will include HTTP headers.
                        var rawData = new string(Encoding.UTF8.GetChars(bytes));

                        EndPoint endPoint = InterpretRequest(rawData);

                        if (endPoint != null)
                        {
                            if (_enableLedStatus)
                            {
                                PingLed();
                            }

                            // dispatch the endpoint
                            var e = new EndPointEventArgs(endPoint, connection);

                            if (EndPointReceived != null)
                            {
                                ThreadUtil.SafeQueueWorkItem(() =>
                                {
                                    EndPointReceived(null, e);

                                    if (e.ManualSent)
                                    {
                                        // the client should close the socket
                                    }
                                    else
                                    {
                                        var response = e.ReturnString;

                                        SendResponse(response, connection);
                                    }
                                });
                            }
                        }
                        else
                        {
                            SendResponse(GetApiList(), connection);
                        }
                    }

                }
            }
        }