Console client: Now able to join game, and (passively) start game. Cannot play yet, though ;-).

This commit is contained in:
lotodore
2008-12-21 17:38:59 +00:00
parent 73778494b7
commit 0ce85c0077
16 changed files with 340 additions and 29 deletions
+2
View File
@@ -49,6 +49,8 @@ namespace pokerth_console
{
foreach (KeyValuePair<uint, GameInfo> i in m_list)
{
outString += i.Key;
outString += " ";
outString += i.Value.Name;
outString += '\n';
}
+7 -4
View File
@@ -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;
}
+15 -1
View File
@@ -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;
+38
View File
@@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
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);
}
}
+8 -1
View File
@@ -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)
+5
View File
@@ -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();
+74
View File
@@ -0,0 +1,74 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketGameStartData
{
NetPacketHeader head;
u_int32_t startDealerPlayerId;
u_int16_t numberOfPlayers;
u_int16_t reserved;
};
*/
namespace pokerth_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<string> playerSlots = new List<string>();
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();
}
}
}
+5
View File
@@ -46,6 +46,11 @@ namespace pokerth_console
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitInit(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
+5
View File
@@ -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();
+5
View File
@@ -43,6 +43,11 @@ namespace pokerth_console
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitJoinGame(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
+5
View File
@@ -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();
@@ -41,6 +41,11 @@ namespace pokerth_console
{
}
public override void Accept(INetPacketVisitor visitor)
{
visitor.VisitRetrievePlayerInfo(this);
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
+73
View File
@@ -0,0 +1,73 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketStartEventData
{
NetPacketHeader head;
u_int16_t startFlags;
u_int16_t reserved;
};
*/
namespace pokerth_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();
}
}
}
+42
View File
@@ -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();
}
}
}
+50 -14
View File
@@ -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<string> 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;
+1 -9
View File
@@ -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();
}