Esempio n. 1
0
        protected void SendTlvPacket(byte[] packet)
        {
            try
            {
                var tlvPacket = NetworkTlvPacket.TlvPacketFrom(packet);

                //    Console.WriteLine("PACKET SIZE: " + packet.Length);

                tcpClient?.GetStream().Write(tlvPacket, 0, tlvPacket.Length);
            }
            catch (Exception)
            {
                Close();
                throw;
            }
        }
Esempio n. 2
0
        protected IAsyncResult SendTlvPacketAsync(byte[] packet)
        {
            var tlvPacket = NetworkTlvPacket.TlvPacketFrom(packet);

            try
            {
                var networkStream = tcpClient.GetStream();

                WriteDelegate write = networkStream.Write;
                //we DONT use BeginWrite because of buggy mono that sometimes does not Derive this message
                var result = write.BeginInvoke(tlvPacket, 0, tlvPacket.Length, WriteCallback, null);
                return(result);
            }
            catch (Exception)
            {
                Close();
            }

            return(null);
        }
Esempio n. 3
0
        public Type GetDotNetTypeFrom(NetworkTlvPacket packet)
        {
            var basicMessage = serializer.DeserializePacket(packet.Value, typeof(NetworkMessageBase));

            if (cache.ContainsKey(basicMessage.TypeName))
            {
                return(cache[basicMessage.TypeName]);
            }

            foreach (var assembly in assembliesToLookup)
            {
                var t = assembly.GetTypes().FirstOrDefault(z => z.Name == basicMessage.TypeName);
                if (t == null)
                {
                    continue;
                }

                cache.Add(basicMessage.TypeName, t);
                return(t);
            }
            throw new TypeNotFoundException("Type not found: " + basicMessage.TypeName);
        }
Esempio n. 4
0
        public void ProcessReceive()
        {
            var headerBytes = ReadComplete(NetworkTypeLengthHeader.Size);
            var header      = headerBytes.ToStructure <NetworkTypeLengthHeader>();


            if (header.MessageSize > 0 && header.MessageSize <= MaxPacketSize &&
                header.Marker == NetworkTypeLengthHeader.ValidHeaderMarker)
            {
                var packet = new NetworkTlvPacket
                {
                    Header = header,
                    Value  = ReadComplete(header.MessageSize)
                };
                OnTlvPacketReceived(packet);
            }
            else
            {
                var exception = new Exception("Invalid Packet Received");
                Close();
                throw exception;
            }
        }
Esempio n. 5
0
 protected abstract void OnTlvPacketReceived(NetworkTlvPacket packet);