Exemplo n.º 1
0
        public byte[] CryptoReceiveMsg()
        {
            bool        recvMode = false;
            BlindPacket packet   = new BlindPacket();

            byte[] result = null;
            do
            {
                packet = CryptoReceive(recvMode);
                if (packet.header == PacketType.Disconnect)
                {
                    return(null);
                }
                result = BlindNetUtil.MergeArray <byte>(result, packet.data);
                Debug.WriteLine("Receive {0}/{1} bytes", packet.data.Length, result.Length);
                recvMode = true;
            } while (packet.header == PacketType.Sending);
            result = BlindNetUtil.ByteTrimEndNull(result);
            return(result);
        }
Exemplo n.º 2
0
        public BlindPacket CryptoReceive(bool isRecieving = false)
        {
            byte[] data   = new byte[BlindNetConst.MINIPACKSIZE];
            int    rcvNum = 0;

            byte[]          decrypted  = null;
            BlindMiniPacket miniPacket = new BlindMiniPacket();

            if (!isRecieving)
            {
                rcvNum = socket.Receive(data, BlindNetConst.MINIPACKSIZE, SocketFlags.None);
                new NetworkStream(socket).Flush();
                if (rcvNum == 0)
                {
                    BlindPacket end;
                    end.data   = null;
                    end.header = PacketType.Disconnect;
                    return(end);
                }
                byte[] temp = new byte[BlindNetConst.MINIPACKSIZE];
                temp[0] = (byte)PacketType.OK;
                socket.Send(temp, BlindNetConst.MINIDATASIZE, SocketFlags.None);

                decrypted  = aes.Decryption(data);
                miniPacket = BlindNetUtil.ByteToStruct <BlindMiniPacket>(decrypted);
            }
            if (isRecieving || miniPacket.header == PacketType.MSG)
            {
                data = new byte[0];
                while (true)
                {
                    byte[] tmp = new byte[BlindNetConst.PACKSIZE];
                    rcvNum = socket.Receive(tmp, BlindNetConst.PACKSIZE - data.Length, SocketFlags.None);
                    using (NetworkStream stream = new NetworkStream(socket))
                        stream.Flush();
#if DEBUG
                    if (tmp[tmp.Length - 1] == 0)
                    {
                        Console.WriteLine("Received less bytes");
                    }
#endif
                    if (rcvNum == 0)
                    {
                        BlindPacket end;
                        end.data   = null;
                        end.header = PacketType.Disconnect;
                        return(end);
                    }

                    data = BlindNetUtil.MergeArray <byte>(data, BlindNetUtil.ByteTrimEndNull(tmp));
                    if (data.Length == BlindNetConst.PACKSIZE)
                    {
                        byte[] temp = new byte[BlindNetConst.MINIPACKSIZE];
                        temp[0] = (byte)PacketType.OK;
                        socket.Send(temp, BlindNetConst.MINIPACKSIZE, SocketFlags.None);
                        using (NetworkStream stream = new NetworkStream(socket))
                            stream.Flush();
                        break;
                    }
                }

                decrypted = aes.Decryption(data);
            }

            return(BlindNetUtil.ByteToStruct <BlindPacket>(decrypted));
        }