Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I have this code:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;

namespace CSSServer.ServerBrowser
{
class Source : ServerBrowserMain
{
	private byte region;
	private string filter;

	public override event ServerFound ServerAdded;
	public override event EndOfList EndOfListReached;
	
	public override void GetServers(string last_ip, byte region, string filter)
	{
		if (last_ip.Length == 0)
		{
			return;
		}

		this.region = region;
		this.filter = filter;

		string query = "1" + (char)region + last_ip + filter;
		try
		{
			this.SendQuery(query);
		}
		catch (SocketException)
		{
			return;
		}

		this.ParseServers();
	}

	protected void ParseServers()
	{
		this.offset = 6;
		this.Cancel = false;

		for (int i = 0; (i < this.buffer1.Length); i++)
		{
			if (this.Cancel)
			{
				break;
			}

			StringBuilder ip = new StringBuilder();
			int port = 0;
			string final_ip = null;

			try
			{
				final_ip = String.Format("{0}.{1}.{2}.{3}", this.buffer1[this.offset++], this.buffer1[this.offset++], this.buffer1[this.offset++], this.buffer1[this.offset++]);
				
				port = BitConverter.ToUInt16(this.buffer1, this.offset++);
				port = IPAddress.HostToNetworkOrder((short)port);
				this.offset++;
			}
			catch (IndexOutOfRangeException)
			{
				this.EndOfListReached();
				break;
			}
			catch (ArgumentException)
			{
				this.EndOfListReached();
				break;
			}
			
			IP serverAddress = new IP(final_ip, port);
			
			Thread t = new Thread(new ParameterizedThreadStart(GetExtraInfo));
			t.Start((object) serverAddress);								
		}
	}			

	private void GetExtraInfo(object address)
	{
		IP serverIp = (IP) address;
		bool add_server = true;
		Protocols.Source server = new Protocols.Source();
		server.Timeout = 5000;

		try
		{
			server.Connect(serverIp.Address, serverIp.Port);
		}
		catch(Exception ex)
		{
			System.Windows.Forms.MessageBox.Show(ex.ToString());
			add_server = false;
		}

		try
		{
			server.GetServerInfo(true);
		}
		catch(Exception ex)
		{
			System.Windows.Forms.MessageBox.Show(ex.ToString());
			add_server = false;
		}

		if (add_server)
		{
			Types.Server new_server = new Types.Server();
			new_server.Host = serverIp.Address;
			new_server.Port = port;
			new_server.Players = Convert.ToInt32(server.ServerInfo["player_count"]);
			new_server.MaxPlayers = Convert.ToInt32(server.ServerInfo["max_players"]);
			new_server.Name = server.ServerInfo["server_name"];
			new_server.Map = server.ServerInfo["map"];
			new_server.Ping = Convert.ToInt32(server.ServerInfo["ping"]);
			new_server.Password = server.ServerInfo["passworded"] == "1" ? true : false;
			new_server.VAC = server.ServerInfo["secure"] == "1" ? true : false;
			new_server.Game = server.ServerInfo["game_desc"];
			new_server.AppID = Convert.ToInt32(server.ServerInfo["app_id"]);
			this.number++;

			this.ServerAdded(new_server);
			this.servers.Add(new_server);

			this.last_ip = serverIp.Address;
			this.last_port = serverIp.Port;
		}
	}
}

public enum SourceRegion : byte
{
	UsEastCoast = 0x00,
	UsWestCoast = 0x01,
	SouthAmerica = 0x02,
	Europe = 0x03,
	Asia = 0x04,
	Australia = 0x05,
	MiddleEast = 0x06,
	Africa = 0x07,
	RestOfTheWorld = 0xFF
}

public class IP
{
	private string address;
	private int port;
	
	public IP()
	{
	}
	
	public IP(string address, int port)
	{
		this.address = address;
		this.port = port;
	}
	
	public string Address
	{
		get
		{
			return this.address;
		}
		set
		{
			this.address = value;
		}
	}
	
	public int Port
	{
		get
		{
			return this.Port;
		}
		set
		{
			this.port = value;
		}
	}
	
	public override string ToString()
	{
		return this.address + ":" + this.port.ToString();
	}
}
}

 

As you can see, this program gets a list of servers from a steam master server, and then parses all info from each server.

 

But then I get the error:

 

An unhandled exception of type 'System.StackOverflowException' occurred in SST.exe

 

What is the problem here?

Edited by MrLucky
  • Administrators
Posted

public int Port
{
get
{
return this.Port;
}
set
{
this.port = value;
}
}

should be

public int Port
{
get
{
return this.port;
}
set
{
this.port = value;
}
}

 

it's why I tend to prefix private variables with an underscore, makes it easier to spot when skimming over the code.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted
The easiest way to find the cause of a stack overflow is to look at a stack trace, whether in the IDE or a logged exception (if you manage to log a stack overflow exception).
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...