Exemple #1
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 创建连接
        /// </summary>
        /// <param name="id"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="listener"></param>
        public void Connect(int id, string host, int port, INetworkMsgHandler listener)
        {
            SetReadyToConnectSID(id);

            if (ENUM_SOCKET_STATE.eSocket_Connected == m_currentConnectState)
            {
                if (id == m_currentConnectedSID)
                {
                    Debug.Log("NetTCPWork::Connect already connect the server ID = " + id);
                    return;
                }

                CNetTCPSocketConnect connect = new CNetTCPSocketConnect();
                bool success = connect.Connect(id, host, port, listener);
                if (success)
                {
                    SetCurrentServerID(id);
                    m_TCPConnects.Add(id, connect);
                }
            }
            else
            {
                Disconnect(id);

                CNetTCPSocketConnect connect = new CNetTCPSocketConnect();
                bool success = connect.Connect(id, host, port, listener);
                if (success)
                {
                    m_TCPConnects.Add(id, connect);
                }
            }
        }
Exemple #2
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 创建连接
        /// </summary>
        /// <param name="id"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="listener"></param>
        public void Connect(int id, string host, int port, INetworkMsgHandler listener)
        {
            SetReadyToConnectSID(id);

            if (ENUM_SOCKET_STATE.eSocket_Connected == m_currentConnectState)
            {
                if (id == m_currentConnectedSID)
                {
                    Debug.Log("NetTCPWork::Connect already connect the server ID = " + id);
                    return;
                }

                CNetTCPSocketConnect connect = new CNetTCPSocketConnect();
                bool success = connect.Connect(id,host, port, listener);
                if (success)
                {
                    SetCurrentServerID(id);
                    m_TCPConnects.Add(id, connect);
                }
            }
            else
            {
                Disconnect(id);

                CNetTCPSocketConnect connect = new CNetTCPSocketConnect();
                bool success = connect.Connect(id,host, port, listener);
                if (success)
                {
                    m_TCPConnects.Add(id, connect);
                }
            }
        }
