/// <summary> /// Starts the network connection with the currnet settings. /// </summary> /// <returns>True if successfull.</returns> public static bool Start() { #if !DEBUG try { #endif if (_isServer == true) { DebugLogger.WriteLog("Setting up server..."); // Load in network configuration file. DebugLogger.WriteLog("Loading network configuration file..."); DebugLogger.IncreaseIndent(); Stream stream = StreamFactory.RequestStream(_networkConfigFile, StreamMode.Open); if (stream == null) return false; XmlDocument document = new XmlDocument(); document.Load(stream); stream.Close(); // Get statistics. _port = Convert.ToInt32(document["network"]["settings"]["port"].InnerText); _maximumClients = Convert.ToInt32(document["network"]["settings"]["maximumclients"].InnerText); _pingDelay = Convert.ToInt32(document["network"]["settings"]["pingdelay"].InnerText); // Load in banned IP's XmlNode bansNode = document["network"]["bans"]; if (bansNode == null) throw new Exception("Network configuration declaration file is missing bans declaration."); foreach (XmlNode subNode in bansNode.ChildNodes) { if (subNode.Name.ToLower() != "ban") continue; NetworkBan ban = new NetworkBan("", 0, DateTime.Now); _bans.Add(ban); foreach (XmlNode subSubNode in subNode.ChildNodes) { switch (subSubNode.Name.ToLower()) { case "ip": ban.IP = subSubNode.InnerText; break; case "accountid": ban.AccountID = Convert.ToInt32(subSubNode.InnerText); break; case "expiration": ban.Expiration = new DateTime(Convert.ToInt64(subSubNode.InnerText)); break; } } DebugLogger.WriteLog("Loaded ban, ip:"+ban.IP+" accountID:" + ban.AccountID); } // Load account details. XmlNode accountsNode = document["network"]["accounts"]; if (accountsNode == null) throw new Exception("Network configuration declaration file is missing accounts declaration."); foreach (XmlNode subNode in accountsNode.ChildNodes) { if (subNode.Name.ToLower() != "account") continue; NetworkAccount account = new NetworkAccount(); _accountList.Add(account); foreach (XmlNode subSubNode in subNode.ChildNodes) { switch (subSubNode.Name.ToLower()) { case "lastknownip": account.LastKnownIP = subSubNode.InnerText; break; case "passwordhash": account.PasswordHash = subSubNode.InnerText; break; case "passwordsalt": account.PasswordSalt = subSubNode.InnerText; break; case "dateregistered": account.DateRegistered = new DateTime(long.Parse(subSubNode.InnerText)); break; case "lastactive": account.LastActive = new DateTime(long.Parse(subSubNode.InnerText)); break; case "username": account.Username = subSubNode.InnerText; break; case "position": account.Position = (NetworkAccountPosition)Enum.Parse(typeof(NetworkAccountPosition), subSubNode.InnerText); break; case "custom": { foreach (XmlNode customNode in subSubNode.ChildNodes) account.CustomSettings[customNode.Name] = customNode.InnerText; break; } } } DebugLogger.WriteLog("Loaded account: " + account.Username); } // Connect to server. DebugLogger.DecreaseIndent(); DebugLogger.WriteLog("Setting up connection..."); _connection = new NetworkConnection(); _connection.Connect("", _port, true); _connection.PacketRecieved += new PacketRecievedEventHandler(OnPacketRecieved); _connection.ClientConnected += new ClientConnectedEventHandler(OnClientConnected); _connection.ClientDisconnected += new ClientDisconnectedEventHandler(OnClientDisconnected); _connection.Disconnected += new DisconnectedEventHandler(OnDisconnected); DebugLogger.WriteLog("Server setup."); return true; } else { DebugLogger.WriteLog("Connecting to server..."); _connection = new NetworkConnection(); _connection.Connect(_serverIP, _port, false); _connection.PacketRecieved += new PacketRecievedEventHandler(OnPacketRecieved); _connection.Disconnected += new DisconnectedEventHandler(OnDisconnected); DebugLogger.WriteLog("Connected to server, waiting for connection result packet."); NetworkPacket packet = _connection.RecievePacket("CONNECTION_RESULT".GetHashCode(), 5000); if (packet != null) { EvaluatePacket(packet); DebugLogger.WriteLog("Connection result packet recieved."); } else { _connection.Close(); _connectionResult = -1; DebugLogger.WriteLog("Connection to server closed, server failed to respond with connection result packet.", BinaryPhoenix.Fusion.Runtime.Debug.LogAlertLevel.Error); return false; } } return true; #if !DEBUG } catch (Exception) { //_isConnected = false; return false; } #endif }
/// <summary> /// Bans a client from the network when the kick button is pressed. /// </summary> /// <param name="sender">Object that invoked this event.</param> /// <param name="e">Reason why this event was invoked.</param> private void banPeerButton_Click(object sender, EventArgs e) { string selectedPeer = ""; int clientID = 0; if (peerListView.SelectedItems.Count != 0) selectedPeer = peerListView.SelectedItems[0].Text; if (int.TryParse(selectedPeer, out clientID) == false) return; // Open ban window. BanWindow banWindow = new BanWindow(); if (banWindow.ShowDialog(this) == DialogResult.OK) { NetworkPacket packet = new NetworkPacket(); packet.ID = "BAN".GetHashCode(); foreach (NetworkClient client in NetworkManager.Connection.ClientList) if (client.ID == clientID) { // Send it the explanation packet. client.Connection.SendPacket(packet); // Create a ban. NetworkBan ban = new NetworkBan("000.000.000.000", -1, banWindow.BanExpiration); NetworkManager.BansList.Add(ban); // Add its IP to the black list. if (banWindow.BanIP == true) { string currentIP = client.Connection.Socket.RemoteEndPoint.ToString(); if (currentIP.IndexOf(":") >= 0) currentIP = currentIP.Substring(0, currentIP.IndexOf(":")); ban.IP = currentIP; } // Ban its account (if its logged in). if (banWindow.BanAccount == true && client.LoggedIn == true) ban.AccountID = client.AccountID; // Close its connection. client.Connection.Close(); } DebugLogger.WriteLog("Banning client " + clientID + "."); } }