/// <summary> /// Creates a new client connection connected to the given IP end point. This method blocks /// until we know if the client has either connected or disconnected. /// </summary> /// <param name="ip">The IP to connect to.</param> /// <param name="player">This computer's player.</param> /// <param name="password">The password that the server is expecting.</param> /// <returns></returns> public static Maybe <NetworkContext> CreateClient(string ip, Player player, string password) { NetClient client = new NetClient(Configuration.GetConfiguration(server: false)); client.Start(); // Write out our hail message { NetOutgoingMessage hailMsg = client.CreateMessage(); HailMessageFormat hail = new HailMessageFormat() { Player = player, Password = password }; string serializedHail = SerializationHelpers.Serialize(hail); hailMsg.Write(serializedHail); Log <NetworkContext> .Info("Trying to connect to " + ip + " on port " + Configuration.Port + " with hailing message " + serializedHail); // Try to connect to the server client.Connect(ip, Configuration.Port, hailMsg); } // Block until we know if we have connected or disconnected. while (true) { NetIncomingMessage msg; while ((msg = client.ReadMessage()) != null) { if (msg.MessageType != NetIncomingMessageType.StatusChanged) { Log <NetworkContext> .Error("While attempting to connect to server, got unexpected message type " + msg.MessageType); continue; } NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); Log <NetworkContext> .Info("While attempting to connect to server, status changed to " + status); goto gotConnectionAttemptResult; } Thread.Sleep(0); } gotConnectionAttemptResult: // If the connection status is not connected, then we failed, so just return an empty // network context if (client.ConnectionStatus != NetConnectionStatus.Connected) { return(Maybe <NetworkContext> .Empty); } // We're connected to the server! Read in the hail message to populate our server // connection with the server player instance { NetIncomingMessage msg = client.ServerConnection.RemoteHailMessage; Player serverPlayer = SerializationHelpers.Deserialize <Player>(msg.ReadString()); client.ServerConnection.Tag = serverPlayer; NetworkContext context = new NetworkContext(player) { _client = client }; return(Maybe.Just(context)); } }
/// <summary> /// Creates a new client connection connected to the given IP end point. This method blocks /// until we know if the client has either connected or disconnected. /// </summary> /// <param name="ip">The IP to connect to.</param> /// <param name="player">This computer's player.</param> /// <param name="password">The password that the server is expecting.</param> /// <returns></returns> public static Maybe<NetworkContext> CreateClient(string ip, Player player, string password) { NetClient client = new NetClient(Configuration.GetConfiguration(server: false)); client.Start(); // Write out our hail message { NetOutgoingMessage hailMsg = client.CreateMessage(); HailMessageFormat hail = new HailMessageFormat() { Player = player, Password = password }; string serializedHail = SerializationHelpers.Serialize(hail); hailMsg.Write(serializedHail); Log<NetworkContext>.Info("Trying to connect to " + ip + " on port " + Configuration.Port + " with hailing message " + serializedHail); // Try to connect to the server client.Connect(ip, Configuration.Port, hailMsg); } // Block until we know if we have connected or disconnected. while (true) { NetIncomingMessage msg; while ((msg = client.ReadMessage()) != null) { if (msg.MessageType != NetIncomingMessageType.StatusChanged) { Log<NetworkContext>.Error("While attempting to connect to server, got unexpected message type " + msg.MessageType); continue; } NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); Log<NetworkContext>.Info("While attempting to connect to server, status changed to " + status); goto gotConnectionAttemptResult; } Thread.Sleep(0); } gotConnectionAttemptResult: // If the connection status is not connected, then we failed, so just return an empty // network context if (client.ConnectionStatus != NetConnectionStatus.Connected) { return Maybe<NetworkContext>.Empty; } // We're connected to the server! Read in the hail message to populate our server // connection with the server player instance { NetIncomingMessage msg = client.ServerConnection.RemoteHailMessage; Player serverPlayer = SerializationHelpers.Deserialize<Player>(msg.ReadString()); client.ServerConnection.Tag = serverPlayer; NetworkContext context = new NetworkContext(player) { _client = client }; return Maybe.Just(context); } }