Example #1
0
        public void SendPacket(byte[] packet)
        {
            Precondition.Check <InvalidOperationException>(Connected, "Socket is not connected");
            Precondition.Check <InvalidOperationException>(Encrypted, "Handshake has not been received yet");
            Precondition.Check <ArgumentException>(packet.Length >= 2, @"Packet length must be greater than 2");

            lock (sendLock) {
                byte[] final = new byte[packet.Length + PACKET_HEADER_SIZE];

                switch (SessionType)
                {
                case SessionType.CLIENT:
                    clientCipher.GetHeaderToServer(packet.Length, final);
                    break;

                case SessionType.SERVER:
                    clientCipher.GetHeaderToClient(packet.Length, final);
                    break;
                }

                clientCipher.Transform(packet);
                Buffer.BlockCopy(packet, 0, final, PACKET_HEADER_SIZE, packet.Length);
                SendRawPacket(final);
            }
        }
Example #2
0
        public void echo_printing_to_parent_in_the_right_fore_and_back_colors()
        {
            var console = new MockConsole(3, 3);

            console.ForegroundColor = ConsoleColor.Red;
            console.BackgroundColor = ConsoleColor.White;
            console.PrintAt(0, 0, "X");

            var expectedBefore = new[]
            {
                "Xrw wk wk",
                " wk wk wk",
                " wk wk wk"
            };

            Precondition.Check(() => expectedBefore.Should().BeEquivalentTo(console.BufferWithColor));

            var w = new Window(console, K.Transparent);

            w.ForegroundColor = ConsoleColor.DarkGreen;
            w.BackgroundColor = ConsoleColor.DarkCyan;
            w.PrintAt(1, 1, "YY");

            var expectedAfter = new[]
            {
                "Xrw wk wk",
                " wkYGCYGC",
                " wk wk wk"
            };

            Assert.AreEqual(expectedAfter, console.BufferWithColor);
        }
        public ThreadPool(int maxThreads, int maxQueueSize)
        {
            Precondition.Check(maxThreads > 0, "Cannot have less than 1 maxThread");
            Precondition.Check(maxQueueSize > 0, "Cannot have less than 1 maxQueueSize");

            mMaxConcurrency = maxThreads;
            mMaxQueueSize   = maxQueueSize;
            Init();
        }
Example #4
0
        //TODO: WaitRecv with timeout?

        protected TScript Requires <TScript>() where TScript : Script
        {
            var script = manager.Get <TScript>();

            Precondition.Check <InvalidOperationException>(!(script is UserScript),
                                                           "You cannot require a UserScript.");
            Precondition.Check <InvalidOperationException>(dependencies.Contains(script),
                                                           $"{script.GetType().Name} is already a dependency.");
            script.Start(); // Make sure script is started
            dependencies.Add(script);

            return(script);
        }
Example #5
0
        public AesCipher(byte[] aesKey)
        {
            Precondition.NotNull(aesKey, nameof(aesKey));
            Precondition.Check <ArgumentOutOfRangeException>(aesKey.Length == 32, "Key length needs to be 32");

            var aes = new RijndaelManaged {
                Key     = aesKey,
                Mode    = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            using (aes) {
                crypto = aes.CreateEncryptor();
            }
        }
Example #6
0
        public void When_child_offset_is_none_zero_scroll_only_the_offset_region()
        {
            var con = new MockConsole(8, 4);

            con.WriteLine("aaaaaaaa");
            con.WriteLine("bbbbbbbb");
            con.WriteLine("cccccccc");
            con.Write("dddddddd");
            var window = new Window(con, 1, 1, 6, 2);

            // are we good at this point?
            var expected = new[]
            {
                "aaaaaaaa",
                "b      b",
                "c      c",
                "dddddddd"
            };

            Precondition.Check(() => con.Buffer.Should().BeEquivalentTo(expected));

            window.WriteLine("123456");
            window.Write("7890AB");
            expected = new[]
            {
                "aaaaaaaa",
                "b123456b",
                "c7890ABc",
                "dddddddd"
            };
            Precondition.Check(() => con.Buffer.Should().BeEquivalentTo(expected));

            window.ScrollDown();
            expected = new[]
            {
                "aaaaaaaa",
                "b7890ABb",
                "c      c",
                "dddddddd"
            };
            con.Buffer.Should().BeEquivalentTo(expected);
        }
        public void WhenNestedTwoWindowsDeep_test()
        {
            Precondition.Check(() => WhenNestedInWindow_test());

            var con    = new MockConsole(20, 8);
            var win    = new Window(con);
            var float1 = Window._CreateFloatingWindow(5, 1, 7, 5, White, Black, true, win);

            float1.Write("00000001111111222222233333334444444");
            var expected = new[] {
                "                    ",
                "     0000000        ",
                "     1111111        ",
                "     2222222        ",
                "     3333333        ",
                "     4444444        ",
                "                    ",
                "                    "
            };

            con.Buffer.Should().BeEquivalentTo(expected);

            // now test creating a nested window
            var float2 = Window._CreateFloatingWindow(3, 2, 3, 2, White, Black, true, float1);

            float2.CursorLeft = 0;
            float2.CursorTop  = 0;
            float2.Write(@"===...");

            expected = new[] {
                "                    ",
                "     0000000        ",
                "     1111111        ",
                "     222===2        ",
                "     333...3        ",
                "     4444444        ",
                "                    ",
                "                    "
            };

            con.Buffer.Should().BeEquivalentTo(expected);
        }
Example #8
0
 protected void RegisterRecv(ushort header, Action <Client, PacketReader> handler)
 {
     Precondition.Check <InvalidOperationException>(client.AddScriptRecv(header, handler),
                                                    $"Failed to register header {header:X4}.");
     headers.Add(header);
 }