コード例 #1
0
        private void _BeginSend()
        {
            if (this._sendingFlag)
            {//正在发送则无需处理,等本次发送完毕,会自行进行下次数据发送
                return;
            }
            this._sendingFlag = true;

            long curMili = Utils.CurrentTimeMillis();

            _socket.BeginSend(_sendStream.Oct.Datas, _sendStream.Position, _sendStream.RemainCount, SocketFlags.None, iar => _actions.Enqueue(() =>
            {
                int length = 0;
                try
                {
                    length = _socket.EndSend(iar);
                    //TestArray.Add(Utils.CurrentTimeMillis() - curMili);
                }
                catch (Exception)
                {
                    ExcetpionHappen();
                    return;
                }
                _sendStream.ReCalculatePosition(length);


                this._sendingFlag = false;
                if (_sendStream.Oct.Count > 0)
                {
                    _BeginSend();
                }
            }), null);
        }
コード例 #2
0
ファイル: ThdLinker.cs プロジェクト: OneBigShrimp/NetManager
        public void Tick(object args)
        {
            lock (_receiveTransitStream)
            {
                if (_receiveTransitStream.RemainCount > 0)
                {
                    SerializeControl.Instance.Deserialize(_receiveTransitStream, _protocols);
                    if (_receiveTransitStream.Position != 0)
                    {
                        _receiveTransitStream.ReCalculatePosition(0);
                    }
                }
            }

            while (_protocols.Count > 0)
            {
                IProtocol p = _protocols.Dequeue();
                p.Process(this, args);
            }

            Action action;

            if (_actions.TryDequeue(out action))
            {
                action();
            }
            else
            {
                //break;
            }
        }
コード例 #3
0
ファイル: ThdLinker.cs プロジェクト: OneBigShrimp/NetManager
        void Send(object socketObj)
        {
            //不直接访问主线程的_socket字段,因为Close的时候会清空该字段,虽然同时会停止发送线程,安全起见,线程内部保存了一个对象
            //接收同理
            Socket socket = socketObj as Socket;

            while (true)
            {
                _sendEvent.WaitOne();
                lock (_sendTransitOct)
                {
                    _sendBuffer.Oct.Write(_sendTransitOct);
                    _sendTransitOct.Clear();
                }

                SocketError error;

                int saveFlag = 0;

                while (_sendBuffer.RemainCount > 0)
                {
                    if (saveFlag++ > 100)
                    {
                        //throw new Exception("Send loop is over 100 times!");
                        //关闭Link
                        Close(NetExceptionType.Send, new Exception("Maybe send thread is dead loop"));
                        return;
                    }
                    int sendCount = 0;
                    try
                    {
                        sendCount = socket.Send(_sendBuffer.Oct.Datas, _sendBuffer.Position, _sendBuffer.RemainCount, SocketFlags.None, out error);
                    }
                    catch (Exception e)
                    {
                        Close(NetExceptionType.Send, e);
                        return;
                    }
                    _sendBuffer.ReCalculatePosition(sendCount);
                }
            }
        }
コード例 #4
0
        public void StartRecieve()
        {
            try
            {
                _socket.BeginReceive(_input, 0, _inputSize, SocketFlags.None, iar => _actions.Enqueue(() =>
                {//添加到_actions列表保证在主线程对ReceiveStream执行添加操作
                    int length = 0;
                    try
                    {
                        length = _socket.EndReceive(iar);
                    }
                    catch (Exception ex)
                    {
                        ExceptionClose(NetExceptionType.Receive, ex);
                        return;
                    }

                    _receiveStream.Oct.Write(_input, 0, length);

                    try
                    {
                        SerializeControl.Instance.Deserialize(_receiveStream, _protocols);
                    }
                    catch (Exception ex)
                    {
                        ExceptionClose(NetExceptionType.Deserialize, ex);
                        return;
                    }

                    if (_receiveStream.Position != 0)
                    {
                        _receiveStream.ReCalculatePosition(0);
                    }
                    StartRecieve();
                }), null);
            }
            catch (Exception ex)
            {
                ExceptionClose(NetExceptionType.Receive, ex);
            }
        }