protected void SocketClient_StateChanged(object sender, SocketClientState state) { InvokeOnMainThread(() => { switch (state) { case SocketClientState.Connected: lblConnectStatus.Text = "Connected"; btnConnect.SetTitle("Disconnect", UIControlState.Normal); lblConnectStatus.TextColor = UIColor.Green; break; case SocketClientState.Disconnected: lblConnectStatus.Text = "Disconnected"; btnConnect.SetTitle("Connect", UIControlState.Normal); lblConnectStatus.TextColor = UIColor.Red; break; } }); }
protected void SocketClient_StateChanged (object sender, SocketClientState state) { InvokeOnMainThread(() => { switch (state) { case SocketClientState.Connected: lblConnectStatus.Text = "Connected"; btnConnect.SetTitle("Disconnect", UIControlState.Normal); lblConnectStatus.TextColor = UIColor.Green; break; case SocketClientState.Disconnected: lblConnectStatus.Text = "Disconnected"; btnConnect.SetTitle("Connect", UIControlState.Normal); lblConnectStatus.TextColor = UIColor.Red; break; } }); }
protected void SocketClient_StateChanged(object sender, SocketClientState state) { RunOnUiThread(() => { switch (state) { case SocketClientState.Connected: lblConnectStatus.Text = "Connected"; btnConnect.Text = "Disconnect"; lblConnectStatus.SetTextColor(Color.Green); break; case SocketClientState.Disconnected: lblConnectStatus.Text = "Disconnected"; btnConnect.Text = "Connect"; lblConnectStatus.SetTextColor(Color.Red); break; } }); }
protected void SocketClient_StateChanged (object sender, SocketClientState state) { RunOnUiThread(() => { switch (state) { case SocketClientState.Connected: lblConnectStatus.Text = "Connected"; btnConnect.Text = "Disconnect"; lblConnectStatus.SetTextColor(Color.Green); break; case SocketClientState.Disconnected: lblConnectStatus.Text = "Disconnected"; btnConnect.Text = "Connect"; lblConnectStatus.SetTextColor(Color.Red); break; } }); }
public void ReadHeader() { try { // Begin receiving the data from the remote device. this._tcpClient.Client.BeginReceive( this._buffer, 0, this._headerSize, SocketFlags.None, ar => { int bytesRead = this._tcpClient.Client.EndReceive(ar); if (bytesRead == this._headerSize) { UInt16 requestId = BitConverter.ToUInt16(this._buffer.SubArray(0, 2).CheckBigEndian(), 0); int errorCode = BitConverter.ToInt32(this._buffer.SubArray(2, 4).CheckBigEndian(), 0); int messageTypeId = BitConverter.ToInt32(this._buffer.SubArray(6, 4).CheckBigEndian(), 0); int protoLength = BitConverter.ToInt32(this._buffer.SubArray(10, 4).CheckBigEndian(), 0); ReceivedProtoEventArgs r = new ReceivedProtoEventArgs(); r.bytes = null; r.messageTypeId = messageTypeId; r.errorCode = errorCode; r.requestId = requestId; if (r.messageTypeId == MessageTypeId.Socket_CheckToken_Response && r.errorCode == ErrorCode.SUCCESS) { this._state = SocketClientState.CheckTokened; } if (protoLength == 0) { if (receiveProtoEventHandler != null) { receiveProtoEventHandler(r); } ReadHeader(); } else { ReadBody(protoLength, r); } } }, null); } catch (Exception e) { this._tcpClient.Close(); Debug.LogError(e.Message); Debug.LogError(e.StackTrace); } }
/// <summary> /// Raised when connection state is changed. /// </summary> /// <param name="state">Client state.</param> protected void OnStateChanged(SocketClientState state) { this.state = state; if (state == SocketClientState.Disconnected) { if (messageQueue != null) { messageQueue.Release(); messageQueue = null; } messagesThread = null; } var handler = StateChanged; if (handler != null) { handler(this, state); } }
/// <summary> /// 这里面机会同时接受数据并解析 /// 这段代码 /// </summary> private void _Connect() { this._retry_times = 0; while (this._keepConnecting) { //如果已经连接就等待50 毫秒,再次检测 if (this.isConnected) { Thread.Sleep(10); } else { if (this.startConnectEventHandler != null) { this.startConnectEventHandler(); } //清掉已有的线程 if (_sendDataThread != null) { try{ _sendDataThread.Abort(); }catch (Exception e) { } } if (_sendCheckTokenThread != null) { try{ _sendCheckTokenThread.Abort(); }catch (Exception e) { } } if (_heartBeatThread != null) { try{ _heartBeatThread.Abort(); }catch (Exception e) { } } this._tcpClient = new TcpClient(); // this._tcpClient.Client.io //.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive this._state = SocketClientState.Closed; //如果没有就连接端口,并且获取数据流 try{ this._tcpClient.Connect(this.ip, this.port); }catch (Exception e) { Debug.LogError(string.Format("IP {0} port {1} message {2} stackTrace {3}", this.ip, this.port, e.Message, e.StackTrace)); this._retry_times += 1; ///抛出异常 if (this._retry_times >= Max_Retry_Times) { this._keepConnecting = false; if (this.networkErrorHandler != null) { this.networkErrorHandler(); } } } if (this.isConnected) { this._retry_times = 0; this._state = SocketClientState.Connected; this._buffer = new byte[this._bufferSize]; this.ReadHeader(); _sendDataThread = new Thread(this.doSendData); _sendDataThread.Start(); _sendCheckTokenThread = new Thread(this._SocketCheckToken); _sendCheckTokenThread.Start(); _heartBeatThread = new Thread(this._HeartBeat); _heartBeatThread.Start(); } } } this._tcpClient.Close(); }
/// <summary> /// Initializes a new instance of the client. /// </summary> public SocketClient() { this.state = SocketClientState.Disconnected; }
/// <summary> /// Raises the client state changed event. /// </summary> /// <param name="state">Client state.</param> protected void OnStateChanged(SocketClientState state) { this.state = state; if (state == SocketClientState.Disconnected) { if (messageQueue != null) { messageQueue.Release(); messageQueue = null; } messagesThread = null; } var handler = StateChanged; if (handler != null) { handler(this, state); } }