Ejemplo n.º 1
0
        void OnAcceptAsync(object _obj, SocketAsyncEventArgs _acceptArgs)
        {
            identity++;
            if (Protocol.DEBUG)
            {
                Console.WriteLine("[{0}]OnAcceptAsync (callback) New Client Connect"
                                  + "\n   >> socket:{1} BytesTransferred:{2} LastOperation:{3} SocketError:{4}",
                                  identity, acceptSocket.Connected, _acceptArgs.BytesTransferred, _acceptArgs.LastOperation, _acceptArgs.SocketError);
            }

            //---------------------------------------
            // acceptSocket -> new Client Socket -> New Client
            // acceptSocket -> Accept Register.
            //---------------------------------------
            Socket _acceptSocket = acceptSocket;
            Socket _clientSocket = _acceptArgs.AcceptSocket;

            _acceptArgs.AcceptSocket = null;
            bool _bAcceptAsync = _acceptSocket.AcceptAsync(_acceptArgs);

            if (_bAcceptAsync)
            {
                if (Protocol.DEBUG)
                {
                    Console.WriteLine("신규유저 받기 접속대기중...");
                }
            }
            else
            {
                Console.WriteLine(" [{0}]OnAcceptAsync #### >> acceptSocket.AcceptAsync Die or Other error", identity);
                OnAcceptAsync(_acceptSocket, _acceptArgs);
            }

            //---------------------------------------
            // Data			<- UserFree pool
            // UserConnect	<- Data
            // Register		<- clientSocket.ReceiveAsync(receiveArgs)
            //---------------------------------------
            CUserToken _token = list_Pool.Dequeue();

            _token.socket     = _clientSocket;
            _token.identityID = identity;
            list_Users.Add(_token);
            int _identityID = identity;

            bool _bNewClientReceiveAsync = _clientSocket.ReceiveAsync(_token.receiveArgs);

            if (_bNewClientReceiveAsync)
            {
                //등록 성공...
            }
            else
            {
                //등록하자마자 바로 데이타 받음...
                //
                // 상황1
                // 서버가 과부하 상태에서 신규 유저 접속하자 마자 바로 종료하는 경우...
                // 100% 발생함...데이타 대량으로 오고 가는중에 발생....
                // > 접속 종료에 대한 메세지를 받은 것임.. ㅎㅎㅎ
                // 접속 -> 대기중... 음... 바로 -> 바로 종료.... 발생....(종료 메세지)
                Console.WriteLine(" [{0}]OnAcceptAsync @@@@ >> 신규유저.ReceiveAsync 바로받음 {1}/{2}/{3}/{4}", _identityID, _token.socket.Connected, _token.receiveArgs.BytesTransferred, _token.receiveArgs.LastOperation, _token.receiveArgs.SocketError);
                OnReceiveAsync(_clientSocket, _token.receiveArgs);
                Console.WriteLine("[{0}]connect free:{1} use:{2}", _identityID, list_Pool.Count, list_Users.Count);
            }
        }
Ejemplo n.º 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)
            {
                //byte read
                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)
                {
                    //fail -> client socket is error
                    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
            {
                //error
                Console.WriteLine("[{0}]OnReceiveAsync [정상종료] >>> :{1} :{2} :{3} :{4}", _identityID + "/" + _debugWorkNum, _token.socket.Connected, _receiveArgs.BytesTransferred, _receiveArgs.LastOperation, _receiveArgs.SocketError);
                Disconnect("[정상종료1]", _token);
            }
        }