예제 #1
0
 public void msgReceiver()
 {
     while (_threadIsAlive)
     {
         bool   read_bytes = false;
         byte[] msg        = new byte[1024];
         try
         {
             do
             {
                 if (_socket != null && _closed == false)
                 {
                     _socket.ReceiveTimeout = 300;
                     this._socket.Receive(msg);
                     if (msg[0] == 2 && (Array.IndexOf(msg, 3) > -1))
                     {
                         _inport.disableInterrupt("msgrecieve");
                         read_bytes = true;
                         //process message (ACK, NAC, GetStatus, SetChannel
                         processMsg(msg);
                         _inport.enableInterrupt("msgrecieve");
                     }
                 }
                 else
                 {
                     read_bytes = false;
                 }
             } while (read_bytes);
         }
         catch (SocketException ex)
         {
             if (ex.ErrorCode == 10060) // SocketError.TimedOut
             {
                 Debug.Print("Nothing more to receive, mem:" + Debug.GC(true));
             }
             else
             {
                 // reboot
                 PowerState.RebootDevice(false);
             }
         }
     }
     _threadDead = true;
 }
예제 #2
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);
                }
            }
        }