protected void CloseSocket(string closeMessage, bool isNotifyDisconnectToLogic, string errorMessage = null) { this.state = NetworkConnectState.NotConnected; if (this.receiveDataBuffer != null) { this.receiveDataBuffer = null; } if (this.socket != null) { try { Debug.LogError(string.Format("{1} ClientShutDown 与服务器 {2} 断开连接,操作:{0}", closeMessage, base.ServerType, this.ipEndPoint)); this.socketStream.Dispose(); this.socketStream.Close(); this.socket.Close(); } catch (Exception ex) { Debug.LogError("ClientShutDown Failed: " + closeMessage + "\n" + ex.get_Message()); } this.socket = null; if (errorMessage != null) { Debug.LogError(errorMessage); } if (isNotifyDisconnectToLogic && !this.IsManualDisconnect) { this.SimulateDisconnectPacket(); } } }
public bool SendPacket(NetBuffer buffer) { Queue <NetBuffer> queue = this.sendQueue; bool result; lock (queue) { Queue <NetBuffer> queue2 = this.sendCacheQueue; lock (queue2) { bool flag = buffer.PacketType == PacketType.Ping || this.IsNetworkAvailableAndConstant; if (this.IsConnected && flag) { if (this.sendCacheQueue.get_Count() > 0 && buffer.PacketType != PacketType.Verify) { this.AddDataToCache(buffer); result = false; } else { this.sendQueue.Enqueue(buffer); result = true; } } else { this.state = NetworkConnectState.NotConnected; this.CheckSendPacketReconnect(buffer); result = false; } } } return(result); }
private void InitMember(string ip, int port) { isStop = false; mIP = ip; // GetIP(ip); mHead = new NetworkHeadFormat(); mContents = null; mPort = port; ConnectState = NetworkConnectState.Already; NetworkHeadFormat temp = new NetworkHeadFormat(); temp.mType = NetworkCommandType.HeartCodec; mHeartBytes = temp.Merge(null); }
protected void SetConnectState(NetworkConnectState state) { if (ConnectState == state) { return; } ConnectState = state; if (IsConnectState(NetworkConnectState.Reconnectting)) { if (mReconnectTimerId == int.MaxValue) { mReconnectTimerId = TimerTaskQueue.Instance.AddTimer(1000, mReconnectInterval, StartConnect); } if (mHeartTimerId != int.MaxValue) { TimerTaskQueue.Instance.DelTimer(mHeartTimerId); mHeartTimerId = int.MaxValue; } } else if (IsConnectState(NetworkConnectState.Connectted)) { if (mReconnectTimerId != int.MaxValue) { TimerTaskQueue.Instance.DelTimer(mReconnectTimerId); mReconnectTimerId = int.MaxValue; } if (mHeartTimerId == int.MaxValue) { mHeartTimerId = TimerTaskQueue.Instance.AddTimer(1000, mHeartbeatInterval, HeartBeat); } mSendWait.Set(); mReceiveWait.Set(); } else if (IsConnectState(NetworkConnectState.Disconnected)) { if (mHeartTimerId != int.MaxValue) { TimerTaskQueue.Instance.DelTimer(mHeartTimerId); mHeartTimerId = int.MaxValue; } if (mReconnectTimerId != int.MaxValue) { TimerTaskQueue.Instance.DelTimer(mReconnectTimerId); mReconnectTimerId = int.MaxValue; } } }
protected void BeginConnect() { Debug.LogError("BeginConnect"); if (this.ipEndPoint == null) { Debug.LogError("ipEndPoint == null"); return; } this.state = NetworkConnectState.Connecting; this.MarkReachabilityState = Application.get_internetReachability(); try { Debug.LogError(string.Concat(new object[] { "OSSupportsIPv6: ", Socket.get_OSSupportsIPv6(), " Port: ", this.ipEndPoint.get_Port(), " ", this.RemotePortInUseCount(this.ipEndPoint.get_Port()) })); this.socket = new TcpClient(2); IAsyncResult asyncResult = this.socket.BeginConnect(this.ipEndPoint.get_Address(), this.ipEndPoint.get_Port(), new AsyncCallback(this.OnGetConnectResult), this.socket); } catch (Exception ex) { this.CloseSocket("BeginConnect Failed", false, ex.get_Message()); if (this.onConnectFailedCallBack != null) { this.onConnectFailedCallBack.Invoke(); } } finally { Debug.LogError(string.Concat(new object[] { "OSSupportsIPv6 1: ", Socket.get_OSSupportsIPv6(), " Port: ", this.ipEndPoint.get_Port(), " ", this.RemotePortInUseCount(this.ipEndPoint.get_Port()) })); } }
protected void HandleSendSocketError(SocketError socketErrorCode, NetBuffer sendData, Action successCallBack = null) { if (this.IsManualDisconnect) { return; } switch (socketErrorCode) { case 10050: case 10053: goto IL_7A; case 10051: case 10052: IL_2E: if (socketErrorCode == null) { if (successCallBack != null) { successCallBack.Invoke(); } return; } if (socketErrorCode != 10058 && socketErrorCode != 10064 && socketErrorCode != 10101) { return; } goto IL_7A; case 10054: Debug.LogError("服务器断开连接"); return; } goto IL_2E; IL_7A: this.state = NetworkConnectState.NotConnected; this.CheckProcessSendQueueReconnect(sendData); }
public void ProcessSendQueue() { try { if (!this.IsSending) { Queue <NetBuffer> queue = this.sendQueue; lock (queue) { if (this.sendQueue.get_Count() != 0) { this.IsSending = true; NetBuffer netBuffer = this.sendQueue.Peek(); SocketError socketError = 0; this.socketStream.BeginWrite(netBuffer.Buffer, 0, netBuffer.MemorySize, new AsyncCallback(this.OnSendData), this.socket); if (socketError != null) { if (socketError == 10053 || socketError == 10101 || socketError == 10058 || socketError == 10064 || socketError == 10050) { Debug.LogError("Final Send Failed: " + socketError); this.state = NetworkConnectState.NotConnected; this.CheckProcessSendQueueReconnect(netBuffer); } else { Debug.LogError("Final Send Failed: " + socketError); } } } } } } catch (Exception ex) { this.CloseSocket("ProcessSendQueue Failed", true, ex.get_Message()); } }
protected void OnGetConnectResult(IAsyncResult connectResult) { Debug.Log(string.Concat(new object[] { "OnGetConnectResult: ", connectResult.get_CompletedSynchronously(), " ", connectResult.get_IsCompleted(), " ", DateTime.get_Now() })); if (connectResult == null) { this.state = NetworkConnectState.NotConnected; if (this.onConnectFailedCallBack != null) { this.onConnectFailedCallBack.Invoke(); } return; } TcpClient tcpClient = (TcpClient)connectResult.get_AsyncState(); if (tcpClient == null || this.socket == null || tcpClient != this.socket || !tcpClient.get_Connected()) { this.state = NetworkConnectState.NotConnected; if (this.onConnectFailedCallBack != null) { this.onConnectFailedCallBack.Invoke(); } return; } try { tcpClient.EndConnect(connectResult); this.socketStream = tcpClient.GetStream(); } catch (Exception ex) { this.CloseSocket("EndConnect Failed", false, ex.get_Message()); if (this.onConnectFailedCallBack != null) { this.onConnectFailedCallBack.Invoke(); } return; } Debug.Log(string.Concat(new object[] { "-----------------------------------连接成功: Time: ", DateTime.get_Now(), " LocalEndPoint: ", this.socket.get_Client().get_LocalEndPoint() })); if (this.socket.get_Client().get_LocalEndPoint() != null) { string[] array = this.socket.get_Client().get_LocalEndPoint().ToString().Split(new char[] { ':' }); if (array.Length > 1) { int port = int.Parse(array[1]); Debug.Log(string.Concat(new object[] { "CheckLocalPort: ", array[1], " ", this.LocalPortInUseCount(port) })); } } this.state = NetworkConnectState.Connected; this.StartReceiveData(); if (this.onConnectSuccessCallBack != null) { this.onConnectSuccessCallBack.Invoke(); } }
private bool IsConnectState(NetworkConnectState state) { return(ConnectState == state); }
public void Initialize(NetworkConnectState owner) { _owner = owner; }