protected override void ReceiveCallback(IAsyncResult ar) { var receiveState = (NetworkState)ar.AsyncState; try { var buffSize = receiveState.Socket.EndReceiveFrom(ar, ref RemoteEndpoint); receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, buffSize); receiveState.RemoteHost = ((IPEndPoint)RemoteEndpoint).ToNode(TransportType.Udp); List <IByteBuf> decoded; Decoder.Decode(this, receiveState.Buffer, out decoded); foreach (var message in decoded) { var networkData = NetworkData.Create(receiveState.RemoteHost, message); InvokeReceiveIfNotNull(networkData); } //shift the contents of the buffer receiveState.Buffer.CompactIfNecessary(); //continue receiving in a loop if (Receiving) { receiveState.Socket.BeginReceiveFrom(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length, SocketFlags.None, ref RemoteEndpoint, ReceiveCallback, receiveState); } } catch (SocketException ex) //typically means that the socket is now closed { Receiving = false; InvokeDisconnectIfNotNull(NodeBuilder.FromEndpoint((IPEndPoint)RemoteEndpoint), new HeliosConnectionException(ExceptionType.Closed, ex)); Dispose(); } catch (ObjectDisposedException ex) { Receiving = false; InvokeDisconnectIfNotNull(NodeBuilder.FromEndpoint((IPEndPoint)RemoteEndpoint), new HeliosConnectionException(ExceptionType.Closed, ex)); } catch (Exception ex) { InvokeErrorIfNotNull(ex); } }
/// <summary> /// 发送第几帧数据 /// </summary> /// <param name="sendFrameIndex"></param> protected virtual NetworkData CreateSendFrameIndexData(short sendFrameIndex) { if (sendFrameIndex > this.sHSerialPacket.FrameCount) { return(null); } List <byte> sendByte = new List <byte>(); byte[] frameNumByte = BitConverter.GetBytes(sendFrameIndex); Array.Reverse(frameNumByte); sendByte.AddRange(frameNumByte); FrameBase frameBase = this.sHSerialPacket.GetFrameBase(sendFrameIndex); sendByte.AddRange(frameBase.FrameBytes); FrameBase frame = this.CreateSHSerialFrame(sendByte.ToArray()); return(NetworkData.Create(this.Local, frame.FrameBytes, frame.FrameBytes.Length)); }
/// <summary> /// 处理数据 /// </summary> /// <param name="receiveState"></param> private void ReceiveCallback(NetworkState receiveState) { try { var received = receiveState.RawBuffer.Length; if (received == 0) { return; } INode node = receiveState.RemoteHost; if (SocketMap.ContainsKey(receiveState.RemoteHost.nodeConfig.ToString())) { var connection = SocketMap[receiveState.RemoteHost.nodeConfig.ToString()]; node = connection.RemoteHost; } else { RefactorProxyResponseChannel adapter = new RefactorProxyResponseChannel(this, null); SocketMap.Add(adapter.RemoteHost.nodeConfig.ToString(), adapter.requestChannel); } receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, received); List <IByteBuf> decoded; Decoder.Decode(ConnectionAdapter, receiveState.Buffer, out decoded); foreach (var message in decoded) { var networkData = NetworkData.Create(receiveState.RemoteHost, message); string log = String.Format("{0}:串口处理数据-->>{1}", DateTime.Now.ToString("hh:mm:ss"), this.Encoder.ByteEncode(networkData.Buffer)); Console.WriteLine(log); if (ConnectionAdapter is ReactorConnectionAdapter) { ((ReactorConnectionAdapter)ConnectionAdapter).networkDataDocker.AddNetworkData(networkData); ((EventWaitHandle)((ReactorConnectionAdapter)ConnectionAdapter).protocolEvents[(int)ProtocolEvents.PortReceivedData]).Set(); } } } catch //node disconnected { var connection = SocketMap[receiveState.RemoteHost.nodeConfig.ToString()]; CloseConnection(connection); } }
private bool SendMessage(string connectString, byte[] data = null) { if (AttemptConnect(connectString)) { try { var networkData = NetworkData.Create(_nodeDictionary[connectString], data, data.Length); _connectionDictionary[connectString].Send(networkData); return(true); } catch (Exception) { // 에러메시지를 기록한다. return(false); } } else { return(false); } }
private void btnSend_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbHost.Text) || string.IsNullOrEmpty(tbPort.Text)) { AppendStatusText("Must supply valid host and port before attempting to connect"); return; } var didConnect = AttemptConnect(tbHost.Text, tbPort.Text); if (!didConnect) { AppendStatusText("Invalid host and port - unable to attempt connection"); return; } if (!string.IsNullOrEmpty(tbSend.Text)) { AppendStatusText(string.Format("Attempting to send: {0}", tbSend.Text)); var bytes = Encoding.ASCII.GetBytes(tbSend.Text); var networkData = NetworkData.Create(RemoteHost, bytes, bytes.Length); Connection.Send(networkData); } }
///// <summary> /// 处理数据 /// </summary> /// <param name="receiveState"></param> private void PortDataReceived(IAsyncResult ar) { var receiveState = (NetworkState)ar.AsyncState; try { var received = ListenerSocket.EndReceiveFrom(ar, ref RemoteEndPoint); if (received == 0) { return; } NetworkData networkData = NetworkData.Create(this.Listener, receiveState.RawBuffer, received); networkData.RemoteHost.EndNodePoint = RemoteEndPoint; this.ReceivedData(networkData); //清除数据继续接收 receiveState.RawBuffer = new byte[this.BufferSize]; ListenerSocket.BeginReceiveFrom(this.networkState.RawBuffer, 0, this.networkState.RawBuffer.Length, SocketFlags.None, ref RemoteEndPoint, PortDataReceived, this.networkState); } catch (Exception ex) { LogAgent.Error(ex.ToString()); } }
private void ReceiveCallback(IAsyncResult ar) { var receiveState = (NetworkState)ar.AsyncState; try { var received = receiveState.Socket.EndReceive(ar); if (!receiveState.Socket.Connected || received == 0) { HeliosTrace.Instance.TcpInboundReceiveFailure(); var connection = SocketMap[receiveState.RemoteHost]; CloseConnection(connection); return; } receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, received); HeliosTrace.Instance.TcpInboundReceive(received); var adapter = SocketMap[receiveState.RemoteHost]; List <IByteBuf> decoded; adapter.Decoder.Decode(ConnectionAdapter, receiveState.Buffer, out decoded); foreach (var message in decoded) { var networkData = NetworkData.Create(receiveState.RemoteHost, message); ReceivedData(networkData, adapter); HeliosTrace.Instance.TcpInboundReceiveSuccess(); } //reuse the buffer if (receiveState.Buffer.ReadableBytes == 0) { receiveState.Buffer.SetIndex(0, 0); } else { receiveState.Buffer.CompactIfNecessary(); var postCompact = receiveState.Buffer; } //continue receiving in a loop receiveState.Socket.BeginReceive(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length, SocketFlags.None, ReceiveCallback, receiveState); } catch (SocketException ex) //node disconnected { if (SocketMap.ContainsKey(receiveState.RemoteHost)) { var connection = SocketMap[receiveState.RemoteHost]; CloseConnection(ex, connection); } HeliosTrace.Instance.TcpInboundReceiveFailure(); } catch (ObjectDisposedException ex) { if (SocketMap.ContainsKey(receiveState.RemoteHost)) { var connection = SocketMap[receiveState.RemoteHost]; CloseConnection(ex, connection); } HeliosTrace.Instance.TcpInboundReceiveFailure(); } catch (Exception ex) { if (SocketMap.ContainsKey(receiveState.RemoteHost)) { var connection = SocketMap[receiveState.RemoteHost]; OnErrorIfNotNull(ex, connection); } HeliosTrace.Instance.TcpInboundReceiveFailure(); } }
private void ReceiveCallback(IAsyncResult ar) { var receiveState = (NetworkState)ar.AsyncState; try { var received = receiveState.Socket.EndReceiveFrom(ar, ref RemoteEndPoint); if (received == 0) { if (SocketMap.ContainsKey(receiveState.RemoteHost)) { var connection = SocketMap[receiveState.RemoteHost]; CloseConnection(connection); } return; } var remoteAddress = (IPEndPoint)RemoteEndPoint; if (receiveState.RemoteHost.IsEmpty()) { receiveState.RemoteHost = remoteAddress.ToNode(TransportType.Udp); } ReactorResponseChannel adapter; if (SocketMap.ContainsKey(receiveState.RemoteHost)) { adapter = SocketMap[receiveState.RemoteHost]; } else { adapter = new ReactorProxyResponseChannel(this, receiveState.Socket, remoteAddress, EventLoop.Clone(ProxiesShareFiber)); ; SocketMap.Add(adapter.RemoteHost, adapter); NodeConnected(adapter.RemoteHost, adapter); } receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, received); List <IByteBuf> decoded; Decoder.Decode(ConnectionAdapter, receiveState.Buffer, out decoded); foreach (var message in decoded) { var networkData = NetworkData.Create(receiveState.RemoteHost, message); ReceivedData(networkData, adapter); } //reuse the buffer if (receiveState.Buffer.ReadableBytes == 0) { receiveState.Buffer.SetIndex(0, 0); } else { receiveState.Buffer.CompactIfNecessary(); } receiveState.Socket.BeginReceiveFrom(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length, SocketFlags.None, ref RemoteEndPoint, ReceiveCallback, receiveState); //receive more messages } catch (SocketException ex) //node disconnected { var connection = SocketMap[receiveState.RemoteHost]; CloseConnection(ex, connection); } catch (ObjectDisposedException ex) { if (SocketMap.ContainsKey(receiveState.RemoteHost)) { var connection = SocketMap[receiveState.RemoteHost]; CloseConnection(ex, connection); } } catch (Exception ex) { var connection = SocketMap[receiveState.RemoteHost]; OnErrorIfNotNull(ex, connection); } }
protected override void OnConnect(INode remoteAddress, IConnection responseChannel) { InitOutbound(responseChannel, remoteAddress, NetworkData.Create(Node.Empty(), new byte[0], 0)); }
protected virtual void ReceiveCallback(IAsyncResult ar) { var receiveState = (NetworkState)ar.AsyncState; try { var received = receiveState.Socket.EndReceive(ar); if (!receiveState.Socket.Connected || received == 0) { Receiving = false; HeliosTrace.Instance.TcpClientReceiveFailure(); Close(new HeliosConnectionException(ExceptionType.Closed)); return; } receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, received); HeliosTrace.Instance.TcpClientReceive(received); List <IByteBuf> decoded; Decoder.Decode(this, receiveState.Buffer, out decoded); foreach (var message in decoded) { var networkData = NetworkData.Create(receiveState.RemoteHost, message); InvokeReceiveIfNotNull(networkData); HeliosTrace.Instance.TcpClientReceiveSuccess(); } //reuse the buffer if (receiveState.Buffer.ReadableBytes == 0) { receiveState.Buffer.SetIndex(0, 0); } else { receiveState.Buffer.CompactIfNecessary(); } //continue receiving in a loop if (Receiving) { receiveState.Socket.BeginReceive(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length, SocketFlags.None, ReceiveCallback, receiveState); } } catch (SocketException ex) //typically means that the socket is now closed { HeliosTrace.Instance.TcpClientReceiveFailure(); Receiving = false; Close(new HeliosConnectionException(ExceptionType.Closed, ex)); } catch (ObjectDisposedException ex) //socket was already disposed { HeliosTrace.Instance.TcpClientReceiveFailure(); Receiving = false; InvokeDisconnectIfNotNull(RemoteHost, new HeliosConnectionException(ExceptionType.Closed, ex)); } catch (Exception ex) { HeliosTrace.Instance.TcpClientReceiveFailure(); InvokeErrorIfNotNull(ex); } }
public void Send(byte[] buffer, int index, int length, INode destination) { Send(NetworkData.Create(destination, buffer.Slice(index, length), length)); }