Exemplo n.º 1
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 EndPoinEventArgs(endPoint, connection);

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

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

                                        SendResponse(response, connection);
                                    }
                                });
                            }
                        }
                        else
                        {
                            SendResponse(GetApiList(), connection);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
 public void Initialize()
 {
     // set our pin listeners on the button
     ThreadUtil.Start(() => ButtonUtils.OnBoardButtonPushed(WriteToLed));
 }