} // Storage for received packets. /// <summary> Starts a Client process. </summary> /// <param name="ServerIP"> String representation of the IP Address of server.</param> /// <param name="PortTCP"> Target port for TCP Communications on the server.</param> /// <param name="PortUDP"> Target port for UDP Communications on the server.</param> /// <param name="Name"> Name of client. </param> /// <param name="ReceiveBufferSize"> Size of buffer for receving incoming packet. Unit: byte. Increase this if you are receiving larger packets. </param> /// <param name="OperationPeriod"> Time to wait after receiving or sending each packet. Unit: ms. Decrease this if the send/receive operations cannot keep up with your data rate. </param> /// <param name="UsePriorityQueue"> If it is true, packet priority control will be enabled, and packets will be sent in an order corresponding to their importance. If false, packets are sent in the order that they are provided. </param> public static void Start(string ServerIP, int PortTCP, int PortUDP, string Name, int ReceiveBufferSize = 64, int OperationPeriod = 20, bool UsePriorityQueue = false) { // Initialize PacketHandler PacketHandler.Start(); // Output to debug that the client is starting up Log.Output(Log.Severity.DEBUG, Log.Source.NETWORK, "Initializing Client."); // Set local variables given the parameters Client.Name = Name; Client.ReceiveBufferSize = ReceiveBufferSize; Client.OperationPeriod = OperationPeriod; IPAddress ServerIPA = IPAddress.Parse(ServerIP); // Setup Endpoints for TCP and UDP ServerEndpointTCP = new IPEndPoint(ServerIPA, PortTCP); ServerEndpointUDP = new IPEndPoint(ServerIPA, PortUDP); if (!Initialized) { // Setup Watchdog // Client watchdog manager is automatically started // when it receives the first watchdog from the server. // Subscribe to the watchdog manager WatchdogManager.ConnectionChanged += ConnectionChange; // Initialize the receiving queue ReceiveQueue = new QueueBuffer(); // Initialize sending queue and default priority if (UsePriorityQueue) { SendQueue = new GenericController(); DefaultPriority = PacketPriority.MEDIUM; } else { SendQueue = new QueueBuffer(); DefaultPriority = 0; } // Initialize packet storage structures SentPackets = new List <Packet>(); ReceivedPackets = new List <Packet>(); // Initialize (but do not start the threads) SendThread = new Thread(new ThreadStart(SendPackets)); ProcessThread = new Thread(new ThreadStart(ProcessPackets)); RetryConnection = RetryConnectionThreadFactory(); ReceiveThreadTCP = new Thread(new ParameterizedThreadStart(ReceiveFromSocket)); ReceiveThreadUDP = new Thread(new ParameterizedThreadStart(ReceiveFromSocket)); // Initialize client for the first time on a new thread so that it doesn't block new Thread(new ThreadStart(InitialStartup)).Start(); // Set the state of client to initialized. Initialized = true; } }
/// <summary> Prepares the server for use, and starts listening for clients. </summary> /// <param name="PortTCP"> The port to listen on for clients communicating via TCP. </param> /// <param name="PortUDP"> The port to listen on for clients communicating via UDP. </param> /// <param name="ReceiveBufferSize"> The size, in bytes, of the receive data buffers. Increase this if your packets are longer than the default. </param> /// <param name="OperationPeriod"> The time, in ms, between network operations. If you are sending/receiving a lot of packets, and notice delays, lower this. </param> /// <param name="UsePriorityQueue"> If it is ture, packet priority control will be enabled. </param> public static void Start(int PortTCP, int PortUDP, int ReceiveBufferSize = 64, int OperationPeriod = 20, bool UsePriorityQueue = false) { Server.ReceiveBufferSize = ReceiveBufferSize; Server.OperationPeriod = OperationPeriod; Stopping = false; if (!Initialized) { Log.Output(Log.Severity.DEBUG, Log.Source.NETWORK, "Initializing Server."); Log.Output(Log.Severity.DEBUG, Log.Source.NETWORK, "Listening on ports " + PortTCP + " (TCP), and " + PortUDP + " (UDP)."); Clients = new Dictionary <string, ScarletClient>(); SendQueues = new Dictionary <string, PacketBuffer>(); ReceiveQueue = new QueueBuffer(); PacketsSent = new List <Packet>(); PacketsReceived = new List <Packet>(); Initialized = true; PriorityQueueEnabled = UsePriorityQueue; // Initialize default priority if (PriorityQueueEnabled) { DefaultPriority = PacketPriority.MEDIUM; } else { DefaultPriority = 0; } // Start Handler and listener PacketHandler.Start(); new Thread(new ParameterizedThreadStart(StartThreads)).Start(new Tuple <int, int>(PortTCP, PortUDP)); // Start watchdog WatchdogManager.Start(false); WatchdogManager.ConnectionChanged += WatchdogStatusUpdate; } else { Log.Output(Log.Severity.WARNING, Log.Source.NETWORK, "Attempted to start Server when already started."); } }