Exemple #1
0
 protected virtual void OnSent(CommsEventArgs e)
 {
     if (Sent != null)
     {
         Sent(this, e);
     }
 }
Exemple #2
0
 protected virtual void OnReceived(CommsEventArgs e)
 {
     if (Received != null)
     {
         Received(this, e);
     }
 }
Exemple #3
0
        private void ReadAvailable(TcpClient client)
        {
            Task.Run(() =>
            {
                using (var s = client.GetStream())
                {
                    CommsEventArgs args = null;
                    var data            = _ReadPacketData(s);
                    VarPacket p         = null;

                    try
                    {
                        p = VarPacket.CreateReceived(this, LocalEndPoint, data);
                    }
                    catch (ArgumentException ex)
                    {
                        args = new CommsEventArgs(p, client.Client.RemoteEndPoint as IPEndPoint, ex.Message);
                        _listenerThread.Report(Tuple.Create(args, false, (VarPacket)null));
                        return;
                    }

                    args = new CommsEventArgs(p, client.Client.RemoteEndPoint as IPEndPoint, null);
                    _listenerThread.Report(Tuple.Create(args, false, p.IsWriteRequest ? p : null));

                    if (p.IsReadRequest)
                    {
                        _RespondToRequest(p, client.Client.RemoteEndPoint as IPEndPoint, s);
                    }
                }
            });
        }
Exemple #4
0
        private void _RespondToRequest(VarPacket packet, IPEndPoint endPoint, NetworkStream ns)
        {
            bool isok   = true;
            var  names  = packet.RequestVarNames;
            var  values = new List <decimal>(names.Length);

            foreach (var n in names)
            {
                Variable <decimal> v;
                if (!LocalVariables.TryGet(n, out v))
                {
                    isok = false;
                    break;
                }
                values.Add(v.Value);
            }

            var p = VarPacket.CreateReadResponse(this, endPoint, isok ? values : null);

            ns.Write(p.Data, 0, p.Data.Length);

            var args = new CommsEventArgs(p, LocalEndPoint, null);

            _listenerThread.Report(Tuple.Create(args, true, (VarPacket)null));
        }
Exemple #5
0
        public void Send(VarPacket packet)
        {
            Task.Run(() =>
            {
                CommsEventArgs args = null;
                try
                {
                    using (var client = new TcpClient(packet.EndPoint.Address.ToString(), packet.EndPoint.Port)
                    {
                        NoDelay = this.NoDelay
                    })
                        using (var s = client.GetStream())
                        {
                            s.Write(packet.Data, 0, packet.Data.Length);
                            s.Flush();

                            args = new CommsEventArgs(packet, LocalEndPoint, null);
                            _listenerThread.Report(Tuple.Create(args, true, (VarPacket)null));

                            if (packet.IsReadRequest)
                            {
                                var data    = _ReadPacketData(s);
                                VarPacket p = null;

                                try
                                {
                                    p = VarPacket.CreateReceived(this, LocalEndPoint, data);
                                }
                                catch (ArgumentException ex)
                                {
                                    args = new CommsEventArgs(p, client.Client.RemoteEndPoint as IPEndPoint, ex.Message);
                                    _listenerThread.Report(Tuple.Create(args, false, (VarPacket)null));
                                    return;
                                }

                                args = new CommsEventArgs(p, client.Client.RemoteEndPoint as IPEndPoint, p.IsReadResponse ? null : "Wrong packet type received.");
                                _listenerThread.Report(Tuple.Create(args, false, (VarPacket)null));
                            }
                        }
                }
                catch (Exception ex)
                {
                    args = new CommsEventArgs(packet, LocalEndPoint, ex.Message);
                    _listenerThread.Report(Tuple.Create(args, true, (VarPacket)null));
                }
            });
        }