Exemple #3
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 变量初始化
        /// </summary>
        private void __Clear()
        {
            m_Socket = null;
            m_IPAddrMsg.Clear();

            m_ReceiveHead = null;

            __DestoryNetWorkThread();

            if (null != m_SendPackList)
            {
                m_SendPackList.Clear();
            }
            m_SendObject = null;

            if (null != m_ReceivePackList)
            {
                m_ReceivePackList.Clear();
            }
            m_ReceiveObject = null;

            if (null != m_ManualSendEvent)
            {
                m_ManualSendEvent.Close();
                m_ManualSendEvent = null;
            }

            if (null != m_AutoConnectEvent)
            {
                m_AutoConnectEvent.Close();
                m_AutoConnectEvent = null;
            }

            m_NetStateListener = null;

            m_unSendTotalBytes = 0;
            m_unRecvTotalBytes = 0;
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// 连接开始
        /// </summary>
        /// <param name="ip">服务器IP地址</param>
        /// <param name="portnumber">端口信息</param>
        /// <returns></returns>
        public bool Connect(int nSocketID, string ip, int portnumber, INetworkMsgHandler listener)
        {
            if (m_IPAddrMsg.m_IsConnect)
            {
                DisConnection();
            }

            if (0 == ip.Length || ip.Equals(SNetCommon.NULL) || 0 >= portnumber)
            {
                Debug.Log("NetTCPSocketConnect::Connect arge is error");
                return false;
            }
            m_nSocketID = nSocketID;
            m_IPAddrMsg.m_IP = ip;
            m_IPAddrMsg.m_Port = portnumber;

            m_NetStateListener = listener;

            // 获取 DNS 主机地址
            //  DNS 服务器中查询与某个主机名关联的 IP 地址。 如果 hostNameOrAddress 是 IP 地址,则不查询 DNS 服务器直接返回此地址
            IPAddress[] addresses = null;
            try
            {
                addresses = Dns.GetHostAddresses(ip);
            }
            catch (System.Exception excep)
            {
                Debug.Log("NetTCPSocketConnect::Connect DNS Error:" + excep.ToString());
                return false;
            }

            if (addresses == null || addresses.Length == 0)
            {
                Debug.Log("NetTCPSocketConnect::Connect addresses:" + addresses.ToString());
                return false;

            }
            // 将网络端点表示为 IP 地址和端口号
            IPEndPoint remoteIP = new IPEndPoint(addresses[0], portnumber);

            if (null != m_NetStateListener)
            {
                m_NetStateListener.del_OnConnectStart(nSocketID);
            }

            ///建立一个TCP连接
            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                Debug.Log("NetTCPSocketConnect::Connect begin connect to server:" + ip + " port:" + portnumber.ToString());
                m_IPAddrMsg.m_IsConnect = false;

                /*
                 * SocketAsyncEventArgs是微软提供的高性能异步Socket实现类,主要为高性能网络服务器应用程序而设计,主要是为了避免在在异步套接字 I/O
                 * 量非常大时发生重复的对象分配和同步。使用此类执行异步套接字操作的模式包含以下步骤:
                 *   1.分配一个新的 SocketAsyncEventArgs 上下文对象,或者从应用程序池中获取一个空闲的此类对象。
                 *   2.将该上下文对象的属性设置为要执行的操作(例如,完成回调方法、数据缓冲区、缓冲区偏移量以及要传输的最大数据量)。
                 *   3.调用适当的套接字方法 (xxxAsync) 以启动异步操作。
                 *   4.如果异步套接字方法 (xxxAsync) 返回 true,则在回调中查询上下文属性来获取完成状态。
                 *   5.如果异步套接字方法 (xxxAsync) 返回 false,则说明操作是同步完成的。 可以查询上下文属性来获取操作结果。
                 *   6.将该上下文重用于另一个操作,将它放回到应用程序池中,或者将它丢弃。
                 */
                SocketAsyncEventArgs asyncEvent = new SocketAsyncEventArgs();
                asyncEvent.RemoteEndPoint = remoteIP;
                asyncEvent.Completed += new EventHandler<SocketAsyncEventArgs>(__OnConnectComplete);
                m_Socket.ConnectAsync(asyncEvent);
                // 等待信号
                m_AutoConnectEvent.WaitOne();

                SocketError errorCode = asyncEvent.SocketError;
                if (errorCode != SocketError.Success)
                {
                    //throw new SocketException((Int32)errorCode);
                    Debug.Log("NetTCPSocketConnect::Connect check the net SocketError = " + errorCode);
                }
            }
            catch (System.Exception e)
            {
                Debug.Log("NetTCPSocketConnect::Connect " + e.ToString());
                return false;
            }

            return true;
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// 变量初始化
        /// </summary>
        private void __Clear()
        {
            m_Socket = null;
            m_IPAddrMsg.Clear();

            m_ReceiveHead = null;

            __DestoryNetWorkThread();

            if (null != m_SendPackList)
            {
                m_SendPackList.Clear();
            }
            m_SendObject = null;

            if (null != m_ReceivePackList)
            {
                m_ReceivePackList.Clear();
            }
            m_ReceiveObject = null;

            if (null != m_ManualSendEvent)
            {
                m_ManualSendEvent.Close();
                m_ManualSendEvent = null;
            }

            if (null != m_AutoConnectEvent)
            {
                m_AutoConnectEvent.Close();
                m_AutoConnectEvent = null;
            }

            m_NetStateListener = null;

            m_unSendTotalBytes = 0;
            m_unRecvTotalBytes = 0;
        }
Exemple #6
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 连接开始
        /// </summary>
        /// <param name="ip">服务器IP地址</param>
        /// <param name="portnumber">端口信息</param>
        /// <returns></returns>
        public bool Connect(int nSocketID, string ip, int portnumber, INetworkMsgHandler listener)
        {
            if (m_IPAddrMsg.m_IsConnect)
            {
                DisConnection();
            }

            if (0 == ip.Length || ip.Equals(SNetCommon.NULL) || 0 >= portnumber)
            {
                Debug.Log("NetTCPSocketConnect::Connect arge is error");
                return(false);
            }
            m_nSocketID        = nSocketID;
            m_IPAddrMsg.m_IP   = ip;
            m_IPAddrMsg.m_Port = portnumber;

            m_NetStateListener = listener;

            // 获取 DNS 主机地址
            //  DNS 服务器中查询与某个主机名关联的 IP 地址。 如果 hostNameOrAddress 是 IP 地址,则不查询 DNS 服务器直接返回此地址
            IPAddress[] addresses = null;
            try
            {
                addresses = Dns.GetHostAddresses(ip);
            }
            catch (System.Exception excep)
            {
                Debug.Log("NetTCPSocketConnect::Connect DNS Error:" + excep.ToString());
                return(false);
            }

            if (addresses == null || addresses.Length == 0)
            {
                Debug.Log("NetTCPSocketConnect::Connect addresses:" + addresses.ToString());
                return(false);
            }
            // 将网络端点表示为 IP 地址和端口号
            IPEndPoint remoteIP = new IPEndPoint(addresses[0], portnumber);

            if (null != m_NetStateListener)
            {
                m_NetStateListener.del_OnConnectStart(nSocketID);
            }

            ///建立一个TCP连接
            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                Debug.Log("NetTCPSocketConnect::Connect begin connect to server:" + ip + " port:" + portnumber.ToString());
                m_IPAddrMsg.m_IsConnect = false;

                /*
                 * SocketAsyncEventArgs是微软提供的高性能异步Socket实现类,主要为高性能网络服务器应用程序而设计,主要是为了避免在在异步套接字 I/O
                 * 量非常大时发生重复的对象分配和同步。使用此类执行异步套接字操作的模式包含以下步骤:
                 *   1.分配一个新的 SocketAsyncEventArgs 上下文对象,或者从应用程序池中获取一个空闲的此类对象。
                 *   2.将该上下文对象的属性设置为要执行的操作(例如,完成回调方法、数据缓冲区、缓冲区偏移量以及要传输的最大数据量)。
                 *   3.调用适当的套接字方法 (xxxAsync) 以启动异步操作。
                 *   4.如果异步套接字方法 (xxxAsync) 返回 true,则在回调中查询上下文属性来获取完成状态。
                 *   5.如果异步套接字方法 (xxxAsync) 返回 false,则说明操作是同步完成的。 可以查询上下文属性来获取操作结果。
                 *   6.将该上下文重用于另一个操作,将它放回到应用程序池中,或者将它丢弃。
                 */
                SocketAsyncEventArgs asyncEvent = new SocketAsyncEventArgs();
                asyncEvent.RemoteEndPoint = remoteIP;
                asyncEvent.Completed     += new EventHandler <SocketAsyncEventArgs>(__OnConnectComplete);
                m_Socket.ConnectAsync(asyncEvent);
                // 等待信号
                m_AutoConnectEvent.WaitOne();

                SocketError errorCode = asyncEvent.SocketError;
                if (errorCode != SocketError.Success)
                {
                    //throw new SocketException((Int32)errorCode);
                    Debug.Log("NetTCPSocketConnect::Connect check the net SocketError = " + errorCode);
                }
            }
            catch (System.Exception e)
            {
                Debug.Log("NetTCPSocketConnect::Connect " + e.ToString());
                return(false);
            }

            return(true);
        }