public static void RunExample() { //Ensure we use the null serializer for unmanaged connections SendReceiveOptions options = new SendReceiveOptions<NullSerializer>(); //Setup listening for incoming unmanaged UDP broadcasts UDPConnectionListener listener = new UDPConnectionListener(options, ApplicationLayerProtocolStatus.Disabled, UDPOptions.None); Connection.StartListening(listener, new IPEndPoint(IPAddress.Any, 10000)); //Add a packet handler for unmanaged connections NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((packetHeader, connection, incomingBytes) => { Console.WriteLine("Received {0} bytes from {1}", incomingBytes.Length, connection.ConnectionInfo.RemoteEndPoint); }); //Generate some test data to broadcast byte[] dataToSend = new byte[] { 1, 2,3, 4 }; //Create an unmanaged packet manually and broadcast the test data //In future this part of the API could potentially be improved to make it clearer using(Packet sendPacket = new Packet("Unmanaged", dataToSend, options)) UDPConnection.SendObject<byte[]>(sendPacket, new IPEndPoint(IPAddress.Broadcast, 10000), options, ApplicationLayerProtocolStatus.Disabled); Console.WriteLine("Client done!"); Console.ReadKey(); }
public static void RunExample() { Console.WriteLine("Please select thrower or catcher mode:"); Console.WriteLine("1 - Thrower Mode (Sends data)"); Console.WriteLine("2 - Catcher Mode (Listens for data)"); //Read in user choice if (Console.ReadKey(true).Key == ConsoleKey.D1) throwerMode = true; else throwerMode = false; if (throwerMode) { var nullCompressionSRO = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), new List<DataProcessor>(), new Dictionary<string, string>()); //IPEndPoint catcherPoint = new IPEndPoint(IPAddress.Parse("131.111.73.200"), 10000); IPEndPoint catcherPoint = new IPEndPoint(IPAddress.Parse("172.24.252.32"), 10000); byte[] throwData = new byte[20*1024*1024]; //byte[] throwData = new byte[1024 * 50]; new Random().NextBytes(throwData); Task.Factory.StartNew(() => { while (true) { TCPConnection.GetConnection(new ConnectionInfo(catcherPoint)).SendObject("ThrowData", throwData, nullCompressionSRO); } }); Console.WriteLine("Let the throwing begin!\n"); while (true) { Console.WriteLine("IN - {0} - Instance Load = {1}, 5 sec load= {2}, 15 sec load= {3}", DateTime.Now.ToLongTimeString(), HostInfo.IP.CurrentNetworkLoadIncoming.ToString("0.000"), HostInfo.IP.AverageNetworkLoadIncoming(5).ToString("0.000"), HostInfo.IP.AverageNetworkLoadIncoming(15).ToString("0.000")); Console.WriteLine("OUT - {0} - Instance Load = {1}, 5 sec load= {2}, 15 sec load= {3}", DateTime.Now.ToLongTimeString(), HostInfo.IP.CurrentNetworkLoadOutgoing.ToString("0.000"), HostInfo.IP.AverageNetworkLoadOutgoing(5).ToString("0.000"), HostInfo.IP.AverageNetworkLoadOutgoing(15).ToString("0.000")); Thread.Sleep(1000); } } else { NetworkComms.AppendGlobalIncomingPacketHandler<byte[]>("ThrowData", (packetHeader, connection, data) => { Console.WriteLine("{0} - Caught {1} bytes thrown by {2}.", DateTime.Now.ToLongTimeString(), data.Length, connection.ConnectionInfo); }); //Start listening for TCP connections //We want to select a random port on all available adaptors so provide //an IPEndPoint using IPAddress.Any and port 0. Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0)); Console.WriteLine("Let the catching begin, on port " + ((IPEndPoint)Connection.ExistingLocalListenEndPoints(ConnectionType.TCP).First()).Port + "!\n"); while (true) { Console.WriteLine("IN - {0} - Instance Load = {1}, 5 sec load= {2}, 15 sec load= {3}", DateTime.Now.ToLongTimeString(), HostInfo.IP.CurrentNetworkLoadIncoming.ToString("0.000"), HostInfo.IP.AverageNetworkLoadIncoming(5).ToString("0.000"), HostInfo.IP.AverageNetworkLoadIncoming(15).ToString("0.000")); Console.WriteLine("OUT - {0} - Instance Load = {1}, 5 sec load= {2}, 15 sec load= {3}", DateTime.Now.ToLongTimeString(), HostInfo.IP.CurrentNetworkLoadOutgoing.ToString("0.000"), HostInfo.IP.AverageNetworkLoadOutgoing(5).ToString("0.000"), HostInfo.IP.AverageNetworkLoadOutgoing(15).ToString("0.000")); Thread.Sleep(10000); } } }
/// <summary> /// Run example /// </summary> public static void RunExample() { SendReceiveOptions nullCompressionSRO = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null); NetworkComms.DefaultSendReceiveOptions = nullCompressionSRO; //We need to define what happens when packets are received. //To do this we add an incoming packet handler for a 'Message' packet type. // //We will define what we want the handler to do inline by using a lambda expression //http://msdn.microsoft.com/en-us/library/bb397687.aspx. //We could also just point the AppendGlobalIncomingPacketHandler method //to a standard method (See AdvancedSend example) // //This handler will convert the incoming raw bytes into a string (this is what //the <string> bit means) and then write that string to the local console window. NetworkComms.AppendGlobalIncomingPacketHandler<PingRequestReturnDC>("Message", (packetHeader, connection, incomingString) => { Console.WriteLine("\n ... Incoming message from " + connection.ToString() + " saying '" + incomingString.ClientID + "'-'" + incomingString .PingID+ "'."); }); //Start listening for incoming 'TCP' connections. //We want to select a random port on all available adaptors so provide //an IPEndPoint using IPAddress.Any and port 0. //See also UDPConnection.StartListening() Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0)); //Print the IP addresses and ports we are listening on to make sure everything //worked as expected. Console.WriteLine("Listening for messages on:"); foreach (System.Net.IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP)) Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); //We loop here to allow any number of test messages to be sent and received while (true) { //Request a message to send somewhere Console.WriteLine("\nPlease enter your message and press enter (Type 'exit' to quit):"); string stringToSend = Console.ReadLine(); //If the user has typed exit then we leave our loop and end the example if (stringToSend == "exit") break; else { //Once we have a message we need to know where to send it //We have created a small wrapper class to help keep things clean here ConnectionInfo targetServerConnectionInfo; ExampleHelper.GetServerDetails(out targetServerConnectionInfo); PingRequestReturnDC test = new PingRequestReturnDC(0,0); //There are loads of ways of sending data (see AdvancedSend example for more) //but the most simple, which we use here, just uses an IP address (string) and port (integer) //We pull these values out of the ConnectionInfo object we got above and voila! NetworkComms.SendObject("Message", ((System.Net.IPEndPoint)targetServerConnectionInfo.RemoteEndPoint).Address.ToString(), ((System.Net.IPEndPoint)targetServerConnectionInfo.RemoteEndPoint).Port, test); } } //We should always call shutdown on comms if we have used it NetworkComms.Shutdown(); }
/// <summary> /// Run example /// </summary> public static void RunExample() { //NetworkComms.PacketConfirmationTimeoutMS = 1000000; //var processors = new List<DataProcessor> { DPSManager.GetDataProcessor<SharpZipLibCompressor.SharpZipLibGzipCompressor>(), DPSManager.GetDataProcessor<RijndaelPSKEncrypter>() }; var processors = new List<DataProcessor> { DPSManager.GetDataProcessor<RijndaelPSKEncrypter>() }; var dataOptions = new Dictionary<string, string>(); RijndaelPSKEncrypter.AddPasswordToOptions(dataOptions, "oj1N0bcfsjtQxfgRKT7B"); var options = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), processors, dataOptions) { UseNestedPacket = false }; //Uncomment to make it work //RijndaelPSKEncrypter.AddPasswordToOptions(NetworkComms.DefaultSendReceiveOptions.Options, "oj1N0bcfsjtQxfgRKT7B"); var points = new List<IPEndPoint>(); var localIPs = HostInfo.IP.FilteredLocalAddresses(); foreach (var ip in localIPs) { var listener = new TCPConnectionListener(options, ApplicationLayerProtocolStatus.Enabled); listener.AppendIncomingPacketHandler<string>("Kill all humans", (header, con, customObject) => { Console.WriteLine("\nReceived custom protobuf object from " + con); }, options); Connection.StartListening(listener, new IPEndPoint(ip, 0)); points.Add(listener.LocalListenEndPoint as IPEndPoint); } Console.WriteLine("Listening on:"); foreach (var endpoint in points) Console.WriteLine("{0}:{1}", endpoint.Address.ToString(), endpoint.Port.ToString()); var point = points.First(); //IPEndPoint point = IPTools.ParseEndPointFromString("::1:46112"); var connectionInfo = new ConnectionInfo(point); var connection = TCPConnection.GetConnection(connectionInfo); options.ReceiveConfirmationRequired = true; connection.SendObject("Kill all humans", "Bite my shiny metal ass", options); Console.ReadLine(); }
/// <summary> /// Run example /// </summary> public static void RunExample() { //Select mode Console.WriteLine("SpeedTest Example ...\n"); Console.WriteLine("Please select host or peer mode:"); Console.WriteLine("1 - Host Mode (Catches Data)"); Console.WriteLine("2 - Peer Mode (Sends Data)"); //Read in user choice if (Console.ReadKey(true).Key == ConsoleKey.D1) hostMode = true; else hostMode = false; if (hostMode) { //Prepare DFS in host mode #region ServerMode Console.WriteLine("\n ... host mode selected."); NetworkComms.ConnectionEstablishShutdownDelegate clientEstablishDelegate = (connection) => { Console.WriteLine("Client " + connection.ConnectionInfo + " connected."); }; NetworkComms.ConnectionEstablishShutdownDelegate clientShutdownDelegate = (connection) => { Console.WriteLine("Client " + connection.ConnectionInfo + " disconnected."); }; NetworkComms.PacketHandlerCallBackDelegate<byte[]> IncomingDataDelegate = (packetHeader, connection, incomingObject) => { Console.WriteLine("Speed bytes received from " + connection.ConnectionInfo +"."); }; NetworkComms.AppendGlobalConnectionEstablishHandler(clientEstablishDelegate); NetworkComms.AppendGlobalConnectionCloseHandler(clientShutdownDelegate); NetworkComms.AppendGlobalIncomingPacketHandler("SpeedData", IncomingDataDelegate); //Start listening for TCP connections //We want to select a random port on all available adaptors so provide //an IPEndPoint using IPAddress.Any and port 0. Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0)); Console.WriteLine("\nListening for incoming connections on:"); foreach (IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP)) Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); Console.WriteLine("\nIdentifier - {0}", NetworkComms.NetworkIdentifier); Console.WriteLine("\nPress 'q' to close host.\n"); while (true) { ConsoleKeyInfo pressedKey = Console.ReadKey(true); if (pressedKey.Modifiers != ConsoleModifiers.Control && pressedKey.Key == ConsoleKey.Q) { Console.WriteLine("Closing host."); break; } } #endregion } else if (!hostMode) { //Prepare DFS in peer mode #region PeerMode Console.WriteLine("\n ... peer mode selected."); Console.WriteLine("\nPlease enter how large the test data packet should be in MB and press return (larger is more accurate), e.g. 1024:"); int numberMegsToCreate = int.Parse(Console.ReadLine()); //Fill a byte[] with random data DateTime startTime = DateTime.Now; Random randGen = new Random(); byte[] someRandomData = new byte[numberMegsToCreate * 1024 * 1024]; randGen.NextBytes(someRandomData); Console.WriteLine("\nTest speed data created. Using {0}MB.\n", numberMegsToCreate); NetworkComms.PacketConfirmationTimeoutMS = 20000; ConnectionInfo serverConnectionInfo = ExampleHelper.GetServerDetails(); Console.WriteLine("\nIdentifier - {0}", NetworkComms.NetworkIdentifier); SendReceiveOptions nullCompressionSRO = new SendReceiveOptions<NullSerializer>(); //Add options which will require receive confirmations and also include packet construction time //in the packet header. nullCompressionSRO.Options.Add("ReceiveConfirmationRequired", ""); nullCompressionSRO.Options.Add("IncludePacketConstructionTime", ""); TCPConnection serverConnection = TCPConnection.GetConnection(serverConnectionInfo); Stopwatch timer = new Stopwatch(); while (true) { timer.Reset(); timer.Start(); serverConnection.SendObject("SpeedData", someRandomData, nullCompressionSRO); timer.Stop(); Console.WriteLine("SpeedData sent successfully with receive confirmation in {0}secs. Corresponds to {1}MB/s", (timer.ElapsedMilliseconds / 1000.0).ToString("0.00"), (numberMegsToCreate / (timer.ElapsedMilliseconds / 1000.0)).ToString("0.00")); } #endregion } NetworkComms.Shutdown(); }
/// <summary> /// Run example /// </summary> public static void RunExample() { NetworkComms.ConnectionEstablishTimeoutMS = int.MaxValue; SendReceiveOptions nullCompressionSRO = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null); NetworkComms.DefaultSendReceiveOptions = nullCompressionSRO; //We need to define what happens when packets are received. //To do this we add an incoming packet handler for a 'Message' packet type. // //We will define what we want the handler to do inline by using a lambda expression //http://msdn.microsoft.com/en-us/library/bb397687.aspx. //We could also just point the AppendGlobalIncomingPacketHandler method //to a standard method (See AdvancedSend example) // //This handler will convert the incoming raw bytes into a string (this is what //the <string> bit means) and then write that string to the local console window. NetworkComms.AppendGlobalIncomingPacketHandler<string>("Message", (packetHeader, connection, incomingString) => { Console.WriteLine("\n ... Incoming message from " + connection.ToString() + " saying '" + incomingString + "'."); }); //Start listenning BluetoothRadio defaultRadio = BluetoothRadio.PrimaryRadio; defaultRadio.Mode = RadioMode.Discoverable; Connection.StartListening(ConnectionType.Bluetooth, new BluetoothEndPoint(defaultRadio.LocalAddress, ServiceGUID), true); //Print the address we are listening on to make sure everything //worked as expected. Console.WriteLine("Listening for messages on:"); foreach (var localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.Bluetooth)) Console.WriteLine("{0}", localEndPoint); PeerDiscovery.EnableDiscoverable(PeerDiscovery.DiscoveryMethod.BluetoothSDP); //We loop here to allow any number of test messages to be sent and received while (true) { //Request a message to send somewhere Console.WriteLine("\nPlease enter your message and press enter (Type 'exit' to quit):"); string stringToSend = Console.ReadLine(); //If the user has typed exit then we leave our loop and end the example if (stringToSend == "exit") break; else { //Once we have a message we need to know where to send it //We have created a small wrapper class to help keep things clean here var endpoints = PeerDiscovery.DiscoverPeers(PeerDiscovery.DiscoveryMethod.BluetoothSDP, 1000); ConnectionInfo targetServerConnectionInfo = new ConnectionInfo(new BluetoothEndPoint(new BluetoothAddress(0xE0B9A5FB552BL), ServiceGUID)); //There are loads of ways of sending data (see AdvancedSend example for more) //but the most simple, which we use here, just uses an IP address (string) and port (integer) //We pull these values out of the ConnectionInfo object we got above and voila! var connection = BluetoothConnection.GetConnection(targetServerConnectionInfo); connection.SendObject("Message", "Hello world"); } } //We should always call shutdown on comms if we have used it NetworkComms.Shutdown(); }
static DFS() { MinTargetLocalPort = 10000; MaxTargetLocalPort = 10999; //BuildTaskFactory = new TaskFactory(new LimitedParallelismTaskScheduler(MaxConcurrentLocalItemBuild)); BuildTaskFactory = new TaskFactory(); nullCompressionSRO = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), new List<DataProcessor>(), new Dictionary<string, string>()); highPrioReceiveSRO = (SendReceiveOptions)NetworkComms.DefaultSendReceiveOptions.Clone(); highPrioReceiveSRO.Options.Add("ReceiveHandlePriority", Enum.GetName(typeof(ThreadPriority), ThreadPriority.AboveNormal)); }
/// <summary> /// TCP - Received by this DFS if a server is telling this instance to build a local file /// </summary> /// <param name="packetHeader"></param> /// <param name="connection"></param> /// <param name="assemblyConfig"></param> private static void IncomingLocalItemBuild(PacketHeader packetHeader, Connection connection, ItemAssemblyConfig assemblyConfig) { //We start the build in the DFS task factory as it will be a long lived task //BuildTaskFactory.StartNew(() => Action assembleAction = new Action(() => { DistributedItem newItem = null; byte[] itemBytes = null; try { if (assemblyConfig == null) throw new NullReferenceException("AssemblyConfig should not be null."); if (DFS.loggingEnabled) DFS._DFSLogger.Debug("IncomingLocalItemBuild from " + connection + " for item " + assemblyConfig.CompleteDataCheckSum + "."); //We check to see if we already have the necessary file locally lock (globalDFSLocker) { if (swarmedItemsDict.ContainsKey(assemblyConfig.CompleteDataCheckSum)) { if (swarmedItemsDict[assemblyConfig.CompleteDataCheckSum].Data.ItemBytesLength != assemblyConfig.TotalItemSizeInBytes) throw new Exception("Possible MD5 conflict detected."); else newItem = swarmedItemsDict[assemblyConfig.CompleteDataCheckSum]; } else { newItem = new DistributedItem(assemblyConfig); swarmedItemsDict.Add(assemblyConfig.CompleteDataCheckSum, newItem); } } //Ensure all possible local listeners are added here List<ConnectionInfo> seedConnectionInfoList = (from current in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP) select new ConnectionInfo(ConnectionType.TCP, NetworkComms.NetworkIdentifier, current, true)).ToList(); foreach (ConnectionInfo info in seedConnectionInfoList) newItem.SwarmChunkAvailability.AddOrUpdateCachedPeerChunkFlags(info, newItem.SwarmChunkAvailability.PeerChunkAvailability(NetworkComms.NetworkIdentifier), newItem.SwarmChunkAvailability.PeerIsSuperPeer(NetworkComms.NetworkIdentifier), false); //Build the item from the swarm //If the item is already complete this will return immediately newItem.AssembleItem((int)(ItemBuildTimeoutSecsPerMB * (assemblyConfig.TotalItemSizeInBytes / (1024.0 * 1024.0)))); //Once complete we pass the item bytes back into network comms //If an exception is thrown we will probably not call this method, timeouts in other areas should then handle and can restart the build. if (newItem.LocalItemComplete() && assemblyConfig.CompletedPacketType != "") { if (DFS.loggingEnabled) DFS._DFSLogger.Debug("IncomingLocalItemBuild completed for item with MD5 " + assemblyConfig.CompleteDataCheckSum + ". Item build target is " + assemblyConfig.ItemBuildMode + "."); itemBytes = newItem.GetCompletedItemBytes(); } else if (assemblyConfig.CompletedPacketType != "") RemoveItem(assemblyConfig.CompleteDataCheckSum); if (DFS.loggingEnabled) { Exception exceptionToLogWith = new Exception("Build completed successfully. Logging was enabled so saving build log."); string fileName = "DFSItemBuildLog_" + newItem.ItemIdentifier + "_" + NetworkComms.NetworkIdentifier; if (newItem != null) LogTools.LogException(exceptionToLogWith, fileName, newItem.BuildLog().Aggregate(Environment.NewLine, (p, q) => { return p + Environment.NewLine + q; })); else LogTools.LogException(exceptionToLogWith, fileName, "newItem==null so no build log was available."); } } catch (ObjectDisposedException) { //The item was closed during assemble, no need to log an errors here RemoveItem(assemblyConfig.CompleteDataCheckSum); } catch (CommsException e) { //Crap an error has happened, let people know we probably don't have a good file RemoveItem(assemblyConfig.CompleteDataCheckSum); //connection.CloseConnection(true, 30); //LogTools.LogException(e, "CommsError_IncomingLocalItemBuild"); if (newItem != null) LogTools.LogException(e, "Error_IncomingLocalItemBuildComms", newItem.BuildLog().Aggregate(Environment.NewLine, (p, q) => { return p + Environment.NewLine + q; })); else LogTools.LogException(e, "Error_IncomingLocalItemBuildComms", "newItem==null so no build log was available."); } catch (Exception e) { //Crap an error has happened, let people know we probably don't have a good file RemoveItem(assemblyConfig.CompleteDataCheckSum); //connection.CloseConnection(true, 31); if (newItem != null) LogTools.LogException(e, "Error_IncomingLocalItemBuild", newItem.BuildLog().Aggregate(Environment.NewLine, (p, q) => { return p + Environment.NewLine + q; })); else LogTools.LogException(e, "Error_IncomingLocalItemBuild", "newItem==null so no build log was available."); } //finally //{ //Putting any code here appears to cause a sigsegv fault on leaving the finally in mono //Just moved the code out to below as it makes no difference //} //Regardless of if the item completed we call the necessary packet handlers //If there was a build error we just pass null data to the handlers so that the errors can get called up the relevant stack traces. try { PacketHeader itemPacketHeader = new PacketHeader(assemblyConfig.CompletedPacketType, newItem == null ? 0 : newItem.Data.ItemBytesLength); //We set the item checksum so that the entire distributed item can be easily retrieved later itemPacketHeader.SetOption(PacketHeaderStringItems.PacketIdentifier, newItem == null ? "" : newItem.ItemTypeStr + "|" + newItem.ItemIdentifier + "|" + newItem.Data.CompleteDataCheckSum); var dataStream = (itemBytes == null ? new MemoryStream(new byte[0], 0, 0, false, true) : new MemoryStream(itemBytes, 0, itemBytes.Length, false, true)); var sendRecieveOptions = new SendReceiveOptions<NullSerializer>(new Dictionary<string, string>()); NetworkComms.TriggerAllPacketHandlers(itemPacketHeader, connection, dataStream, sendRecieveOptions); } catch (Exception ex) { LogTools.LogException(ex, "Error_IncomingLocalItemBuildFinal"); } }); if (BuildTaskFactory == null) LogTools.LogException(new NullReferenceException("BuildTaskFactory is null in IncomingLocalItemBuild"), "IncomingLocalBuildError"); else //Thread buildThread = new Thread(buildAction); //buildThread.Name = "DFS_" + assemblyConfig.ItemIdentifier + "_Build"; //buildThread.Start(); BuildTaskFactory.StartNew(assembleAction); }
/// <summary> /// Determines whether the supplied <see cref="SendReceiveOptions"/> is compatible, from a serialization point of view, with this instance /// </summary> /// <param name="options">The <see cref="SendReceiveOptions"/> to compare against</param> /// <returns>True if the options are compatible, false otherwise</returns> /// <remarks>Two <see cref="SendReceiveOptions"/> instances will be compatible if they use the same <see cref="DPSBase.DataSerializer"/> and the same set of <see cref="DPSBase.DataProcessor"/>s</remarks> public bool OptionsCompatible(SendReceiveOptions options) { if (options == null) throw new ArgumentNullException("options", "Provided SendReceiveOptions cannot be null."); bool equal = options.DataSerializer == DataSerializer; for (int i = 0; i < options.DataProcessors.Count; i++) equal &= options.DataProcessors[i] == DataProcessors[i]; return equal; }
/// <summary> /// Creates a new packetHeader /// </summary> /// <param name="packetTypeStr">The packet type to be used.</param> /// <param name="payloadPacketSize">The size on bytes of the payload</param> /// <param name="sendReceiveOptions">Send receive options which may contain header relevant options.</param> /// <param name="requestedReturnPacketTypeStr">An optional field representing the expected return packet type</param> /// <param name="checkSumHash">An optional field representing the payload checksum</param> public PacketHeader(string packetTypeStr, long payloadPacketSize, SendReceiveOptions sendReceiveOptions = null, string requestedReturnPacketTypeStr = null, string checkSumHash = null) { if (packetTypeStr == requestedReturnPacketTypeStr) throw new ArgumentException("The provided packetTypeStr and requestedReturnPacketTypeStr parameters must be different."); longItems = new Dictionary<PacketHeaderLongItems, long>(); stringItems = new Dictionary<PacketHeaderStringItems, string>(); stringItems.Add(PacketHeaderStringItems.PacketType, packetTypeStr); longItems.Add(PacketHeaderLongItems.TotalPayloadSize, payloadPacketSize); if (payloadPacketSize < 0) throw new Exception("payloadPacketSize can not be less than 0."); if (requestedReturnPacketTypeStr != null) stringItems.Add(PacketHeaderStringItems.RequestedReturnPacketType, requestedReturnPacketTypeStr); if (checkSumHash != null) stringItems.Add(PacketHeaderStringItems.CheckSumHash, checkSumHash); if (sendReceiveOptions != null) { if (sendReceiveOptions.Options.ContainsKey("ReceiveConfirmationRequired")) stringItems.Add(PacketHeaderStringItems.ReceiveConfirmationRequired, ""); if (sendReceiveOptions.Options.ContainsKey("IncludePacketConstructionTime")) longItems.Add(PacketHeaderLongItems.PacketCreationTime, DateTime.Now.Ticks); } }
/// <summary> /// Constructor used for deserialisation /// </summary> /// <param name="packetHeaderStream"></param> /// <param name="headerSendReceiveOptions"></param> internal PacketHeader(MemoryStream packetHeaderStream, SendReceiveOptions headerSendReceiveOptions) { try { if (packetHeaderStream == null) throw new ArgumentNullException("packetData", "Provided MemoryStream parameter cannot be null."); if (headerSendReceiveOptions == null) throw new ArgumentNullException("sendReceiveOptions", "Provided SendReceiveOptions parameter cannot be null."); if (packetHeaderStream.Length == 0) throw new SerialisationException("Attempted to create packetHeader using byte[0]."); PacketHeader tempObject = headerSendReceiveOptions.DataSerializer.DeserialiseDataObject<PacketHeader>(packetHeaderStream, headerSendReceiveOptions.DataProcessors, headerSendReceiveOptions.Options); if (tempObject == null || !tempObject.longItems.ContainsKey(PacketHeaderLongItems.TotalPayloadSize) || !tempObject.stringItems.ContainsKey(PacketHeaderStringItems.PacketType)) throw new SerialisationException("Failed to deserialize a valid packet header. Deserialized header result was null or did not contain the compulsory fields, TotalPayloadSize and PacketType."); else { stringItems = new Dictionary<PacketHeaderStringItems, string>(); foreach (var pair in tempObject.stringItems) stringItems.Add(pair.Key, pair.Value); longItems = new Dictionary<PacketHeaderLongItems, long>(); foreach (var pair in tempObject.longItems) longItems.Add(pair.Key, pair.Value); } } catch (Exception ex) { NetworkCommsDotNet.Tools.LogTools.LogException(ex, "PacketHeaderDeserialisationError", "The header data follows:" + BitConverter.ToString(packetHeaderStream.ToArray())); throw new SerialisationException("Error deserializing packetHeader. " + ex.ToString()); } }
public static void RunExample() { Console.WriteLine("Please select mode:"); Console.WriteLine("1 - Server (Listens for connections)"); Console.WriteLine("2 - Client (Creates connections to server)"); sendReceiveOptions = new SendReceiveOptions<NullSerializer>(); //RijndaelPSKEncrypter.AddPasswordToOptions(sendReceiveOptions.Options, "test"); //sendReceiveOptions.DataProcessors.Add(DPSManager.GetDataProcessor<RijndaelPSKEncrypter>()); //NetworkComms.ConnectionEstablishTimeoutMS = 600000; NetworkComms.ConnectionListenModeUseSync = true; X509Certificate cert = new X509Certificate2("testCertificate.pfx"); sslOptions = new SSLOptions(cert, true); //Read in user choice if (Console.ReadKey(true).Key == ConsoleKey.D1) serverMode = true; else serverMode = false; UDPConnection.DefaultUDPOptions = UDPOptions.None; IPAddress localIPAddress = IPAddress.Parse("::1"); packetTypeStr = "Unmanaged"; if (serverMode) { //NetworkComms.DOSProtection.Enabled = true; //NetworkComms.DisableLogging(); //Listen for connections int totalNumberOfListenPorts = 500; int portDivisor = 0; if ((mode & TestMode.TCP_Managed) == TestMode.TCP_Managed) portDivisor++; if ((mode & TestMode.TCP_Unmanaged) == TestMode.TCP_Unmanaged) portDivisor++; if ((mode & TestMode.UDP_Managed) == TestMode.UDP_Managed) portDivisor++; if ((mode & TestMode.UDP_Unmanaged) == TestMode.UDP_Unmanaged) portDivisor++; if ((mode & TestMode.TCPSSL_Managed) == TestMode.TCPSSL_Managed) portDivisor++; List<EndPoint> localIPEndPoints = new List<EndPoint>(); List<ConnectionListenerBase> listeners = new List<ConnectionListenerBase>(); for (int i = 0; i < totalNumberOfListenPorts/portDivisor; i++) { if ((mode & TestMode.TCP_Managed) == TestMode.TCP_Managed) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 10000 + i)); listeners.Add(new TCPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Enabled)); } if ((mode & TestMode.TCP_Unmanaged) == TestMode.TCP_Unmanaged) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 20000 + i)); listeners.Add(new TCPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Disabled)); } if ((mode & TestMode.UDP_Managed) == TestMode.UDP_Managed) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 30000 + i)); listeners.Add(new UDPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Enabled, UDPConnection.DefaultUDPOptions)); } if ((mode & TestMode.UDP_Unmanaged) == TestMode.UDP_Unmanaged) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 40000 + i)); listeners.Add(new UDPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Disabled, UDPConnection.DefaultUDPOptions)); } if ((mode & TestMode.TCPSSL_Managed) == TestMode.TCPSSL_Managed) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 50000 + i)); listeners.Add(new TCPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Enabled, sslOptions)); } } Connection.StartListening(listeners, localIPEndPoints, true); object locker = new object(); int messageCount = 0; long totalBytesReceived = 0; int tcpFragmentationConcatCount = 0; int connectionEstablishCount = 0; int connectionCloseCount = 0; //List<string> packetSequenceNumbers = new List<string>(); NetworkComms.AppendGlobalIncomingPacketHandler<byte[]>(packetTypeStr, (header, connection, data) => { lock (locker) { //long seqNumber = header.GetOption(PacketHeaderLongItems.PacketSequenceNumber); //packetSequenceNumbers.Add(connection.ToString() + "," + seqNumber); //Increment a global counter messageCount++; if (data.Length != testDataSize) tcpFragmentationConcatCount++; totalBytesReceived += data.Length; } }, sendReceiveOptions); //Establish handler NetworkComms.AppendGlobalConnectionEstablishHandler((connection) => { lock (locker) connectionEstablishCount++; }); //Close handler NetworkComms.AppendGlobalConnectionCloseHandler((connection) => { lock (locker) connectionCloseCount++; }); //Save the ports list out to disk using (StreamWriter sw = new StreamWriter("TCPServerPorts.txt", false)) { List<EndPoint> localListenEndPoints = Connection.ExistingLocalListenEndPoints(ConnectionType.TCP); foreach (IPEndPoint endPoint in localListenEndPoints) { if (Connection.ExistingLocalListeners<TCPConnectionListener>(endPoint)[0].ApplicationLayerProtocol == ApplicationLayerProtocolStatus.Enabled) sw.WriteLine("T-"+endPoint.Address.ToString() + "-" + endPoint.Port); else sw.WriteLine("F-" + endPoint.Address.ToString() + "-" + endPoint.Port); } } //Save the ports list out to disk using (StreamWriter sw = new StreamWriter("UDPServerPorts.txt", false)) { List<EndPoint> localListenEndPoints = Connection.ExistingLocalListenEndPoints(ConnectionType.UDP); foreach (IPEndPoint endPoint in localListenEndPoints) { if (Connection.ExistingLocalListeners<UDPConnectionListener>(endPoint)[0].ApplicationLayerProtocol == ApplicationLayerProtocolStatus.Enabled) sw.WriteLine("T-" + endPoint.Address.ToString() + "-" + endPoint.Port); else sw.WriteLine("F-" + endPoint.Address.ToString() + "-" + endPoint.Port); } } Console.WriteLine("\nSelected mode = {0}, UDPOptions = {1}", mode, UDPConnection.DefaultUDPOptions); Console.WriteLine("Connection close after send = {0}", (closeConnectionAfterSend? "TRUE" : "FALSE")); Console.WriteLine("\nListening for incoming connections on {0} ports. Press 'c' key to see message count.", totalNumberOfListenPorts); while (true) { ConsoleKeyInfo key = Console.ReadKey(true); if (key.KeyChar == 'c') { Console.WriteLine("#Handlers={0}, #Data={2}, #TCPFragConcat={1}, #Establish={3}, #Close={4}. Press 'c' to refresh message count, any other key to quit.", messageCount, tcpFragmentationConcatCount, totalBytesReceived / (double)testDataSize, connectionEstablishCount, connectionCloseCount); //using (StreamWriter sw = new StreamWriter("seqNumbers.txt", false)) //{ // List<string> copy = packetSequenceNumbers.ToList(); // foreach (string line in copy) // sw.WriteLine(line); //} } else break; } } else { //NetworkComms.DisableLogging(); //Load server port list string[] tcpServerPortList = File.ReadAllLines("TCPServerPorts.txt"); TCPServerEndPoints = new Dictionary<IPEndPoint, ApplicationLayerProtocolStatus>(); foreach (string current in tcpServerPortList) TCPServerEndPoints.Add(new IPEndPoint(IPAddress.Parse(current.Split('-')[1]), int.Parse(current.Split('-')[2])), (current.Substring(0, 1) == "T" ? ApplicationLayerProtocolStatus.Enabled : ApplicationLayerProtocolStatus.Disabled)); TCPServerEndPointsKeys = TCPServerEndPoints.Keys.ToList(); string[] udpServerPortList = File.ReadAllLines("UDPServerPorts.txt"); UDPServerEndPoints = new Dictionary<IPEndPoint, ApplicationLayerProtocolStatus>(); foreach (string current in udpServerPortList) UDPServerEndPoints.Add(new IPEndPoint(IPAddress.Parse(current.Split('-')[1]), int.Parse(current.Split('-')[2])), (current.Substring(0, 1) == "T" ? ApplicationLayerProtocolStatus.Enabled : ApplicationLayerProtocolStatus.Disabled)); //UDPConnectionListener udpListener = new UDPConnectionListener(sendReceiveOptions, // ((UDPConnection.DefaultUDPOptions & UDPOptions.Handshake) != UDPOptions.Handshake ? ApplicationLayerProtocolStatus.Disabled : ApplicationLayerProtocolStatus.Enabled), // UDPConnection.DefaultUDPOptions); //Connection.StartListening(udpListener, new IPEndPoint(localIPAddress, 10010), true); Console.WriteLine("Listening for connections on:"); foreach (System.Net.IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.UDP)) Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); UDPServerEndPointsKeys = UDPServerEndPoints.Keys.ToList(); Console.WriteLine("\nLoaded {0} TCP & {1} UDP server ports. Press any key to start the hammer!", TCPServerEndPoints.Count, UDPServerEndPointsKeys.Count); Console.ReadKey(true); Console.WriteLine("It's hammer time ..."); clientHammerData = new byte[testDataSize]; //Lets start by making as many connections as possible, go to absolute maximum performance ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = 8; Stopwatch timer = new Stopwatch(); timer.Start(); Parallel.For(0, connectionHammerExecCount, options, ConnectionHammer); timer.Stop(); Console.WriteLine("\nCompleted {0} connections in {1} secs. {2}ms per connection. {3} exceptions. Press any key to quit.", connectionHammerExecCount * connectionsPerHammer, (timer.ElapsedMilliseconds / 1000.0).ToString("0.00"), ((double)timer.ElapsedMilliseconds / (connectionHammerExecCount * connectionsPerHammer)).ToString("0.00"), exceptionCount); Console.ReadKey(true); } NetworkComms.Shutdown(); }
/// <summary> /// Run the AdvancedSend example. /// </summary> public static void RunExample() { Console.WriteLine("Unmanaged Connection Example ...\n"); //***************************************************************// // Start of interesting stuff // //***************************************************************// Console.WriteLine("\nNOTE: From this point on make sure both clients are configured in the same way if you want the example to work."); Console.WriteLine("\nIMPORTANT!! - Many of the features offered by NetworkComms.Net rely on managed connections, "+ "i.e. those which enable the custom ApplicationLayerProtocol. If you use unmanaged connections, i.e. where the custom "+ "application protocol has been disabled, you must take into account TCP packet fragmentation and concatenation, "+ "correctly handling it, for all circumstances."); //Choose between unmanaged TCP or UDP SelectConnectionType(); //Add a packet handler for dealing with incoming unmanaged data NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) => { Console.WriteLine("\nReceived unmanaged byte[] from " + connection.ToString()); for (int i = 0; i < array.Length; i++) Console.WriteLine(i.ToString() + " - " + array[i].ToString()); }); //Create suitable send receive options for use with unmanaged connections SendReceiveOptions optionsToUse = new SendReceiveOptions<NullSerializer>(); //Get the local IPEndPoints we intend to listen on //The port provided is '0' meaning select a random port. List<IPEndPoint> localIPEndPoints = (from current in HostInfo.IP.FilteredLocalAddresses() select new IPEndPoint(current, 0)).ToList(); //Create suitable listeners depending on the desired connectionType List<ConnectionListenerBase> listeners; if (connectionTypeToUse == ConnectionType.TCP) { //For each localIPEndPoint get a TCP listener //We need to set the ApplicationLayerProtocolStatus to Disabled listeners = (from current in localIPEndPoints select (ConnectionListenerBase)new TCPConnectionListener(optionsToUse, ApplicationLayerProtocolStatus.Disabled)).ToList(); } else { //We need to set the ApplicationLayerProtocolStatus to Disabled listeners = (from current in localIPEndPoints select (ConnectionListenerBase)new UDPConnectionListener(optionsToUse, ApplicationLayerProtocolStatus.Disabled, UDPConnection.DefaultUDPOptions)).ToList(); } //Start listening Connection.StartListening(listeners, localIPEndPoints, true); //***************************************************************// // End of interesting stuff // //***************************************************************// Console.WriteLine("Listening for incoming byte[] on:"); List<EndPoint> localListeningEndPoints = Connection.ExistingLocalListenEndPoints(connectionTypeToUse); foreach (IPEndPoint localEndPoint in localListeningEndPoints) Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); Console.WriteLine("\nPress any key if you want to send data from this client. Press q to quit."); while (true) { //Wait for user to press something before sending anything from this end var keyContinue = Console.ReadKey(true); if (keyContinue.Key == ConsoleKey.Q) break; //Create the send object based on user input byteDataToSend = CreateSendArray(); //Get remote endpoint address //Expecting user to enter IP address as 192.168.0.1:4000 ConnectionInfo connectionInfo = ExampleHelper.GetServerDetails(ApplicationLayerProtocolStatus.Disabled); //***************************************************************// // Start of interesting stuff // //***************************************************************// Connection connectionToUse; //Create the connection if (connectionTypeToUse == ConnectionType.TCP) connectionToUse = TCPConnection.GetConnection(connectionInfo, optionsToUse); else connectionToUse = UDPConnection.GetConnection(connectionInfo, UDPOptions.None, optionsToUse); //Send the object connectionToUse.SendUnmanagedBytes(byteDataToSend); //***************************************************************// // End of interesting stuff // //***************************************************************// Console.WriteLine("\nSend complete. Press 'q' to quit or any other key to send something else."); } //***************************************************************// // Start of interesting stuff // //***************************************************************// //Make sure you call shutdown when finished to clean up. NetworkComms.Shutdown(); //***************************************************************// // End of interesting stuff // //***************************************************************// }
/// <summary> /// Discover peers using TCP port scan /// </summary> /// <param name="discoverTimeMS"></param> /// <returns></returns> private static Dictionary<ShortGuid, Dictionary<ConnectionType, List<EndPoint>>> DiscoverPeersTCP(int discoverTimeMS) { #region Determine All Possible Peers/Port Combinations //Get a list of all IPEndPoint that we should try and connect to //This requires the network and peer portion of current IP addresses List<IPEndPoint> allIPEndPointsToConnect = new List<IPEndPoint>(); //Look at all possible addresses foreach (var iFace in NetworkInterface.GetAllNetworkInterfaces()) { bool interfaceValid = false; var unicastAddresses = iFace.GetIPProperties().UnicastAddresses; //Check if this adaptor is allowed if (HostInfo.RestrictLocalAdaptorNames != null) { foreach (var currentName in HostInfo.RestrictLocalAdaptorNames) { if (iFace.Name == currentName) { interfaceValid = true; break; } } } else interfaceValid = true; //If the interface is not allowed move to the next adaptor if (!interfaceValid) continue; //If the adaptor is allowed we can now investigate the individual addresses foreach (var address in unicastAddresses) { //We are only interested in IPV4 ranges. A TCPPortScan on an IPV6 range may take a while if (address.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !IPRange.IsAutoAssignedAddress(address.Address)) { //Check if we have restricted the addresses bool addressAllowed = true; if (HostInfo.IP.RestrictLocalAddressRanges != null) addressAllowed = IPRange.Contains(HostInfo.IP.RestrictLocalAddressRanges, address.Address); if (addressAllowed) { //Generate all possible IPEndPoints for the current address and subnetmask //We have a special catch for the loopback address which has a very large range List<IPAddress> addressesInRange; if (address.Address.Equals(IPAddress.Loopback)) addressesInRange = new List<IPAddress>() { IPAddress.Loopback }; else { IPRange range = new IPRange(address.Address, address.IPv4Mask); addressesInRange = range.AllAddressesInRange(); } foreach (IPAddress currentAddressInRange in addressesInRange) { for (int port = MinTargetLocalIPPort; port <= MaxTargetLocalIPPort; port++) allIPEndPointsToConnect.Add(new IPEndPoint(currentAddressInRange, port)); } } } } } #endregion #region Send Discovery Packet & Wait //For each address send the discovery packet SendReceiveOptions nullOptions = new SendReceiveOptions<NullSerializer>(); StreamTools.StreamSendWrapper sendStream = new StreamTools.StreamSendWrapper(new StreamTools.ThreadSafeStream(new MemoryStream(new byte[0]))); int previousConnectionTimeout = NetworkComms.ConnectionEstablishTimeoutMS; NetworkComms.ConnectionEstablishTimeoutMS = 1000; AutoResetEvent allSendsCompleteEvent = new AutoResetEvent(false); long interlockedCompletedCount = 0; object _syncRoot = new object(); List<Connection> allConnections = new List<Connection>(); //Get unconnected TCP connections foreach (IPEndPoint remoteEndPoint in allIPEndPointsToConnect) { Connection conn = TCPConnection.GetConnection(new ConnectionInfo(remoteEndPoint), false); conn.AppendIncomingPacketHandler<byte[]>(discoveryPacketType, PeerDiscoveryHandler); allConnections.Add(conn); } using (Packet sendPacket = new Packet(discoveryPacketType, sendStream, nullOptions)) { foreach (Connection conn in allConnections) { Connection innerConnection = conn; //The longest wait for the port scan is the TCP connect timeout //The thread pool will start a large number of threads (each of which does very little) // to greatly speed this up _tcpPortScanThreadPool.EnqueueItem(QueueItemPriority.Normal, (state) => { try { try { innerConnection.EstablishConnection(); innerConnection.SendPacket<byte[]>(sendPacket); } catch (CommsException) { } lock (_syncRoot) { interlockedCompletedCount++; if (interlockedCompletedCount == allIPEndPointsToConnect.Count) allSendsCompleteEvent.Set(); } } catch (Exception) { } }, null); } allSendsCompleteEvent.WaitOne(); } NetworkComms.ConnectionEstablishTimeoutMS = previousConnectionTimeout; sendStream.ThreadSafeStream.Dispose(true); AutoResetEvent sleep = new AutoResetEvent(false); //We wait at least 1 second so that connected peers can respond //If we do not wait and close all connections immediately we may miss some replies #if NET2 sleep.WaitOne(Math.Max(discoverTimeMS, 500), false); #else sleep.WaitOne(Math.Max(discoverTimeMS, 500)); #endif //Close any connections we may have established foreach (Connection conn in allConnections) { try { conn.CloseConnection(false); } catch (CommsException) { } } #endregion #region Return Discovered Peers Dictionary<ShortGuid, Dictionary<ConnectionType, List<EndPoint>>> result = new Dictionary<ShortGuid, Dictionary<ConnectionType, List<EndPoint>>>(); lock (_syncRoot) { foreach (var idPair in _discoveredPeers) { if (!result.ContainsKey(idPair.Key)) result.Add(idPair.Key, new Dictionary<ConnectionType, List<EndPoint>>()); foreach (var typePair in idPair.Value) { if (!result[idPair.Key].ContainsKey(typePair.Key)) result[idPair.Key].Add(typePair.Key, new List<EndPoint>()); foreach (var endPoint in typePair.Value) if (!result[idPair.Key][typePair.Key].Contains(endPoint.Key)) result[idPair.Key][typePair.Key].Add(endPoint.Key); } } } return result; #endregion }
/// <summary> /// Discover peers using UDP broadcast /// </summary> /// <param name="discoverTimeMS"></param> /// <returns></returns> private static Dictionary<ShortGuid, Dictionary<ConnectionType, List<EndPoint>>> DiscoverPeersUDP(int discoverTimeMS) { SendReceiveOptions nullOptions = new SendReceiveOptions<NullSerializer>(); StreamTools.StreamSendWrapper sendStream = new StreamTools.StreamSendWrapper(new StreamTools.ThreadSafeStream(new MemoryStream(new byte[0]))); using (Packet sendPacket = new Packet(discoveryPacketType, sendStream, nullOptions)) { for (int port = MinTargetLocalIPPort; port <= MaxTargetLocalIPPort; port++) UDPConnection.SendObject<byte[]>(sendPacket, new IPEndPoint(IPAddress.Broadcast, port), nullOptions, ApplicationLayerProtocolStatus.Enabled); } sendStream.ThreadSafeStream.Dispose(true); AutoResetEvent sleep = new AutoResetEvent(false); #if NET2 sleep.WaitOne(discoverTimeMS, false); #else sleep.WaitOne(discoverTimeMS); #endif Dictionary<ShortGuid, Dictionary<ConnectionType, List<EndPoint>>> result = new Dictionary<ShortGuid, Dictionary<ConnectionType, List<EndPoint>>>(); lock (_syncRoot) { foreach (var idPair in _discoveredPeers) { if(!result.ContainsKey(idPair.Key)) result.Add(idPair.Key, new Dictionary<ConnectionType,List<EndPoint>>()); foreach(var typePair in idPair.Value) { if(!result[idPair.Key].ContainsKey(typePair.Key)) result[idPair.Key].Add(typePair.Key, new List<EndPoint>()); foreach(var endPoint in typePair.Value) if(!result[idPair.Key][typePair.Key].Contains(endPoint.Key)) result[idPair.Key][typePair.Key].Add(endPoint.Key); } } } return result; }
/// <summary> /// Select the SSL options /// </summary> private static void SelectSSLOptions() { int selectedOption; //Configure the server options //These will be applied for incoming connections Console.WriteLine("\nRequire connecting clients to provide certificate?\n1 - Yes (Connection will only be successful if client provides certificate.) \n2 - No (Client only requires certificate name to connect.)"); while (true) { bool parseSucces = int.TryParse(Console.ReadKey(true).KeyChar.ToString(), out selectedOption); if (parseSucces && selectedOption <= 2) break; Console.WriteLine("Invalid connection type choice. Please try again."); } if (selectedOption == 1) { Console.WriteLine(" ... selected yes."); listenerSSLOptions = new SSLOptions(certificate, true, true); } else if (selectedOption == 2) { Console.WriteLine(" ... selected no."); listenerSSLOptions = new SSLOptions(certificate, true, false); } else throw new Exception("Unable to determine selected option."); //Configure the connection options //These will be used when establishing outgoing connections Console.WriteLine("\nProvide certificate for outgoing connections?\n1 - Yes (Connection will only be successful if client and server certificate match.)\n2 - No (Client will accept any certificate from server.)"); while (true) { bool parseSucces = int.TryParse(Console.ReadKey(true).KeyChar.ToString(), out selectedOption); if (parseSucces && selectedOption <= 2) break; Console.WriteLine("Invalid connection type choice. Please try again."); } if (selectedOption == 1) { Console.WriteLine(" ... selected yes."); connectionSSLOptions = new SSLOptions(certificate, true); } else if (selectedOption == 2) { Console.WriteLine(" ... selected no."); connectionSSLOptions = new SSLOptions("networkcomms.net", true); } else throw new Exception("Unable to determine selected option."); //Select if the dataPadder will be enabled Console.WriteLine("\nWhen sending encrypted data"+ " the quantity of traffic can give away a significant amount of information. To prevent this"+ " traffic analysis attack we have included a data processor which if enabled ensures every packet sent"+ " is of a fixed size. Do you want to enable this padding data processor? " + "\n1 - Yes\n2 - No"); while (true) { bool parseSucces = int.TryParse(Console.ReadKey(true).KeyChar.ToString(), out selectedOption); if (parseSucces && selectedOption <= 2) break; Console.WriteLine("Invalid choice. Please try again."); } if (selectedOption == 1) { Console.WriteLine(" ... selected yes."); sendingSendReceiveOptions = new SendReceiveOptions<ProtobufSerializer, DataPadder>(); DataPadder.AddPaddingOptions(sendingSendReceiveOptions.Options, 1024, DataPadder.DataPaddingType.Random, true); } else if (selectedOption == 2) { Console.WriteLine(" ... selected no."); sendingSendReceiveOptions = NetworkComms.DefaultSendReceiveOptions; } else throw new Exception("Unable to determine selected option."); }