Adding pokerth console client. Written in C#, .NET 2.0, License is Gnu AGPL 3 or later. Very early stuff, currently only displays list of games. Runs with Mono.

This commit is contained in:
lotodore
2008-12-19 00:36:04 +00:00
parent 53af719071
commit 03c82b3e7c
13 changed files with 1528 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
namespace pokerth_console
{
class GameInfo
{
public GameInfo(string name)
{
m_name = name;
}
public override string ToString()
{
return m_name;
}
private string m_name;
}
}
+58
View File
@@ -0,0 +1,58 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace pokerth_console
{
class GameInfoList
{
public GameInfoList()
{
m_list = new List<GameInfo>();
}
public void AddGameInfo(GameInfo info)
{
lock (m_list)
{
m_list.Add(info);
}
}
public override string ToString()
{
string outString = "";
lock (m_list)
{
foreach (GameInfo i in m_list)
{
outString += i.ToString();
outString += '\n';
}
}
return outString;
}
private List<GameInfo> m_list;
}
}
+76
View File
@@ -0,0 +1,76 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace pokerth_console
{
class Program
{
static int Main(string[] args)
{
// Retrieve server list.
WebClient webcl = new WebClient();
string tmpFilePath = Path.GetTempPath();
string tmpZipFileName = tmpFilePath + "pokerth_serverlist.xml.z";
string tmpXmlFileName = tmpFilePath + "pokerth_serverlist.xml";
webcl.DownloadFile("http://pokerth.net/serverlist.xml.z", tmpZipFileName);
ZlibHelper.UncompressFile(tmpZipFileName, tmpXmlFileName);
FileStream f = new FileStream(tmpXmlFileName, FileMode.Open, FileAccess.Read);
XmlReader x = XmlReader.Create(f);
x.Read();
x.ReadStartElement("ServerList");
x.ReadStartElement("Server");
x.ReadToFollowing("IPv4Address");
x.MoveToFirstAttribute();
string ipAddress = x.Value;
x.ReadToFollowing("Port");
x.MoveToFirstAttribute();
int port = Convert.ToInt32(x.Value);
x.Close();
f.Close();
File.Delete(tmpZipFileName);
File.Delete(tmpXmlFileName);
// Connect to the server.
TcpClient client = new TcpClient(ipAddress, port);
NetPacket test = new NetPacketInit();
test.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMajor, "4");
test.Properties.Add(NetPacket.PropertyType.PropRequestedVersionMinor, "2");
test.Properties.Add(NetPacket.PropertyType.PropPlayerName, "Loto");
test.Properties.Add(NetPacket.PropertyType.PropPlayerPassword, "");
byte[] buf = test.ToByteArray();
client.GetStream().Write(buf, 0, buf.Length);
GameInfoList list = new GameInfoList();
ReceiverThread t = new ReceiverThread(client.GetStream(), list);
ReceiverThread.Run(t);
Thread.Sleep(5000);
t.SetTerminateFlag();
Console.Write(list.ToString());
t.WaitTermination();
return 0;
}
}
}
+70
View File
@@ -0,0 +1,70 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using zlib;
namespace pokerth_console
{
class ZlibHelper
{
public static void UncompressFile(string compressedFile, string outputFile)
{
ZStream zStream = new ZStream();
zStream.inflateInit();
FileStream inputStream = File.Open(compressedFile, FileMode.Open,FileAccess.Read);
FileStream outputStream = File.Open(outputFile, FileMode.Create, FileAccess.Write);
const int InBufSize = 4096;
const int OutBufSize = 8192;
byte[] inBuf = new byte[InBufSize];
byte[] outBuf = new byte[OutBufSize];
int bytesRead;
int ret;
do
{
bytesRead = inputStream.Read(inBuf, 0, InBufSize);
if (bytesRead == 0)
throw new IOException("Unexpected end-of-file during uncompression.");
zStream.next_in = inBuf;
zStream.next_in_index = 0;
zStream.avail_in = bytesRead;
do
{
zStream.next_out = outBuf;
zStream.next_out_index = 0;
zStream.avail_out = OutBufSize;
ret = zStream.inflate(zlibConst.Z_NO_FLUSH);
if (ret != zlibConst.Z_OK && ret != zlibConst.Z_STREAM_END)
throw new IOException("Error uncompressing file: " + zStream.msg);
outputStream.Write(outBuf, 0, OutBufSize - zStream.avail_out);
} while (zStream.avail_out == 0);
} while (ret != zlibConst.Z_STREAM_END);
zStream.inflateEnd();
// Close files here, because otherwise it might take some time.
inputStream.Close();
outputStream.Close();
}
}
}
+163
View File
@@ -0,0 +1,163 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace pokerth_console
{
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 PropertyType
{
PropRequestedVersionMajor,
PropRequestedVersionMinor,
PropPlayerName,
PropPlayerPassword,
PropLatestGameVersion,
PropLatestBetaRevision,
PropSessionId,
PropPlayerId,
PropGameId,
PropAdminPlayerId,
PropGameMode,
PropGameName,
PropCurNumPlayers,
PropGameFlags
}
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;
default:
break;
}
return tmpPacket;
}
public NetPacket(int type)
{
m_type = type;
m_properties = new Dictionary<PropertyType, string>();
}
public Dictionary<PropertyType, string> Properties
{
set
{
m_properties = value;
}
get
{
return m_properties;
}
}
public int Type
{
get
{
return m_type;
}
}
public abstract byte[] ToByteArray();
protected int AddPadding(int size)
{
return ((((size) + 3) / 4) * 4);
}
private int m_type;
private Dictionary<PropertyType, string> m_properties;
}
}
+33
View File
@@ -0,0 +1,33 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
namespace pokerth_console
{
class NetPacketException : Exception
{
public NetPacketException(string message)
: base(message)
{
}
}
}
+98
View File
@@ -0,0 +1,98 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED GameInfoData
{
u_int16_t maxNumberOfPlayers;
u_int16_t raiseIntervalMode;
u_int16_t raiseSmallBlindInterval;
u_int16_t raiseMode;
u_int16_t endRaiseMode;
u_int16_t numberOfManualBlinds;
u_int16_t proposedGuiSpeed;
u_int16_t playerActionTimeout;
u_int32_t firstSmallBlind;
u_int32_t endRaiseSmallBlindValue;
u_int32_t startMoney;
};
*/
/*
struct GCC_PACKED NetPacketGameListNewData
{
NetPacketHeader head;
u_int32_t gameId;
u_int32_t adminPlayerId;
u_int16_t gameMode;
u_int16_t gameNameLength;
u_int16_t curNumberOfPlayers;
u_int16_t gameFlags;
GameInfoData gameData;
};
*/
namespace pokerth_console
{
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(PropertyType.PropGameId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropertyType.PropAdminPlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropertyType.PropGameMode,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
int gameNameLen = IPAddress.NetworkToHostOrder((short)r.ReadUInt16());
Properties.Add(PropertyType.PropCurNumPlayers,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropertyType.PropGameFlags,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
r.ReadBytes(28); // Skip game info block for now.
byte[] tmpName = r.ReadBytes(gameNameLen);
Properties.Add(PropertyType.PropGameName,
Encoding.UTF8.GetString(tmpName));
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
+87
View File
@@ -0,0 +1,87 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.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_console
{
class NetPacketInit : NetPacket
{
public NetPacketInit()
: base(NetPacket.NetTypeInit)
{
}
public override byte[] ToByteArray()
{
MemoryStream memStream = new MemoryStream();
BinaryWriter w = new BinaryWriter(memStream);
string playerPassword = Properties[PropertyType.PropPlayerPassword];
byte[] tmpPassword = Encoding.UTF8.GetBytes(playerPassword);
int passwordWithPadding = AddPadding(tmpPassword.Length);
string playerName = Properties[PropertyType.PropPlayerName];
byte[] tmpName = Encoding.UTF8.GetBytes(playerName);
int nameWithPadding = AddPadding(tmpName.Length);
int size = 16 + passwordWithPadding + nameWithPadding;
w.Write(IPAddress.HostToNetworkOrder((short)NetPacket.NetTypeInit));
w.Write(IPAddress.HostToNetworkOrder((short)size));
w.Write(IPAddress.HostToNetworkOrder((short)
Convert.ToUInt16(Properties[PropertyType.PropRequestedVersionMajor])));
w.Write(IPAddress.HostToNetworkOrder((short)
Convert.ToUInt16(Properties[PropertyType.PropRequestedVersionMinor])));
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();
}
}
}
+67
View File
@@ -0,0 +1,67 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
/*
struct GCC_PACKED NetPacketInitAckData
{
NetPacketHeader head;
u_int16_t latestGameVersion;
u_int16_t latestBetaRevision;
u_int32_t sessionId;
u_int32_t playerId;
};
*/
namespace pokerth_console
{
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(PropertyType.PropLatestGameVersion,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropertyType.PropLatestBetaRevision,
Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16())));
Properties.Add(PropertyType.PropSessionId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
Properties.Add(PropertyType.PropPlayerId,
Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32())));
}
public override byte[] ToByteArray()
{
throw new NotImplementedException();
}
}
}
+165
View File
@@ -0,0 +1,165 @@
/***************************************************************************
* Copyright (C) 2008 by Lothar May *
* *
* This file is part of pokerth_console. *
* pokerth_console is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* pokerth_console is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the *
* GNU Affero General Public License along with pokerth_console. *
* If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace pokerth_console
{
class ReceiverThread
{
const uint MaxPacketSize = 268;
const uint MinPacketSize = 8;
public ReceiverThread(NetworkStream stream, GameInfoList list)
{
m_recThread = new Thread(ThreadProc);
m_terminateFlag = false;
m_terminateFlagMutex = new System.Object();
m_recStream = stream;
m_recStream.ReadTimeout = 50;
m_recBuf = new byte[8192];
m_recBufOffset = 0;
m_packetList = new List<NetPacket>();
m_lobbyGameInfoList = list;
}
public static void Run(ReceiverThread me)
{
me.m_recThread.Start(me);
}
protected static void ThreadProc(object obj)
{
ReceiverThread me = (ReceiverThread)obj;
me.Start();
}
protected void Start()
{
while (!IsTerminateFlagSet())
{
ReadFromStream();
ScanPackets();
ParsePackets();
}
}
public void WaitTermination()
{
m_recThread.Join();
}
public void SetTerminateFlag()
{
lock (m_terminateFlagMutex)
{
m_terminateFlag = true;
}
}
protected bool IsTerminateFlagSet()
{
lock (m_terminateFlagMutex)
{
return m_terminateFlag;
}
}
protected void ReadFromStream()
{
if (m_recStream.DataAvailable)
m_recBufOffset += m_recStream.Read(m_recBuf, m_recBufOffset, m_recBuf.Length - m_recBufOffset);
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)
{
if (p.Type == NetPacket.NetTypeGameListNew)
{
m_lobbyGameInfoList.AddGameInfo(new GameInfo(
p.Properties[NetPacket.PropertyType.PropGameName]));
}
}
m_packetList.Clear();
}
private Thread m_recThread;
private bool m_terminateFlag;
private Object m_terminateFlagMutex;
private NetworkStream m_recStream;
private byte[] m_recBuf;
private int m_recBufOffset;
private List<NetPacket> m_packetList;
private GameInfoList m_lobbyGameInfoList;
}
}