diff --git a/console/src/ConsoleCallback.cs b/console/src/ConsoleCallback.cs index c6f6faad..99885324 100644 --- a/console/src/ConsoleCallback.cs +++ b/console/src/ConsoleCallback.cs @@ -1,10 +1,30 @@ -using System; +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; using System.Collections.Generic; using System.Text; +using pokerth_lib; namespace pokerth_console { - class ConsoleCallback : ICallback + class ConsoleCallback : pokerth_lib.ICallback { public void InitDone() { @@ -28,7 +48,7 @@ namespace pokerth_console Console.WriteLine("Game was started. Players: {0}", outPlayers); } - public void HandStarted(Hand h) + public void HandStarted(pokerth_lib.Hand h) { Console.WriteLine("New hand. Your cards: {0} {1}. Your money: {2}.", Log.CardToString(h.Players[h.MyPlayerId].Cards[0]), diff --git a/console/src/Program.cs b/console/src/Program.cs index e8e7e5c0..eb357e53 100644 --- a/console/src/Program.cs +++ b/console/src/Program.cs @@ -21,6 +21,7 @@ using System; using System.Collections.Generic; using System.Text; using System.Threading; +using pokerth_lib; namespace pokerth_console { diff --git a/console/src/GameInfo.cs b/console/src/lib/GameInfo.cs similarity index 94% rename from console/src/GameInfo.cs rename to console/src/lib/GameInfo.cs index b11ef153..6d6192ac 100644 --- a/console/src/GameInfo.cs +++ b/console/src/lib/GameInfo.cs @@ -21,9 +21,9 @@ using System; using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - class GameInfo : IdObject + public class GameInfo : IdObject { public enum Mode { diff --git a/console/src/GameInfoList.cs b/console/src/lib/GameInfoList.cs similarity index 94% rename from console/src/GameInfoList.cs rename to console/src/lib/GameInfoList.cs index 372746e5..dbf7776d 100644 --- a/console/src/GameInfoList.cs +++ b/console/src/lib/GameInfoList.cs @@ -22,9 +22,9 @@ using System.Collections.Generic; using System.Text; using System.IO; -namespace pokerth_console +namespace pokerth_lib { - class GameInfoList + public class GameInfoList { public GameInfoList() { diff --git a/console/src/Hand.cs b/console/src/lib/Hand.cs similarity index 94% rename from console/src/Hand.cs rename to console/src/lib/Hand.cs index 19f93b4e..b3437920 100644 --- a/console/src/Hand.cs +++ b/console/src/lib/Hand.cs @@ -21,9 +21,9 @@ using System; using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - class Hand + public class Hand { public const int MaxPlayers = 7; diff --git a/console/src/ICallback.cs b/console/src/lib/ICallback.cs similarity index 78% rename from console/src/ICallback.cs rename to console/src/lib/ICallback.cs index f1aa4d9e..ef04ebc3 100644 --- a/console/src/ICallback.cs +++ b/console/src/lib/ICallback.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - interface ICallback + public interface ICallback { void InitDone(); void JoinedGame(string name); diff --git a/console/src/IdObject.cs b/console/src/lib/IdObject.cs similarity index 94% rename from console/src/IdObject.cs rename to console/src/lib/IdObject.cs index 09ea3b95..857cf059 100644 --- a/console/src/IdObject.cs +++ b/console/src/lib/IdObject.cs @@ -21,9 +21,9 @@ using System; using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - class IdObject + public class IdObject { public IdObject(uint id, string name) { diff --git a/console/src/Log.cs b/console/src/lib/Log.cs similarity index 91% rename from console/src/Log.cs rename to console/src/lib/Log.cs index 56cc07d1..a44f80d7 100644 --- a/console/src/Log.cs +++ b/console/src/lib/Log.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - class Log + public class Log { public static string CardToString(int card) { diff --git a/console/src/Player.cs b/console/src/lib/Player.cs similarity index 87% rename from console/src/Player.cs rename to console/src/lib/Player.cs index 9ebc6c1d..d4322b51 100644 --- a/console/src/Player.cs +++ b/console/src/lib/Player.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - class Player + public class Player { public Player() { diff --git a/console/src/PlayerInfo.cs b/console/src/lib/PlayerInfo.cs similarity index 94% rename from console/src/PlayerInfo.cs rename to console/src/lib/PlayerInfo.cs index 308c9ea5..c4e45640 100644 --- a/console/src/PlayerInfo.cs +++ b/console/src/lib/PlayerInfo.cs @@ -21,9 +21,9 @@ using System; using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - class PlayerInfo : IdObject + public class PlayerInfo : IdObject { public PlayerInfo(uint id, string name) : base(id, name) diff --git a/console/src/PlayerInfoList.cs b/console/src/lib/PlayerInfoList.cs similarity index 94% rename from console/src/PlayerInfoList.cs rename to console/src/lib/PlayerInfoList.cs index 16b4a2e4..66e3a664 100644 --- a/console/src/PlayerInfoList.cs +++ b/console/src/lib/PlayerInfoList.cs @@ -22,9 +22,9 @@ using System.Collections.Generic; using System.Text; using System.IO; -namespace pokerth_console +namespace pokerth_lib { - class PlayerInfoList + public class PlayerInfoList { public PlayerInfoList() { diff --git a/console/src/PokerTHData.cs b/console/src/lib/PokerTHData.cs similarity index 93% rename from console/src/PokerTHData.cs rename to console/src/lib/PokerTHData.cs index 58615a25..8b56dbca 100644 --- a/console/src/PokerTHData.cs +++ b/console/src/lib/PokerTHData.cs @@ -21,9 +21,9 @@ using System; using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - class PokerTHData + public class PokerTHData { public PokerTHData(string name) { diff --git a/console/src/ServerSettings.cs b/console/src/lib/ServerSettings.cs similarity index 94% rename from console/src/ServerSettings.cs rename to console/src/lib/ServerSettings.cs index 037dc63d..fff0eb4b 100644 --- a/console/src/ServerSettings.cs +++ b/console/src/lib/ServerSettings.cs @@ -21,9 +21,9 @@ using System; using System.Collections.Generic; using System.Text; -namespace pokerth_console +namespace pokerth_lib { - class ServerSettings + public class ServerSettings { public ServerSettings() { diff --git a/console/src/Settings.cs b/console/src/lib/Settings.cs similarity index 95% rename from console/src/Settings.cs rename to console/src/lib/Settings.cs index e8c6a607..6aedaf04 100644 --- a/console/src/Settings.cs +++ b/console/src/lib/Settings.cs @@ -25,9 +25,9 @@ using System.Net; using System.Net.Sockets; using System.IO; -namespace pokerth_console +namespace pokerth_lib { - class Settings + public class Settings { private const string ServerListUrl = "http://pokerth.net/serverlist.xml.z"; diff --git a/console/src/ZlibHelper.cs b/console/src/lib/ZlibHelper.cs similarity index 96% rename from console/src/ZlibHelper.cs rename to console/src/lib/ZlibHelper.cs index 19e7e3f8..63af5edf 100644 --- a/console/src/ZlibHelper.cs +++ b/console/src/lib/ZlibHelper.cs @@ -23,7 +23,7 @@ using System.Text; using System.IO; using zlib; -namespace pokerth_console +namespace pokerth_lib { class ZlibHelper { diff --git a/console/src/lib/net/Client.cs b/console/src/lib/net/Client.cs new file mode 100644 index 00000000..ddc03346 --- /dev/null +++ b/console/src/lib/net/Client.cs @@ -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 . * + ***************************************************************************/ + +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; + } +} diff --git a/console/src/lib/net/INetPacketVisitor.cs b/console/src/lib/net/INetPacketVisitor.cs new file mode 100644 index 00000000..8b8f0c64 --- /dev/null +++ b/console/src/lib/net/INetPacketVisitor.cs @@ -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 . * + ***************************************************************************/ + +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); + } +} diff --git a/console/src/lib/net/NetPacket.cs b/console/src/lib/net/NetPacket.cs new file mode 100644 index 00000000..aa5440cc --- /dev/null +++ b/console/src/lib/net/NetPacket.cs @@ -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 . * + ***************************************************************************/ + +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 blindSlots = new List(); + 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(); + m_listProperties = new Dictionary>(); + } + + public Dictionary Properties + { + set + { + m_properties = value; + } + get + { + return m_properties; + } + } + + public Dictionary> 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 m_properties; + private Dictionary> m_listProperties; + } +} diff --git a/console/src/lib/net/NetPacketException.cs b/console/src/lib/net/NetPacketException.cs new file mode 100644 index 00000000..b21e2abd --- /dev/null +++ b/console/src/lib/net/NetPacketException.cs @@ -0,0 +1,33 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace pokerth_lib +{ + class NetPacketException : Exception + { + public NetPacketException(string message) + : base(message) + { + } + } +} diff --git a/console/src/lib/net/NetPacketGameListNew.cs b/console/src/lib/net/NetPacketGameListNew.cs new file mode 100644 index 00000000..399ca3a1 --- /dev/null +++ b/console/src/lib/net/NetPacketGameListNew.cs @@ -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 . * + ***************************************************************************/ + +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 playerSlots = new List(); + 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(); + } + } +} diff --git a/console/src/lib/net/NetPacketGameListUpdate.cs b/console/src/lib/net/NetPacketGameListUpdate.cs new file mode 100644 index 00000000..a087780c --- /dev/null +++ b/console/src/lib/net/NetPacketGameListUpdate.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketGameStart.cs b/console/src/lib/net/NetPacketGameStart.cs new file mode 100644 index 00000000..5c297125 --- /dev/null +++ b/console/src/lib/net/NetPacketGameStart.cs @@ -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 . * + ***************************************************************************/ + +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 playerSlots = new List(); + 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(); + } + } +} diff --git a/console/src/lib/net/NetPacketHandStart.cs b/console/src/lib/net/NetPacketHandStart.cs new file mode 100644 index 00000000..8b354760 --- /dev/null +++ b/console/src/lib/net/NetPacketHandStart.cs @@ -0,0 +1,69 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.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(); + } + } +} diff --git a/console/src/lib/net/NetPacketInit.cs b/console/src/lib/net/NetPacketInit.cs new file mode 100644 index 00000000..00b4913d --- /dev/null +++ b/console/src/lib/net/NetPacketInit.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketInitAck.cs b/console/src/lib/net/NetPacketInitAck.cs new file mode 100644 index 00000000..e8a704a9 --- /dev/null +++ b/console/src/lib/net/NetPacketInitAck.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketJoinGame.cs b/console/src/lib/net/NetPacketJoinGame.cs new file mode 100644 index 00000000..f8a1e5c7 --- /dev/null +++ b/console/src/lib/net/NetPacketJoinGame.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketJoinGameAck.cs b/console/src/lib/net/NetPacketJoinGameAck.cs new file mode 100644 index 00000000..6d723d4c --- /dev/null +++ b/console/src/lib/net/NetPacketJoinGameAck.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketPlayerInfo.cs b/console/src/lib/net/NetPacketPlayerInfo.cs new file mode 100644 index 00000000..397cf0a3 --- /dev/null +++ b/console/src/lib/net/NetPacketPlayerInfo.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketPlayersAction.cs b/console/src/lib/net/NetPacketPlayersAction.cs new file mode 100644 index 00000000..93a67bfa --- /dev/null +++ b/console/src/lib/net/NetPacketPlayersAction.cs @@ -0,0 +1,68 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.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(); + } + } +} diff --git a/console/src/lib/net/NetPacketPlayersActionDone.cs b/console/src/lib/net/NetPacketPlayersActionDone.cs new file mode 100644 index 00000000..b1a20cc6 --- /dev/null +++ b/console/src/lib/net/NetPacketPlayersActionDone.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketPlayersActionRejected.cs b/console/src/lib/net/NetPacketPlayersActionRejected.cs new file mode 100644 index 00000000..22207374 --- /dev/null +++ b/console/src/lib/net/NetPacketPlayersActionRejected.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketPlayersTurn.cs b/console/src/lib/net/NetPacketPlayersTurn.cs new file mode 100644 index 00000000..7cd71f23 --- /dev/null +++ b/console/src/lib/net/NetPacketPlayersTurn.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketRetrievePlayerInfo.cs b/console/src/lib/net/NetPacketRetrievePlayerInfo.cs new file mode 100644 index 00000000..1b9e4ea0 --- /dev/null +++ b/console/src/lib/net/NetPacketRetrievePlayerInfo.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketStartEvent.cs b/console/src/lib/net/NetPacketStartEvent.cs new file mode 100644 index 00000000..e4b7ffed --- /dev/null +++ b/console/src/lib/net/NetPacketStartEvent.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetPacketStartEventAck.cs b/console/src/lib/net/NetPacketStartEventAck.cs new file mode 100644 index 00000000..e659ef5d --- /dev/null +++ b/console/src/lib/net/NetPacketStartEventAck.cs @@ -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 . * + ***************************************************************************/ + +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(); + } + } +} diff --git a/console/src/lib/net/NetParser.cs b/console/src/lib/net/NetParser.cs new file mode 100644 index 00000000..78566b92 --- /dev/null +++ b/console/src/lib/net/NetParser.cs @@ -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 . * + ***************************************************************************/ + +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(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 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 strPlayers = new List(); + + List slots = p.ListProperties[NetPacket.ListPropertyType.PlayerSlots]. + ConvertAll(Convert.ToUInt32); + m_players = new Dictionary(); + + 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 m_players; + } +} diff --git a/console/src/lib/net/NetThread.cs b/console/src/lib/net/NetThread.cs new file mode 100644 index 00000000..46e1f117 --- /dev/null +++ b/console/src/lib/net/NetThread.cs @@ -0,0 +1,87 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Net; +using System.Net.Sockets; +using System.IO; + +namespace pokerth_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; + } +} diff --git a/console/src/lib/net/ReceiverThread.cs b/console/src/lib/net/ReceiverThread.cs new file mode 100644 index 00000000..7accaaf1 --- /dev/null +++ b/console/src/lib/net/ReceiverThread.cs @@ -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 . * + ***************************************************************************/ + +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(); + 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 m_packetList; + private SenderThread m_sender; + private NetParser m_parser; + } +} diff --git a/console/src/lib/net/SenderThread.cs b/console/src/lib/net/SenderThread.cs new file mode 100644 index 00000000..3b9fc825 --- /dev/null +++ b/console/src/lib/net/SenderThread.cs @@ -0,0 +1,68 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Net; +using System.Net.Sockets; +using System.IO; + +namespace pokerth_lib +{ + class SenderThread : NetThread + { + public SenderThread(NetworkStream stream) + : base(stream) + { + m_packetQueue = new Queue(); + } + + public void Send(NetPacket p) + { + lock (m_packetQueue) + { + m_packetQueue.Enqueue(p); + } + } + + protected override void Start() + { + while (!IsTerminateFlagSet()) + { + bool sleep = false; + lock (m_packetQueue) + { + if (m_packetQueue.Count > 0) + { + byte[] outBuf = m_packetQueue.Dequeue().ToByteArray(); + NetStream.Write(outBuf, 0, outBuf.Length); + } + else + sleep = true; + } + if (sleep) + Thread.Sleep(15); + } + } + + private Queue m_packetQueue; + } +}