Beispiel #1
0
        /// <summary>
        /// 异步发送
        /// </summary>
        /// <param name="data">发送数据</param>
        /// <returns> </returns>
        public bool Send(byte[] data)
        {
            if (kcp == null || !connected)
            {
                return(false);
            }

            if (kcp.WaitSnd() < MaxWaitSnd)
            {
                kcp.Send(data);
                return(true);
            }

            return(false);
        }
Beispiel #2
0
    void update(UInt32 current)
    {
        if (mInConnectStage)
        {
            if (connect_timeout(current))
            {
                evHandler(cliEvent.ConnectFailed, null, "Timeout");
                mInConnectStage = false;
                return;
            }

            if (need_send_connect_packet(current))
            {
                mLastSendConnectTime = current;
                mUdpClient.Send(new byte[4] {
                    0, 0, 0, 0
                }, 4);
            }

            process_connect_packet();

            return;
        }

        if (mConnectSucceed)
        {
            // 处理数据队列
            process_recv_queue();

            // 更新kcp
            if (mNeedUpdateFlag || current >= mNextUpdateTime)
            {
                mKcp.Update(current);
                mNextUpdateTime = mKcp.Check(current);
                mNeedUpdateFlag = false;
            }
        }

        // 关闭阶段
        if (mInCloseStage)
        {
            // 如果有待发送的数据等待数据发完之后在发送
            // 如果超时仍然没有发完那么直接关闭
            if (mKcp == null || mKcp.WaitSnd() <= 0 || close_timeout(current))
            {
                evHandler(cliEvent.Disconnect, null, mCloseReason);
                mUdpClient.Close();
                mInCloseStage = false;
            }
        }
    }