Encode() public method

public Encode ( G2Protocol protocol ) : byte[]
protocol G2Protocol
return byte[]
Exemplo n.º 1
0
        public void TrySend()
        {
            // cant send if we're in the process of sending
            if (FinalSendBuffSize > 0 || State != TcpState.Connected || OnlyFillerInSendBuffer)
                return;

            if (Encryptor == null)
                CreateEncryptor();

            // try to move from send buff to final buff
            lock (FinalSendBuffer)
            {
                int remainder = SendBuffSize % Encryptor.InputBlockSize;
                if (remainder > 0)
                {
                    CryptPadding padding = new CryptPadding();

                    int fillerNeeded = Encryptor.InputBlockSize - remainder;

                    if (fillerNeeded > 2)
                        padding.Filler = new byte[fillerNeeded - 2];

                    var filler = padding.Encode(Protocol);
                    filler.CopyTo(SendBuffer, SendBuffSize);
                    SendBuffSize += filler.Length;
                    OnlyFillerInSendBuffer = true;
                }

                int tryTransform = SendBuffSize - (SendBuffSize % Encryptor.InputBlockSize);
                if (tryTransform > 0)
                {
                    int tranformed = Encryptor.TransformBlock(SendBuffer, 0, tryTransform, FinalSendBuffer, FinalSendBuffSize);
                    if (tranformed > 0)
                    {
                        FinalSendBuffSize += tranformed;
                        SendBuffSize -= tranformed;
                        Buffer.BlockCopy(SendBuffer, tranformed, SendBuffer, 0, SendBuffSize);
                    }
                }
            }

            if (FinalSendBuffSize == 0)
                return;

            try
            {
                lock (FinalSendBuffer)
                {
                    //Core.UpdateConsole("Begin Send " + SendBufferSize.ToString());

                    //TcpSocket.Blocking = false
                    //Log("x", "Sending " + FinalSendBuffSize.ToString() + " bytes");
                    TcpSocket.BeginSend(FinalSendBuffer, 0, FinalSendBuffSize, SocketFlags.None, new AsyncCallback(Socket_Send), TcpSocket);
                    //bytesSent = TcpSocket.Send(FinalSendBuffer, FinalSendBuffSize, SocketFlags.None);
                }
            }

            catch (Exception ex)
            {
                Log("TrySend", ex.Message);
                Disconnect();
            }
        }
Exemplo n.º 2
0
        public void TrySend()
        {
            // cant send if we're in the process of sending
            if (FinalSendBuffSize > 0 || State != TcpState.Connected || OnlyFillerInSendBuffer)
            {
                return;
            }

            if (Encryptor == null)
            {
                CreateEncryptor();
            }

            // try to move from send buff to final buff
            lock (FinalSendBuffer)
            {
                int remainder = SendBuffSize % Encryptor.InputBlockSize;
                if (remainder > 0)
                {
                    CryptPadding padding = new CryptPadding();

                    int fillerNeeded = Encryptor.InputBlockSize - remainder;

                    if (fillerNeeded > 2)
                    {
                        padding.Filler = new byte[fillerNeeded - 2];
                    }

                    var filler = padding.Encode(Protocol);
                    filler.CopyTo(SendBuffer, SendBuffSize);
                    SendBuffSize          += filler.Length;
                    OnlyFillerInSendBuffer = true;
                }

                int tryTransform = SendBuffSize - (SendBuffSize % Encryptor.InputBlockSize);
                if (tryTransform > 0)
                {
                    int tranformed = Encryptor.TransformBlock(SendBuffer, 0, tryTransform, FinalSendBuffer, FinalSendBuffSize);
                    if (tranformed > 0)
                    {
                        FinalSendBuffSize += tranformed;
                        SendBuffSize      -= tranformed;
                        Buffer.BlockCopy(SendBuffer, tranformed, SendBuffer, 0, SendBuffSize);
                    }
                }
            }

            if (FinalSendBuffSize == 0)
            {
                return;
            }

            try
            {
                lock (FinalSendBuffer)
                {
                    //Core.UpdateConsole("Begin Send " + SendBufferSize.ToString());

                    //TcpSocket.Blocking = false
                    //Log("x", "Sending " + FinalSendBuffSize.ToString() + " bytes");
                    TcpSocket.BeginSend(FinalSendBuffer, 0, FinalSendBuffSize, SocketFlags.None, new AsyncCallback(Socket_Send), TcpSocket);
                    //bytesSent = TcpSocket.Send(FinalSendBuffer, FinalSendBuffSize, SocketFlags.None);
                }
            }

            catch (Exception ex)
            {
                Log("TrySend", ex.Message);
                Disconnect();
            }
        }