Beispiel #1
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));
                }
            });
        }
Beispiel #2
0
        public static VarPacket CreateReadResponse(object owner, IPEndPoint endPoint, List <decimal> varValues)
        {
            if (endPoint == null)
            {
                throw new ArgumentNullException();
            }

            var p = new VarPacket()
            {
                EndPoint = endPoint, Owner = owner
            };

            int index = 0;

            if (varValues == null)
            {
                p.Data = new byte[9];
                p.Data.WriteAt(BitConverter.GetBytes(9), ref index);
                p.Data[4] = 1;
                ++index;
                p.Data.WriteAt(BitConverter.GetBytes(0), ref index);
            }
            else
            {
                int len = 9 + (varValues.Count << 4);
                p.Data = new byte[len];
                p.Data.WriteAt(BitConverter.GetBytes(len), ref index);
                p.Data[4] = 1;
                ++index;

                p.Data.WriteAt(BitConverter.GetBytes(varValues.Count), ref index);
                for (int i = 0; i < varValues.Count; ++i)
                {
                    p.Data.WriteAt(DecimalConverter.GetBytes(varValues[i]), ref index);
                }
            }
            return(p);
        }
Beispiel #3
0
        public static VarPacket CreateWriteRequest(object owner, IPEndPoint endPoint, List <string> varNames, decimal value)
        {
            if (endPoint == null || varNames == null)
            {
                throw new ArgumentNullException();
            }

            var p = new VarPacket()
            {
                EndPoint = endPoint, Owner = owner
            };

            int len = 25 + (varNames.Count << 2);

            for (int i = 0; i < varNames.Count; ++i)
            {
                len += (sizeof(char) * varNames[i].Length);
            }

            int index = 0;

            p.Data = new byte[len];
            p.Data.WriteAt(BitConverter.GetBytes(len), ref index);
            p.Data[4] = 2;
            ++index;
            p.Data.WriteAt(BitConverter.GetBytes(varNames.Count), ref index);

            for (int i = 0; i < varNames.Count; ++i)
            {
                p.Data.WriteAt(BitConverter.GetBytes(varNames[i].Length * sizeof(char)), ref index);
                p.Data.WriteAt(varNames[i].ToCharArray(), ref index);
            }
            p.Data.WriteAt(DecimalConverter.GetBytes(value), ref index);

            return(p);
        }
 public CommsEventArgs(VarPacket packet, IPEndPoint source, string msg)
 {
     Packet = packet;
     Source = source;
     Msg    = msg;
 }
Beispiel #5
0
        public static VarPacket CreateReceived(object owner, IPEndPoint endPoint, byte[] data)
        {
            if (endPoint == null || data == null)
            {
                throw new ArgumentNullException();
            }
            if (data.Length < 9)
            {
                throw new ArgumentException("Wrong packet size.");
            }

            var p = new VarPacket()
            {
                EndPoint = endPoint, Owner = owner
            };
            int len = BitConverter.ToInt32(data, 0);

            if (data.Length != len)
            {
                throw new ArgumentException("Wrong packet size.");
            }
            if (data[4] > 2)
            {
                throw new ArgumentException("Wrong packet type.");
            }
            int varCount = BitConverter.ToInt32(data, 5);

            if (data[4] == 0)
            {
                int index = 9;
                for (int i = 0; i < varCount; ++i)
                {
                    int nlen = BitConverter.ToInt32(data, index);
                    index += 4 + nlen;
                    if (index > len)
                    {
                        throw new ArgumentException("Wrong packet size.");
                    }
                }
                if (index != len)
                {
                    throw new ArgumentException("Wrong packet size.");
                }
            }
            else if (data[4] == 1)
            {
                int index = 9 + (varCount << 4);
                if (index != len)
                {
                    throw new ArgumentException("Wrong packet size.");
                }
            }
            else
            {
                int index = 9;
                for (int i = 0; i < varCount; ++i)
                {
                    int nlen = BitConverter.ToInt32(data, index);
                    index += 4 + nlen;
                    if (index > len)
                    {
                        throw new ArgumentException("Wrong packet size.");
                    }
                }
                if (index + 16 != len)
                {
                    throw new ArgumentException("Wrong packet size.");
                }
            }

            p.Data = data;
            return(p);
        }