Esempio n. 1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the client socket
            // from the asynchronous state object.
            var state = (StateObject)ar.AsyncState;

            if (!state.WorkSocket.Connected)
            {
                return;
            }

            var client = state.WorkSocket;

            // Read data from the remote device.
            var bytesRead = client.EndReceive(ar);

            client.BeginReceive(state.Buffer, 0, StateObject.BUFFER_SIZE, 0,
                                new AsyncCallback(ReceiveCallback), state);

            if (bytesRead > 0)
            {
                // There might be more data, so store the data received so far.
                var data = Encoding.ASCII.GetString(state.Buffer, 0, bytesRead);


                if (data.StartsWith("#"))
                {
                    return;
                }

                PacketInfo packetInfo;
                try
                {
                    packetInfo = new PacketInfo(data);
                }
                catch (Exception)
                {
                    //TODO: log/show an error
                    return;
                }

                if (PacketReceived != null)
                {
                    PacketReceived(this, new PacketInfoEventArgs(packetInfo));
                }
            }
        }
 public PacketInfoEventArgs(PacketInfo pi)
 {
     PacketInfo = pi;
 }