private Timer TimeoutTimer; // The timer used to cause sync timeouts. #endregion Fields #region Constructors /// <summary> /// Create a new network server. /// </summary> /// <param name="gameDataUpdater">The function to use to update the game when receiving game packets.</param> /// <param name="port">The port to create the network server on.</param> public NetworkServer(int port) : base(NetworkStateMachine.NetworkState.SERVERSTART) { connections = new Dictionary<IPEndPoint, ConnectionID>(); Debug.WriteLine("Starting Server"); networkWorker = new NetworkWorker(port); networkThread.Start(); return; }
/// <summary> /// Connect to the game server. /// </summary> /// <param name="host">The name of the host to connect to.</param> /// <param name="port">The port on the host to connect to.</param> /// <returns>Returns a boolean representing whether or not the connection was succesful.</returns> public bool Connect(String host, int port) { try { serverEndPoint = new IPEndPoint(IPAddress.Parse(host), port); // Store server info networkWorker = new NetworkWorker();//Spawn the client reader/writer threads networkStateMachine.DoTransition(NetworkStateMachine.TransitionEvent.CLIENTCONNECT, null); // Client may now try to connect serverReadySemaphore.Release(); // Inform the client thread that the server info is ready SendHandshake(); // Send the handshake request to the server clientConnectedSemaphore.WaitOne(); // Wait for the connection to be established return true; } catch (Exception error) { Debug.WriteLine("Unable to connect to server: {0}", error.Message); return false; } }