From 4f7cf0bdb23ea004d6b3d6e3c2c150f4ba5d19f6 Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 11 Jan 2009 11:18:51 +0000 Subject: [PATCH] Console client: Adding support for additional network packets (avatar handling). This is not specifically for use in the console, but for .NET based test cases. --- console/src/ConsoleCallback.cs | 15 +++++ console/src/lib/GameInfo.cs | 8 +++ console/src/lib/ICallback.cs | 3 + console/src/lib/net/INetPacketVisitor.cs | 5 ++ console/src/lib/net/NetPacket.cs | 24 +++++++ console/src/lib/net/NetPacketAvatarEnd.cs | 56 ++++++++++++++++ console/src/lib/net/NetPacketAvatarFile.cs | 62 +++++++++++++++++ console/src/lib/net/NetPacketAvatarHeader.cs | 60 +++++++++++++++++ .../src/lib/net/NetPacketRetrieveAvatar.cs | 67 +++++++++++++++++++ console/src/lib/net/NetPacketUnknownAvatar.cs | 56 ++++++++++++++++ console/src/lib/net/NetParser.cs | 49 ++++++++++++++ 11 files changed, 405 insertions(+) create mode 100644 console/src/lib/net/NetPacketAvatarEnd.cs create mode 100644 console/src/lib/net/NetPacketAvatarFile.cs create mode 100644 console/src/lib/net/NetPacketAvatarHeader.cs create mode 100644 console/src/lib/net/NetPacketRetrieveAvatar.cs create mode 100644 console/src/lib/net/NetPacketUnknownAvatar.cs diff --git a/console/src/ConsoleCallback.cs b/console/src/ConsoleCallback.cs index 247a236e..262559a1 100644 --- a/console/src/ConsoleCallback.cs +++ b/console/src/ConsoleCallback.cs @@ -190,5 +190,20 @@ namespace pokerth_console Console.WriteLine("Hint: Try using a different nickname."); System.Environment.Exit(1); } + + public void AvatarBegin(uint id, uint fileSize, GameInfo.AvatarFileType type) + { + throw new NotImplementedException(); + } + + public void AvatarData(uint id, byte[] block) + { + throw new NotImplementedException(); + } + + public void AvatarEnd(uint id) + { + throw new NotImplementedException(); + } } } diff --git a/console/src/lib/GameInfo.cs b/console/src/lib/GameInfo.cs index 8a6b56fe..350071c3 100644 --- a/console/src/lib/GameInfo.cs +++ b/console/src/lib/GameInfo.cs @@ -25,6 +25,14 @@ namespace pokerth_lib { public class GameInfo : IdObject { + public enum AvatarFileType + { + PNG, + GIF, + JPG, + UNKNOWN + } + public enum Mode { Created = 1, diff --git a/console/src/lib/ICallback.cs b/console/src/lib/ICallback.cs index c3371d84..ca94bff5 100644 --- a/console/src/lib/ICallback.cs +++ b/console/src/lib/ICallback.cs @@ -48,5 +48,8 @@ namespace pokerth_lib void RemovedFromGame(); void ChatText(string name, string message); void Error(string message); + void AvatarBegin(uint id, uint fileSize, GameInfo.AvatarFileType type); + void AvatarData(uint id, byte[] block); + void AvatarEnd(uint id); } } diff --git a/console/src/lib/net/INetPacketVisitor.cs b/console/src/lib/net/INetPacketVisitor.cs index 441868ca..a6075c8a 100644 --- a/console/src/lib/net/INetPacketVisitor.cs +++ b/console/src/lib/net/INetPacketVisitor.cs @@ -55,5 +55,10 @@ namespace pokerth_lib void VisitRemovedFromGame(NetPacket p); void VisitChatText(NetPacket p); void VisitError(NetPacket p); + void VisitRetrieveAvatar(NetPacket p); + void VisitAvatarHeader(NetPacket p); + void VisitAvatarFile(NetPacket p); + void VisitAvatarEnd(NetPacket p); + void VisitUnknownAvatar(NetPacket p); } } diff --git a/console/src/lib/net/NetPacket.cs b/console/src/lib/net/NetPacket.cs index 72b0c41f..3a059ec3 100644 --- a/console/src/lib/net/NetPacket.cs +++ b/console/src/lib/net/NetPacket.cs @@ -146,6 +146,12 @@ namespace pokerth_lib ChatText, ErrorReason, RemoveReason, + RequestId, + AvatarMD5, + AvatarFileSize, + AvatarFileType, + AvatarBlockSize, + AvatarFileData, } public enum ListPropType @@ -288,6 +294,9 @@ namespace pokerth_lib case NetTypeError : tmpPacket = new NetPacketError(); break; + case NetTypeRetrieveAvatar : + tmpPacket = new NetPacketRetrieveAvatar(); + break; } return tmpPacket; } @@ -367,6 +376,21 @@ namespace pokerth_lib case NetTypeError : tmpPacket = new NetPacketError(size, reader); break; + case NetTypeRetrieveAvatar : + tmpPacket = new NetPacketRetrieveAvatar(size, reader); + break; + case NetTypeAvatarHeader : + tmpPacket = new NetPacketAvatarHeader(size, reader); + break; + case NetTypeAvatarFile : + tmpPacket = new NetPacketAvatarFile(size, reader); + break; + case NetTypeAvatarEnd : + tmpPacket = new NetPacketAvatarEnd(size, reader); + break; + case NetTypeUnknownAvatar : + tmpPacket = new NetPacketUnknownAvatar(size, reader); + break; default: break; } diff --git a/console/src/lib/net/NetPacketAvatarEnd.cs b/console/src/lib/net/NetPacketAvatarEnd.cs new file mode 100644 index 00000000..bfcba34c --- /dev/null +++ b/console/src/lib/net/NetPacketAvatarEnd.cs @@ -0,0 +1,56 @@ +/*************************************************************************** + * Copyright (C) 2009 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 +{ + class NetPacketAvatarEnd : NetPacket + { + public NetPacketAvatarEnd() + : base(NetPacket.NetTypeAvatarEnd) + { + } + + public NetPacketAvatarEnd(int size, BinaryReader r) + : base(NetPacket.NetTypeAvatarEnd) + { + if (size != 8) + throw new NetPacketException("NetPacketAvatarEnd invalid size."); + Properties.Add(PropType.RequestId, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + } + + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitAvatarEnd(this); + } + + public override byte[] ToByteArray() + { + throw new NotImplementedException(); + } + } +} diff --git a/console/src/lib/net/NetPacketAvatarFile.cs b/console/src/lib/net/NetPacketAvatarFile.cs new file mode 100644 index 00000000..57d0ca39 --- /dev/null +++ b/console/src/lib/net/NetPacketAvatarFile.cs @@ -0,0 +1,62 @@ +/*************************************************************************** + * Copyright (C) 2009 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 +{ + class NetPacketAvatarFile : NetPacket + { + public NetPacketAvatarFile() + : base(NetPacket.NetTypeAvatarFile) + { + } + + public NetPacketAvatarFile(int size, BinaryReader r) + : base(NetPacket.NetTypeAvatarFile) + { + if (size < 12) + throw new NetPacketException("NetPacketAvatarFile invalid size."); + Properties.Add(PropType.RequestId, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + int avatarBlockSize = IPAddress.NetworkToHostOrder((short)r.ReadUInt16()); + Properties.Add(PropType.AvatarBlockSize, + Convert.ToString(avatarBlockSize)); + r.ReadUInt16(); // reserved + Properties.Add(PropType.AvatarFileData, + Convert.ToBase64String(r.ReadBytes(avatarBlockSize))); + } + + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitAvatarFile(this); + } + + public override byte[] ToByteArray() + { + throw new NotImplementedException(); + } + } +} diff --git a/console/src/lib/net/NetPacketAvatarHeader.cs b/console/src/lib/net/NetPacketAvatarHeader.cs new file mode 100644 index 00000000..ec365f85 --- /dev/null +++ b/console/src/lib/net/NetPacketAvatarHeader.cs @@ -0,0 +1,60 @@ +/*************************************************************************** + * Copyright (C) 2009 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 +{ + class NetPacketAvatarHeader : NetPacket + { + public NetPacketAvatarHeader() + : base(NetPacket.NetTypeAvatarHeader) + { + } + + public NetPacketAvatarHeader(int size, BinaryReader r) + : base(NetPacket.NetTypeAvatarHeader) + { + if (size != 16) + throw new NetPacketException("NetPacketAvatarHeader invalid size."); + Properties.Add(PropType.RequestId, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + Properties.Add(PropType.AvatarFileSize, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + Properties.Add(PropType.AvatarFileType, + Convert.ToString(IPAddress.NetworkToHostOrder((short)r.ReadUInt16()))); + } + + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitAvatarHeader(this); + } + + public override byte[] ToByteArray() + { + throw new NotImplementedException(); + } + } +} diff --git a/console/src/lib/net/NetPacketRetrieveAvatar.cs b/console/src/lib/net/NetPacketRetrieveAvatar.cs new file mode 100644 index 00000000..6de8d55e --- /dev/null +++ b/console/src/lib/net/NetPacketRetrieveAvatar.cs @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2009 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 +{ + class NetPacketRetrieveAvatar : NetPacket + { + public NetPacketRetrieveAvatar() + : base(NetPacket.NetTypeRetrieveAvatar) + { + } + + public NetPacketRetrieveAvatar(int size, BinaryReader r) + : base(NetPacket.NetTypeRetrieveAvatar) + { + if (size != 24) + throw new NetPacketException("NetTypeRetrieveAvatar invalid size."); + Properties.Add(PropType.RequestId, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + Properties.Add(PropType.AvatarMD5, + Convert.ToBase64String(r.ReadBytes(16))); + } + + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitRetrieveAvatar(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)24)); + w.Write(IPAddress.HostToNetworkOrder((int) + Convert.ToUInt32(Properties[PropType.RequestId]))); + w.Write(Convert.FromBase64String(Properties[PropType.AvatarMD5])); + + return memStream.ToArray(); + } + } +} diff --git a/console/src/lib/net/NetPacketUnknownAvatar.cs b/console/src/lib/net/NetPacketUnknownAvatar.cs new file mode 100644 index 00000000..9f82a9d1 --- /dev/null +++ b/console/src/lib/net/NetPacketUnknownAvatar.cs @@ -0,0 +1,56 @@ +/*************************************************************************** + * Copyright (C) 2009 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 +{ + class NetPacketUnknownAvatar : NetPacket + { + public NetPacketUnknownAvatar() + : base(NetPacket.NetTypeUnknownAvatar) + { + } + + public NetPacketUnknownAvatar(int size, BinaryReader r) + : base(NetPacket.NetTypeUnknownAvatar) + { + if (size != 8) + throw new NetPacketException("NetPacketUnknownAvatar invalid size."); + Properties.Add(PropType.RequestId, + Convert.ToString(IPAddress.NetworkToHostOrder((int)r.ReadUInt32()))); + } + + public override void Accept(INetPacketVisitor visitor) + { + visitor.VisitUnknownAvatar(this); + } + + public override byte[] ToByteArray() + { + throw new NotImplementedException(); + } + } +} diff --git a/console/src/lib/net/NetParser.cs b/console/src/lib/net/NetParser.cs index f2d27c4c..a5295fb6 100644 --- a/console/src/lib/net/NetParser.cs +++ b/console/src/lib/net/NetParser.cs @@ -398,6 +398,55 @@ namespace pokerth_lib m_callback.Error(p.Properties[NetPacket.PropType.ErrorReason]); } + public void VisitRetrieveAvatar(NetPacket p) + { + // TODO + } + + public void VisitAvatarHeader(NetPacket p) + { + uint tmpAvatarType = Convert.ToUInt32(p.Properties[NetPacket.PropType.AvatarFileType]); + GameInfo.AvatarFileType avatarType; + switch (tmpAvatarType) + { + case 1 : + avatarType = GameInfo.AvatarFileType.PNG; + break; + case 2 : + avatarType = GameInfo.AvatarFileType.JPG; + break; + case 3 : + avatarType = GameInfo.AvatarFileType.GIF; + break; + default : + avatarType = GameInfo.AvatarFileType.UNKNOWN; + break; + + } + m_callback.AvatarBegin( + Convert.ToUInt32(p.Properties[NetPacket.PropType.RequestId]), + Convert.ToUInt32(p.Properties[NetPacket.PropType.AvatarFileSize]), + avatarType); + } + + public void VisitAvatarFile(NetPacket p) + { + m_callback.AvatarData( + Convert.ToUInt32(p.Properties[NetPacket.PropType.RequestId]), + Convert.FromBase64String(p.Properties[NetPacket.PropType.AvatarFileData])); + } + + public void VisitAvatarEnd(NetPacket p) + { + m_callback.AvatarEnd( + Convert.ToUInt32(p.Properties[NetPacket.PropType.RequestId])); + } + + public void VisitUnknownAvatar(NetPacket p) + { + // TODO + } + private PokerTHData m_data; private SenderThread m_sender; private ICallback m_callback;