From 73778494b7b22eff2ce7a1862b8133ac3ba46688 Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 21 Dec 2008 14:23:40 +0000 Subject: [PATCH] Console client: Retrieve player names. --- console/src/GameInfo.cs | 18 +---- console/src/GameInfoList.cs | 13 ++-- console/src/IdObject.cs | 53 +++++++++++++++ console/src/PlayerInfo.cs | 33 ++++++++++ console/src/PlayerInfoList.cs | 69 ++++++++++++++++++++ console/src/PokerTHData.cs | 53 +++++++++++++++ console/src/Program.cs | 12 ++-- console/src/net/Client.cs | 35 ++++++---- console/src/net/NetPacket.cs | 3 + console/src/net/NetPacketGameListNew.cs | 4 ++ console/src/net/NetParser.cs | 64 ++++++++++++++++++ console/src/net/NetThread.cs | 87 +++++++++++++++++++++++++ console/src/net/ReceiverThread.cs | 70 +++++--------------- console/src/net/SenderThread.cs | 68 +++++++++++++++++++ 14 files changed, 493 insertions(+), 89 deletions(-) create mode 100644 console/src/IdObject.cs create mode 100644 console/src/PlayerInfo.cs create mode 100644 console/src/PlayerInfoList.cs create mode 100644 console/src/PokerTHData.cs create mode 100644 console/src/net/NetParser.cs create mode 100644 console/src/net/NetThread.cs create mode 100644 console/src/net/SenderThread.cs diff --git a/console/src/GameInfo.cs b/console/src/GameInfo.cs index 104301c5..e5eaa9cc 100644 --- a/console/src/GameInfo.cs +++ b/console/src/GameInfo.cs @@ -23,25 +23,11 @@ using System.Text; namespace pokerth_console { - class GameInfo + class GameInfo : IdObject { public GameInfo(uint id, string name) + : base(id, name) { - m_id = id; - m_name = name; } - - public uint GetId() - { - return m_id; - } - - public override string ToString() - { - return m_name; - } - - private uint m_id; - private string m_name; } } diff --git a/console/src/GameInfoList.cs b/console/src/GameInfoList.cs index 15befdbb..db017b4e 100644 --- a/console/src/GameInfoList.cs +++ b/console/src/GameInfoList.cs @@ -28,14 +28,17 @@ namespace pokerth_console { public GameInfoList() { - m_list = new List(); + m_list = new Dictionary(); } public void AddGameInfo(GameInfo info) { lock (m_list) { - m_list.Add(info); + if (m_list.ContainsKey(info.Id)) + m_list[info.Id] = info; + else + m_list.Add(info.Id, info); } } @@ -44,15 +47,15 @@ namespace pokerth_console string outString = ""; lock (m_list) { - foreach (GameInfo i in m_list) + foreach (KeyValuePair i in m_list) { - outString += i.ToString(); + outString += i.Value.Name; outString += '\n'; } } return outString; } - private List m_list; + private Dictionary m_list; } } diff --git a/console/src/IdObject.cs b/console/src/IdObject.cs new file mode 100644 index 00000000..6d0e997c --- /dev/null +++ b/console/src/IdObject.cs @@ -0,0 +1,53 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace pokerth_console +{ + class IdObject + { + public IdObject(uint id, string name) + { + m_id = id; + m_name = name; + } + + public uint Id + { + get + { + return m_id; + } + } + + public string Name + { + get + { + return m_name; + } + } + + private uint m_id; + private string m_name; + } +} diff --git a/console/src/PlayerInfo.cs b/console/src/PlayerInfo.cs new file mode 100644 index 00000000..308c9ea5 --- /dev/null +++ b/console/src/PlayerInfo.cs @@ -0,0 +1,33 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace pokerth_console +{ + class PlayerInfo : IdObject + { + public PlayerInfo(uint id, string name) + : base(id, name) + { + } + } +} diff --git a/console/src/PlayerInfoList.cs b/console/src/PlayerInfoList.cs new file mode 100644 index 00000000..42654322 --- /dev/null +++ b/console/src/PlayerInfoList.cs @@ -0,0 +1,69 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; + +namespace pokerth_console +{ + class PlayerInfoList + { + public PlayerInfoList() + { + m_list = new Dictionary(); + } + + public void AddPlayerInfo(PlayerInfo info) + { + lock (m_list) + { + if (m_list.ContainsKey(info.Id)) + m_list[info.Id] = info; + else + m_list.Add(info.Id, info); + } + } + + public bool HasPlayer(uint id) + { + lock (m_list) + { + return m_list.ContainsKey(id); + } + } + + public override string ToString() + { + string outString = ""; + lock (m_list) + { + foreach (KeyValuePair i in m_list) + { + outString += i.Value.Name; + outString += '\n'; + } + } + return outString; + } + + private Dictionary m_list; + } +} diff --git a/console/src/PokerTHData.cs b/console/src/PokerTHData.cs new file mode 100644 index 00000000..99929fe7 --- /dev/null +++ b/console/src/PokerTHData.cs @@ -0,0 +1,53 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace pokerth_console +{ + class PokerTHData + { + public PokerTHData() + { + m_gameInfoList = new GameInfoList(); + m_playerInfoList = new PlayerInfoList(); + } + + public GameInfoList GameList + { + get + { + return m_gameInfoList; + } + } + + public PlayerInfoList PlayerList + { + get + { + return m_playerInfoList; + } + } + + private GameInfoList m_gameInfoList; + private PlayerInfoList m_playerInfoList; + } +} diff --git a/console/src/Program.cs b/console/src/Program.cs index 8fdace89..803e6144 100644 --- a/console/src/Program.cs +++ b/console/src/Program.cs @@ -29,13 +29,17 @@ namespace pokerth_console static int Main(string[] args) { Settings settings = new Settings(); - GameInfoList list = new GameInfoList(); - Client client = new Client(settings); + PokerTHData data = new PokerTHData(); + Client client = new Client(settings, data); client.Connect(); - client.Start(list); + client.Start(); Thread.Sleep(5000); client.SetTerminateFlag(); - Console.Write(list.ToString()); + Console.WriteLine("Games:"); + Console.Write(data.GameList.ToString()); + Console.WriteLine(); + Console.WriteLine("Players:"); + Console.Write(data.PlayerList.ToString()); client.WaitTermination(); return 0; } diff --git a/console/src/net/Client.cs b/console/src/net/Client.cs index 11967111..c33c2513 100644 --- a/console/src/net/Client.cs +++ b/console/src/net/Client.cs @@ -27,10 +27,11 @@ namespace pokerth_console { class Client { - public Client(Settings settings) + public Client(Settings settings, PokerTHData data) { m_tcpClient = new TcpClient(); m_settings = settings; + m_data = data; } public void Connect() @@ -43,41 +44,51 @@ namespace pokerth_console m_tcpClient.Connect(addresses, m_settings.ServerSettings.Port); } - public void Start(GameInfoList list) + public void Start() { - StartReceiveThread(list); + StartSendThread(); + StartReceiveThread(); SendInit(); } public void SetTerminateFlag() { + m_sender.SetTerminateFlag(); m_receiver.SetTerminateFlag(); } public void WaitTermination() { + m_sender.WaitTermination(); m_receiver.WaitTermination(); } - protected void StartReceiveThread(GameInfoList list) + protected void StartReceiveThread() { - m_receiver = new ReceiverThread(m_tcpClient.GetStream(), list); + m_receiver = new ReceiverThread(m_tcpClient.GetStream(), m_sender, m_data); m_receiver.Run(); } + protected void StartSendThread() + { + m_sender = new SenderThread(m_tcpClient.GetStream()); + m_sender.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); + NetPacket init = new NetPacketInit(); + init.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMajor, "4"); + init.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMinor, "2"); + init.Properties.Add(NetPacket.PropertyType.PropPlayerName, "Loto"); + init.Properties.Add(NetPacket.PropertyType.PropPlayerPassword, ""); + m_sender.Send(init); } private TcpClient m_tcpClient; private ReceiverThread m_receiver; + private SenderThread m_sender; private Settings m_settings; + private PokerTHData m_data; } } diff --git a/console/src/net/NetPacket.cs b/console/src/net/NetPacket.cs index 884d1833..bd09ac2e 100644 --- a/console/src/net/NetPacket.cs +++ b/console/src/net/NetPacket.cs @@ -125,6 +125,9 @@ namespace pokerth_console case NetTypeGameListNew: tmpPacket = new NetPacketGameListNew(size, reader); break; + case NetTypePlayerInfo: + tmpPacket = new NetPacketPlayerInfo(size, reader); + break; default: break; } diff --git a/console/src/net/NetPacketGameListNew.cs b/console/src/net/NetPacketGameListNew.cs index 11d2ca7f..dbcbe7cf 100644 --- a/console/src/net/NetPacketGameListNew.cs +++ b/console/src/net/NetPacketGameListNew.cs @@ -89,6 +89,10 @@ namespace pokerth_console byte[] tmpName = r.ReadBytes(gameNameLen); Properties.Add(PropertyType.PropGameName, Encoding.UTF8.GetString(tmpName)); + // Skip the padding. + int namePadding = AddPadding(tmpName.Length) - tmpName.Length; + if (namePadding > 0) + r.ReadBytes(namePadding); // Read player ids. List playerSlots = new List(); diff --git a/console/src/net/NetParser.cs b/console/src/net/NetParser.cs new file mode 100644 index 00000000..c81fa957 --- /dev/null +++ b/console/src/net/NetParser.cs @@ -0,0 +1,64 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace pokerth_console +{ + class NetParser + { + public NetParser(PokerTHData data, SenderThread sender) + { + m_data = data; + m_sender = sender; + } + + public void ParseGameListNew(NetPacket p) + { + // Request player names for player ids. + List playerList = p.ListProperties[NetPacket.ListPropertyType.PropPlayerSlots]; + foreach (string player in playerList) + { + if (!m_data.PlayerList.HasPlayer(Convert.ToUInt32(player))) + { + NetPacketRetrievePlayerInfo request = new NetPacketRetrievePlayerInfo(); + request.Properties.Add(NetPacket.PropertyType.PropPlayerId, player); + m_sender.Send(request); + } + } + // Add game to list. + m_data.GameList.AddGameInfo(new GameInfo( + Convert.ToUInt32(p.Properties[NetPacket.PropertyType.PropGameId]), + p.Properties[NetPacket.PropertyType.PropGameName])); + } + + public void ParsePlayerInfo(NetPacket p) + { + // Add player to list. + m_data.PlayerList.AddPlayerInfo(new PlayerInfo( + Convert.ToUInt32(p.Properties[NetPacket.PropertyType.PropPlayerId]), + p.Properties[NetPacket.PropertyType.PropPlayerName])); + } + + private PokerTHData m_data; + private SenderThread m_sender; + } +} diff --git a/console/src/net/NetThread.cs b/console/src/net/NetThread.cs new file mode 100644 index 00000000..2bb9b119 --- /dev/null +++ b/console/src/net/NetThread.cs @@ -0,0 +1,87 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Net; +using System.Net.Sockets; +using System.IO; + +namespace pokerth_console +{ + abstract class NetThread + { + public NetThread(NetworkStream stream) + { + m_thread = new Thread(ThreadProc); + m_terminateFlag = false; + m_terminateFlagMutex = new System.Object(); + m_netStream = stream; + } + + public void Run() + { + m_thread.Start(this); + } + + protected NetworkStream NetStream + { + get + { + return m_netStream; + } + } + + protected static void ThreadProc(object obj) + { + NetThread me = (NetThread)obj; + me.Start(); + } + + protected abstract void Start(); + + public void WaitTermination() + { + m_thread.Join(); + } + + public void SetTerminateFlag() + { + lock (m_terminateFlagMutex) + { + m_terminateFlag = true; + } + } + + protected bool IsTerminateFlagSet() + { + lock (m_terminateFlagMutex) + { + return m_terminateFlag; + } + } + + private Thread m_thread; + private bool m_terminateFlag; + private Object m_terminateFlagMutex; + private NetworkStream m_netStream; + } +} diff --git a/console/src/net/ReceiverThread.cs b/console/src/net/ReceiverThread.cs index f116bb2b..07a45ab6 100644 --- a/console/src/net/ReceiverThread.cs +++ b/console/src/net/ReceiverThread.cs @@ -27,35 +27,22 @@ using System.IO; namespace pokerth_console { - class ReceiverThread + class ReceiverThread : NetThread { const uint MaxPacketSize = 268; const uint MinPacketSize = 8; - public ReceiverThread(NetworkStream stream, GameInfoList list) + public ReceiverThread(NetworkStream stream, SenderThread sender, PokerTHData data) + : base(stream) { - m_recThread = new Thread(ThreadProc); - m_terminateFlag = false; - m_terminateFlagMutex = new System.Object(); - m_recStream = stream; m_recBuf = new byte[8192]; m_recBufOffset = 0; m_packetList = new List(); - m_lobbyGameInfoList = list; + m_sender = sender; + m_parser = new NetParser(data, sender); } - public void Run() - { - m_recThread.Start(this); - } - - protected static void ThreadProc(object obj) - { - ReceiverThread me = (ReceiverThread)obj; - me.Start(); - } - - protected void Start() + protected override void Start() { while (!IsTerminateFlagSet()) { @@ -65,31 +52,10 @@ namespace pokerth_console } } - public void WaitTermination() - { - m_recThread.Join(); - } - - public void SetTerminateFlag() - { - lock (m_terminateFlagMutex) - { - m_terminateFlag = true; - } - } - - protected bool IsTerminateFlagSet() - { - lock (m_terminateFlagMutex) - { - return m_terminateFlag; - } - } - protected void ReadFromStream() { - if (m_recStream.DataAvailable) - m_recBufOffset += m_recStream.Read(m_recBuf, m_recBufOffset, m_recBuf.Length - m_recBufOffset); + if (NetStream.DataAvailable) + m_recBufOffset += NetStream.Read(m_recBuf, m_recBufOffset, m_recBuf.Length - m_recBufOffset); else Thread.Sleep(15); } @@ -144,23 +110,23 @@ namespace pokerth_console { foreach (NetPacket p in m_packetList) { - if (p.Type == NetPacket.NetTypeGameListNew) + switch (p.Type) { - m_lobbyGameInfoList.AddGameInfo(new GameInfo( - Convert.ToUInt32(p.Properties[NetPacket.PropertyType.PropGameId]), - p.Properties[NetPacket.PropertyType.PropGameName])); + case NetPacket.NetTypeGameListNew : + m_parser.ParseGameListNew(p); + break; + case NetPacket.NetTypePlayerInfo : + m_parser.ParsePlayerInfo(p); + break; } } m_packetList.Clear(); } - private Thread m_recThread; - private bool m_terminateFlag; - private Object m_terminateFlagMutex; - private NetworkStream m_recStream; private byte[] m_recBuf; private int m_recBufOffset; - private List m_packetList; - private GameInfoList m_lobbyGameInfoList; + private List m_packetList; + private SenderThread m_sender; + private NetParser m_parser; } } diff --git a/console/src/net/SenderThread.cs b/console/src/net/SenderThread.cs new file mode 100644 index 00000000..72bfb090 --- /dev/null +++ b/console/src/net/SenderThread.cs @@ -0,0 +1,68 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Net; +using System.Net.Sockets; +using System.IO; + +namespace pokerth_console +{ + class SenderThread : NetThread + { + public SenderThread(NetworkStream stream) + : base(stream) + { + m_packetQueue = new Queue(); + } + + public void Send(NetPacket p) + { + lock (m_packetQueue) + { + m_packetQueue.Enqueue(p); + } + } + + protected override void Start() + { + while (!IsTerminateFlagSet()) + { + bool sleep = false; + lock (m_packetQueue) + { + if (m_packetQueue.Count > 0) + { + byte[] outBuf = m_packetQueue.Dequeue().ToByteArray(); + NetStream.Write(outBuf, 0, outBuf.Length); + } + else + sleep = true; + } + if (sleep) + Thread.Sleep(15); + } + } + + private Queue m_packetQueue; + } +}