void ProcessReceivedData(SocketState connection, byte[] buffer, int bufferSize) { //Console.WriteLine("--Send:{0}, Packet: {1}", tunnel.Name, Encoding.UTF8.GetString(buffer.Take(bufferSize).ToArray())); byte[] sendBuffer = Packetizer.AssembleMessagePacket(buffer, bufferSize, false, null, null); connection.Peer.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None); }
private void OnDataReceived(IAsyncResult asyn) { SocketState connection = null; try { connection = (SocketState)asyn.AsyncState; connection.BytesReceived = connection.Socket.EndReceive(asyn); if (connection.BytesReceived == 0) { CleanupConnection(connection); return; } //Console.WriteLine("--Recv:{0}, Packet: {1}", tunnel.Name, Encoding.UTF8.GetString(connection.Buffer.Take(connection.BytesReceived).ToArray())); List <PacketEnvelope> envelopes = Packetizer.DissasemblePacketData(connection, false, null, null); foreach (var envelope in envelopes) { if (envelope.Label == null) { ProcessReceivedData(connection, envelope.Payload, envelope.Payload.Length); } else { ProcessPeerCommand(connection, envelope); } } WaitForData(connection); } catch (ObjectDisposedException) { if (connection != null) { CleanupConnection(connection); } return; } catch (SocketException) { if (connection != null) { CleanupConnection(connection); } return; } catch (Exception ex) { Singletons.EventLog.WriteEvent(new EventLogging.EventPayload { Severity = EventLogging.Severity.Error, CustomText = "Failed to process received data.", Exception = ex }); } }
private void SendPacketEnvelope(SocketState connection, PacketEnvelope envelope) { byte[] sendBuffer = Packetizer.AssembleMessagePacket(envelope, false, null, null); connection.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None); }
private void SendPacketEnvelope(SocketState connection, PacketEnvelope envelope, string encryptionKey, string salt) { byte[] sendBuffer = Packetizer.AssembleMessagePacket(envelope, true, encryptionKey, salt); connection.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None); }