Exemplo n.º 1
0
        /**
         * 从data中分离出数据包
         * @param data
         */
        private void ReadDataPacket(ByteArray data)
        {
            while (data.bytesAvailable > 0)
            {
                //开始读取一个新包
                if (packetLength == 0)
                {
                    if (data.bytesAvailable > PACKET_LENGTH_SIZE)
                    {
                        //包长按网络字节顺序
                        packetLength  = IPAddress.HostToNetworkOrder(data.ReadInt32());
                        packetLength -= PACKET_LENGTH_SIZE;
                    }
                    else
                    {
                        break;
                    }
                }

                if (data.bytesAvailable >= packetLength)
                {
                    //读取完整包
                    var packet = data.ReadBytes(packetLength);
                    onReceive.Invoke(this, packet);
                    packetLength = 0;
                }
                else
                {
                    //等待后续数据
                    break;
                }
            }
        }
Exemplo n.º 2
0
        public void Reconnect()
        {
            if (hasDisposed || socket.Connected)
            {
                if (hasDisposed)
                {
                    error = "instance has disposed";
                    onError.Invoke(this, error);
                }
                else
                {
                    onConnect.Invoke(this);
                }

                return;
            }

            if (!socket.ConnectAsync(sendSAEA))
            {
                OnSocketCompleted(socket, sendSAEA);
            }
        }
Exemplo n.º 3
0
        public static void Update()
        {
            if (lastTime == 0f)
            {
                lastTime = Time.time;
            }
            else if (Time.time - lastTime >= 1f)
            {
                onPerSecond.Invoke();
                lastTime = Time.time;
            }

            foreach (var item in intervals.Values)
            {
                item.Update();
            }
        }
Exemplo n.º 4
0
        public void Connect(string host, int port)
        {
            if (hasDisposed)
            {
                error = "instance has disposed";
                onError.Invoke(this, error);
                return;
            }

            this.host = host;
            this.port = port;

            IPAddress ipAddr;

            if (IPAddress.TryParse(host, out ipAddr))
            {
                sendSAEA.RemoteEndPoint = new IPEndPoint(ipAddr, port);
                Reconnect();
            }
            else
            {
                Dns.BeginGetHostAddresses(host, (result) =>
                {
                    var addr = Dns.EndGetHostAddresses(result);
                    if (addr.Length > 0)
                    {
                        sendSAEA.RemoteEndPoint = new IPEndPoint(addr[0], port);
                        Reconnect();
                    }
                    else
                    {
                        error = "TcpClient.Connect, unknown host: " + host;
                        onError.Invoke(this, error);
                    }
                }, null);
            }
        }