Ejemplo n.º 1
0
        private void OnLineReceived(string line)
        {
            string[] data = line.Split(' ');

            //begin with "@": treat as event message
            int eventId = 0;

            if (data.Length > 0 &&
                data[0].Length > 1 &&
                data[0][0] == '@' &&
                int.TryParse(data[0].Substring(1), out eventId)
                )
            {
                var args = new string[data.Length - 1];
                Array.Copy(data, 1, args, 0, args.Length);
                var eventMessage = new UArmEventMessage(eventId, args);
                ReceivedEvent?.Invoke(this, new UArmEventMessageEventArgs(eventMessage));

                return;
            }

            //begin with "$": treas as response
            int id = -1;

            if (data.Length > 1 &&
                data[0].Length > 1 &&
                data[0][0] == '$' &&
                int.TryParse(data[0].Substring(1), out id))
            {
                var args = new string[data.Length - 1];
                Array.Copy(data, 1, args, 0, args.Length);
                var response = new UArmResponse(id, args);

                //Terminate waiting process if pending exists
                if (_pendingCommandIds.ContainsKey(id))
                {
                    _responses[id] = response;
                    _pendingCommandIds[id].Cancel();
                }

                ReceivedResponse?.Invoke(this, new UArmResponseEventArgs(response));

                return;
            }


            //Unknown data
        }
Ejemplo n.º 2
0
 public override void OnReceivedBroadcast(string fromAddress, string data)
 {
     base.OnReceivedBroadcast(fromAddress, data);
     received.Invoke(fromAddress, data);
 }
Ejemplo n.º 3
0
        // BeginRead callback
        private void HandleReceive(IAsyncResult ar)
        {
            int numBytes = 0;

            _buffer = null;
            try
            {
                // end read returns number of bytes read
                numBytes = _clientsock.EndReceive(ar);

                if (numBytes > 0 && numBytes == _request_size)
                {
                    int req_size = -1;
                    if (!int.TryParse(_req_buffer.Decode(numBytes), out req_size))
                    {
                        throw new Exception("Client request is corrupted");
                    }
                    if (req_size > 0)
                    {
                        _buffer = new SockBuffer(req_size);
                        int num_bytes_request = 0;
                        int time_out          = 0; // 15 seconds

                        while (_clientsock.Available < req_size)
                        {
                            if (time_out >= 15000)
                            {
                                throw new Exception("The timeout period elapsed while waiting for the client to finish sending data - the client has either disconnected or failed to send a complete message");
                            }
                            ;
                            Thread.Sleep(50);
                            time_out += 50;
                        }

                        num_bytes_request += _clientsock.Receive(_buffer.BufferByte, 0, _buffer.Size, SocketFlags.None);
                    }
                }
                else if (numBytes > 0 && numBytes < _request_size)
                {
                    // drop this received data, it is probably garbage
                }
                else
                {
                    throw new Exception("Server did not receive any data from client");
                }
            }
            catch (Exception ex)
            {
                _ex = ex;
            }
            if (_ex != null)
            {
                Console.WriteLine("Error encountered during reading client data: " + _ex.Message);
                DisconnectedEvent?.Invoke(this, new IPCConnectedEventArgs(_clientsock, _address, _logPath));
                // raise disconnected event
                _connected = false;
            }
            else if (_buffer != null)
            {
                ReceivedEvent?.Invoke(this, new IPCReceivedEventArgs(_clientsock, _buffer.Decode(_buffer.Size)));
                // raise received event
            }
            signal_read.Set();
            //return ar;
        }