Exemplo n.º 1
0
        //Process an incomming request.
        private async Task Process(TcpClient tcpClient, ServiceCallback callback)
        {
            //Print the client connection to the console.
            //TODO: Remove this for version 1.
            string clientEndPoint = tcpClient.Client.RemoteEndPoint.ToString();

            Console.WriteLine("Received connection request from " + clientEndPoint);

            try
            {
                //Create and configure a stream we can use to send and receive data.
                NetworkStream networkStream = tcpClient.GetStream();
                StreamReader  reader        = new StreamReader(networkStream);
                StreamWriter  writer        = new StreamWriter(networkStream);
                writer.AutoFlush = true;


                //Stealing Liberally from https://stackoverflow.com/questions/26058594/how-to-get-all-data-from-networkstream
                //If we can read information from the networkStream...read it into a StringBuilder
                if (networkStream.CanRead)
                {
                    byte[] readBuffer = new byte[1024];

                    //Full webrequest.
                    StringBuilder fullMessage = new StringBuilder();

                    int numberOfBytesRead = 0;

                    //Read all data from the stream into fullMessage
                    do
                    {
                        numberOfBytesRead = await networkStream.ReadAsync(readBuffer, 0, readBuffer.Length);

                        fullMessage.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numberOfBytesRead));
                    } while (networkStream.DataAvailable);


                    //Examine the request to see if it is properly formatted.
                    Tuple <String, String> opRequest = IsProperlyFormatted(fullMessage.ToString());

                    if (opRequest != null)
                    {
                        //Match the sent web request type with our Enum WebRequestType.
                        WebRequestType operation;
                        Enum.TryParse <WebRequestType>(opRequest.Item1, out operation);

                        //Invoke the callback that corresponds to Controller.HttpRequestHandler();
                        string dataFromController = callback.Invoke(operation, opRequest.Item2);

                        //Builder to prepare our response.
                        StringBuilder res = new StringBuilder("HTTP/1.1 200 OK\r\n");

                        //TODO: restrict cross origin to webserver only.
                        res.Append("Access-Control-Allow-Origin: *\r\n");
                        res.Append("Content-Type: text/*\r\n");
                        res.Append("\r\n");
                        res.Append(dataFromController);
                        await writer.WriteLineAsync(res.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Network Stream Unreadable.");
                }

                tcpClient.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (tcpClient.Connected)
                {
                    tcpClient.Close();
                }
            }
        } // Process