Console client: Separate lib stuff from console client, in order to use it for tests.

This commit is contained in:
lotodore
2008-12-23 17:24:31 +00:00
parent 04616ef89c
commit 457ce36a98
39 changed files with 2165 additions and 28 deletions
+97
View File
@@ -0,0 +1,97 @@
/***************************************************************************
* 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_lib
{
public class GameInfo : IdObject
{
public enum Mode
{
Created = 1,
Started,
Closed
}
public GameInfo(uint id, string name, Mode mode, List<uint> playerSlots, uint startMoney)
: base(id, name)
{
m_mutex = new Object();
m_mode = mode;
m_playerSlots = playerSlots;
m_startMoney = startMoney;
}
public List<uint> PlayerSlots
{
get
{
lock (m_playerSlots)
{
// returns a copy(!)
return new List<uint>(m_playerSlots);
}
}
set
{
lock (m_playerSlots)
{
m_playerSlots = value;
}
}
}
public Mode CurrentMode
{
get
{
lock (m_mutex)
{
return m_mode;
}
}
set
{
lock (m_mutex)
{
m_mode = value;
}
}
}
public uint StartMoney
{
get
{
lock (m_mutex)
{
return m_startMoney;
}
}
}
private Object m_mutex;
private Mode m_mode;
private List<uint> m_playerSlots;
private uint m_startMoney;
}
}
+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.IO;
namespace pokerth_lib
{
public class GameInfoList
{
public GameInfoList()
{
m_list = new Dictionary<uint, GameInfo>();
}
public void AddGameInfo(GameInfo info)
{
lock (m_list)
{
if (m_list.ContainsKey(info.Id))
m_list[info.Id] = info;
else
m_list.Add(info.Id, info);
}
}
public GameInfo GetGameInfo(uint id)
{
lock (m_list)
{
return m_list[id];
}
}
public void SetGameInfo(uint id, GameInfo info)
{
lock (m_list)
{
m_list[id] = info;
}
}
public void RemoveGameInfo(uint id)
{
lock (m_list)
{
m_list.Remove(id);
}
}
public override string ToString()
{
string outString = "";
lock (m_list)
{
foreach (KeyValuePair<uint, GameInfo> i in m_list)
{
outString += i.Key;
outString += " ";
outString += i.Value.Name;
outString += '\n';
}
}
return outString;
}
private Dictionary<uint, GameInfo> m_list;
}
}
+92
View File
@@ -0,0 +1,92 @@
/***************************************************************************
* 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_lib
{
public class Hand
{
public const int MaxPlayers = 7;
public enum State
{
Preflop = 0,
Flop,
Turn,
River
}
public Hand(Dictionary<uint, Player> players, uint myPlayerId, uint smallBlind)
{
m_mutex = new Object();
m_state = State.Preflop;
m_players = players;
m_myPlayerId = myPlayerId;
}
public State CurState
{
get
{
lock (m_mutex)
{
return m_state;
}
}
set
{
lock (m_mutex)
{
m_state = value;
}
}
}
public Dictionary<uint, Player> Players
{
get
{
lock (m_mutex)
{
// Should not be modified. This is actually a const return ;-).
return m_players;
}
}
}
public uint MyPlayerId
{
get
{
lock (m_mutex)
{
return m_myPlayerId;
}
}
}
private Object m_mutex;
private State m_state;
private Dictionary<uint, Player> m_players;
uint m_myPlayerId;
}
}
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace pokerth_lib
{
public interface ICallback
{
void InitDone();
void JoinedGame(string name);
void GameStarted(List<string> players);
void HandStarted(Hand h);
void Error(string message);
}
}
+55
View File
@@ -0,0 +1,55 @@
/***************************************************************************
* 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_lib
{
public class IdObject
{
public IdObject(uint id, string name)
{
m_id = id;
m_name = name;
}
public uint Id
{
get
{
// No lock because only read access.
return m_id;
}
}
public string Name
{
get
{
// No lock because only read access.
return m_name;
}
}
private uint m_id;
private string m_name;
}
}
+81
View File
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace pokerth_lib
{
public class Log
{
public static string CardToString(int card)
{
int cardValue = card % 13;
int cardColor = card / 13;
string cardString;
switch (cardValue)
{
case 0:
cardString = "2";
break;
case 1:
cardString = "3";
break;
case 2:
cardString = "4";
break;
case 3:
cardString = "5";
break;
case 4:
cardString = "6";
break;
case 5:
cardString = "7";
break;
case 6:
cardString = "8";
break;
case 7:
cardString = "9";
break;
case 8:
cardString = "T";
break;
case 9:
cardString = "J";
break;
case 10:
cardString = "Q";
break;
case 11:
cardString = "K";
break;
case 12:
cardString = "A";
break;
default:
cardString = "Invalid Card ";
break;
}
switch (cardColor)
{
case 0 :
cardString += "d";
break;
case 1 :
cardString += "h";
break;
case 2 :
cardString += "s";
break;
case 3:
cardString += "c";
break;
default:
cardString += " Invalid Color";
break;
}
return cardString;
}
}
}
+74
View File
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace pokerth_lib
{
public class Player
{
public Player()
{
m_mutex = new Object();
}
public int[] Cards
{
get
{
lock (m_mutex)
{
// Return a copy of the array.
return (int[])m_cards.Clone();
}
}
set
{
lock (m_mutex)
{
m_cards = value;
}
}
}
public uint Money
{
get
{
lock (m_mutex)
{
return m_money;
}
}
set
{
lock (m_mutex)
{
m_money = value;
}
}
}
private uint TotalBet
{
get
{
lock (m_mutex)
{
return m_totalBet;
}
}
set
{
lock (m_mutex)
{
m_totalBet = value;
}
}
}
private Object m_mutex;
private int[] m_cards;
private uint m_money;
private uint m_totalBet;
}
}
+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_lib
{
public class PlayerInfo : IdObject
{
public PlayerInfo(uint id, string name)
: base(id, name)
{
}
}
}
+77
View File
@@ -0,0 +1,77 @@
/***************************************************************************
* 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_lib
{
public 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 PlayerInfo GetPlayerInfo(uint id)
{
lock (m_list)
{
return m_list[id];
}
}
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;
}
}
+124
View File
@@ -0,0 +1,124 @@
/***************************************************************************
* 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_lib
{
public class PokerTHData
{
public PokerTHData(string name)
{
m_gameInfoList = new GameInfoList();
m_playerInfoList = new PlayerInfoList();
m_mutex = new Object();
m_myPlayerId = 0;
m_myGameId = 0;
m_myName = name;
}
public GameInfoList GameList
{
get
{
return m_gameInfoList;
}
}
public PlayerInfoList PlayerList
{
get
{
return m_playerInfoList;
}
}
public Hand CurHand
{
get
{
lock (m_mutex)
{
return m_curHand;
}
}
set
{
lock (m_mutex)
{
m_curHand = value;
}
}
}
public string MyName
{
get
{
return m_myName;
}
}
public uint MyPlayerId
{
get
{
lock (m_mutex)
{
return m_myPlayerId;
}
}
set
{
lock (m_mutex)
{
m_myPlayerId = value;
}
}
}
public uint MyGameId
{
get
{
lock (m_mutex)
{
return m_myGameId;
}
}
set
{
lock (m_mutex)
{
m_myGameId = value;
}
}
}
private GameInfoList m_gameInfoList;
private PlayerInfoList m_playerInfoList;
private Hand m_curHand;
private Object m_mutex;
private uint m_myPlayerId;
private uint m_myGameId;
private string m_myName;
}
}
+72
View File
@@ -0,0 +1,72 @@
/***************************************************************************
* 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_lib
{
public 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;
}
}
+97
View File
@@ -0,0 +1,97 @@
/***************************************************************************
* 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.Xml;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_lib
{
public 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;
}
}
+70
View File
@@ -0,0 +1,70 @@
/***************************************************************************
* 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;
using zlib;
namespace pokerth_lib
{
class ZlibHelper
{
public static void UncompressFile(string compressedFile, string outputFile)
{
ZStream zStream = new ZStream();
zStream.inflateInit();
FileStream inputStream = File.Open(compressedFile, FileMode.Open,FileAccess.Read);
FileStream outputStream = File.Open(outputFile, FileMode.Create, FileAccess.Write);
const int InBufSize = 4096;
const int OutBufSize = 8192;
byte[] inBuf = new byte[InBufSize];
byte[] outBuf = new byte[OutBufSize];
int bytesRead;
int ret;
do
{
bytesRead = inputStream.Read(inBuf, 0, InBufSize);
if (bytesRead == 0)
throw new IOException("Unexpected end-of-file during uncompression.");
zStream.next_in = inBuf;
zStream.next_in_index = 0;
zStream.avail_in = bytesRead;
do
{
zStream.next_out = outBuf;
zStream.next_out_index = 0;
zStream.avail_out = OutBufSize;
ret = zStream.inflate(zlibConst.Z_NO_FLUSH);
if (ret != zlibConst.Z_OK && ret != zlibConst.Z_STREAM_END)
throw new IOException("Error uncompressing file: " + zStream.msg);
outputStream.Write(outBuf, 0, OutBufSize - zStream.avail_out);
} while (zStream.avail_out == 0);
} while (ret != zlibConst.Z_STREAM_END);
zStream.inflateEnd();
// Close files here, because otherwise it might take some time.
inputStream.Close();
outputStream.Close();
}
}
}
+110
View File
@@ -0,0 +1,110 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
namespace pokerth_lib
{
public class Client
{
public Client(Settings settings, PokerTHData data, ICallback callback)
{
m_tcpClient = new TcpClient();
m_settings = settings;
m_data = data;
m_callback = callback;
}
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);*/
m_tcpClient.Connect("localhost", 7234);
}
public void Start()
{
StartSendThread();
StartReceiveThread();
SendInit();
}
public void JoinGame(uint gameId)
{
SendJoinGame(gameId);
}
public void SetTerminateFlag()
{
m_sender.SetTerminateFlag();
m_receiver.SetTerminateFlag();
}
public void WaitTermination()
{
m_sender.WaitTermination();
m_receiver.WaitTermination();
}
protected void StartReceiveThread()
{
m_receiver = new ReceiverThread(m_tcpClient.GetStream(), m_sender, m_data, m_callback);
m_receiver.Run();
}
protected void StartSendThread()
{
m_sender = new SenderThread(m_tcpClient.GetStream());
m_sender.Run();
}
protected void SendInit()
{
NetPacket init = new NetPacketInit();
init.Properties.Add(NetPacket.PropType.RequestedVersionMajor, "5");
init.Properties.Add(NetPacket.PropType.RequestedVersionMinor, "0");
init.Properties.Add(NetPacket.PropType.PlayerName, m_data.MyName);
init.Properties.Add(NetPacket.PropType.PlayerPassword, "");
m_sender.Send(init);
}
protected void SendJoinGame(uint gameId)
{
NetPacket join = new NetPacketJoinGame();
join.Properties.Add(NetPacket.PropType.GameId, Convert.ToString(gameId));
join.Properties.Add(NetPacket.PropType.GamePassword, ""); // no password for now
m_sender.Send(join);
}
private TcpClient m_tcpClient;
private ReceiverThread m_receiver;
private SenderThread m_sender;
private Settings m_settings;
private PokerTHData m_data;
private ICallback m_callback;
}
}
+45
View File
@@ -0,0 +1,45 @@
/***************************************************************************
* 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_lib
{
interface INetPacketVisitor
{
void VisitInit(NetPacketInit p);
void VisitInitAck(NetPacketInitAck p);
void VisitGameListNew(NetPacketGameListNew p);
void VisitGameListUpdate(NetPacketGameListUpdate p);
void VisitRetrievePlayerInfo(NetPacketRetrievePlayerInfo p);
void VisitPlayerInfo(NetPacketPlayerInfo p);
void VisitJoinGame(NetPacketJoinGame p);
void VisitJoinGameAck(NetPacketJoinGameAck p);
void VisitStartEvent(NetPacketStartEvent p);
void VisitStartEventAck(NetPacketStartEventAck p);
void VisitGameStart(NetPacketGameStart p);
void VisitHandStart(NetPacketHandStart p);
void VisitPlayersTurn(NetPacketPlayersTurn p);
void VisitPlayersAction(NetPacketPlayersAction p);
void VisitPlayersActionDone(NetPacketPlayersActionDone p);
void VisitPlayersActionRejected(NetPacketPlayersActionRejected p);
}
}
+264
View File
@@ -0,0 +1,264 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_lib
{
abstract class NetPacket
{
public const int NetTypeInit = 0x0001;
public const int NetTypeInitAck = 0x0002;
public const int NetTypeRetrieveAvatar = 0x0003;
public const int NetTypeAvatarHeader = 0x0004;
public const int NetTypeAvatarFile = 0x0005;
public const int NetTypeAvatarEnd = 0x0006;
public const int NetTypeUnknownAvatar = 0x0007;
public const int NetTypeGameListNew = 0x0010;
public const int NetTypeGameListUpdate = 0x0011;
public const int NetTypeGameListPlayerJoined = 0x0012;
public const int NetTypeGameListPlayerLeft = 0x0013;
public const int NetTypeGameListAdminChanged = 0x0014;
public const int NetTypeRetrievePlayerInfo = 0x0020;
public const int NetTypePlayerInfo = 0x0021;
public const int NetTypeUnknownPlayerId = 0x0022;
public const int NetTypeUnsubscribeGameList = 0x0023;
public const int NetTypeResubscribeGameList = 0x0024;
public const int NetTypeCreateGame = 0x0030;
public const int NetTypeJoinGame = 0x0031;
public const int NetTypeJoinGameAck = 0x0032;
public const int NetTypeJoinGameFailed = 0x0033;
public const int NetTypePlayerJoined = 0x0034;
public const int NetTypePlayerLeft = 0x0035;
public const int NetTypeGameAdminChanged = 0x0036;
public const int NetTypeKickPlayer = 0x0040;
public const int NetTypeLeaveCurrentGame = 0x0041;
public const int NetTypeStartEvent = 0x0042;
public const int NetTypeStartEventAck = 0x0043;
public const int NetTypeGameStart = 0x0050;
public const int NetTypeHandStart = 0x0051;
public const int NetTypePlayersTurn = 0x0052;
public const int NetTypePlayersAction = 0x0053;
public const int NetTypePlayersActionDone = 0x0054;
public const int NetTypePlayersActionRejected = 0x0055;
public const int NetTypeDealFlopCards = 0x0060;
public const int NetTypeDealTurnCard = 0x0061;
public const int NetTypeDealRiverCard = 0x0062;
public const int NetTypeAllInShowCards = 0x0063;
public const int NetTypeEndOfHandShowCards = 0x0064;
public const int NetTypeEndOfHandHideCards = 0x0065;
public const int NetTypeEndOfGame = 0x0070;
public const int NetTypeAskKickPlayer = 0x0071;
public const int NetTypeAskKickPlayerDenied = 0x0072;
public const int NetTypeStartKickPlayerPetition = 0x0073;
public const int NetTypeVoteKickPlayer = 0x0074;
public const int NetTypeVoteKickPlayerAck = 0x0075;
public const int NetTypeVoteKickPlayerDenied = 0x0076;
public const int NetTypeKickPlayerPetitionUpdate = 0x0077;
public const int NetTypeEndKickPlayerPetition = 0x0078;
public const int NetTypeStatisticsChanged = 0x0080;
public const int NetTypeRemovedFromGame = 0x0100;
public const int NetTypeTimeoutWarning = 0x0101;
public const int NetTypeResetTimeout = 0x0102;
public const int NetTypeSendChatText = 0x0200;
public const int NetTypeChatText = 0x0201;
public const int NetTypeError = 0x0400;
public enum PropType
{
RequestedVersionMajor,
RequestedVersionMinor,
PlayerId,
PlayerName,
PlayerPassword,
PlayerFlags,
PlayerRights,
PlayerAction,
PlayerBet,
PlayerBetTotal,
PlayerMoney,
LatestGameVersion,
LatestBetaRevision,
SessionId,
GameId,
GameMode,
GameName,
GamePrivacyFlags,
GamePassword,
GameState,
AdminPlayerId,
MaxNumPlayers,
RaiseIntervalMode,
RaiseSmallBlindInterval,
RaiseMode,
EndRaiseMode,
ProposedGuiSpeed,
PlayerActionTimeout,
FirstSmallBlind,
EndRaiseSmallBlindValue,
StartMoney,
CurNumPlayers,
StartFlags,
StartDealerPlayerId,
FirstCard,
SecondCard,
SmallBlind,
HighestSet,
MinimumRaise,
ActionRejectReason,
}
public enum ListPropertyType
{
PlayerSlots,
ManualBlindSlots,
}
public static NetPacket Create(int type, int size, BinaryReader reader)
{
NetPacket tmpPacket = null;
switch (type)
{
// Only consider those packets which are sent by the server.
case NetTypeInitAck :
tmpPacket = new NetPacketInitAck(size, reader);
break;
case NetTypeGameListNew :
tmpPacket = new NetPacketGameListNew(size, reader);
break;
case NetTypeGameListUpdate :
tmpPacket = new NetPacketGameListUpdate(size, reader);
break;
case NetTypeJoinGameAck :
tmpPacket = new NetPacketJoinGameAck(size, reader);
break;
case NetTypePlayerInfo :
tmpPacket = new NetPacketPlayerInfo(size, reader);
break;
case NetTypeStartEvent :
tmpPacket = new NetPacketStartEvent(size, reader);
break;
case NetTypeGameStart :
tmpPacket = new NetPacketGameStart(size, reader);
break;
case NetTypeHandStart :
tmpPacket = new NetPacketHandStart(size, reader);
break;
case NetTypePlayersTurn :
tmpPacket = new NetPacketPlayersTurn(size, reader);
break;
default:
break;
}
return tmpPacket;
}
public void ScanGameInfoBlock(BinaryReader r)
{
Properties.Add(PropType.MaxNumPlayers,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.RaiseIntervalMode,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.RaiseSmallBlindInterval,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.RaiseMode,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.EndRaiseMode,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
int numManualBlinds = r.ReadUInt16();
Properties.Add(PropType.ProposedGuiSpeed,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.PlayerActionTimeout,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.FirstSmallBlind,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.EndRaiseSmallBlindValue,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.StartMoney,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
// Scan manual blinds.
List<string> blindSlots = new List<string>();
for (int i = 0; i < numManualBlinds; i++)
blindSlots.Add(Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
ListProperties.Add(ListPropertyType.ManualBlindSlots, blindSlots);
}
public NetPacket(int type)
{
m_type = type;
m_properties = new Dictionary<PropType, string>();
m_listProperties = new Dictionary<ListPropertyType, List<string>>();
}
public Dictionary<PropType, string> Properties
{
set
{
m_properties = value;
}
get
{
return m_properties;
}
}
public Dictionary<ListPropertyType, List<string>> ListProperties
{
set
{
m_listProperties = value;
}
get
{
return m_listProperties;
}
}
public int Type
{
get
{
return m_type;
}
}
public abstract void Accept(INetPacketVisitor visitor);
public abstract byte[] ToByteArray();
static protected int AddPadding(int size)
{
return ((((size) + 3) / 4) * 4);
}
private int m_type;
private Dictionary<PropType, string> m_properties;
private Dictionary<ListPropertyType, List<string>> m_listProperties;
}
}
+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_lib
{
class NetPacketException : Exception
{
public NetPacketException(string message)
: base(message)
{
}
}
}
+114
View File
@@ -0,0 +1,114 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED GameInfoData
{
u_int16_t maxNumberOfPlayers;
u_int16_t raiseIntervalMode;
u_int16_t raiseSmallBlindInterval;
u_int16_t raiseMode;
u_int16_t endRaiseMode;
u_int16_t numberOfManualBlinds;
u_int16_t proposedGuiSpeed;
u_int16_t playerActionTimeout;
u_int32_t firstSmallBlind;
u_int32_t endRaiseSmallBlindValue;
u_int32_t startMoney;
};
*/
/*
struct GCC_PACKED NetPacketGameListNewData
{
NetPacketHeader head;
u_int32_t gameId;
u_int32_t adminPlayerId;
u_int16_t gameMode;
u_int16_t gameNameLength;
u_int16_t curNumberOfPlayers;
u_int16_t gameFlags;
GameInfoData gameData;
};
*/
namespace pokerth_lib
{
class NetPacketGameListNew : NetPacket
{
public NetPacketGameListNew()
: base(NetPacket.NetTypeGameListNew)
{
}
public NetPacketGameListNew(int size, BinaryReader r)
: base(NetPacket.NetTypeGameListNew)
{
if (size < 20)
throw new NetPacketException("NetPacketGameListNew invalid size.");
Properties.Add(PropType.GameId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.AdminPlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.GameMode,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
int gameNameLen = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
int curNumPlayers = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
Properties.Add(PropType.CurNumPlayers, Convert.ToString(curNumPlayers));
Properties.Add(PropType.GamePrivacyFlags,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
ScanGameInfoBlock(r);
// Read name of the game.
byte[] tmpName = r.ReadBytes(gameNameLen);
Properties.Add(PropType.GameName,
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>();
for (int i = 0; i < curNumPlayers; i++)
playerSlots.Add(Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
ListProperties.Add(ListPropertyType.PlayerSlots, playerSlots);
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitGameListNew(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,67 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketGameListUpdateData
{
NetPacketHeader head;
u_int32_t gameId;
u_int16_t gameMode;
u_int16_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketGameListUpdate : NetPacket
{
public NetPacketGameListUpdate()
: base(NetPacket.NetTypeGameListUpdate)
{
}
public NetPacketGameListUpdate(int size, BinaryReader r)
: base(NetPacket.NetTypeGameListUpdate)
{
if (size != 12)
throw new NetPacketException("NetPacketGameListUpdate invalid size.");
Properties.Add(PropType.GameId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.GameMode,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitGameListUpdate(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
+74
View File
@@ -0,0 +1,74 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketGameStartData
{
NetPacketHeader head;
u_int32_t startDealerPlayerId;
u_int16_t numberOfPlayers;
u_int16_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketGameStart : NetPacket
{
public NetPacketGameStart()
: base(NetPacket.NetTypeGameStart)
{
}
public NetPacketGameStart(int size, BinaryReader r)
: base(NetPacket.NetTypeGameStart)
{
if (size < 20)
throw new NetPacketException("NetPacketGameStart invalid size.");
Properties.Add(PropType.StartDealerPlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
int curNumPlayers = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
Properties.Add(PropType.CurNumPlayers, Convert.ToString(curNumPlayers));
r.ReadBytes(2); // reserved
// Read player ids.
List<string> playerSlots = new List<string>();
for (int i = 0; i < curNumPlayers; i++)
playerSlots.Add(Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
ListProperties.Add(ListPropertyType.PlayerSlots, playerSlots);
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitGameStart(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
+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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketHandStartData
{
NetPacketHeader head;
u_int16_t yourCard1;
u_int16_t yourCard2;
u_int32_t smallBlind;
};
*/
namespace pokerth_lib
{
class NetPacketHandStart : NetPacket
{
public NetPacketHandStart()
: base(NetPacket.NetTypeHandStart)
{
}
public NetPacketHandStart(int size, BinaryReader r)
: base(NetPacket.NetTypeHandStart)
{
if (size != 12)
throw new NetPacketException("NetPacketHandStart invalid size.");
Properties.Add(PropType.FirstCard,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.SecondCard,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.SmallBlind,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitHandStart(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
+93
View File
@@ -0,0 +1,93 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketInitData
{
NetPacketHeader head;
u_int16_t requestedVersionMajor;
u_int16_t requestedVersionMinor;
u_int16_t passwordLength;
u_int16_t playerNameLength;
u_int16_t privacyFlags;
u_int16_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketInit : NetPacket
{
public NetPacketInit()
: base(NetPacket.NetTypeInit)
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitInit(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
BinaryWriter w = new BinaryWriter(memStream);
string playerPassword = Properties[PropType.PlayerPassword];
byte[] tmpPassword = Encoding.UTF8.GetBytes(playerPassword);
int passwordWithPadding = AddPadding(tmpPassword.Length);
string playerName = Properties[PropType.PlayerName];
byte[] tmpName = Encoding.UTF8.GetBytes(playerName);
int nameWithPadding = AddPadding(tmpName.Length);
int size = 16 + passwordWithPadding + nameWithPadding;
w.Write(IPAddress.HostToNetworkOrder((short)Type));
w.Write(IPAddress.HostToNetworkOrder((short)size));
w.Write(IPAddress.HostToNetworkOrder((short)
Convert.ToUInt16(Properties[PropType.RequestedVersionMajor])));
w.Write(IPAddress.HostToNetworkOrder((short)
Convert.ToUInt16(Properties[PropType.RequestedVersionMinor])));
w.Write(IPAddress.HostToNetworkOrder((short)playerPassword.Length));
w.Write(IPAddress.HostToNetworkOrder((short)playerName.Length));
w.Write(IPAddress.HostToNetworkOrder((short)0)); // Privacy flags.
w.Write(IPAddress.HostToNetworkOrder((short)0)); // Reserved.
w.Write(tmpPassword);
// Add padding.
int passwordPadding = passwordWithPadding - tmpPassword.Length;
if (passwordPadding > 0)
w.Write(new byte[passwordPadding]);
w.Write(tmpName);
// Add padding.
int namePadding = nameWithPadding - tmpName.Length;
if (namePadding > 0)
w.Write(new byte[namePadding]);
return memStream.ToArray();
}
}
}
+72
View File
@@ -0,0 +1,72 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketInitAckData
{
NetPacketHeader head;
u_int16_t latestGameVersion;
u_int16_t latestBetaRevision;
u_int32_t sessionId;
u_int32_t playerId;
};
*/
namespace pokerth_lib
{
class NetPacketInitAck : NetPacket
{
public NetPacketInitAck()
: base(NetPacket.NetTypeInitAck)
{
}
public NetPacketInitAck(int size, BinaryReader r)
: base(NetPacket.NetTypeInitAck)
{
if (size != 16)
throw new NetPacketException("NetPacketInitAck invalid size.");
Properties.Add(PropType.LatestGameVersion,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.LatestBetaRevision,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.SessionId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.PlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitInitAck(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
+76
View File
@@ -0,0 +1,76 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketJoinGameData
{
NetPacketHeader head;
u_int32_t gameId;
u_int16_t passwordLength;
u_int16_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketJoinGame : NetPacket
{
public NetPacketJoinGame()
: base(NetPacket.NetTypeJoinGame)
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitJoinGame(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
BinaryWriter w = new BinaryWriter(memStream);
string gamePassword = Properties[PropType.GamePassword];
byte[] tmpPassword = Encoding.UTF8.GetBytes(gamePassword);
int passwordWithPadding = AddPadding(tmpPassword.Length);
int size = 12 + passwordWithPadding;
w.Write(IPAddress.HostToNetworkOrder((short)Type));
w.Write(IPAddress.HostToNetworkOrder((short)size));
w.Write(IPAddress.HostToNetworkOrder((int)
Convert.ToUInt32(Properties[PropType.GameId])));
w.Write(IPAddress.HostToNetworkOrder((short)gamePassword.Length));
w.Write(IPAddress.HostToNetworkOrder((short)0)); // Reserved.
// Add padding.
int passwordPadding = passwordWithPadding - tmpPassword.Length;
if (passwordPadding > 0)
w.Write(new byte[passwordPadding]);
return memStream.ToArray();
}
}
}
@@ -0,0 +1,72 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketJoinGameAckData
{
NetPacketHeader head;
u_int32_t gameId;
u_int16_t playerRights;
u_int16_t reserved;
GameInfoData gameData;
};
*/
namespace pokerth_lib
{
class NetPacketJoinGameAck : NetPacket
{
public NetPacketJoinGameAck()
: base(NetPacket.NetTypeJoinGameAck)
{
}
public NetPacketJoinGameAck(int size, BinaryReader r)
: base(NetPacket.NetTypeJoinGameAck)
{
if (size < 12)
throw new NetPacketException("NetTypeJoinGameAck invalid size.");
Properties.Add(PropType.GameId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.PlayerRights,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
r.ReadUInt16(); // reserved
// Scan game info block
ScanGameInfoBlock(r);
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitJoinGameAck(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,79 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketPlayerInfoData
{
NetPacketHeader head;
u_int32_t playerId;
u_int16_t playerFlags;
u_int16_t playerNameLength;
u_int32_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketPlayerInfo : NetPacket
{
public const int PlayerFlagHuman = 0x01;
public const int PlayerFlagAvatar = 0x02;
public NetPacketPlayerInfo()
: base(NetPacket.NetTypePlayerInfo)
{
}
public NetPacketPlayerInfo(int size, BinaryReader r)
: base(NetPacket.NetTypePlayerInfo)
{
if (size < 16)
throw new NetPacketException("NetPacketPlayerInfo invalid size.");
Properties.Add(PropType.PlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
int playerFlags = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
Properties.Add(PropType.PlayerFlags, Convert.ToString(playerFlags));
int playerNameLen = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
r.ReadUInt32(); // reserved
if ((playerFlags & PlayerFlagAvatar) == PlayerFlagAvatar)
r.ReadBytes(16); // Skip avatar md5.
byte[] tmpName = r.ReadBytes(playerNameLen);
Properties.Add(PropType.PlayerName,
Encoding.UTF8.GetString(tmpName));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitPlayerInfo(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketPlayersActionData
{
NetPacketHeader head;
u_int16_t gameState;
u_int16_t playerAction;
u_int32_t playerBet;
};
*/
namespace pokerth_lib
{
class NetPacketPlayersAction : NetPacket
{
public NetPacketPlayersAction()
: base(NetPacket.NetTypePlayersAction)
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitPlayersAction(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
BinaryWriter w = new BinaryWriter(memStream);
w.Write(IPAddress.HostToNetworkOrder((short)Type));
w.Write(IPAddress.HostToNetworkOrder((short)8));
w.Write(IPAddress.HostToNetworkOrder((short)
Convert.ToUInt16(Properties[PropType.GameState])));
w.Write(IPAddress.HostToNetworkOrder((short)
Convert.ToUInt16(Properties[PropType.PlayerAction])));
w.Write(IPAddress.HostToNetworkOrder((int)
Convert.ToUInt32(Properties[PropType.PlayerBet])));
return memStream.ToArray();
}
}
}
@@ -0,0 +1,81 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketPlayersActionDoneData
{
NetPacketHeader head;
u_int32_t playerId;
u_int16_t gameState;
u_int16_t playerAction;
u_int32_t totalPlayerBet;
u_int32_t playerMoney;
u_int32_t highestSet;
u_int32_t minimumRaise;
};
*/
namespace pokerth_lib
{
class NetPacketPlayersActionDone : NetPacket
{
public NetPacketPlayersActionDone()
: base(NetPacket.NetTypePlayersActionDone)
{
}
public NetPacketPlayersActionDone(int size, BinaryReader r)
: base(NetPacket.NetTypePlayersActionDone)
{
if (size != 28)
throw new NetPacketException("NetPacketPlayersActionDone invalid size.");
Properties.Add(PropType.PlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.GameState,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.PlayerAction,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.PlayerBetTotal,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.PlayerMoney,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.HighestSet,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.MinimumRaise,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitPlayersActionDone(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,73 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketPlayersActionRejectedData
{
NetPacketHeader head;
u_int16_t gameState;
u_int16_t playerAction;
u_int32_t playerBet;
u_int16_t rejectionReason;
u_int16_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketPlayersActionRejected : NetPacket
{
public NetPacketPlayersActionRejected()
: base(NetPacket.NetTypePlayersActionRejected)
{
}
public NetPacketPlayersActionRejected(int size, BinaryReader r)
: base(NetPacket.NetTypePlayersActionRejected)
{
if (size != 28)
throw new NetPacketException("NetPacketPlayersActionRejected invalid size.");
Properties.Add(PropType.GameState,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.PlayerAction,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropType.PlayerBet,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.ActionRejectReason,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitPlayersActionRejected(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,67 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketPlayersTurnData
{
NetPacketHeader head;
u_int32_t playerId;
u_int16_t gameState;
u_int16_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketPlayersTurn : NetPacket
{
public NetPacketPlayersTurn()
: base(NetPacket.NetTypePlayersTurn)
{
}
public NetPacketPlayersTurn(int size, BinaryReader r)
: base(NetPacket.NetTypePlayersTurn)
{
if (size != 12)
throw new NetPacketException("NetPacketPlayersTurn invalid size.");
Properties.Add(PropType.PlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropType.GameState,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitPlayersTurn(this);
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,62 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketRetrievePlayerInfoData
{
NetPacketHeader head;
u_int32_t playerId;
};
*/
namespace pokerth_lib
{
class NetPacketRetrievePlayerInfo : NetPacket
{
public NetPacketRetrievePlayerInfo()
: base(NetPacket.NetTypeRetrievePlayerInfo)
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitRetrievePlayerInfo(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
BinaryWriter w = new BinaryWriter(memStream);
w.Write(IPAddress.HostToNetworkOrder((short)Type));
w.Write(IPAddress.HostToNetworkOrder((short)8));
w.Write(IPAddress.HostToNetworkOrder((int)
Convert.ToUInt32(Properties[PropType.PlayerId])));
return memStream.ToArray();
}
}
}
@@ -0,0 +1,73 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketStartEventData
{
NetPacketHeader head;
u_int16_t startFlags;
u_int16_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketStartEvent : NetPacket
{
public NetPacketStartEvent()
: base(NetPacket.NetTypeStartEvent)
{
}
public NetPacketStartEvent(int size, BinaryReader r)
: base(NetPacket.NetTypeStartEvent)
{
if (size != 8)
throw new NetPacketException("NetTypeStartEvent invalid size.");
Properties.Add(PropType.StartFlags,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitStartEvent(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
BinaryWriter w = new BinaryWriter(memStream);
w.Write(IPAddress.HostToNetworkOrder((short)Type));
w.Write(IPAddress.HostToNetworkOrder((short)8));
w.Write(IPAddress.HostToNetworkOrder((short)
Convert.ToUInt16(Properties[PropType.StartFlags])));
w.Write(IPAddress.HostToNetworkOrder((short)0)); // reserved
return memStream.ToArray();
}
}
}
@@ -0,0 +1,61 @@
/***************************************************************************
* 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.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketStartEventAckData
{
NetPacketHeader head;
u_int32_t reserved;
};
*/
namespace pokerth_lib
{
class NetPacketStartEventAck : NetPacket
{
public NetPacketStartEventAck()
: base(NetPacket.NetTypeStartEventAck)
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitStartEventAck(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
BinaryWriter w = new BinaryWriter(memStream);
w.Write(IPAddress.HostToNetworkOrder((short)Type));
w.Write(IPAddress.HostToNetworkOrder((short)8));
w.Write(IPAddress.HostToNetworkOrder((int)0)); // reserved
return memStream.ToArray();
}
}
}
+184
View File
@@ -0,0 +1,184 @@
/***************************************************************************
* 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_lib
{
class NetParser : INetPacketVisitor
{
public NetParser(PokerTHData data, SenderThread sender, ICallback callback)
{
m_data = data;
m_sender = sender;
m_callback = callback;
}
public void VisitInit(NetPacketInit p)
{
throw new NotImplementedException();
}
public void VisitInitAck(NetPacketInitAck p)
{
m_data.MyPlayerId =
Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]);
m_callback.InitDone();
}
public void VisitGameListNew(NetPacketGameListNew p)
{
// Add game to list.
m_data.GameList.AddGameInfo(new GameInfo(
Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]),
p.Properties[NetPacket.PropType.GameName],
(GameInfo.Mode)Convert.ToInt32(p.Properties[NetPacket.PropType.GameMode]),
p.ListProperties[NetPacket.ListPropertyType.PlayerSlots].
ConvertAll<uint>(Convert.ToUInt32),
Convert.ToUInt32(p.Properties[NetPacket.PropType.StartMoney])));
}
public void VisitGameListUpdate(NetPacketGameListUpdate p)
{
GameInfo.Mode mode =
(GameInfo.Mode)Convert.ToInt32(p.Properties[NetPacket.PropType.GameMode]);
uint id = Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]);
if (mode == GameInfo.Mode.Closed) // Remove game if it is has been closed.
m_data.GameList.RemoveGameInfo(id);
else
m_data.GameList.GetGameInfo(id).CurrentMode = mode;
}
public void VisitRetrievePlayerInfo(NetPacketRetrievePlayerInfo p)
{
throw new NotImplementedException();
}
public void VisitPlayerInfo(NetPacketPlayerInfo p)
{
// Add player to list.
m_data.PlayerList.AddPlayerInfo(new PlayerInfo(
Convert.ToUInt32(p.Properties[NetPacket.PropType.PlayerId]),
p.Properties[NetPacket.PropType.PlayerName]));
}
public void VisitJoinGame(NetPacketJoinGame p)
{
throw new NotImplementedException();
}
public void VisitJoinGameAck(NetPacketJoinGameAck p)
{
m_data.MyGameId =
Convert.ToUInt32(p.Properties[NetPacket.PropType.GameId]);
m_callback.JoinedGame(m_data.GameList.GetGameInfo(m_data.MyGameId).Name);
}
public void VisitStartEvent(NetPacketStartEvent p)
{
// Request player names for player ids.
List<uint> playerSlots = m_data.GameList.GetGameInfo(m_data.MyGameId).PlayerSlots;
foreach (uint id in playerSlots)
{
if (!m_data.PlayerList.HasPlayer(id))
{
NetPacketRetrievePlayerInfo request = new NetPacketRetrievePlayerInfo();
request.Properties.Add(NetPacket.PropType.PlayerId, Convert.ToString(id));
m_sender.Send(request);
}
}
// Acknowledge start event.
NetPacketStartEventAck ack = new NetPacketStartEventAck();
m_sender.Send(ack);
}
public void VisitStartEventAck(NetPacketStartEventAck p)
{
throw new NotImplementedException();
}
public void VisitGameStart(NetPacketGameStart p)
{
// Generate player list, for gui and as hand data.
List<string> strPlayers = new List<string>();
List<uint> slots = p.ListProperties[NetPacket.ListPropertyType.PlayerSlots].
ConvertAll<uint>(Convert.ToUInt32);
m_players = new Dictionary<uint, Player>();
foreach (uint i in slots)
{
if (m_data.PlayerList.HasPlayer(i))
strPlayers.Add(m_data.PlayerList.GetPlayerInfo(i).Name);
else if (i == m_data.MyPlayerId)
strPlayers.Add(m_data.MyName);
else
strPlayers.Add(Convert.ToString(i));
// Set player data.
Player tmpPlayer = new Player();
tmpPlayer.Money = m_data.GameList.GetGameInfo(m_data.MyGameId).StartMoney;
m_players.Add(i, tmpPlayer);
}
m_callback.GameStarted(strPlayers);
}
public void VisitHandStart(NetPacketHandStart p)
{
int[] tmpCards = new int[2];
tmpCards[0] =
Convert.ToInt32(p.Properties[NetPacket.PropType.FirstCard]);
tmpCards[1] =
Convert.ToInt32(p.Properties[NetPacket.PropType.SecondCard]);
m_players[m_data.MyPlayerId].Cards = tmpCards;
m_data.CurHand = new Hand(
m_players,
m_data.MyPlayerId,
Convert.ToUInt32(p.Properties[NetPacket.PropType.SmallBlind]));
m_callback.HandStarted(m_data.CurHand);
}
public void VisitPlayersTurn(NetPacketPlayersTurn p)
{
// TODO
}
public void VisitPlayersAction(NetPacketPlayersAction p)
{
throw new NotImplementedException();
}
public void VisitPlayersActionDone(NetPacketPlayersActionDone p)
{
// TODO
}
public void VisitPlayersActionRejected(NetPacketPlayersActionRejected p)
{
// TODO
}
private PokerTHData m_data;
private SenderThread m_sender;
private ICallback m_callback;
private Dictionary<uint, Player> m_players;
}
}
+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_lib
{
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;
}
}
+124
View File
@@ -0,0 +1,124 @@
/***************************************************************************
* 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_lib
{
class ReceiverThread : NetThread
{
const uint MaxPacketSize = 268;
const uint MinPacketSize = 8;
public ReceiverThread(NetworkStream stream, SenderThread sender, PokerTHData data, ICallback callback)
: base(stream)
{
m_recBuf = new byte[8192];
m_recBufOffset = 0;
m_packetList = new List<NetPacket>();
m_sender = sender;
m_parser = new NetParser(data, sender, callback);
}
protected override void Start()
{
while (!IsTerminateFlagSet())
{
ReadFromStream();
ScanPackets();
ParsePackets();
}
}
protected void ReadFromStream()
{
if (NetStream.DataAvailable)
m_recBufOffset += NetStream.Read(m_recBuf, m_recBufOffset, m_recBuf.Length - m_recBufOffset);
else
Thread.Sleep(15);
}
protected void ScanPackets()
{
bool packetFound;
do
{
packetFound = false;
if (m_recBufOffset >= MinPacketSize)
{
// Treat input buffer as memory stream.
MemoryStream memStream = new MemoryStream(m_recBuf);
BinaryReader r = new BinaryReader(memStream);
int type = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
int size = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
if (m_recBufOffset >= size)
{
packetFound = true;
if (size > MaxPacketSize)
{
// Ignore packets which are too long.
m_recBufOffset -= size;
}
else
{
// Scan Packet.
NetPacket packet = NetPacket.Create(type, size, r);
if (packet != null)
m_packetList.Add(packet);
// Advance within buf.
if (m_recBufOffset > size)
{
for (int i = size, j = 0; i < m_recBufOffset; i++, j++)
{
m_recBuf[j] = m_recBuf[i];
}
m_recBufOffset -= size;
}
else
m_recBufOffset = 0;
}
}
}
}
while (packetFound);
}
protected void ParsePackets()
{
foreach (NetPacket p in m_packetList)
{
p.Accept(m_parser);
}
m_packetList.Clear();
}
private byte[] m_recBuf;
private int m_recBufOffset;
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_lib
{
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;
}
}