Skip to content

Commit

Permalink
将char改为ushort,添加ReadString方法
Browse files Browse the repository at this point in the history
  • Loading branch information
sgkoishi committed Oct 14, 2018
1 parent ad7b08a commit cb3b136
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 57 deletions.
6 changes: 3 additions & 3 deletions QQ.Framework/Packets/PCTLV/TLV_0100.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public TLV0100()
}

public string ErrorMsg { get; private set; }
public char PacketCommand { get; private set; }
public ushort PacketCommand { get; private set; }

public void Parser_Tlv(QQUser user, BinaryReader buf)
{
Expand All @@ -29,9 +29,9 @@ public void Parser_Tlv2(QQUser user, BinaryReader buf, int length)
WSubVer = buf.BeReadUInt16(); //wSubVer
if (WSubVer == 0x0001)
{
PacketCommand = (char) buf.BeReadUInt16();
PacketCommand = buf.BeReadUInt16();
var errorCode = buf.BeReadUInt32();
ErrorMsg = Encoding.UTF8.GetString(buf.ReadBytes(buf.BeReadUInt16()));
ErrorMsg = buf.ReadString();
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions QQ.Framework/Packets/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Packet(byte[] byteBuffer, QQUser user)
/// <summary>
/// 版本标志
/// </summary>
public char Version { get; set; }
public ushort Version { get; set; }

/// <summary>
/// 包命令, 如:0x0825
Expand All @@ -75,7 +75,7 @@ public Packet(byte[] byteBuffer, QQUser user)
/// <summary>
/// 包序号
/// </summary>
public char Sequence { get; set; }
public ushort Sequence { get; set; }

/// <summary>
/// 包的接收时间或发送时间
Expand Down Expand Up @@ -139,7 +139,7 @@ public override int GetHashCode()
/// <param name="sequence">The sequence.</param>
/// <param name="command">The command.</param>
/// <returns></returns>
public static int Hash(char sequence, QQCommand command)
public static int Hash(ushort sequence, QQCommand command)
{
return (sequence << 16) | (ushort) command;
}
Expand Down
10 changes: 5 additions & 5 deletions QQ.Framework/Packets/Receive/Login/Receive_0x00BA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ protected override void ParseBody()
{
Decrypt(SecretKey);
VerifyType = Reader.ReadByte();
Reader.BeReadChar();
Reader.BeReadUInt16();
Status = Reader.ReadByte();
Reader.ReadBytes(4);
User.TXProtocol.BufSigPic = Reader.ReadBytes(Reader.BeReadChar());
User.TXProtocol.BufSigPic = Reader.ReadBytes(Reader.BeReadUInt16());
if (VerifyType == 0x13)
{
VerifyCode = Reader.ReadBytes(Reader.BeReadChar());
VerifyCode = Reader.ReadBytes(Reader.BeReadUInt16());
VerifyCommand = Reader.ReadByte();
if (VerifyCommand == 0x00)
{
Expand All @@ -53,8 +53,8 @@ protected override void ParseBody()
User.QQPacket00BaVerifyCode = resultArr;
}

User.TXProtocol.PngToken = Reader.ReadBytes(Reader.BeReadChar());
Reader.ReadBytes(Reader.BeReadChar());
User.TXProtocol.PngToken = Reader.ReadBytes(Reader.BeReadUInt16());
Reader.ReadBytes(Reader.BeReadUInt16());
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions QQ.Framework/Packets/Receive/Message/Receive_0x0017.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ protected override void ParseBody()
Reader.ReadBytes(4);
Reader.ReadBytes(4); //自己的QQ
Reader.ReadBytes(10);
Reader.BeReadChar();
Reader.BeReadUInt16();
Reader.ReadBytes(2);
Reader.ReadBytes(Reader.BeReadChar());
Reader.ReadBytes(Reader.BeReadUInt16());
Group = (long) Util.GetQQNumRetUint(Util.ToHex(Reader.ReadBytes(4))); //群号
if (Reader.ReadByte() == 0x01)
{
FromQQ = (long) Util.GetQQNumRetUint(Util.ToHex(Reader.ReadBytes(4))); //发消息人的QQ
FromQQ = Reader.ReadUInt32(); // (long) Util.GetQQNumRetUint(Util.ToHex(Reader.ReadBytes(4))); //发消息人的QQ
MessageIndex = Reader.ReadBytes(4); //姑且叫消息索引吧
ReceiveTime = Reader.ReadBytes(4); //接收时间
Reader.ReadBytes(24);
SendTime = Reader.ReadBytes(4); //发送时间
MessageId = Reader.ReadBytes(4); //消息 id
Reader.ReadBytes(8);
Font = Reader.ReadBytes(Reader.BeReadChar()); //字体
Font = Reader.ReadBytes(Reader.BeReadUInt16()); //字体
Reader.ReadByte();
Reader.ReadByte();
Message = Reader.ReadRichtext();
Expand Down
10 changes: 5 additions & 5 deletions QQ.Framework/Packets/Receive/Message/Receive_0x00CE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ protected override void ParseBody()
Reader.ReadBytes(4); //自己的QQ
Reader.ReadBytes(10);
Reader.ReadBytes(2);
Reader.BeReadChar();
Reader.ReadBytes(Reader.BeReadChar()); //未知
Reader.BeReadChar(); //消息来源QQ的版本号
Reader.BeReadUInt16();
Reader.ReadBytes(Reader.BeReadUInt16()); //未知
Reader.BeReadUInt16(); //消息来源QQ的版本号
Reader.ReadBytes(4); //FromQQ
Reader.ReadBytes(4); //自己的QQ
Reader.ReadBytes(20);
MessageDateTime = Reader.ReadBytes(4);
Reader.BeReadChar(); //00
Reader.BeReadUInt16(); //00
Reader.ReadBytes(4); //MessageDateTime
Reader.ReadBytes(5); //00
Reader.ReadBytes(3);
Reader.ReadBytes(5); //00
Reader.ReadBytes(4); //MessageDateTime
MessageId = Reader.ReadBytes(4);
Reader.ReadBytes(8);
FontStyle = Reader.ReadBytes(Reader.BeReadChar());
FontStyle = Reader.ReadBytes(Reader.BeReadUInt16());
Reader.ReadByte();
Reader.ReadByte();
Message = Reader.ReadRichtext();
Expand Down
4 changes: 2 additions & 2 deletions QQ.Framework/Packets/ReceivePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ protected virtual void ParseBody()
protected virtual void ParseHeader()
{
Header = Reader.ReadByte();
Version = Reader.BeReadChar();
Version = Reader.BeReadUInt16();
Command = (QQCommand) Reader.BeReadUInt16();
Sequence = Reader.BeReadChar();
Sequence = Reader.BeReadUInt16();
QQ = Reader.BeReadInt32();
Reader.ReadBytes(3);
}
Expand Down
2 changes: 1 addition & 1 deletion QQ.Framework/Packets/Send/Message/Send_0x0017.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Send_0X0017 : SendPacket
/// <param name="user"></param>
/// <param name="data">要发送的数据内容</param>
/// <param name="sequence">序号</param>
public Send_0X0017(QQUser user, byte[] data, char sequence)
public Send_0X0017(QQUser user, byte[] data, ushort sequence)
: base(user)
{
Sequence = sequence;
Expand Down
2 changes: 1 addition & 1 deletion QQ.Framework/Packets/Send/Message/Send_0x00CE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Send_0X00Ce : SendPacket
/// <param name="user"></param>
/// <param name="data">要发送的数据内容</param>
/// <param name="sequence">序号</param>
public Send_0X00Ce(QQUser user, byte[] data, char sequence)
public Send_0X00Ce(QQUser user, byte[] data, ushort sequence)
: base(user)
{
Sequence = sequence;
Expand Down
6 changes: 3 additions & 3 deletions QQ.Framework/Packets/SendPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class SendPacket : Packet
/// <summary>
/// 包起始序列号
/// </summary>
protected static char _seq = (char) 0x3635; // (char)Util.Random.Next();
protected static ushort _seq = 0x3635; // (char)Util.Random.Next();

public MemoryStream BodyStream;
public BinaryWriter BodyWriter;
Expand Down Expand Up @@ -67,12 +67,12 @@ protected void SendPACKET_FIX()
Writer.Write(User.TXProtocol.XxooD);
}

protected static char GetNextSeq()
protected static ushort GetNextSeq()
{
_seq++;
// 为了兼容iQQ
// iQQ把序列号的高位都为0,如果为1,它可能会拒绝,wqfox称是因为TX是这样做的
_seq &= (char) 0x7FFF;
_seq &= 0x7FFF;
if (_seq == 0)
{
_seq++;
Expand Down
6 changes: 3 additions & 3 deletions QQ.Framework/QQGlobal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public static class QQGlobal
/// <summary>
/// 程序缺省使用的客户端版本号
/// </summary>
public const char QQClientVersion = QQClientVersion_0E1B;
public const ushort QQClientVersion = QQClientVersion_0E1B;

/// <summary>
/// 客户端版本号标志 - TIM1.0
/// </summary>
public const char QQClientVersion_0E1B = (char) 0x3713;
public const ushort QQClientVersion_0E1B = 0x3713;

/// <summary>
/// QQ UDP缺省端口
Expand Down Expand Up @@ -140,7 +140,7 @@ public static class QQGlobal
/// 不一定真的是表示服务器端版本号,似乎和发出的包不同,这个有其他的含义,
/// 感觉像是包的类型标志
/// </summary>
public const char QQServerVersion0100 = (char) 0x0100;
public const ushort QQServerVersion0100 = 0x0100;

/// <summary>
/// 是否打开控制台日志
Expand Down
38 changes: 17 additions & 21 deletions QQ.Framework/Utils/Richtext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static Richtext Parse(BinaryReader reader)
try
{
var messageType = reader.ReadByte();
var dataLength = reader.BeReadChar();
var dataLength = reader.BeReadUInt16();
var pos = reader.BaseStream.Position;
while (pos + dataLength < reader.BaseStream.Length)
{
Expand All @@ -27,7 +27,7 @@ public static Richtext Parse(BinaryReader reader)
{
case 0x01: //文本消息
{
var messageStr = Encoding.UTF8.GetString(reader.ReadBytes(reader.BeReadChar()));
var messageStr = reader.ReadString();
if (messageStr.StartsWith("@") && pos + dataLength - reader.BaseStream.Position == 16)
{
reader.ReadBytes(10);
Expand All @@ -41,24 +41,20 @@ public static Richtext Parse(BinaryReader reader)

break;
}

case 0x02: //小黄豆表情
{
result.Snippets.Add(new TextSnippet(
Util.GetQQNumRetUint(Util.ToHex(reader.ReadBytes(reader.BeReadChar()))).ToString(),
MessageType.Emoji));
reader.BeReadUInt16(); // 这里的数字貌似总是1:系统表情只有208个。
result.Snippets.Add(new TextSnippet("", MessageType.Emoji, ("Type", reader.ReadByte())));
break;
}
case 0x03: //图片
{
result.Snippets.Add(new TextSnippet(
Encoding.UTF8.GetString(reader.ReadBytes(reader.BeReadChar())), MessageType.Picture));
result.Snippets.Add(new TextSnippet(reader.ReadString(), MessageType.Picture));
break;
}
case 0x0A: //音频
{
result.Snippets.Add(new TextSnippet(
Encoding.UTF8.GetString(reader.ReadBytes(reader.BeReadChar())), MessageType.Audio));
result.Snippets.Add(new TextSnippet(reader.ReadString(), MessageType.Audio));
break;
}
case 0x0E: //未知
Expand All @@ -79,36 +75,36 @@ public static Richtext Parse(BinaryReader reader)
}
case 0x18: //群文件
{
reader.ReadBytes(3);
var fileName = reader.ReadBytes(reader.ReadByte()); //文件名称
reader.ReadBytes(5);
var fileName = reader.ReadBytes(reader.ReadByte()); // 文件名称... 长度总是一个byte
reader.ReadByte();
reader.ReadBytes(reader.ReadByte()); //文件大小
reader.ReadBytes(reader.ReadByte()); // 文件大小
result.Snippets.Add(new TextSnippet(Encoding.UTF8.GetString(fileName),
MessageType.OfflineFile));
break;
}
case 0x19: //红包秘钥段
case 0x19: // 红包秘钥段
{
if (reader.ReadByte() != 0xC2)
{
break;
}

reader.ReadBytes(19);
reader.ReadBytes(reader.ReadByte()); //恭喜发财
reader.ReadBytes(reader.ReadByte()); // 恭喜发财
reader.ReadByte();
reader.ReadBytes(reader.ReadByte()); //赶紧点击拆开吧
reader.ReadBytes(reader.ReadByte()); // 赶紧点击拆开吧
reader.ReadByte();
reader.ReadBytes(reader.ReadByte()); //QQ红包
reader.ReadBytes(reader.ReadByte()); // QQ红包
reader.ReadBytes(5);
reader.ReadBytes(reader.ReadByte()); //[QQ红包]恭喜发财
reader.ReadBytes(reader.ReadByte()); // [QQ红包]恭喜发财
reader.ReadBytes(22);
var redId = Encoding.UTF8.GetString(reader.ReadBytes(32)); //redid
reader.ReadBytes(12);
reader.ReadBytes(reader.BeReadChar());
reader.ReadBytes(reader.BeReadUInt16());
reader.ReadBytes(0x10);
var key1 = Encoding.UTF8.GetString(reader.ReadBytes(reader.ReadByte())); //Key1
reader.BeReadChar();
reader.BeReadUInt16();
var key2 = Encoding.UTF8.GetString(reader.ReadBytes(reader.ReadByte())); //Key2
result.Snippets.Add(new TextSnippet("", MessageType.RedBag, ("RedId", redId),
("Key1", key1), ("Key2", key2)));
Expand All @@ -118,7 +114,7 @@ public static Richtext Parse(BinaryReader reader)

reader.ReadBytes((int) (pos + dataLength - reader.BaseStream.Position));
messageType = reader.ReadByte();
dataLength = reader.BeReadChar();
dataLength = reader.BeReadUInt16();
pos = reader.BaseStream.Position;
}
}
Expand Down
11 changes: 6 additions & 5 deletions QQ.Framework/Utils/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,6 @@ public static void BeWrite(this BinaryWriter bw, ulong v)
bw.Write(BitConverter.GetBytes((uint) v).Reverse().ToArray());
}

public static char BeReadChar(this BinaryReader br)
{
return (char) br.BeReadUInt16();
}

public static ushort BeReadUInt16(this BinaryReader br)
{
return (ushort) ((br.ReadByte() << 8) + br.ReadByte());
Expand All @@ -558,6 +553,12 @@ public static uint BeReadUInt32(this BinaryReader br)
return (uint) ((br.ReadByte() << 24) | (br.ReadByte() << 16) | (br.ReadByte() << 8) | br.ReadByte());
}

public static string ReadString(this BinaryReader br, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
return encoding.GetString(br.ReadBytes(br.BeReadUInt16()));
}

/// <summary>
/// 写入一串秘钥(因为结构需要前置秘钥长度)
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion QQLoginTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class Program
{
private static void Main(string[] args)
{
var user = new QQUser(0, "");
var user = new QQUser(2822952499, "5uXTB44BbsLJ");
var socketServer = new SocketServiceImpl(user);
var transponder = new Transponder();
var sendService = new SendMessageServiceImpl(socketServer, user);
Expand Down

0 comments on commit cb3b136

Please sign in to comment.