예제 #1
0
파일: Test1.cs 프로젝트: vskit/LightingCat
    void Start()
    {
        byte[] recv = new byte[16 * 1024];

        connector = new TcpConnector("192.168.31.236", 8888);
        connector.On(SocketEvents.Message, (d) =>
        {
            byte[] reData = d as byte[];
            Debug.LogError(reData.Length);
            unpacker.Input(reData);
            int length;
            ushort msgId;
            while (unpacker.MoveNext(recv, out length, out msgId))
            {
                LoginS2C lmsg = ProtobufTool.BytesToProtoBuf <LoginS2C>(recv, 0, length);
                Debug.LogError("recv msgID: " + msgId + " data:" + lmsg.playerId);
            }
        });
        var wait = connector.Connect();

        if (!Util.Wait(wait, 1000))
        {
            connector.Dispose();
        }
    }
예제 #2
0
        public void TestOffEvent()
        {
            var             connector = new TcpConnector("127.0.0.1", 7777);
            Action <object> call      = (ex) =>
            {
                Assert.Fail();
            };

            connector.On(SocketEvents.Error, call);
            connector.On(SocketEvents.Error, call);

            connector.On(SocketEvents.Error, (ex) =>
            {
            });

            connector.Off(SocketEvents.Error, call);
            connector.Trigger(SocketEvents.Error, new Exception());
        }
예제 #3
0
        public void SimpleTcpTest()
        {
            var connector = new TcpConnector("127.0.0.1", 7777);

            var data   = new byte[0];
            var server = new TcpTestsServer(b =>
            {
                data = b;
            });

            var reData = new byte[0];

            connector.On(SocketEvents.Message, (d) =>
            {
                reData = d as byte[];
            });

            var wait = connector.Connect();

            if (!Util.Wait(wait, 3000))
            {
                server.Dispose();
                connector.Dispose();
                Assert.Fail("wait faild");
            }

            wait = connector.Send(System.Text.Encoding.Default.GetBytes("hello world"));

            if (!Util.Wait(wait, 3000) ||
                !Util.Wait(ref data, 3000) ||
                !Util.Wait(ref reData, 3000))
            {
                server.Dispose();
                connector.Dispose();
                Assert.Fail("wait faild");
            }

            server.Dispose();
            connector.Disconnect();

            Assert.AreEqual("hello world", System.Text.Encoding.Default.GetString(data));
            Assert.AreEqual("hello world", System.Text.Encoding.Default.GetString(reData));
        }
예제 #4
0
        public void TestDnsException()
        {
            var       connector = new TcpConnector("http://123.0.0.1", 7777);
            Exception e         = null;

            connector.On(SocketEvents.Error, (ex) =>
            {
                e = ex as Exception;
            });

            var wait = connector.Connect();

            if (!Util.Wait(wait, 10000))
            {
                connector.Dispose();
                Assert.Fail("wait faild");
            }

            Assert.AreEqual(typeof(SocketException), (wait.Result as Exception).GetType());
        }