Exemple #1
0
        public bool ConnectToServer(CMessagePool msgPool)
        {
            this.msgPool     = msgPool;
            this.recvHandler = new AsyncCallback(this.handlerOfRecv);
            this.sendHandler = new AsyncCallback(this.handlerOfSend);
            try
            {
                IPAddress  ipAddr     = System.Net.IPAddress.Parse(this.address);
                IPEndPoint ipEndPoint = new System.Net.IPEndPoint(ipAddr, this.port);
                this.socket.Connect(ipEndPoint);
            }
            catch (SocketException SCE)
            {
                this.ErrorHandler(ERROR_CODE.SOCKET_CONNECT, SCE.ToString());
                return(false);
            }

            CAsyncTask at = new CAsyncTask(4096);

            at.workingSocket = this.socket;

            this.socket.BeginReceive(at.buf, 0, at.buf.Length, SocketFlags.None, recvHandler, at);

            return(true);
        }
Exemple #2
0
        private void handlerOfRecv(IAsyncResult ar)
        {
            CAsyncTask at = (CAsyncTask)ar.AsyncState;
            Int32      recvBytes;

            try
            {
                recvBytes = at.workingSocket.EndReceive(ar);
            }
            catch
            {
                return;
            }

            if (recvBytes > 0)
            {
                //자료처리
                //패킷들을 담아두고 자세한 처리는 위에서
                msgPool.PushMessge(new CPacket(at.buf));
            }

            try
            {
                // 다시 수신대기
                // Begin을 사용해서 비동기 작업을 대기 시켰으면 End를 통해서 작업이 끝났다고 알려줘야함
                at.workingSocket.BeginReceive(at.buf, 0, at.buf.Length, SocketFlags.None, this.recvHandler, this);
            }
            catch (Exception ex)
            {
                this.ErrorHandler(ERROR_CODE.SOCKET_RECV, ex.ToString());
                return;
            }

            return;
        }
Exemple #3
0
        private void handlerOfSend(IAsyncResult ar)
        {
            CAsyncTask at = (CAsyncTask)ar.AsyncState;
            Int32      sentBytes;

            try
            {
                sentBytes = at.workingSocket.EndSend(ar);
            }
            catch (Exception ex)
            {
                this.ErrorHandler(ERROR_CODE.SOCKET_SEND, ex.ToString());
                return;
            }

            return;
        }
Exemple #4
0
        public bool RequestToServer(byte[] msg)
        {
            CAsyncTask at = new CAsyncTask(1);

            at.buf           = msg;
            at.workingSocket = this.socket;

            try
            {
                this.socket.BeginSend(at.buf, 0, at.buf.Length, SocketFlags.None, sendHandler, at);
            }
            catch (Exception ex)
            {
                this.ErrorHandler(ERROR_CODE.SOCKET_SEND, ex.ToString());
                return(false);
            }

            return(true);
        }