Exemplo n.º 1
0
        /// <summary>
        /// Tries the connect.
        /// </summary>
        /// <exception cref='Exception'>
        /// Represents errors that occur during application execution.
        /// </exception>
        public void TryConnect()
        {
            try {
                IPEndPoint _ep = null;

                //
                if (m_bSecurityPolicy)
                {
                }
                else
                {
                    _ep = this.GetServerAddress();
                    if (null == _ep)
                    {
                        ConnectState = EClientConnectState.CONNECT_STATE_FAILED;
                        ClientLog.LogError("Connect timeout : no valid server ip or port");
                        return;
                    }
                }

                ConnectState = EClientConnectState.CONNECT_STATE_TRY_CONNECT;
                _socketClient.BeginConnect(_ep, new AsyncCallback(ConnectCallback), _socketClient);
                ClientLog.Log("Connect server : " + _ep.Address.ToString() + ":" + _ep.Port.ToString());
            } catch (Exception ex)
            {
                ClientConnectState = EClientConnectState.CONNECT_STATE_FAILED;
                ClientLog.LogError(ex.ToString());
            }
        }
Exemplo n.º 2
0
        /**
         *
         */
        private void ReceiveMsgCallback(IAsyncResult receiveRes)
        {
            if (!IsConnected())
            {
                ClientLog.LogError("ReceiveMsgCallback : the socket is not connected!!!");
                return;
            }

            MsgReceiveHelper receiveHelper = null;

            try {
                int _recSize = this._socketClient.EndReceive(receiveRes);
                if (_recSize > 0)
                {
                    receiveHelper = (MsgReceiveHelper)receiveRes.AsyncState;
                    this.DecodeMsg(receiveHelper.buffer, _recSize);
                }
                // < 0, the remote socket is close...
                else
                {
                    SetShowNetworkError(1, 0, SocketError.SocketError);
                    ClientLog.LogError("Socket EndReceive failed, the size is 0. The remote socket is closed. Disconnect...");
                    this.Close();
                    return;
                }
            }
            //
            catch (SocketException se) {
                SetShowNetworkError(2, se.ErrorCode, se.SocketErrorCode);
                ClientLog.LogError("receive msg failed : " + se.ToString());
                ClientLog.LogError("Socket EndReceive Exception, ErrorCode = " + se.ErrorCode.ToString() + ", SocketErrorCode = " + se.SocketErrorCode.ToString());

                // Disconnect, WSAEWOULDBLOCK
                if (!se.SocketErrorCode.Equals(SocketError.WouldBlock))
                {
                    ClientLog.LogError("Socket fatal exception, disconnect...");
                    this.Close();
                    return;
                }
            } catch (Exception e) {
                SetShowNetworkError(3, 0, SocketError.SocketError);
                ClientLog.LogError("receive msg failed : " + e.ToString());
            }
            //
            finally {
                if (receiveHelper != null)
                {
                    receiveHelper.socket.BeginReceive(receiveHelper.buffer, 0, receiveHelper.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMsgCallback), receiveHelper);
                }
                else
                {
                    this.StartRecevieMsg();
                }
            }
        }
Exemplo n.º 3
0
 public void SendMessage(byte[] bytes)
 {
     try {
         if (!this.IsConnected())
         {
             ClientLog.LogError("server is not connected!");
         }
         else
         {
             _socketClient.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, SendMsgCallback, _socketClient);
         }
     } catch (Exception ex) {
         ClientLog.LogError(ex.ToString());
     }
 }
Exemplo n.º 4
0
        //
                #if USE_THREAD_RECEIVE_MSG
        void    RecvThreadDoWork()
        {
            byte[] _buffer = new byte[DEFAULT_RECEIVE_SIZE];
            while (mThreadWork)
            {
                try {
                    int _recSize = this._socketClient.Receive(_buffer, DEFAULT_RECEIVE_SIZE, SocketFlags.None);
                    if (_recSize > 0)
                    {
                        this.DecodeMsg(_buffer, _recSize);
                    }
                    // < 0, the remote socket is close...
                    else
                    {
                        SetShowNetworkError(1, 0, SocketError.SocketError);
                        ClientLog.LogError("Socket EndReceive failed, the size is 0. The remote socket is closed. Disconnect...");
                        this.Close();
                        break;
                    }
                } catch (SocketException se) {
                    if (se.SocketErrorCode == SocketError.WouldBlock ||
                        se.SocketErrorCode == SocketError.IOPending ||
                        se.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                    {
                        // socket buffer is probably empty, wait and try again
                        Thread.Sleep(50);
                    }
                    else
                    {
                        SetShowNetworkError(2, se.ErrorCode, se.SocketErrorCode);
                        ClientLog.LogError("receive msg failed : " + se.ToString());
                        ClientLog.LogError("Socket EndReceive Exception, ErrorCode = " + se.ErrorCode.ToString() + ", SocketErrorCode = " + se.SocketErrorCode.ToString());
                        ClientLog.LogError("Socket fatal exception, disconnect...");
                        this.Close();
                        break;
                    }
                }

                Thread.Sleep(1);
            }

            //
            mRecvThread.Join();
        }
Exemplo n.º 5
0
        /**
         *
         */
        private void SendMsgCallback(IAsyncResult sendRes)
        {
//			ClientLog.Log("send Message callback");
            try {
                Socket _socket   = (Socket)sendRes.AsyncState;
                int    nSentByte = _socket.EndSend(sendRes);

                if (nSentByte <= 0)
                {
                    SetShowNetworkError(4, 0, SocketError.SocketError);
                    ClientLog.LogError("send msg failed!");
                }
            } catch (Exception e) {
                ClientLog.LogError("send msg failed : " + e.ToString());
                //
                SocketException se = e as SocketException;
                if (null != se)
                {
                    SetShowNetworkError(5, se.ErrorCode, se.SocketErrorCode);
                    ClientLog.LogError("Socket EndSend Exception, ErrorCode = " + se.ErrorCode.ToString() + ", SocketErrorCode = " + se.SocketErrorCode.ToString());
                }
            }
        }
Exemplo n.º 6
0
        /**
         *
         */
        private void ConnectCallback(IAsyncResult ar)
        {
            try {
                Socket _socket = (Socket)ar.AsyncState;
                if (!_socket.Connected)
                {
                    ConnectState = EClientConnectState.CONNECT_STATE_FAILED;
                    ClientLog.LogError(_socket.LocalEndPoint + " connect failed!, try connect again");
//					this.DoRetryConnect ();
                }
                else
                {
                    _socket.EndConnect(ar);
                    this.StartRecevieMsg();
                    ConnectState = EClientConnectState.CONNECT_STATE_CONNECTED;
                    ClientLog.Log(_socket.LocalEndPoint + " connect successful!");
                }
            } catch (Exception e) {
                ConnectState = EClientConnectState.CONNECT_STATE_FAILED;
                // do something
                Console.WriteLine(e.ToString());
            }
        }