Example #1
0
        private void LogPacket(PacketLogEntry logEntry)
        {
            lock (Remote.LoggedPackets)
            {
                Remote.LoggedPackets.Enqueue(logEntry);

                while (Remote.LoggedPackets.Count > 50)
                {
                    Remote.LoggedPackets.Dequeue();
                }
            }
        }
Example #2
0
        public int SendPacket(G2Packet packet)
        {
            Debug.Assert(Thread.CurrentThread.ManagedThreadId == XRay.CoreThread.ManagedThreadId, "Send packet called not on core thread");

            if (State != TcpState.Connected)
            {
                return(0);
            }

            try
            {
                byte[]         encoded  = packet.Encode(Protocol);
                PacketLogEntry logEntry = new PacketLogEntry(DateTime.Now, DirectionType.Out, ToString(), encoded);
                LogPacket(logEntry);

                // fill up final buffer, keep encrypt buffer clear
                if (BUFF_SIZE - SendBuffSize < encoded.Length + 128)
                {
                    throw new Exception("SendBuff Full");
                }

                // encrypt
                encoded.CopyTo(SendBuffer, SendBuffSize);
                SendBuffSize += encoded.Length;

                OnlyFillerInSendBuffer = false;

                TrySend();

                // record bandwidth
                return(encoded.Length);
            }
            catch (Exception ex)
            {
                Log("SendPacket", ex.Message);
            }

            return(0);
        }
Example #3
0
        void ReceivePackets()
        {
            int          Start        = 0;
            G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;

            while (streamStatus == G2ReadResult.PACKET_GOOD)
            {
                G2ReceivedPacket packet = new G2ReceivedPacket();
                packet.Root = new G2Header(FinalRecvBuffer);

                streamStatus = G2Protocol.ReadNextPacket(packet.Root, ref Start, ref FinalRecvBuffSize);

                if (streamStatus != G2ReadResult.PACKET_GOOD)
                {
                    break;
                }

                packet.Tcp = this;

                // extract data from final recv buffer so it can be referenced without being overwritten by this thread
                byte[] extracted = Utilities.ExtractBytes(packet.Root.Data, packet.Root.PacketPos, packet.Root.PacketSize);
                packet.Root = new G2Header(extracted);
                G2Protocol.ReadPacket(packet.Root);

                PacketLogEntry logEntry = new PacketLogEntry(DateTime.Now, DirectionType.In, ToString(), packet.Root.Data);
                LogPacket(logEntry);

                Remote.IncomingPacket(this, packet);
            }

            // re-align buffer
            if (Start > 0 && FinalRecvBuffSize > 0)
            {
                Buffer.BlockCopy(FinalRecvBuffer, Start, FinalRecvBuffer, 0, FinalRecvBuffSize);
                //Network.UpdateConsole(PacketBytesReady.ToString() + " bytes moved to front of receive buffer");
            }
        }
Example #4
0
        void ReceivePackets()
        {
            int Start = 0;
            G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;

            while (streamStatus == G2ReadResult.PACKET_GOOD)
            {
                G2ReceivedPacket packet = new G2ReceivedPacket();
                packet.Root = new G2Header(FinalRecvBuffer);

                streamStatus = G2Protocol.ReadNextPacket(packet.Root, ref Start, ref FinalRecvBuffSize);

                if (streamStatus != G2ReadResult.PACKET_GOOD)
                    break;

                packet.Tcp = this;

                // extract data from final recv buffer so it can be referenced without being overwritten by this thread
                byte[] extracted = Utilities.ExtractBytes(packet.Root.Data, packet.Root.PacketPos, packet.Root.PacketSize);
                packet.Root = new G2Header(extracted);
                G2Protocol.ReadPacket(packet.Root);

                PacketLogEntry logEntry = new PacketLogEntry(DateTime.Now, DirectionType.In, ToString(), packet.Root.Data);
                LogPacket(logEntry);

                Remote.IncomingPacket(this, packet);
            }

            // re-align buffer
            if (Start > 0 && FinalRecvBuffSize > 0)
            {
                Buffer.BlockCopy(FinalRecvBuffer, Start, FinalRecvBuffer, 0, FinalRecvBuffSize);
                //Network.UpdateConsole(PacketBytesReady.ToString() + " bytes moved to front of receive buffer");
            }
        }
Example #5
0
        private void LogPacket(PacketLogEntry logEntry)
        {
            lock (Remote.LoggedPackets)
            {
                Remote.LoggedPackets.Enqueue(logEntry);

                while (Remote.LoggedPackets.Count > 50)
                    Remote.LoggedPackets.Dequeue();
            }
        }
Example #6
0
        public int SendPacket(G2Packet packet)
        {
            Debug.Assert(Thread.CurrentThread.ManagedThreadId == XRay.CoreThread.ManagedThreadId, "Send packet called not on core thread");

            if (State != TcpState.Connected)
                return 0;

            try
            {
                byte[] encoded = packet.Encode(Protocol);
                PacketLogEntry logEntry = new PacketLogEntry(DateTime.Now, DirectionType.Out, ToString(), encoded);
                LogPacket(logEntry);

                // fill up final buffer, keep encrypt buffer clear
                if (BUFF_SIZE - SendBuffSize < encoded.Length + 128)
                    throw new Exception("SendBuff Full");

                // encrypt
                encoded.CopyTo(SendBuffer, SendBuffSize);
                SendBuffSize += encoded.Length;

                OnlyFillerInSendBuffer = false;

                TrySend();

                // record bandwidth
                return encoded.Length;
            }
            catch (Exception ex)
            {
                Log("SendPacket", ex.Message);
            }

            return 0;
        }