コード例 #1
0
ファイル: HS_SocketStream.cs プロジェクト: qipa/HSFramework
 public void Send(HS_MsgWrapper msg)
 {
     lock (((ICollection)_SendQueue).SyncRoot)
     {
         _SendQueue.Enqueue(msg);
     }
 }
コード例 #2
0
ファイル: HS_SocketManager.cs プロジェクト: qipa/HSFramework
        private void Run()
        {
            D.Log("网络管理器 -> 启动线程");
            while (_IsRun)
            {
                try
                {
                    long interval = DateTime.Now.Ticks;

                    lock (((ICollection)_SocketMap).SyncRoot)
                    {
                        foreach (HS_SocketStream s in _SocketMap.Values)
                        {
                            if (s.NativeSocket == null)
                            {
                                continue;
                            }

                            if (s.ConnectFinished)
                            {
                                s.OnRecv();
                                s.OnSend();
                                HS_MsgWrapper msg = s.GetMsg();
                                if (msg != null)
                                {
                                    lock (((ICollection)_AllMessage).SyncRoot)
                                    {
                                        SocketMsg socketMsg;
                                        socketMsg.socket = s;
                                        socketMsg.msg    = msg;
                                        _AllMessage.Enqueue(socketMsg);
                                    }
                                }
                            }
                            else
                            {
                                string log = string.Format("{0} 正在连接中....", s.Address);
                                D.Log(log);
                            }
                        }
                    }


                    interval = System.DateTime.Now.Ticks - interval;
                    int sleep = HS_Define.SOCK_THREAD_TIMER - (int)(interval / 10000);
                    if (sleep > 0)
                    {
                        Thread.Sleep(1);
                    }
                }
                catch (System.Exception ex)
                {
                    if (!(ex is System.Threading.ThreadAbortException))
                    {
                        if (_IsRun)
                        {
                            D.LogException(ex);
                        }
                    }
                }
            }
        }
コード例 #3
0
ファイル: HS_SocketStream.cs プロジェクト: qipa/HSFramework
        private void OnSendAsync()
        {
            int count    = 0;
            int totalLen = 0;

            while (true)
            {
                HS_MsgWrapper msg = null;
                int           len = 0;
                lock (((ICollection)_SendQueue).SyncRoot)
                {
                    if (_SendQueue.Count <= 0)
                    {
                        break;
                    }

                    msg = _SendQueue.Peek();

                    if (totalLen + len > HS_Define.SEND_PACKET_MAX_LEN)
                    {
                        D.LogError("To send data is greater than the specified number of bytes");
                        break;
                    }

                    msg = _SendQueue.Dequeue();
                }
                if (msg == null)
                {
                    break;
                }

                lock (((ICollection)_SendBuffer).SyncRoot)
                {
                    //                     int val = IPAddress.HostToNetworkOrder(len);
                    //                     Array.Copy(BitConverter.GetBytes(val), 0, _SendBuffer,
                    //                                totalLen, 4);
                    //                     val = IPAddress.HostToNetworkOrder(msg.Opcode);
                    //                     Array.Copy(BitConverter.GetBytes(val), 0, _SendBuffer,
                    //                                totalLen + 4, 4);
                    //                     Array.Copy(msg.Value, 0, _SendBuffer, totalLen + 8, len - 8);

                    byte[] describeBuf = HS_Base.ConsolidationByteArray(C2JSecurityCodeBuf, Int2bytes(msg.Value.Length));
                    byte[] dataBuf     = HS_Base.ConsolidationByteArray(describeBuf, msg.Value);
                    len = dataBuf.Length;
                    Array.Copy(dataBuf, 0, _SendBuffer, totalLen, len);
                }

                totalLen += len;
                count++;
            }
            if (totalLen > 0)
            {
                try
                {
                    byte[] buf = new byte[totalLen];
                    Array.Copy(_SendBuffer, buf, totalLen);

                    SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                    socketEventArg.RemoteEndPoint = _Socket.RemoteEndPoint;
                    socketEventArg.SetBuffer(buf, 0, totalLen);
                    socketEventArg.UserToken    = null;
                    socketEventArg.AcceptSocket = null;
                    socketEventArg.Completed   += OnSendCompleted;

                    if (!_Socket.SendAsync(socketEventArg))
                    {
                        OnSendCompleted(this, socketEventArg);
                    }


                    ++_SocketPendingSend;
                    D.Log("发送次数 + ");
                }
                catch (SocketException _e)
                {
                    this._Socket.Close();
                    D.LogError("SocketError:" + _e.Message);
                    //Logger.LogError("SocketError:" + _e.Message);
                }
                catch (ObjectDisposedException _e)
                {
                    this.OnDisconnect(this);
                    this._Socket.Close();
                    D.LogError("SocketError:" + _e.Message);
                }
            }
            //_Socket.Send(_SendBuffer, 0, totalLen, SocketFlags.None);
        }
コード例 #4
0
ファイル: HS_SocketStream.cs プロジェクト: qipa/HSFramework
        private void OnRecvAsync()
        {
            lock (((ICollection)_RecvdQueue).SyncRoot)
            {
                while (_RecvdQueue.Count > 0)
                {
                    TempBuffer tmpbuf = _RecvdQueue.Peek();
                    if (tmpbuf == null)
                    {
                        break;
                    }
                    lock (((ICollection)_RecvBuffer).SyncRoot)
                    {
                        if (tmpbuf.Size > (_RecvBuffer.Length - _RecvSize))
                        {
                            break;
                        }
                        tmpbuf = _RecvdQueue.Dequeue();
                        Array.Copy(tmpbuf.Buffer, 0, _RecvBuffer, _RecvSize, tmpbuf.Size);
                        _RecvSize += tmpbuf.Size;
                    }
                }
            }


            while (true)
            {
                if (_RecvSize < 8)
                {
                    break;
                }
                //D.Log("接受数据大于 8 -> " + _RecvSize);

                lock (((ICollection)_RecvBuffer).SyncRoot)
                {
                    Array.Clear(_SecurityCodeBuf, 0, 4);
                    Array.Clear(_PackerSizeBuf, 0, 4);

                    Array.Copy(_RecvBuffer, 0, _SecurityCodeBuf, 0, 4);
                    int securityCode = Bytes2Int(_SecurityCodeBuf);

                    if (SECURITYCODE != securityCode)
                    {
                        this.Close();
                        D.LogError("Illegal links ..............." + SECURITYCODE + ":" + securityCode);
                        return;
                    }

                    Array.Copy(_RecvBuffer, 4, _PackerSizeBuf, 0, 4);
                    int packerSize = Bytes2Int(_PackerSizeBuf);

                    if (_RecvSize - 8 < packerSize)
                    {
                        D.Log("The data of the subcontract");
                        break;
                    }
                    byte[] value = new byte[packerSize];
                    Array.Copy(_RecvBuffer, 8, value, 0, packerSize);

                    HS_MsgWrapper msg = this.IHandler.OnHandleRecv(value, packerSize);

                    if (null == msg)
                    {
                        break;
                    }

                    lock (((ICollection)_RecvQueue).SyncRoot)
                    {
                        _RecvQueue.Enqueue(msg);
                    }

                    lock (((ICollection)_RecvBuffer).SyncRoot)
                    {
                        //_RecvSize -= len;
                        _RecvSize -= packerSize + 8;
                        if (_RecvSize > 0)
                        {
                            Array.Copy(_RecvBuffer, packerSize + 8, _RecvBuffer, 0, _RecvSize);
                        }
                    }

                    if (_ConnectType == HS_ConnectType.HTTP)
                    {
                        this.Close();
                    }
                }
            }
        }