Console client: Retrieve player names.

This commit is contained in:
lotodore
2008-12-21 14:23:40 +00:00
parent cc2e0251d3
commit 73778494b7
14 changed files with 493 additions and 89 deletions
+2 -16
View File
@@ -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;
}
}
+8 -5
View File
@@ -28,14 +28,17 @@ namespace pokerth_console
{
public GameInfoList()
{
m_list = new List<GameInfo>();
m_list = new Dictionary<uint, GameInfo>();
}
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<uint, GameInfo> i in m_list)
{
outString += i.ToString();
outString += i.Value.Name;
outString += '\n';
}
}
return outString;
}
private List<GameInfo> m_list;
private Dictionary<uint, GameInfo> m_list;
}
}
+53
View File
@@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
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;
}
}
+33
View File
@@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
namespace pokerth_console
{
class PlayerInfo : IdObject
{
public PlayerInfo(uint id, string name)
: base(id, name)
{
}
}
}
+69
View File
@@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace pokerth_console
{
class PlayerInfoList
{
public PlayerInfoList()
{
m_list = new Dictionary<uint, PlayerInfo>();
}
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<uint, PlayerInfo> i in m_list)
{
outString += i.Value.Name;
outString += '\n';
}
}
return outString;
}
private Dictionary<uint, PlayerInfo> m_list;
}
}
+53
View File
@@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
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;
}
}
+8 -4
View File
@@ -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;
}
+23 -12
View File
@@ -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;
}
}
+3
View File
@@ -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;
}
+4
View File
@@ -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<string> playerSlots = new List<string>();
+64
View File
@@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
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<string> 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;
}
}
+87
View File
@@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
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;
}
}
+18 -52
View File
@@ -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<NetPacket>();
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<NetPacket> m_packetList;
private GameInfoList m_lobbyGameInfoList;
private List<NetPacket> m_packetList;
private SenderThread m_sender;
private NetParser m_parser;
}
}
+68
View File
@@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
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<NetPacket>();
}
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<NetPacket> m_packetQueue;
}
}