Beispiel #1
0
        public void HeaderSerializingTest()
        {
            MessageHeader expectedHeader = new MessageHeader()
            {
                Version  = "9.99",
                MetaSize = 100,
                BodySize = 1024 * 1024,
            };

            byte[]        header       = expectedHeader.SerializeHeader();
            MessageHeader actualHeader = MessageHeader.DeserializeHeader(header);

            Assert.AreEqual(header.Length, 18);
            Assert.AreEqual(expectedHeader.Version, actualHeader.Version);
            Assert.AreEqual(expectedHeader.MetaSize, actualHeader.MetaSize);
            Assert.AreEqual(expectedHeader.BodySize, actualHeader.BodySize);
        }
Beispiel #2
0
        public void TestClientMessageSendEvent()
        {
            string       expectedIp   = "127.0.0.1";
            int          expectedPort = 44403;
            SocketServer target       = new SocketServer();

            target.Port       = expectedPort;
            target.BindingIPs = new string[] { expectedIp };
            int  clientStartCount = 0;
            int  clientStopCount  = 0;
            long clientId         = -1;

            using (var isStarted = new AutoResetEvent(false))
                using (var isStopped = new AutoResetEvent(false))
                {
                    target.RegisterClientEvents(c =>
                    {
                        clientStartCount++;
                        clientId = c.Id;
                        isStarted.Set();
                    }
                                                , c =>
                    {
                        clientStopCount++;
                        isStopped.Set();
                    }
                                                , (c, m, bs, b) => { }
                                                );
                    target.Start();

                    TcpClient client = new TcpClient();
                    client.Connect(expectedIp, expectedPort);

                    isStarted.WaitOne(Debugger.IsAttached ? Timeout.Infinite : 1500);

                    string expectedMeta = "This is a test";
                    string expectedBody = "This is a test body";
                    byte[] body         = Encoding.UTF8.GetBytes(expectedBody);
                    using (MemoryStream bodyStream = new MemoryStream(body))
                    {
                        target.SendMessage(clientId, expectedMeta, bodyStream);
                    }

                    string actualMeta = null;
                    string actualBody = null;
                    using (var stream = client.GetStream())
                    {
                        byte[] headerBytes = new byte[MessageHeader.BinarySize];
                        // read header
                        stream.Read(headerBytes, 0, headerBytes.Length);
                        var header = MessageHeader.DeserializeHeader(headerBytes);

                        // read the meta data and convert to string
                        byte[] metaBytes = new byte[header.MetaSize];
                        stream.Read(metaBytes, 0, metaBytes.Length);

                        actualMeta = Encoding.UTF8.GetString(metaBytes);
                        byte[] bodyBytes = new byte[header.BodySize];
                        stream.Read(bodyBytes, 0, bodyBytes.Length);

                        actualBody = Encoding.UTF8.GetString(bodyBytes);
                    }

                    client.Close();
                    isStopped.WaitOne(Debugger.IsAttached ? Timeout.Infinite : 1500);

                    target.Stop();

                    Assert.AreEqual(clientStartCount, 1);
                    Assert.AreEqual(clientStopCount, 1);

                    Assert.AreEqual(expectedMeta, actualMeta);
                    Assert.AreEqual(expectedBody, actualBody);
                };
        }