예제 #1
0
        /// <summary>
        /// 傳送一個 Dictionary 給 Server ,目前 Dictionary.Value 不支援不能序列化的物件,例如 Dictionary 。不能使用多層 Dictionary
        /// </summary>
        /// <param name="code">此封包的索引碼,在伺服器中用此作為索引</param>
        /// <param name="dic">傳給伺服器的封包</param>
        private void deliver(byte code, Dictionary <byte, object> dic)
        {    //普通的傳送
            CanSend = false;
            //宣告一個傳送暫存
            byte[] tx;
            //將傳入的 code , dic , 傳換為我定義的封包
            IPacket packet = new IPacket(code, new Dictionary <byte, object>(dic))
            {
            };

            try
            {
                //將此封包,序列化並加入暫存區
                tx = Math.Serialize.ToByteArray(packet);
                tx = Math.Serialize.Compress(tx);
                Network.BeginSend(tx, tx.Length);
            }
            catch (Exception exc)
            {
                LogProxy.WriteError(exc.Message);
            }
        }
예제 #2
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);
                }
            };
        }
예제 #3
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);
                }
            };
        }