private void OnConnectCallback(IAsyncResult ar) { try { Socket handler = (Socket)ar.AsyncState; handler.EndConnect(ar); } catch (SocketException ex) { Utility.Log.Error("连接服务器{0}:{1}失败:{2}", curIP, curPort, ex.ToString()); NetManager.Instance().PushConnectError(NetWorkError.NetWorkError_ConnectFailed); } finally { if (m_tcpClient.Connected) { // 设置属性 m_tcpClient.NoDelay = true; m_tcpClient.ReceiveBufferSize = 1024 * 1024; // *2; m_tcpClient.ReceiveTimeout = 10000; m_tcpClient.SendBufferSize = 1024 * 1024; // *2; m_tcpClient.SendTimeout = 10000; //Utility.Log.Trace("m_tcpClient.ReceiveBufferSize : " + m_tcpClient.ReceiveBufferSize.ToString()); //Utility.Log.Trace("m_tcpClient.SendBufferSize : " + m_tcpClient.SendBufferSize.ToString()); if (DiaoxpackOut != null) { DiaoxpackOut.Clear(); } if (socketstate == SCOKET_CONNECT_STATE.CONNECTE_STOP) { if (m_sender != null) { m_sender.PopPackage(out DiaoxpackOut); Utility.Log.Trace("断线重连``````````````弹出包->count=" + DiaoxpackOut.Count); } // 重新连接回调 if (m_NetLinkSink != null) { m_NetLinkSink.OnReConnected(); } } // 启动收包, 发包线程 StartSendThread(); StartReceiveThread(); socketstate = SCOKET_CONNECT_STATE.CONNECTED; //等到收到seed 才能发送消息 //NetManager.Instance().PushConnectSuccess(); } else { socketstate = SCOKET_CONNECT_STATE.UNCONNECTED; NetManager.Instance().PushConnectError(NetWorkError.NetWorkError_UnConnect); } } }
private void testConnect() { if (socketstate == SCOKET_CONNECT_STATE.CONNECTED && m_tcpClient != null && (m_tcpClient.Client != null)) { // 另外说明:tcpc.Connected同tcpc.Client.Connected; // tcpc.Client.Connected只能表示Socket上次操作(send,recieve,connect等)时是否能正确连接到资源, // 不能用来表示Socket的实时连接状态。 //m_tcpClient.Connected //((m_tcpClient.Client.Poll(1000, SelectMode.SelectRead) && (m_tcpClient.Client.Available == 0)) || !m_tcpClient.Client.Connected) if ((!m_tcpClient.Client.Connected || (m_tcpClient.Client.Poll(1000, SelectMode.SelectRead) && m_tcpClient.Client.Available == 0))) { //try //{ // //m_tcpClient.Client.Send(null,) //} //catch (System.Exception ex) //{ // 断开连接 Disconnect(); socketstate = SCOKET_CONNECT_STATE.CONNECTE_STOP; NetManager.Instance().PushConnectError(NetWorkError.NetWorkError_DisConnect); //Utility.Log.Error("断开连接{0}",ex.ToString()); //} } } }
// 断开连接 public void Disconnect() { // hasLoginCheckConnected = false; if (null != m_tcpClient && m_tcpClient.Connected) { m_tcpClient.GetStream().Close(); m_tcpClient.Close(); m_tcpClient = null; this.socketstate = SCOKET_CONNECT_STATE.UNCONNECTED; NetManager.Instance().PushClose(); } SetTerminateFlag(); }
/// <summary> /// 断开连接 /// </summary> public void Disconnect() { if (null != m_tcpClient && m_tcpClient.Connected) { m_tcpClient.GetStream().Close(); m_tcpClient.Close(); m_tcpClient = null; this.socketstate = SCOKET_CONNECT_STATE.UNCONNECTED; //TODO // NetManager.Instance().PushClose(); } SetTerminateFlag(); }
/// <summary> /// 连接回调 /// </summary> /// <param name="ar"></param> private void OnConnectCallback(IAsyncResult ar) { try { Socket handler = (Socket)ar.AsyncState; handler.EndConnect(ar); } catch (SocketException ex) { //TODO // Utility.Log.Error("连接服务器{0}:{1}失败:{2}", curIP, curPort, ex.ToString()); // NetManager.Instance().PushConnectError(NetWorkError.NetWorkError_ConnectFailed); } finally { if (m_tcpClient.Connected) { // 设置属性 m_tcpClient.NoDelay = true; m_tcpClient.ReceiveBufferSize = 1024 * 1024;// *2; m_tcpClient.ReceiveTimeout = 10000; m_tcpClient.SendBufferSize = 1024 * 1024;// *2; m_tcpClient.SendTimeout = 10000; //Utility.Log.Trace("m_tcpClient.ReceiveBufferSize : " + m_tcpClient.ReceiveBufferSize.ToString()); //Utility.Log.Trace("m_tcpClient.SendBufferSize : " + m_tcpClient.SendBufferSize.ToString()); if (DiaoxpackOut != null) { DiaoxpackOut.Clear(); } if (socketstate == SCOKET_CONNECT_STATE.CONNECTE_STOP) { if (m_sender != null) { m_sender.PopPackage(out DiaoxpackOut); // Utility.Log.Trace("断线重连``````````````弹出包->count=" + DiaoxpackOut.Count); } // 重新连接回调 if (m_NetLinkSink != null) { m_NetLinkSink.OnReConnected(); } } // 启动收包, 发包线程 StartSendThread(); StartReceiveThread(); socketstate = SCOKET_CONNECT_STATE.CONNECTED; //TODO //NetManager.Instance().PushConnectSuccess(); } else {//TODO socketstate = SCOKET_CONNECT_STATE.UNCONNECTED; // NetManager.Instance().PushConnectError(NetWorkError.NetWorkError_UnConnect); } } }
void CheckConnnectState() { // 另外说明:tcpc.Connected同tcpc.Client.Connected; // tcpc.Client.Connected只能表示Socket上次操作(send,recieve,connect等)时是否能正确连接到资源, // 不能用来表示Socket的实时连接状态。 if (!m_tcpClient.Client.Connected || //如果连接已关闭、重置或终止,则返回 true (更多...) //从网络接收的、可供读取的数据的字节数。 (m_tcpClient.Client.Poll(1000, SelectMode.SelectRead) && (m_tcpClient.Client.Available == 0))) { byte[] buff = new byte[32]; int nRead = m_tcpClient.Client.Receive(buff); if (nRead == 0) { socketstate = SCOKET_CONNECT_STATE.CONNECTE_STOP; } } }