Exemplo n.º 1
0
 public WebSocketChannel(WebSocket client,
                         IPacketBuffer buffer,
                         MqttConfiguration configuration)
 {
     this.client        = client;
     this.buffer        = buffer;
     this.configuration = configuration;
     receiver           = new ReplaySubject <byte[]> (window: TimeSpan.FromSeconds(configuration.WaitTimeoutSecs));
     sender             = new ReplaySubject <byte[]> (window: TimeSpan.FromSeconds(configuration.WaitTimeoutSecs));
     streamSubscription = SubscribeStream();
 }
Exemplo n.º 2
0
 public TcpChannel(TcpClient client,
                   IPacketBuffer buffer,
                   MqttConfiguration configuration)
 {
     this.client = client;
     this.client.ReceiveBufferSize = configuration.BufferSize;
     this.client.SendBufferSize    = configuration.BufferSize;
     this.buffer        = buffer;
     receiver           = new ReplaySubject <byte[]> (window: TimeSpan.FromSeconds(configuration.WaitTimeoutSecs));
     sender             = new ReplaySubject <byte[]> (window: TimeSpan.FromSeconds(configuration.WaitTimeoutSecs));
     streamSubscription = SubscribeStream();
 }
Exemplo n.º 3
0
 public GenericChannel(
     IChannelClient client,
     IPacketBuffer buffer,
     MqttConfiguration configuration)
 {
     _client = client;
     _client.PreferedReceiveBufferSize = configuration.BufferSize;
     _client.PreferedSendBufferSize    = configuration.BufferSize;
     _buffer             = buffer;
     _receiver           = new ReplaySubject <byte[]>(window: TimeSpan.FromSeconds(configuration.WaitTimeoutSecs));
     _sender             = new ReplaySubject <byte[]>(window: TimeSpan.FromSeconds(configuration.WaitTimeoutSecs));
     _streamSubscription = SubscribeStream();
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Message Unpack(byte[] buffer, IPacketBuffer packetBuffer = null)
        {
            if (packetBuffer == null)
            {
                packetBuffer = new RealmPacketBuffer();
            }


            using (MemoryStream stream = new MemoryStream(buffer))
            {
                using (BinaryBitReader reader = new BinaryBitReader((Stream)stream))
                {
                    if (!CheckSignature(reader.ReadByte()))
                    {
                        throw new FormatException("Invalid signature!");
                    }

                    var direction = reader.ReadBoolean() ? Direction.MO : Direction.MT;
                    var composite = reader.ReadBoolean() ? Composite.Complex : Composite.Simple;
                    var version   = reader.ReadUInt(3);

                    var length = (0 + (reader.ReadBoolean() ? ((ushort)0x400) : ((ushort)0)))
                                 + (reader.ReadBoolean() ? ((ushort)0x200) : ((ushort)0))
                                 + (reader.ReadBoolean() ? ((ushort)0x100) : ((ushort)0));

                    int parts = 1;
                    int part  = 0;
                    int group = 0;

                    if (composite == Composite.Complex)
                    {
                        group = reader.ReadByte();
                        parts = reader.ReadByte();
                        part  = reader.ReadByte();
                    }

                    MessageType messageType = (MessageType)reader.ReadByte();
                    length += reader.ReadByte();

                    byte[] payload = reader.ReadBytes(length);
                    byte   sum     = reader.ReadByte();
                    int    index   = 0;

                    while (true)
                    {
                        if (index >= (stream.Length - 1L))
                        {
                            if (sum != 0)
                            {
                                throw new FormatException("Invalid checksum!");
                            }

                            var     types   = direction == Direction.MT ? KnownMTTypes : KnownMOTypes;
                            var     type    = types.Where(t => t.Key == messageType).FirstOrDefault();
                            Message message = (Message)Activator.CreateInstance(type.Value, true);

                            message.Version    = (ProtocolVersion)version;
                            message.Composite  = composite;
                            message.Group      = (byte)group;
                            message.Index      = (byte)part;
                            message.TotalParts = (byte)parts;
                            message.Payload    = payload;

                            var packet = new Packet()
                            {
                                Direction  = direction == Direction.MO ? PacketDirection.Outbound : PacketDirection.Inbound,
                                Index      = message.Index,
                                Group      = message.Group,
                                TotalParts = message.TotalParts,
                                Payload    = message.Payload,
                            };
                            packetBuffer.SavePacket(packet);

                            message.ReadyParts = (byte)packetBuffer.GetPacketCount(message.Group, packet.Direction);

                            if (message.Complete)
                            {
                                var __parts = packetBuffer
                                              .GetPackets(message.Group, packet.Direction)
                                              .OrderBy(x => x.Index)
                                              .Select(x => x.Payload)
                                              .ToList();

                                message.Payload = ByteArrayHelper.Merge(__parts);

                                using (MemoryStream stream2 = new MemoryStream(message.Payload))
                                {
                                    using (BinaryBitReader reader2 = new BinaryBitReader((Stream)stream2))
                                    {
                                        message.unpack(reader2);
                                    }
                                }

                                packetBuffer.DeletePackets(message.Group, packet.Direction);
                            }

                            return(message);
                        }

                        sum -= buffer[index];
                        index++;
                    }
                }
            }
        }
        public static Legacy_MessageMT Unpack(byte[] buffer, IPacketBuffer partsBuffer = null)
        {
            using (MemoryStream stream = new MemoryStream(buffer))
            {
                using (BinaryBitReader reader = new BinaryBitReader((Stream)stream))
                {
                    if (!CheckSignature(reader.ReadByte()))
                    {
                        throw new FormatException("Invalid signature!");
                    }

                    var group      = reader.ReadUInt(16);
                    var totalParts = reader.ReadUInt(8);
                    var partIndex  = reader.ReadUInt(8);
                    var payload    = reader.ReadAllBytes();

                    var message = new Legacy_MessageMT()
                    {
                        Group      = group,
                        Payload    = payload,
                        Index      = partIndex,
                        TotalParts = totalParts,
                    };


                    if (partsBuffer == null)
                    {
                        partsBuffer = new RealmPacketBuffer();
                    }


                    partsBuffer.SavePacket(new Packet()
                    {
                        Index      = message.Index,
                        Group      = message.Group,
                        TotalParts = message.TotalParts,
                        Payload    = message.Payload,
                        Direction  = PacketDirection.Inbound
                    });

                    message.ReadyParts = partsBuffer.GetPacketCount(message.Group, PacketDirection.Inbound);

                    if (message.Complete)
                    {
                        var ordered = partsBuffer
                                      .GetPackets(message.Group, PacketDirection.Inbound)
                                      .OrderBy(x => x.Index)
                                      .Select(x => x.Payload)
                                      .ToList();

                        message.Payload = ByteArrayHelper.Merge(ordered);

                        partsBuffer.DeletePackets(message.Group, PacketDirection.Inbound);
                    }


                    if (message.Complete)
                    {
                        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                        string text  = Encoding.GetEncoding(1251).GetString(message.Payload).Trim();
                        var    parts = text.Split(new string[] { ":" }, 2, StringSplitOptions.RemoveEmptyEntries);

                        if (parts.Count() < 2)
                        {
                            throw new FormatException($"Invalid legacy message format");
                        }

                        message.Address = parts[0];
                        message.RawText = parts[1];
                    }

                    return(message);
                }
            }
        }
Exemplo n.º 6
0
 public void Write(IPacketBuffer buffer)
 {
     Write(buffer.GetData(), buffer.BytesLength);
 }
Exemplo n.º 7
0
 public void Write(IPacketBuffer buffer)
 {
     Write(buffer.GetData(), buffer.BytesLength);
 }