/** 添加缓冲 */
        public void append(byte[] bs, int off, int length)
        {
            int dLen = 0;

            int i = 0;

            while (i < length)
            {
                try
                {
                    Buffer.BlockCopy(bs, off + i, _buf, _length, dLen = Math.Min(length - i, _buf.Length - _length));
                }
                catch (Exception e)
                {
                    Ctrl.printExceptionForIO(e);
                    Ctrl.print(off, length, i, _buf.Length, _length);
                }

                i       += dLen;
                _length += dLen;

                if (!toRead())
                {
                    return;
                }
            }
        }
Beispiel #2
0
        private void onSocketConnect(IAsyncResult result)
        {
            bool isError = false;

            try
            {
                _socket.EndConnect(result);
            }
            catch (Exception e)
            {
                // Ctrl.printExceptionForIO(e);
                isError = true;
            }

            try
            {
                EndPoint localEndPoint = _socket.LocalEndPoint;

                if (localEndPoint != null)
                {
                    Ctrl.debugLogForIO("本地端口:", localEndPoint.Serialize().ToString(), doIndex);
                }
            }
            catch (Exception e)
            {
                Ctrl.printExceptionForIO(e);
            }

            Ctrl.debugLogForIO("socketContent连接结果,success:", !isError, this.GetHashCode());

            ThreadControl.addMainFunc(() =>
            {
                if (isError)
                {
                    connectFailedOnce();
                }
                else
                {
                    connectSuccess();
                }
            });
        }
Beispiel #3
0
        protected override void doClose()
        {
            try
            {
                _socket.Close();
            }
            catch (Exception e)
            {
                Ctrl.printExceptionForIO(e);
            }

            try
            {
                _kcp.Dispose();
            }
            catch (Exception e)
            {
                Ctrl.printExceptionForIO(e);
            }
        }
Beispiel #4
0
        public override void onKcpLoop()
        {
            if (!checkIsCurrent())
            {
                return;
            }

            if (!_socket.Connected)
            {
                return;
            }

            _parent.runSendQueue();

            if (!isSending())
            {
                try
                {
                    _kcp.Update(DateTime.UtcNow);
                }
                catch (Exception e)
                {
                    Ctrl.printExceptionForIO(e);
                }
            }

            // int ps = _kcp.PeekSize();
            //
            // while(ps>0)
            // {
            //     int ps2=_kcp.Recv(_receiveCach2);
            //
            //     _receiveBuffer.append(_receiveCach2, 0, ps2);
            //     ps=_kcp.PeekSize();
            // }
        }
Beispiel #5
0
        private void run()
        {
            try
            {
                SFuncQueue queue = _queue;

                _running = true;

                long lastTime = Ctrl.getTimer();
                long time;
                int  delay;
                int  tickMax  = _tickDelay;
                int  tickTime = 0;

                while (_running)
                {
                    time     = Ctrl.getTimer();
                    delay    = (int)(time - lastTime);
                    lastTime = time;

                    //防止系统时间改小
                    if (delay < 0)
                    {
                        delay = 0;
                    }

                    if (delay > 0)
                    {
                        if ((tickTime += delay) >= tickMax)
                        {
                            try
                            {
                                tick(tickTime);
                            }
                            catch (Exception e)
                            {
                                Ctrl.errorLog("线程tick出错", e);
                            }

                            tickTime = 0;
                        }
                    }

                    try
                    {
                        runEx();
                    }
                    catch (Exception e)
                    {
                        Ctrl.errorLog("线程runEx出错", e);
                    }

                    //再事务
                    queue.runOnce();

                    //最后睡
                    threadSleep();
                }
            }
            catch (ThreadAbortException)
            {
                //线程结束
            }
            catch (Exception e)
            {
                Ctrl.printExceptionForIO(e);
            }
        }
Beispiel #6
0
        private void toGetDNS()
        {
            _state       = Connecting;
            _connectTime = 0;

            int index = ++_doIndex;

            _currentInfo = _ipInfoDic.get(_host);

            if (_currentInfo != null)
            {
                preConnectAfterAddress();
            }
            else
            {
                IPAddress address;

                if (!IPAddress.TryParse(_host, out address))
                {
                    Ctrl.debugLog("dns上");

                    Dns.BeginGetHostAddresses(_host, v =>
                    {
                        IPAddress[] addresses = null;

                        try
                        {
                            addresses = Dns.EndGetHostAddresses(v);
                        }
                        catch (Exception e)
                        {
                            Ctrl.printExceptionForIO(e);
                        }

                        ThreadControl.addMainFunc(() =>
                        {
                            if (index == _doIndex)
                            {
                                if (addresses != null)
                                {
                                    foreach (IPAddress ipAddress in addresses)
                                    {
                                        Ctrl.print("dns看收到的上地址", ipAddress);
                                    }

                                    _currentInfo = registIPInfo(_host, addresses);
                                    preConnectAfterAddress();
                                }
                                else
                                {
                                    Ctrl.printForIO("解析dns失败");
                                }
                            }
                            else
                            {
                                Ctrl.print("dns获取完,已经到下个index");
                            }
                        });
                    }, null);
                }
                else
                {
                    Ctrl.debugLog("dns下");
                    _currentInfo = registIPInfo(_host, new[] { address });
                    preConnectAfterAddress();
                }
            }
        }