/// <summary> /// 主线程内更新 /// </summary> public void Update() { if (IOSocket == null || IOSocket.Connected == false) { return; } // 接收数据 if (_isReceiving == false) { _isReceiving = true; // 清空缓存 _packageCoder.ClearReceiveBuffer(); // 请求操作 _receiveArgs.SetBuffer(0, _packageCoder.GetReceiveBufferCapacity()); bool willRaiseEvent = IOSocket.ReceiveAsync(_receiveArgs); if (!willRaiseEvent) { ProcessReceive(_receiveArgs); } } // 发送数据 if (_isSending == false && _sendQueue.Count > 0) { _isSending = true; // 清空缓存 _packageCoder.ClearSendBuffer(); // 合并数据一起发送 while (_sendQueue.Count > 0) { // 数据压码 System.Object packet = _sendQueue.Dequeue(); _packageCoder.Encode(packet); // 如果已经超过一个最大包体尺寸 // 注意:发送的数据理论最大值为俩个最大包体大小 if (_packageCoder.GetSendBufferWriterIndex() >= _packageMaxSize) { break; } } // 请求操作 _sendArgs.SetBuffer(0, _packageCoder.GetSendBufferReadableBytes()); bool willRaiseEvent = IOSocket.SendAsync(_sendArgs); if (!willRaiseEvent) { ProcessSend(_sendArgs); } } }
private void OnConnectionMade(PoweredUnit connection, IOSocket connectedSocket) { this.connection = connection; this.connectedSocket = connectedSocket; if (this.connectionMadeCallback != null) { this.connectionMadeCallback.Invoke(); } }
/// <summary> /// 主线程内更新 /// </summary> public void Update() { if (IOSocket == null || IOSocket.Connected == false) { return; } //接收数据 if (_isReceiving == false) { _isReceiving = true; //清空缓存 _receiveBuffer.Clear(); //请求操作 _receiveArgs.SetBuffer(0, _receiveBuffer.Capacity); bool willRaiseEvent = IOSocket.ReceiveAsync(_receiveArgs); if (!willRaiseEvent) { ProcessReceive(_receiveArgs); } } //发送数据 if (_isSending == false && _sendQueue.Count > 0) { _isSending = true; //清空缓存 _sendBuffer.Clear(); //合并数据一起发送 while (_sendQueue.Count > 0) { IPackage packet = _sendQueue.Dequeue(); packet.Encode(_sendBuffer, _tempSBuffer); //如果已经超过一个最大包体尺寸 //注意:发送的数据理论最大值为俩个最大包体大小 if (_sendBuffer.WriterIndex >= NetDefine.PackageMaxSize) { break; } } //请求操作 _sendArgs.SetBuffer(0, _sendBuffer.ReadableBytes()); bool willRaiseEvent = IOSocket.SendAsync(_sendArgs); if (!willRaiseEvent) { ProcessSend(_sendArgs); } } }
public void SetConnection(PoweredUnit connection, IOSocket connectedSocket) { if (connection != null && connectedSocket != null) { this.OnConnectionMade(connection, connectedSocket); } else { this.OnConnectionRemoved(); } }
private void OnConnectionRemoved() { /* Temp variables are required as the callback will require * the connection to be null and at the same time the original * connection (before removal) must be passed into the callback. */ PoweredUnit tempConnection = this.connection; IOSocket tempSocket = this.connectedSocket; this.connection = null; this.connectedSocket = null; if (tempConnection != null && tempSocket != null) { if (this.connectionRemovedCallback != null) { this.connectionRemovedCallback.Invoke(tempConnection, tempSocket); } } }
private void BtnConnectIO_Click(object sender, RoutedEventArgs e) { if (IOSocket == null) { IOSocket = new SocketManager("127.0.0.1:10001"); IOSocket.ConnectState += IOSocket_ConnectState; if (IOSocket.Connect()) { IOSocket.DataReceived += IOSocket_DataReceived; IOSocket.StartReceiveAsync(); } } else { IOSocket.StopReceiveAsync(); IOSocket.Close(); } }
/// <summary> /// 数据接收完成时 /// </summary> private void ProcessReceive(object obj) { SocketAsyncEventArgs e = obj as SocketAsyncEventArgs; // check if the remote host closed the connection if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { _packageParser.SetReceiveDataSize(e.BytesTransferred); // 如果数据写穿 if (_packageParser.GetReceiveBufferWriterIndex() > _packageParser.GetReceiveBufferCapacity()) { HandleError(true, "The channel fatal error"); return; } // 数据解码 _decodeTempList.Clear(); _packageParser.Decode(_decodeTempList); lock (_receiveQueue) { for (int i = 0; i < _decodeTempList.Count; i++) { _receiveQueue.Enqueue(_decodeTempList[i]); } } // 为接收下一段数据,投递接收请求 e.SetBuffer(_packageParser.GetReceiveBufferWriterIndex(), _packageParser.GetReceiveBufferWriteableBytes()); bool willRaiseEvent = IOSocket.ReceiveAsync(e); if (!willRaiseEvent) { ProcessReceive(e); } } else { HandleError(true, $"ProcessReceive error : {e.SocketError}"); } }
/// <summary> /// Dispose /// </summary> public void Dispose() { try { if (IOSocket != null) { IOSocket.Shutdown(SocketShutdown.Both); } _receiveArgs.Dispose(); _sendArgs.Dispose(); _sendQueue.Clear(); _receiveQueue.Clear(); _decodeTempList.Clear(); if (_packageParser != null) { _packageParser.Dispose(); } _isSending = false; _isReceiving = false; } catch (Exception) { // throws if client process has already closed, so it is not necessary to catch. } finally { if (IOSocket != null) { IOSocket.Close(); IOSocket = null; } } }
private void BtnOutputs_Click(object sender, RoutedEventArgs e) => IOSocket?.Write(txtOutputs.Text + "\r\n");
private void BtnInputs_Click(object sender, RoutedEventArgs e) { txtIOResponse.Text = ""; IOSocket?.Write(txtInputs.Text + "\r\n"); }
/// <summary> /// 数据接收完成时 /// </summary> private void ProcessReceive(object obj) { SocketAsyncEventArgs e = obj as SocketAsyncEventArgs; // check if the remote host closed the connection if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { _receiveBuffer.WriterIndex += e.BytesTransferred; //如果数据写穿 if (_receiveBuffer.WriterIndex > _receiveBuffer.Capacity) { HandleError(true, "The channel fatal error"); return; } //循环解包 while (true) { //如果数据不够一个包头 if (_receiveBuffer.ReadableBytes() < NetDefine.PackageHeadSize) { break; } _receiveBuffer.MarkReaderIndex(); //如果包头标记不正确 Int16 headMark = _receiveBuffer.ReadShort(); if (headMark != NetDefine.PackageHeadMark) { _receiveBuffer.ResetReaderIndex(); _receiveBuffer.ReaderIndex++; continue; } //获取包体信息 Int16 msgType = _receiveBuffer.ReadShort(); Int32 msgSize = _receiveBuffer.ReadInt(); int remainHeadSize = NetDefine.PackageHeadSize - 2 - 2 - 4; for (int i = 0; i < remainHeadSize; i++) { _receiveBuffer.ReadByte(); } //如果协议大小超过最大长度 if (msgSize > NetDefine.PackageMaxSize) { HandleError(false, "The package {0} size is exceeds max size.", msgType); _receiveBuffer.ResetReaderIndex(); _receiveBuffer.ReaderIndex++; continue; } //如果剩余可读数据小于包体长度 if (msgSize > 0 && _receiveBuffer.ReadableBytes() < msgSize) { _receiveBuffer.ResetReaderIndex(); break; //需要退出读够数据再解包 } //正常解包 try { _receiveBuffer.ResetReaderIndex(); IPackage msg = MoNetMsgHandler.Handle(msgType); msg.Decode(_receiveBuffer, _tempRBuffer); lock (_receiveQueue) { _receiveQueue.Enqueue(msg); } } catch (Exception ex) { // 解包异常后继续解包 HandleError(false, "The package {0} decode error : {1}", msgType, ex.ToString()); _receiveBuffer.ResetReaderIndex(); _receiveBuffer.ReaderIndex++; } } //while end //将剩余数据移至起始 _receiveBuffer.DiscardReadBytes(); //为接收下一段数据,投递接收请求 e.SetBuffer(_receiveBuffer.WriterIndex, _receiveBuffer.WriteableBytes()); bool willRaiseEvent = IOSocket.ReceiveAsync(e); if (!willRaiseEvent) { ProcessReceive(e); } } else { HandleError(true, "ProcessReceive error : {0}", e.SocketError); } }