diff --git a/console/src/GameInfoList.cs b/console/src/GameInfoList.cs index db017b4e..1534681b 100644 --- a/console/src/GameInfoList.cs +++ b/console/src/GameInfoList.cs @@ -49,6 +49,8 @@ namespace pokerth_console { foreach (KeyValuePair i in m_list) { + outString += i.Key; + outString += " "; outString += i.Value.Name; outString += '\n'; } diff --git a/console/src/Program.cs b/console/src/Program.cs index 803e6144..9c4645dd 100644 --- a/console/src/Program.cs +++ b/console/src/Program.cs @@ -34,12 +34,15 @@ namespace pokerth_console client.Connect(); client.Start(); Thread.Sleep(5000); - client.SetTerminateFlag(); - Console.WriteLine("Games:"); + Console.WriteLine("Open Games:"); Console.Write(data.GameList.ToString()); Console.WriteLine(); - Console.WriteLine("Players:"); - Console.Write(data.PlayerList.ToString()); + Console.WriteLine("Enter game id"); + string input = Console.ReadLine(); + uint gameId = Convert.ToUInt32(input); + client.JoinGame(gameId); + Thread.Sleep(5000000); + client.SetTerminateFlag(); client.WaitTermination(); return 0; } diff --git a/console/src/net/Client.cs b/console/src/net/Client.cs index c33c2513..e43fcd67 100644 --- a/console/src/net/Client.cs +++ b/console/src/net/Client.cs @@ -42,6 +42,7 @@ namespace pokerth_console 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() @@ -51,6 +52,11 @@ namespace pokerth_console SendInit(); } + public void JoinGame(uint gameId) + { + SendJoinGame(gameId); + } + public void SetTerminateFlag() { m_sender.SetTerminateFlag(); @@ -80,11 +86,19 @@ namespace pokerth_console NetPacket init = new NetPacketInit(); init.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMajor, "4"); init.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMinor, "2"); - init.Properties.Add(NetPacket.PropertyType.PropPlayerName, "Loto"); + init.Properties.Add(NetPacket.PropertyType.PropPlayerName, "Testuser"); init.Properties.Add(NetPacket.PropertyType.PropPlayerPassword, ""); m_sender.Send(init); } + protected void SendJoinGame(uint gameId) + { + NetPacket join = new NetPacketJoinGame(); + join.Properties.Add(NetPacket.PropertyType.PropGameId, Convert.ToString(gameId)); + join.Properties.Add(NetPacket.PropertyType.PropGamePassword, ""); // no password for now + m_sender.Send(join); + } + private TcpClient m_tcpClient; private ReceiverThread m_receiver; private SenderThread m_sender; diff --git a/console/src/net/INetPacketVisitor.cs b/console/src/net/INetPacketVisitor.cs new file mode 100644 index 00000000..d5ea6f40 --- /dev/null +++ b/console/src/net/INetPacketVisitor.cs @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (C) 2008 by Lothar May * + * * + * This file is part of pokerth_console. * + * pokerth_console is free software: you can redistribute it and/or * + * modify it under the terms of the GNU Affero General Public License * + * as published by the Free Software Foundation, either version 3 of * + * the License, or (at your option) any later version. * + * * + * pokerth_console is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the * + * GNU Affero General Public License along with pokerth_console. * + * If not, see . * + ***************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace pokerth_console +{ + interface INetPacketVisitor + { + void VisitInit(NetPacketInit p); + void VisitInitAck(NetPacketInitAck p); + void VisitGameListNew(NetPacketGameListNew p); + void VisitRetrievePlayerInfo(NetPacketRetrievePlayerInfo p); + void VisitPlayerInfo(NetPacketPlayerInfo p); + void VisitJoinGame(NetPacketJoinGame p); + void VisitStartEvent(NetPacketStartEvent p); + void VisitStartEventAck(NetPacketStartEventAck p); + void VisitGameStart(NetPacketGameStart p); + } +} diff --git a/console/src/net/NetPacket.cs b/console/src/net/NetPacket.cs index bd09ac2e..a07b7e8c 100644 --- a/console/src/net/NetPacket.cs +++ b/console/src/net/NetPacket.cs @@ -105,7 +105,9 @@ namespace pokerth_console PropGamePrivacyFlags, PropGamePassword, PropAdminPlayerId, - PropCurNumPlayers + PropCurNumPlayers, + PropStartFlags, + PropStartDealerPlayerId } public enum ListPropertyType @@ -128,6 +130,9 @@ namespace pokerth_console case NetTypePlayerInfo: tmpPacket = new NetPacketPlayerInfo(size, reader); break; + case NetTypeStartEvent: + tmpPacket = new NetPacketStartEvent(size, reader); + break; default: break; } @@ -173,6 +178,8 @@ namespace pokerth_console } } + public abstract void Accept(INetPacketVisitor visitor); + public abstract byte[] ToByteArray(); protected int AddPadding(int size) diff --git a/console/src/net/NetPacketGameListNew.cs b/console/src/net/NetPacketGameListNew.cs index dbcbe7cf..fcbee8fd 100644 --- a/console/src/net/NetPacketGameListNew.cs +++ b/console/src/net/NetPacketGameListNew.cs @@ -101,6 +101,11 @@ namespace pokerth_console ListProperties.Add(ListPropertyType.PropPlayerSlots, playerSlots); } + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitGameListNew(this); + } + public override byte[] ToByteArray() { throw new NotImplementedException(); diff --git a/console/src/net/NetPacketGameStart.cs b/console/src/net/NetPacketGameStart.cs new file mode 100644 index 00000000..8e71b5eb --- /dev/null +++ b/console/src/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_console +{ + class NetPacketGameStart : NetPacket + { + public NetPacketGameStart() + : base(NetPacket.NetTypeGameStart) + { + } + + public NetPacketGameStart(int size, BinaryReader r) + : base(NetPacket.NetTypeGameListNew) + { + if (size < 20) + throw new NetPacketException("NetPacketGameStart invalid size."); + Properties.Add(PropertyType.PropStartDealerPlayerId, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + int curNumPlayers = IPAddress.NetworkToHostOrder((short)r.ReadUInt16()); + Properties.Add(PropertyType.PropCurNumPlayers, 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.PropPlayerSlots, playerSlots); + } + + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitGameStart(this); + } + + public override byte[] ToByteArray() + { + throw new NotImplementedException(); + } + } +} diff --git a/console/src/net/NetPacketInit.cs b/console/src/net/NetPacketInit.cs index ad5c4214..265ee15b 100644 --- a/console/src/net/NetPacketInit.cs +++ b/console/src/net/NetPacketInit.cs @@ -46,6 +46,11 @@ namespace pokerth_console { } + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitInit(this); + } + public override byte[] ToByteArray() { MemoryStream memStream = new MemoryStream(); diff --git a/console/src/net/NetPacketInitAck.cs b/console/src/net/NetPacketInitAck.cs index 3a01d375..7c50e482 100644 --- a/console/src/net/NetPacketInitAck.cs +++ b/console/src/net/NetPacketInitAck.cs @@ -59,6 +59,11 @@ namespace pokerth_console 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/net/NetPacketJoinGame.cs b/console/src/net/NetPacketJoinGame.cs index 36ada960..b0c2f06c 100644 --- a/console/src/net/NetPacketJoinGame.cs +++ b/console/src/net/NetPacketJoinGame.cs @@ -43,6 +43,11 @@ namespace pokerth_console { } + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitJoinGame(this); + } + public override byte[] ToByteArray() { MemoryStream memStream = new MemoryStream(); diff --git a/console/src/net/NetPacketPlayerInfo.cs b/console/src/net/NetPacketPlayerInfo.cs index 6a8a2b7e..356b891f 100644 --- a/console/src/net/NetPacketPlayerInfo.cs +++ b/console/src/net/NetPacketPlayerInfo.cs @@ -66,6 +66,11 @@ namespace pokerth_console 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/net/NetPacketRetrievePlayerInfo.cs b/console/src/net/NetPacketRetrievePlayerInfo.cs index 74f60782..6f5d0f2b 100644 --- a/console/src/net/NetPacketRetrievePlayerInfo.cs +++ b/console/src/net/NetPacketRetrievePlayerInfo.cs @@ -41,6 +41,11 @@ namespace pokerth_console { } + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitRetrievePlayerInfo(this); + } + public override byte[] ToByteArray() { MemoryStream memStream = new MemoryStream(); diff --git a/console/src/net/NetPacketStartEvent.cs b/console/src/net/NetPacketStartEvent.cs new file mode 100644 index 00000000..2b5cc505 --- /dev/null +++ b/console/src/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_console +{ + 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(PropertyType.PropStartFlags, + 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[PropertyType.PropStartFlags]))); + w.Write(IPAddress.HostToNetworkOrder((short)0)); // reserved + + return memStream.ToArray(); + } + } +} diff --git a/console/src/net/NetPacketStartEventAck.cs b/console/src/net/NetPacketStartEventAck.cs new file mode 100644 index 00000000..de9d6d69 --- /dev/null +++ b/console/src/net/NetPacketStartEventAck.cs @@ -0,0 +1,42 @@ +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_console +{ + 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/net/NetParser.cs b/console/src/net/NetParser.cs index c81fa957..dd085c25 100644 --- a/console/src/net/NetParser.cs +++ b/console/src/net/NetParser.cs @@ -23,7 +23,7 @@ using System.Text; namespace pokerth_console { - class NetParser + class NetParser : INetPacketVisitor { public NetParser(PokerTHData data, SenderThread sender) { @@ -31,7 +31,55 @@ namespace pokerth_console m_sender = sender; } - public void ParseGameListNew(NetPacket p) + public void VisitInit(NetPacketInit p) + { + throw new NotImplementedException(); + } + + public void VisitInitAck(NetPacketInitAck p) + { + // TODO + } + + public void VisitGameListNew(NetPacketGameListNew p) + { + // Add game to list. + m_data.GameList.AddGameInfo(new GameInfo( + Convert.ToUInt32(p.Properties[NetPacket.PropertyType.PropGameId]), + p.Properties[NetPacket.PropertyType.PropGameName])); + } + + 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.PropertyType.PropPlayerId]), + p.Properties[NetPacket.PropertyType.PropPlayerName])); + } + + public void VisitJoinGame(NetPacketJoinGame p) + { + throw new NotImplementedException(); + } + + public void VisitStartEvent(NetPacketStartEvent p) + { + // Acknowledge start event. + NetPacketStartEventAck ack = new NetPacketStartEventAck(); + m_sender.Send(ack); + } + + public void VisitStartEventAck(NetPacketStartEventAck p) + { + throw new NotImplementedException(); + } + + public void VisitGameStart(NetPacketGameStart p) { // Request player names for player ids. List playerList = p.ListProperties[NetPacket.ListPropertyType.PropPlayerSlots]; @@ -44,18 +92,6 @@ namespace pokerth_console m_sender.Send(request); } } - // Add game to list. - m_data.GameList.AddGameInfo(new GameInfo( - Convert.ToUInt32(p.Properties[NetPacket.PropertyType.PropGameId]), - p.Properties[NetPacket.PropertyType.PropGameName])); - } - - public void ParsePlayerInfo(NetPacket p) - { - // Add player to list. - m_data.PlayerList.AddPlayerInfo(new PlayerInfo( - Convert.ToUInt32(p.Properties[NetPacket.PropertyType.PropPlayerId]), - p.Properties[NetPacket.PropertyType.PropPlayerName])); } private PokerTHData m_data; diff --git a/console/src/net/ReceiverThread.cs b/console/src/net/ReceiverThread.cs index 07a45ab6..10d1e6b4 100644 --- a/console/src/net/ReceiverThread.cs +++ b/console/src/net/ReceiverThread.cs @@ -110,15 +110,7 @@ namespace pokerth_console { foreach (NetPacket p in m_packetList) { - switch (p.Type) - { - case NetPacket.NetTypeGameListNew : - m_parser.ParseGameListNew(p); - break; - case NetPacket.NetTypePlayerInfo : - m_parser.ParsePlayerInfo(p); - break; - } + p.Accept(m_parser); } m_packetList.Clear(); }