예제 #1
0
        void OnSendAsync(object _obj, SocketAsyncEventArgs _sendArgs)
        {
            CUserToken _token        = _sendArgs.UserToken as CUserToken;
            int        _debugWorkNum = _token.GetWorkNum();
            int        _identityID   = _token.identityID;

            if (Protocol.DEBUG)
            {
                Console.WriteLine("[{0}]OnSendAsync (callback) \n   >> socket:{1} BytesTransferred:{2} LastOperation:{3} SocketError:{4}", _identityID + "/" + _debugWorkNum, _token.socket.Connected, _sendArgs.BytesTransferred, _sendArgs.LastOperation, _sendArgs.SocketError);
            }

            if (_sendArgs.LastOperation == SocketAsyncOperation.Send &&
                _sendArgs.SocketError == SocketError.Success &&
                _sendArgs.BytesTransferred > 0)
            {
                //큐에서 빼고 > 더있는지 검사후에 다음것 등록...
                _token.SendMessage_CheckAndReSend(_debugWorkNum);
            }
            else
            {
                Console.WriteLine("[{0}]OnSendAsync [정상종료] >>> :{1} :{2} :{3} :{4}", _identityID + "/" + _debugWorkNum, _token.socket.Connected, _sendArgs.BytesTransferred, _sendArgs.LastOperation, _sendArgs.SocketError);
                Disconnect("[정상종료1]", _token);
            }
        }
예제 #2
0
        void OnReceiveAsync(object _obj, SocketAsyncEventArgs _receiveArgs)
        {
            CUserToken _token        = _receiveArgs.UserToken as CUserToken;
            int        _debugWorkNum = _token.GetWorkNum();
            int        _identityID   = _token.identityID;

            if (Protocol.DEBUG)
            {
                Console.WriteLine("[{0}]OnReceiveAsync (callback) \n   >> socket:{1} BytesTransferred:{2} LastOperation:{3} SocketError:{4}", _identityID + "/" + _debugWorkNum, _token.socket.Connected, _receiveArgs.BytesTransferred, _receiveArgs.LastOperation, _receiveArgs.SocketError);
            }
            if (Protocol.DEBUG_PACKET_LOOP_SHOW && _debugWorkNum % Protocol.DEBUG_PACKET_LOOP_COUNT <= 1)
            {
                Console.WriteLine("[{0}]OnReceiveAsync (callback) \n   >> socket:{1} BytesTransferred:{2} LastOperation:{3} SocketError:{4}", _identityID + "/" + _debugWorkNum, _token.socket.Connected, _receiveArgs.BytesTransferred, _receiveArgs.LastOperation, _receiveArgs.SocketError);
            }

            if (_receiveArgs.LastOperation == SocketAsyncOperation.Receive &&
                _receiveArgs.SocketError == SocketError.Success &&
                _receiveArgs.BytesTransferred > 0)
            {
                Socket _clientSocket           = _token.socket;
                SocketAsyncEventArgs _sendArgs = _token.sendArgs;

                //---------------------------------------
                // Client -> Socket -> (callback) -> OnReceiveAsync
                // Data Parsing
                //---------------------------------------
                int _transferred = _receiveArgs.BytesTransferred;
                Array.Copy(_receiveArgs.Buffer, _receiveArgs.Offset, _token.receiveBuffer2, 0, _transferred);
                string _text = Encoding.ASCII.GetString(_token.receiveBuffer2, 0, _transferred);
                if (Protocol.DEBUG_PACKET)
                {
                    Console.WriteLine("[{0}] size:{1} >> {2}", _identityID + "/" + _debugWorkNum, _transferred, _text);
                }

                //-------------------------------
                // SendAsync....
                // Message Queue에 넣어두기... (보내는 byte[]가 반드시 다른 버퍼)
                //-------------------------------
                _token.SendMessage(Encoding.ASCII.GetBytes(_text), _debugWorkNum);

                //-------------------------------
                //ReceiveAsync...
                //-------------------------------
                bool _bReceiveAsync = _clientSocket.ReceiveAsync(_receiveArgs);
                if (_bReceiveAsync == false)
                {
                    Console.WriteLine(" [{0}]OnReceiveAsync .ReceiveAsync 재등록후(소켓꺼짐) {1}/{2}/{3}/{4}", _identityID + "/" + _debugWorkNum, _token.socket.Connected, _receiveArgs.BytesTransferred, _receiveArgs.LastOperation, _receiveArgs.SocketError);
                    if (_token.socket.Connected == false)
                    {
                        Console.WriteLine(" >> Disconnect ");
                        Disconnect("[정상종료2]", _token);
                    }
                    else
                    {
                        Console.WriteLine(" [{0}] #### OnReceiveAsync > _socket.ReceiveAsync 메세지 받기(2) 등록하자마사 바로 받음.{1}", _identityID, _clientSocket.Connected);
                        OnReceiveAsync(_clientSocket, _receiveArgs);
                    }
                }
            }
            else
            {
                Console.WriteLine("[{0}]OnReceiveAsync [정상종료] >>> :{1} :{2} :{3} :{4}", _identityID + "/" + _debugWorkNum, _token.socket.Connected, _receiveArgs.BytesTransferred, _receiveArgs.LastOperation, _receiveArgs.SocketError);
                Disconnect("[정상종료1]", _token);
            }
        }