/// <summary>
        /// Waiting for client to connect.
        /// When bytes were read they get wrapped to a "Reqeust"
        /// </summary>
        private void WaitingForRequest()
        {
            while (true)
            {
                try
                {
                    using (Socket clientSocket = _ListeningSocket.Accept())
                    {
                        Debug.Print("Client Connected");
                        int availableBytes = 0;
                        int newAvBytes     = 0;
                        Thread.Sleep(100);

                        do
                        {
                            newAvBytes = clientSocket.Available - availableBytes;

                            if (newAvBytes == 0)
                            {
                                break;
                            }

                            availableBytes += newAvBytes;
                            newAvBytes      = 0;
                            Thread.Sleep(1);
                        } while (true);

                        Debug.Print("Available Bytes: " + availableBytes);

                        if (availableBytes > 0)
                        {
                            byte[] buffer = new byte[availableBytes > Settings.MaxRequestSize ? Settings.MaxRequestSize : availableBytes];
                            byte[] header = new byte[0];

                            int readByteCount = clientSocket.Receive(buffer, buffer.Length, SocketFlags.None);
                            Debug.Print(readByteCount + " bytes read");

                            for (int headerend = 0; headerend < buffer.Length - 3; headerend++)
                            {
                                if (buffer[headerend] == '\r' && buffer[headerend + 1] == '\n' && buffer[headerend + 2] == '\r' && buffer[headerend + 3] == '\n')
                                {
                                    header = new byte[headerend + 4];
                                    Array.Copy(buffer, 0, header, 0, headerend + 4);
                                    break;
                                }
                            }

                            //reqeust created, checking the response possibilities
                            using (Request tempRequest = new Request(Encoding.UTF8.GetChars(header), clientSocket))
                            {
                                Debug.Print("... Client connected ... URL: " + tempRequest.URL + " ... Final byte count: " + availableBytes);

                                if (tempRequest.Method == "POST")
                                {
                                    //POST was incoming, it will be saved to SD card at Settings.POST_TEMP_PATH
                                    PostToSdWriter post = new PostToSdWriter(tempRequest, buffer, header.Length);
                                    post.Receive();
                                }

                                //Let's check if we have to take some action or if it is a file-response
                                HandleGETResponses(tempRequest);
                            }

                            Debug.Print("Client loop finished");

                            try
                            {
                                //Close client, otherwise the browser / client won't work properly
                                clientSocket.Close();
                            }
                            catch (Exception ex)
                            {
                                Debug.Print(ex.ToString());
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                }
            }
        }
        /// <summary>
        /// Waiting for client to connect.
        /// When bytes were read they get wrapped to a "Reqeust"
        /// </summary>
        private void WaitingForRequest()
        {
            while (true)
            {
                try
                {
                    using (Socket clientSocket = _ListeningSocket.Accept())
                    {
                        Debug.Print("Client Connected");
                        int availableBytes = 0;
                        int newAvBytes = 0;
                        Thread.Sleep(100);

                        do
                        {
                            newAvBytes = clientSocket.Available - availableBytes;

                            if (newAvBytes == 0)
                                break;

                            availableBytes += newAvBytes;
                            newAvBytes = 0;
                            Thread.Sleep(1);
                        } while (true);

                        Debug.Print("Available Bytes: " + availableBytes);

                        if (availableBytes > 0)
                        {
                            byte[] buffer = new byte[availableBytes > Settings.MaxRequestSize ? Settings.MaxRequestSize : availableBytes];
                            byte[] header = new byte[0];

                            int readByteCount = clientSocket.Receive(buffer, buffer.Length, SocketFlags.None);
                            Debug.Print(readByteCount + " bytes read");

                            for (int headerend = 0; headerend < buffer.Length - 3; headerend++)
                            {
                                if (buffer[headerend] == '\r' && buffer[headerend + 1] == '\n' && buffer[headerend + 2] == '\r' && buffer[headerend + 3] == '\n')
                                {
                                    header = new byte[headerend + 4];
                                    Array.Copy(buffer, 0, header, 0, headerend + 4);
                                    break;
                                }
                            }

                            //reqeust created, checking the response possibilities
                            using (Request tempRequest = new Request(Encoding.UTF8.GetChars(header), clientSocket))
                            {
                                Debug.Print("... Client connected ... URL: " + tempRequest.URL + " ... Final byte count: " + availableBytes);

                                if (tempRequest.Method == "POST")
                                {
                                    //POST was incoming, it will be saved to SD card at Settings.POST_TEMP_PATH
                                    PostToSdWriter post = new PostToSdWriter(tempRequest, buffer, header.Length);
                                    post.Receive();
                                }

                                //Let's check if we have to take some action or if it is a file-response
                                HandleGETResponses(tempRequest);
                            }

                            Debug.Print("Client loop finished");

                            try
                            {
                                //Close client, otherwise the browser / client won't work properly
                                clientSocket.Close();
                            }
                            catch (Exception ex)
                            {
                                Debug.Print(ex.ToString());
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Waiting for client to connect.
        /// When bytes were read they get wrapped to a "Reqeust"
        /// </summary>
        private void WaitingForRequest()
        {
            while (true)
            {
                try
                {
                    // show ready status
                    _statusLED.greenLED();

                    using (Socket clientSocket = listeningSocket.Accept())
                    {
                        _statusLED.blueLED();
                        _lidar_reader.disableInterrupt("server");
                        //Wait to get the bytes in the sockets "available buffer"
                        int availableBytes = AwaitAvailableBytes(clientSocket);

                        if (availableBytes > 0)
                        {
                            byte[] buffer = new byte[availableBytes > Settings.MAX_REQUESTSIZE ? Settings.MAX_REQUESTSIZE : availableBytes];

                            byte[] header = FilterHeader(clientSocket, buffer);

                            // something wrong with request, ignore it
                            if (header.Length == 0)
                            {
                                continue;
                            }

                            //reqeust created, checking the response possibilities
                            using (Request tempRequest = new Request(Encoding.UTF8.GetChars(header), clientSocket))
                            {
                                // add Laser obj to pass it down to response function
                                tempRequest.setLaser(this._laser);

                                // add lidar distance object
                                tempRequest.setLidarDistance(this._distanceValue);

                                Debug.Print("\n\nClient connected\nURL: " + tempRequest.URL + "\nFinal byte count: " + availableBytes + "\n");

                                if (tempRequest.Method == "POST")
                                {
                                    //POST was incoming, it will be saved to SD card at Settings.POST_TEMP_PATH
                                    // This file can later be handled in a normal response method by using PostFileReader
                                    PostToSdWriter post = new PostToSdWriter(tempRequest);
                                    post.ReceiveAndSaveData(buffer, header.Length);
                                }

                                //Let's check if we have to take some action or if it is a file-response
                                SendResponse(tempRequest);
                            }

                            try
                            {
                                //Close client, otherwise the browser / client won't work properly
                                clientSocket.Close();
                            }
                            catch (Exception ex)
                            {
                                Debug.Print(ex.ToString());
                            }

                            Debug.Print("Request finished");
                            Debug.Print("End Request, freemem: " + Debug.GC(true));
                            _statusLED.greenLED();
                            _lidar_reader.enableInterrupt("server");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                }
            }
        }