Пример #1
0
        private void _onConnectCallback(IAsyncResult pAsyncResult)
        {
            lock (__sysQueueLock)
            {
                try
                {
                    TcpClient pTCPClient = pAsyncResult.AsyncState as TcpClient;

                    Debugger.Assert(System.Object.ReferenceEquals(pTCPClient, _tcpClient));
                    pTCPClient.EndConnect(pAsyncResult);

                    _tcpClient.NoDelay = true;

                    _bIsNetDown = false;
                    __error     = null;

                    _receiveThread = new Thread(new ParameterizedThreadStart(_receiveThreadFunc));
                    _receiveThread.IsBackground = true;
                    _receiveThread.Start(_tcpClient.GetStream());

                    _sendThread = new Thread(new ParameterizedThreadStart(_sendThreadFunc));
                    _sendThread.IsBackground = true;
                    _sendThread.Start(_tcpClient.GetStream());

                    _SysMsg sysMsg = new _SysMsg(_SysMsgType.Connected);
                    __sysMsgQueue.Enqueue(sysMsg);
                }
                catch (Exception e)
                {
                    _SysMsg errMsg = new _SysMsg(_SysMsgType.ConnectFailed, e);
                    __sysMsgQueue.Enqueue(errMsg);
                }
            }
        }
Пример #2
0
        protected void _sendThreadFunc(object pThreadArgument)
        {
            NetworkStream pNetStream = pThreadArgument as NetworkStream;

            while (!_bIsNetDown)
            {
                byte[] dataBytes = null;

                lock (_sendLock)
                {
                    if (_sendQueue.Count > 0)
                    {
                        dataBytes = _sendQueue.Dequeue();
                    }
                }

                if (dataBytes == null)
                {
                    Thread.Sleep(8);
                }
                else
                {
                    try
                    {
                        pNetStream.Write(dataBytes, 0, dataBytes.Length);
                    }
                    catch (Exception e)
                    {
                        _SysMsg sysMsg = new _SysMsg(_SysMsgType.Disconnected, e);
                        lock (__sysQueueLock) { __sysMsgQueue.Enqueue(sysMsg); }
                        break;
                    }
                }
            }
        }
Пример #3
0
        ///------------------------------------
        protected void _receiveThreadFunc(object pThreadArgument)
        {
            NetworkStream pNetStream = pThreadArgument as NetworkStream;

            try
            {
                while (!_bIsNetDown)
                {
                    /// Receive head
                    _doReceive(pNetStream, _arrRecvBuffer, 0, 2);

                    int len = (_arrRecvBuffer[0] << 8) | _arrRecvBuffer[1];
                    if ((len <= 0) || (len > MAX_DATA_SIZE))
                    {
                        __error = new Exception(string.Format("Incorrect message length ({0}), length should not exceed 64K", len));
                        return;
                    }

                    /// Receive body - 2
                    _doReceive(pNetStream, _arrRecvBuffer, LEN_SIZE, len - LEN_SIZE);

                    byte[] destBuffer = new byte[len];
                    Array.Copy(_arrRecvBuffer, 0, destBuffer, 0, len);

                    if (OnReceivingFilter != null)
                    {
                        if (OnReceivingFilter(destBuffer, len))
                        {
                            lock (_receiveLock) _receiveQueue.Enqueue(destBuffer);
                        }
                        else
                        {
                            if (OnReceivingFiltered != null)
                            {
                                OnReceivingFiltered(destBuffer, len);
                            }
                        }
                    }
                    else
                    {
                        lock (_receiveLock) _receiveQueue.Enqueue(destBuffer);
                    }
                }
            }
            catch (Exception e)
            {
                _SysMsg sysMsg = new _SysMsg(_SysMsgType.Disconnected, e);
                lock (__sysQueueLock) { __sysMsgQueue.Enqueue(sysMsg); }
            }
        }
Пример #4
0
        public void Update()
        {
            lock (_receiveLock)
            {
                while (_receiveQueue.Count > 0)
                {
                    byte[] item = _receiveQueue.Dequeue();
                    OnData(item);
                }
            }

            lock (__sysQueueLock)
            {
                while (__sysMsgQueue.Count != 0)
                {
                    _SysMsg sysMsg = __sysMsgQueue.Dequeue();

                    switch (sysMsg.type)
                    {
                    case _SysMsgType.Connected:
                        if (__onConnect != null)
                        {
                            __onConnect(true, null);
                        }
                        __onConnect = null;
                        break;

                    case _SysMsgType.ConnectFailed:
                        if (__onConnect != null)
                        {
                            __onConnect(false, sysMsg.exception);
                        }
                        __onConnect = null;
                        break;

                    case _SysMsgType.Disconnected:
                        Disconnect();
                        if (OnDisconnected != null)
                        {
                            OnDisconnected(sysMsg.exception);
                        }
                        break;
                    }
                }
            }
        }