// 通知已经与服务器建立连接 public void notifyConnectServer() { // 建立连接后将消息列表中残留的消息清空 mOutputBuffer.clear(); mConnectStateLock.waitForUnlock(); mConnectState = CONNECT_STATE.CS_CONNECTED; mConnectStateLock.unlock(); mHeartBeatTimer.start(); }
public SocketConnectClient(string name) : base(name) { mDataBytesPool = new Dictionary <int, List <byte[]> >(); mCollectedBytes = new DoubleBuffer <byte[]>(); mOutputBuffer = new DoubleBuffer <byte[]>(); mMaxReceiveCount = 8 * 1024; mReceiveBuffer = new DoubleBuffer <SocketPacket>(); mReceiveThread = new CustomThread("SocketReceive"); mSendThread = new CustomThread("SocketSend"); mRecvBuff = new byte[mMaxReceiveCount]; mInputBuffer = new StreamBuffer(1024 * 1024); mRemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); mConnectStateLock = new ThreadLock(); mReceivePacketHistory = new Queue <string>(); mHeartBeatTimer = new CustomTimer(); mConnectState = CONNECT_STATE.CS_NOT_CONNECT; }
// 重新连接服务器 public void reconnectServer(bool clearOldSocket) { if (mConnectState != CONNECT_STATE.CS_NOT_CONNECT) { return; } mConnectStateLock.waitForUnlock(); mConnectState = CONNECT_STATE.CS_CONNECTING; mConnectStateLock.unlock(); if (clearOldSocket) { clearSocket(); } if (mServerSocket == null) { mServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } mServerSocket.BeginConnect(mIP, mPort, connectCallback, null); }
public virtual void init(IPAddress ip, int port, float heartBeatTimeOut) { mIP = ip; mPort = port; mHeartBeatTimer.init(-1.0f, heartBeatTimeOut, false); try { // 创建socket mServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); mServerSocket.Connect(mIP, mPort); } catch (SocketException e) { logInfo("init socket exception : " + e.Message, LOG_LEVEL.LL_FORCE); mServerSocket.Close(); mServerSocket = null; // 服务器连接失败也要开启接收和发送线程 mSendThread.start(sendSocket); mReceiveThread.start(receiveSocket); NET_STATE state = NET_STATE.NS_NET_CLOSE; if (e.ErrorCode == 10051) { state = NET_STATE.NS_NET_CLOSE; } else if (e.ErrorCode == 10061) { state = NET_STATE.NS_SERVER_CLOSE; } setNetState(state); return; } notifyConnectServer(); mConnectState = CONNECT_STATE.CS_CONNECTED; mSendThread.start(sendSocket); mReceiveThread.start(receiveSocket); }
// 通知已经与服务器断开连接 public void notifyDisconnectServer() { mConnectStateLock.waitForUnlock(); mConnectState = CONNECT_STATE.CS_NOT_CONNECT; mConnectStateLock.unlock(); }
public void SetConnectState(CONNECT_STATE cState) { PlayerPrefs.SetInt("connectState", (int)cState); }