diff --git a/console/src/Program.cs b/console/src/Program.cs index ca0cc7eb..8fdace89 100644 --- a/console/src/Program.cs +++ b/console/src/Program.cs @@ -20,10 +20,6 @@ using System; using System.Collections.Generic; using System.Text; -using System.IO; -using System.Xml; -using System.Net; -using System.Net.Sockets; using System.Threading; namespace pokerth_console @@ -32,44 +28,15 @@ namespace pokerth_console { static int Main(string[] args) { - // Retrieve server list. - WebClient webcl = new WebClient(); - string tmpFilePath = Path.GetTempPath(); - string tmpZipFileName = tmpFilePath + "pokerth_serverlist.xml.z"; - string tmpXmlFileName = tmpFilePath + "pokerth_serverlist.xml"; - webcl.DownloadFile("http://pokerth.net/serverlist.xml.z", tmpZipFileName); - ZlibHelper.UncompressFile(tmpZipFileName, tmpXmlFileName); - FileStream f = new FileStream(tmpXmlFileName, FileMode.Open, FileAccess.Read); - XmlReader x = XmlReader.Create(f); - x.Read(); - x.ReadStartElement("ServerList"); - x.ReadStartElement("Server"); - x.ReadToFollowing("IPv4Address"); - x.MoveToFirstAttribute(); - string ipAddress = x.Value; - x.ReadToFollowing("Port"); - x.MoveToFirstAttribute(); - int port = Convert.ToInt32(x.Value); - x.Close(); - f.Close(); - File.Delete(tmpZipFileName); - File.Delete(tmpXmlFileName); - // Connect to the server. - TcpClient client = new TcpClient(ipAddress, port); - NetPacket test = new NetPacketInit(); - test.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMajor, "4"); - test.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMinor, "2"); - test.Properties.Add(NetPacket.PropertyType.PropPlayerName, "Loto"); - test.Properties.Add(NetPacket.PropertyType.PropPlayerPassword, ""); - byte[] buf = test.ToByteArray(); - client.GetStream().Write(buf, 0, buf.Length); + Settings settings = new Settings(); GameInfoList list = new GameInfoList(); - ReceiverThread t = new ReceiverThread(client.GetStream(), list); - ReceiverThread.Run(t); + Client client = new Client(settings); + client.Connect(); + client.Start(list); Thread.Sleep(5000); - t.SetTerminateFlag(); + client.SetTerminateFlag(); Console.Write(list.ToString()); - t.WaitTermination(); + client.WaitTermination(); return 0; } } diff --git a/console/src/ServerSettings.cs b/console/src/ServerSettings.cs new file mode 100644 index 00000000..dcde909b --- /dev/null +++ b/console/src/ServerSettings.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace pokerth_console +{ + class ServerSettings + { + public ServerSettings() + { + } + + public string IPv4Address + { + get + { + return m_ipv4Address; + } + set + { + m_ipv4Address = value; + } + } + + public string IPv6Address + { + get + { + return m_ipv6Address; + } + set + { + m_ipv6Address = value; + } + } + + public int Port + { + get + { + return m_port; + } + set + { + m_port = value; + } + } + + private string m_ipv4Address = ""; + private string m_ipv6Address = ""; + private int m_port = 0; + } +} diff --git a/console/src/Settings.cs b/console/src/Settings.cs new file mode 100644 index 00000000..5ada5eda --- /dev/null +++ b/console/src/Settings.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml; +using System.Net; +using System.Net.Sockets; +using System.IO; + +namespace pokerth_console +{ + class Settings + { + private const string ServerListUrl = "http://pokerth.net/serverlist.xml.z"; + + public Settings() + { + m_serverSettings = RetrieveServerSettings(); + } + + public ServerSettings ServerSettings + { + get + { + return m_serverSettings; + } + } + + protected ServerSettings RetrieveServerSettings() + { + // Retrieve server list. + string tmpFile = DownloadXmlServerList(); + // Parse Xml data; + ServerSettings settings = ParseServerSettings(tmpFile); + File.Delete(tmpFile); + return settings; + } + + protected string DownloadXmlServerList() + { + // Download the server list from the official site. + WebClient webcl = new WebClient(); + string tmpFilePath = Path.GetTempPath(); + string tmpZipFile = tmpFilePath + "pokerth_serverlist.xml.z"; + string tmpXmlFile = tmpFilePath + "pokerth_serverlist.xml"; + webcl.DownloadFile(ServerListUrl, tmpZipFile); + // The list is zlib compressed - uncompress. + ZlibHelper.UncompressFile(tmpZipFile, tmpXmlFile); + File.Delete(tmpZipFile); + + return tmpXmlFile; + } + + protected ServerSettings ParseServerSettings(string file) + { + ServerSettings settings = new ServerSettings(); + FileStream f = new FileStream(file, FileMode.Open, FileAccess.Read); + XmlReader x = XmlReader.Create(f); + x.Read(); + x.ReadStartElement("ServerList"); + x.ReadStartElement("Server"); + x.ReadToFollowing("IPv4Address"); + x.MoveToFirstAttribute(); + settings.IPv4Address = x.Value; + x.ReadToFollowing("IPv6Address"); + x.MoveToFirstAttribute(); + settings.IPv6Address = x.Value; + x.ReadToFollowing("Port"); + x.MoveToFirstAttribute(); + settings.Port = Convert.ToInt32(x.Value); + x.Close(); + f.Close(); + + return settings; + } + + private ServerSettings m_serverSettings; + } +} diff --git a/console/src/net/Client.cs b/console/src/net/Client.cs new file mode 100644 index 00000000..18ac4602 --- /dev/null +++ b/console/src/net/Client.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Net; +using System.Net.Sockets; + +namespace pokerth_console +{ + class Client + { + public Client(Settings settings) + { + m_tcpClient = new TcpClient(); + m_settings = settings; + } + + public void Connect() + { + // Connect to the server. + // Try IPv6 first. + IPAddress[] addresses = new IPAddress[2]; + addresses[0] = IPAddress.Parse(m_settings.ServerSettings.IPv6Address); + addresses[1] = IPAddress.Parse(m_settings.ServerSettings.IPv4Address); + m_tcpClient.Connect(addresses, m_settings.ServerSettings.Port); + } + + public void Start(GameInfoList list) + { + StartReceiveThread(list); + SendInit(); + } + + public void SetTerminateFlag() + { + m_receiver.SetTerminateFlag(); + } + + public void WaitTermination() + { + m_receiver.WaitTermination(); + } + + protected void StartReceiveThread(GameInfoList list) + { + m_receiver = new ReceiverThread(m_tcpClient.GetStream(), list); + m_receiver.Run(); + } + + protected void SendInit() + { + NetPacket test = new NetPacketInit(); + test.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMajor, "4"); + test.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMinor, "2"); + test.Properties.Add(NetPacket.PropertyType.PropPlayerName, "Loto"); + test.Properties.Add(NetPacket.PropertyType.PropPlayerPassword, ""); + byte[] buf = test.ToByteArray(); + m_tcpClient.GetStream().Write(buf, 0, buf.Length); + } + + private TcpClient m_tcpClient; + private ReceiverThread m_receiver; + private Settings m_settings; + } +} diff --git a/console/src/net/ReceiverThread.cs b/console/src/net/ReceiverThread.cs index 0d5099fb..c52317e9 100644 --- a/console/src/net/ReceiverThread.cs +++ b/console/src/net/ReceiverThread.cs @@ -38,16 +38,15 @@ namespace pokerth_console m_terminateFlag = false; m_terminateFlagMutex = new System.Object(); m_recStream = stream; - m_recStream.ReadTimeout = 50; m_recBuf = new byte[8192]; m_recBufOffset = 0; m_packetList = new List(); m_lobbyGameInfoList = list; } - public static void Run(ReceiverThread me) + public void Run() { - me.m_recThread.Start(me); + m_recThread.Start(this); } protected static void ThreadProc(object obj) @@ -91,7 +90,8 @@ namespace pokerth_console { if (m_recStream.DataAvailable) m_recBufOffset += m_recStream.Read(m_recBuf, m_recBufOffset, m_recBuf.Length - m_recBufOffset); - Thread.Sleep(15); + else + Thread.Sleep(15); } protected void ScanPackets()