コード例 #1
0
        public NetworkBase(NetworkProtocol protocol)
        {
            // CompositeResolver is singleton helper for use custom resolver.
            // Ofcourse you can also make custom resolver.
            MessagePack.Resolvers.CompositeResolver.RegisterAndSetAsDefault(
                // use generated resolver first, and combine many other generated/custom resolvers
                MessagePack.Resolvers.GeneratedResolver.Instance,
                MessagePack.Resolvers.BuiltinResolver.Instance,
                MessagePack.Resolvers.AttributeFormatterResolver.Instance,
                MessagePack.Resolvers.PrimitiveObjectResolver.Instance
                );

            switch (protocol)
            {
            case NetworkProtocol.WebSocket:
                Network = new WebSocketLis();
                break;

            case NetworkProtocol.UDP:
                Network = new Udp();
                break;

            case NetworkProtocol.TCP:
                Network = new Tcp();
                break;

            case NetworkProtocol.RUDP:
                Network = new RUdp();
                break;
            }

            Network.CompleteConnect += () =>
            {
                OnStatusChanged(StatusCode.Connect);
            };
            Network.CompleteDisConnect += () =>
            {
                OnStatusChanged(StatusCode.Disconnect);
            };
            Network.CompleteSend += () =>
            {
                CanSend = true;
            };
            Network.CompleteReadFromServerStream += result =>
            {
                byte[] receiveBytes = result;
                if (!receiveBytes.Length.Equals(1))
                {
                    receiveBytes = Math.Serialize.Decompress(receiveBytes);
                    IPacket packet = null;
                    //解包成我定義的封包
                    try
                    {
                        packet = (IPacket)Math.Serialize.ToObject(receiveBytes);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                    if (packet != null) //將資料存入全域暫存區
                    {
                        SocketReceiver.AddToCurrectLine(packet);
                    }
                }
                else
                {
                    //Debug.Log("小包到達");
                    //回傳一小包代表該 Client 還活者
                    Network.BeginSend(new byte[] { 0 }, 1);
                }
            };
        }
コード例 #2
0
        public NetworkBase(NetworkProtocol protocol)
        {
            switch (protocol)
            {
            case NetworkProtocol.WebSocket:
                Network = new WebSocketLis();
                break;

            case NetworkProtocol.UDP:
                Network = new Udp();
                break;

            case NetworkProtocol.TCP:
                Network = new Tcp();
                break;

            case NetworkProtocol.RUDP:
                Network = new RUdp();
                break;
            }

            Network.CompleteConnect += () =>
            {
                OnStatusChanged(StatusCode.Connect);
            };
            Network.CompleteDisConnect += () =>
            {
                OnStatusChanged(StatusCode.Disconnect);
            };
            Network.CompleteSend += () =>
            {
                CanSend = true;
            };
            Network.CompleteReadFromServerStream += result =>
            {
                byte[] receiveBytes = result;
                if (!receiveBytes.Length.Equals(1))
                {
                    receiveBytes = Math.Serialize.Decompress(receiveBytes);
                    IPacket packet = null;
                    //解包成我定義的封包
                    try
                    {
                        packet = (IPacket)Math.Serialize.ToObject(receiveBytes);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                    if (packet != null) //將資料存入全域暫存區
                    {
                        SocketReceiver.AddToCurrectLine(packet);
                    }
                }
                else
                {
                    //Debug.Log("小包到達");
                    //回傳一小包代表該 Client 還活者
                    Network.BeginSend(new byte[] { 0 }, 1);
                }
            };
        }