1
+ using System . Diagnostics ;
2
+ using System . IO ;
3
+ using System . Text ;
4
+
5
+ namespace DemoLoginServer
6
+ {
7
+ public class LoginMessages
8
+ {
9
+ // Note: PangYa does not use UTF-8.
10
+ public static readonly Encoding StringEncoding = Encoding . UTF8 ;
11
+
12
+ public interface IMessage
13
+ {
14
+ byte [ ] ToBytes ( ) ;
15
+ }
16
+
17
+ public struct ClientLoginMessage : IMessage
18
+ {
19
+ public const ushort MessageId = 0x0001 ;
20
+
21
+ public readonly string Username ;
22
+ public readonly string Password ;
23
+
24
+ public ClientLoginMessage ( string username , string password )
25
+ {
26
+ Username = username ;
27
+ Password = password ;
28
+ }
29
+
30
+ public static ClientLoginMessage FromBytes ( byte [ ] data )
31
+ {
32
+ using ( var reader = new BinaryReader ( new MemoryStream ( data ) ) )
33
+ {
34
+ Debug . Assert ( reader . ReadUInt16 ( ) == MessageId ) ;
35
+ var username = reader . ReadPString ( StringEncoding ) ;
36
+ var password = reader . ReadPString ( StringEncoding ) ;
37
+ return new ClientLoginMessage ( username , password ) ;
38
+ }
39
+ }
40
+
41
+ public byte [ ] ToBytes ( )
42
+ {
43
+ var stream = new MemoryStream ( ) ;
44
+
45
+ using ( var writer = new BinaryWriter ( stream ) )
46
+ {
47
+ writer . Write ( MessageId ) ;
48
+ writer . WritePString ( Username , StringEncoding ) ;
49
+ writer . WritePString ( Password , StringEncoding ) ;
50
+ }
51
+
52
+ return stream . GetBuffer ( ) ;
53
+ }
54
+ }
55
+
56
+ public struct ServerSecurity1Message : IMessage
57
+ {
58
+ public const ushort MessageId = 0x0010 ;
59
+
60
+ public readonly string Token ;
61
+
62
+ public ServerSecurity1Message ( string token )
63
+ {
64
+ Token = token ;
65
+ }
66
+
67
+ public static ServerSecurity1Message FromBytes ( byte [ ] data )
68
+ {
69
+ using ( var reader = new BinaryReader ( new MemoryStream ( data ) ) )
70
+ {
71
+ Debug . Assert ( reader . ReadUInt16 ( ) == MessageId ) ;
72
+ return new ServerSecurity1Message ( reader . ReadPString ( StringEncoding ) ) ;
73
+ }
74
+ }
75
+
76
+ public byte [ ] ToBytes ( )
77
+ {
78
+ var stream = new MemoryStream ( ) ;
79
+
80
+ using ( var writer = new BinaryWriter ( stream ) )
81
+ {
82
+ writer . Write ( MessageId ) ;
83
+ writer . WritePString ( Token , StringEncoding ) ;
84
+ }
85
+
86
+ return stream . ToArray ( ) ;
87
+ }
88
+ }
89
+
90
+ public struct ServerEntry
91
+ {
92
+ public readonly string ServerName ;
93
+ public readonly ushort ServerId ;
94
+ public readonly ushort NumUsers ;
95
+ public readonly ushort MaxUsers ;
96
+ public readonly string IpAddress ;
97
+ public readonly ushort Port ;
98
+ public readonly ushort Flags ;
99
+
100
+ public ServerEntry ( string serverName , ushort serverId , ushort numUsers , ushort maxUsers , string ipAddress ,
101
+ ushort port , ushort flags )
102
+ {
103
+ ServerName = serverName ;
104
+ ServerId = serverId ;
105
+ NumUsers = numUsers ;
106
+ MaxUsers = maxUsers ;
107
+ IpAddress = ipAddress ;
108
+ Port = port ;
109
+ Flags = flags ;
110
+ }
111
+
112
+ public static ServerEntry FromReader ( BinaryReader reader )
113
+ {
114
+ var serverName = reader . ReadFixedString ( 40 , StringEncoding ) ;
115
+ var serverId = reader . ReadUInt16 ( ) ;
116
+ var numUsers = reader . ReadUInt16 ( ) ;
117
+ var maxUsers = reader . ReadUInt16 ( ) ;
118
+ reader . ReadUInt32 ( ) ;
119
+ reader . ReadUInt16 ( ) ;
120
+ var ipAddress = reader . ReadFixedString ( 18 , StringEncoding ) ;
121
+ var port = reader . ReadUInt16 ( ) ;
122
+ reader . ReadUInt16 ( ) ;
123
+ var flags = reader . ReadUInt16 ( ) ;
124
+ reader . ReadBytes ( 16 ) ;
125
+ return new ServerEntry ( serverName , serverId , numUsers , maxUsers , ipAddress , port , flags ) ;
126
+ }
127
+
128
+ public void ToWriter ( BinaryWriter writer )
129
+ {
130
+ writer . WriteFixedString ( ServerName , 40 , StringEncoding ) ;
131
+ writer . Write ( ServerId ) ;
132
+ writer . Write ( NumUsers ) ;
133
+ writer . Write ( MaxUsers ) ;
134
+ writer . Write ( ( uint ) 0 ) ;
135
+ writer . Write ( ( ushort ) 0 ) ;
136
+ writer . WriteFixedString ( IpAddress , 18 , StringEncoding ) ;
137
+ writer . Write ( Port ) ;
138
+ writer . Write ( ( ushort ) 0 ) ;
139
+ writer . Write ( Flags ) ;
140
+ writer . Write ( new byte [ 16 ] ) ;
141
+ }
142
+ }
143
+
144
+ public struct ServerListMessage : IMessage
145
+ {
146
+ public const ushort MessageId = 0x0002 ;
147
+
148
+ public readonly ServerEntry [ ] Servers ;
149
+
150
+ public ServerListMessage ( ServerEntry [ ] servers )
151
+ {
152
+ Servers = servers ;
153
+ }
154
+
155
+ public static ServerListMessage FromBytes ( byte [ ] data )
156
+ {
157
+ using ( var reader = new BinaryReader ( new MemoryStream ( data ) ) )
158
+ {
159
+ Debug . Assert ( reader . ReadUInt16 ( ) == MessageId ) ;
160
+ var servers = new ServerEntry [ reader . ReadByte ( ) ] ;
161
+ for ( var i = 0 ; i < servers . Length ; i ++ )
162
+ servers [ i ] = ServerEntry . FromReader ( reader ) ;
163
+ return new ServerListMessage ( servers ) ;
164
+ }
165
+ }
166
+
167
+ public byte [ ] ToBytes ( )
168
+ {
169
+ var stream = new MemoryStream ( ) ;
170
+
171
+ using ( var writer = new BinaryWriter ( stream ) )
172
+ {
173
+ writer . Write ( MessageId ) ;
174
+ writer . Write ( ( byte ) Servers . Length ) ;
175
+ for ( var i = 0 ; i < Servers . Length ; i ++ )
176
+ Servers [ i ] . ToWriter ( writer ) ;
177
+ }
178
+
179
+ return stream . ToArray ( ) ;
180
+ }
181
+ }
182
+ }
183
+ }
0 commit comments