예제 #1
0
    private void ThreadFunction_Recv()
    {
        try
        {
            while (_thread_running && isConnected)
            {
                var bufraw = _inner_socket.Receive(ref _server_endpoint);
                if (bufraw.Length > 4)
                {
                    byte[] buf = Base.MemoryPool.Alloc(bufraw.Length - 4);    //new byte[bufraw.Length - 4];
                    Array.Copy(bufraw, 4, buf, 0, bufraw.Length - 4);
                    XOREncrypt.Decrypt(buf, bufraw.Length - 4);
                    MsgStream msg = MsgStream.Create(buf, bufraw.Length - 4);

                    if (msg.IsCustomCmd)
                    {
                        if (msg.CustomType == CustomMsgType.ping)
                        {
                            //服务器返回了 证明连接成功了
                            _IsConnected = true;
                            TimeSpan ts = DateTime.Now - mLastPingTime;//mLastPingTimes[idx];
                            mPing         = (int)(ts.TotalMilliseconds);
                            mLastPingTime = DateTime.Now;
                            //    this.LastSendPingTimestamp = Utils.GetTimestampSecondsInt();
                            msg.Dispose();
                            continue;
                        }
                    }
                    //服务器返回了 证明连接成功了
                    _IsConnected = true;
                    _recvQueue.Enqueue(msg);
                }
                else
                {
                    //<=4
                }
            }
        }
        catch (SocketException e)
        {
            Debug.Log("UdpSocket:" + e.Message + "   " + e.ErrorCode + "  " + e.NativeErrorCode + "  " + e.SocketErrorCode);
        }
        catch (Exception e)
        {
            Debug.Log("UdpSocket:" + e.Message);
        }
        this.Disconnected();
        while (_recvQueue.Empty() == false)
        {
            var x = _recvQueue.Dequeue();
            if (x != null)
            {
                x.Dispose();
            }
        }
        Debug.Log("[NetWork]:UdpSocket Recv Thread Close");
    }
예제 #2
0
    public void SendPingMsg(int ping)
    {
        var msg = MsgStream.Create(MsgType.CUSTOM_CMD, CustomMsgType.ping);

        msg.Write(mPing);
        mLastPingTime = DateTime.Now;
        //    mLastPingTimes.Add(DateTime.Now);
        this.AddSendMsg(msg);
        // if (pingMsgNum >= 2)
        {
            //已经发送过了 但是服务器未响应 认为断开连接了
        }
        ++pingMsgNum;
        //  this.LastSendPingTimestamp = Utils.GetTimestampSecondsInt();
    }
예제 #3
0
    private void ThreadFunction_Recv()
    {
        try
        {
            _inner_socket.NoDelay = true;
            while (_thread_running && isConnected)
            {
                //      Thread.Sleep(1);//能减少socket error 10054 出现的 概率
                byte[] buffer     = _buffer_head;
                int    c          = 0;
                bool   will_break = false;
                while (c < 4)
                {
                    int l = _inner_socket.Receive(buffer, c, 4 - c, SocketFlags.None);  // _inner_tcp_stream.Read(buffer, c, 4 - c);
                    if (l > 0)
                    {
                        c += l;
                    }
                    else if (l == 0)
                    {
                        this.Disconnected();
                        Debug.Log("socket read faild 4 server disconnected");
                        will_break = true;
                        break;
                    }
                    else
                    {
                        this.Disconnected();
                        Debug.Log("socket read faild 1");
                        will_break = true;
                        break;
                    }
                }
                if (will_break)
                {
                    break;
                }
                if (!isConnected || !_thread_running)
                {
                    break;
                }
                int len = (int)System.BitConverter.ToInt32(buffer, 0);
                if (len < MAX_VALID_BUFFER_LEN)
                {
                    //byte[] buf = new byte[len];
                    byte[] buf = Base.MemoryPool.Alloc(len);
                    try
                    {
                        int read_len = 0;
                        while (read_len < len)
                        {
                            int l = _inner_socket.Receive(buf, read_len, len - read_len, SocketFlags.None);// _inner_tcp_stream.Read(buf, read_len, len - read_len);
                            if (l > 0)
                            {
                                read_len += l;
                            }
                            else if (l == 0)
                            {
                                /*
                                 * //https://msdn.microsoft.com/zh-cn/library/8s4y8aff(v=vs.90).aspx
                                 * 如果当前使用的是面向连接的 Socket,那么 Receive 方法将会读取所有可用的数据,直到达到缓冲区的大小为止。如果远程主机使用 Shutdown 方法关闭了 Socket 连接,并且所有可用数据均已收到,则 Receive 方法将立即完成并返回零字节。*/
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (read_len != len)
                        {
                            this.Disconnected();
                            Debug.Log("socket read faild 2");
                            break;
                        }
                        XOREncrypt.Decrypt(buf, len);
                        byte by = buf[0];
                        if (by == 1) // 占位符
                        {
                            MsgStream msg = MsgStream.Create(buf, len);

                            if (msg.IsCustomCmd)
                            {
                                if (msg.CustomType == CustomMsgType.ping)
                                {
                                    TimeSpan ts = DateTime.Now - mLastPingTime;//mLastPingTimes[idx];
                                    mPing         = (int)(ts.TotalMilliseconds);
                                    mLastPingTime = DateTime.Now;
                                    //    this.LastSendPingTimestamp = Utils.GetTimestampSecondsInt();
                                    msg.Dispose();
                                    continue;
                                }
                            }
                            _recvQueue.Enqueue(msg);
                        }
                        else
                        {
                            //maybe dicconnected by server or net error
                            Debug.LogError("****************** unexp bytes headdrer=" + (int)by + " len=" + len + " read_len=" + read_len);
                            this.Disconnected();
                        }
                    }
                    catch (Exception e)
                    {
                        //不回收  让他GC掉好了
                    }
                }
                else
                {
                    this.Disconnected();
                    Debug.LogError("TcpSocket error send buffer overfollow 0x2  ");
                }
            }
        }
        catch (SocketException e)
        {
            Debug.Log("TcpSocket:" + e.Message + "   " + e.ErrorCode + "  " + e.NativeErrorCode + "  " + e.SocketErrorCode);
        }
        catch (Exception e)
        {
            Debug.Log("TcpSocket:" + e.Message);
        }
        this.Disconnected();
        while (_recvQueue.Empty() == false)
        {
            var x = _recvQueue.Dequeue();
            if (x != null)
            {
                x.Dispose();
            }
        }
        Debug.Log("[NetWork]:Socket Recv Thread Close");
    }