Example #1
0
        /// <summary>
        /// Starts the server.
        /// </summary>
        private void StartServer()
        {
            // Initialize Socket class
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // Request and bind to an IP from DHCP server
            socket.Bind(new IPEndPoint(IPAddress.Any, 80));
            // Debug print our IP address
            Debug.Print("Listening on " + Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress + ":80");
            // Start listen for web requests
            socket.Listen(10);

            while (true)
            {
                using (Socket clientSocket = socket.Accept())
                {
                    // Get clients IP
                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
                    Debug.Print("Received request from " + clientIP.ToString());
                    EndPoint clientEndPoint = clientSocket.RemoteEndPoint;

                    if (clientSocket.Poll(-1, SelectMode.SelectRead))
                    {
                        // Create buffer and receive raw bytes.
                        byte[] bytes = new byte[clientSocket.Available];
                        int    count = clientSocket.Receive(bytes);

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

                        if (command != null)
                        {
                            WebCommandEventArgs args = new WebCommandEventArgs(command);
                            if (CommandReceived != null)
                            {
                                CommandReceived(this, args);
                                byte[] returnBytes = Encoding.UTF8.GetBytes(args.ReturnString);
                                clientSocket.Send(returnBytes, 0, returnBytes.Length, SocketFlags.None);
                            }
                        }
                    }
                }
                Thread.Sleep(10);
            }
        }
Example #2
0
        /// <summary>
        /// Parses a raw web request and filters out the command and arguments.
        /// </summary>
        /// <param name="rawData">The raw web request (including headers).</param>
        /// <returns>The parsed WebCommand if the request is valid, otherwise Null.</returns>
        private WebCommand InterpretRequest(string rawData)
        {
            string commandData;

            // Remove GET + Space
            if (rawData != null && rawData.Length > 5)
            {
                commandData = rawData.Substring(5, rawData.Length - 5);
            }
            else
            {
                return(null);
            }

            // Remove everything after first space
            int idx = commandData.IndexOf("HTTP/1.1");

            commandData = commandData.Substring(0, idx - 1);

            // Split command and arguments
            string[] parts = commandData.Split('/');

            string command = null;

            if (parts.Length > 0)
            {
                // Parse first part to command
                command = parts[0].ToLower();
            }

            // Check if this is a valid command
            WebCommand outCmd = null;

            foreach (WebCommand cmd in AllowedCommands)
            {
                if (cmd.CommandString.ToLower() == command)
                {
                    outCmd = new WebCommand(cmd.CommandString, cmd.ArgumentCount);
                    break;
                }
            }
            if (outCmd == null)
            {
                return(null);
            }
            else // Get command arguments
            {
                if ((parts.Length - 1) != outCmd.ArgumentCount)
                {
                    return(null);
                }
                else
                {
                    outCmd.Arguments = new string[parts.Length - 1];

                    for (int i = 1; i < parts.Length; i++)
                    {
                        outCmd.Arguments[i - 1] = parts[i];
                    }
                    return(outCmd);
                }
            }
        }
Example #3
0
 public WebCommandEventArgs(WebCommand command)
 {
     Command = command;
 }