1 module draklib.protocol.online;
2 import draklib.core : RakNetInfo;
3 import draklib.bytestream : ByteStream, OutOfBoundsException;
4 import draklib.protocol.packet;
5 
6 import std.array;
7 import std.conv;
8 
9 class OnlineConnectionRequest : Packet {
10 	long GUID;
11 	long time;
12 
13 	override {
14 		override {
15 			protected void _encode(ref ByteStream stream) {
16 				stream.writeLong(GUID);
17 				stream.writeLong(time);
18 				//Extra?
19 			}
20 			
21 			protected void _decode(ref ByteStream stream) {
22 				GUID = stream.readLong();
23 				time = stream.readUInt24_LE();
24 				//Extra?
25 			}
26 			
27 			ubyte getID() {
28 				return RakNetInfo.ONLINE_CONNECTION_REQUEST;
29 			}
30 			
31 			uint getSize() {
32 				return 18;
33 			}
34 		}
35 	}
36 }
37 
38 class OnlineConnectionRequestAccepted : Packet {
39 	string clientAddress;
40 	ushort clientPort;
41 	short sysIndex = 0;
42 	string[int] internalIds;
43 	long requestTime;
44 	long time;
45 	
46 	override {
47 		override {
48 			protected void _encode(ref ByteStream stream) {
49 				internalIds = [
50 					0:"127.0.0.1:0",
51 					1:"0.0.0.0:0",
52 					2:"0.0.0.0:0",
53 					3:"0.0.0.0:0",
54 					4:"0.0.0.0:0",
55 					5:"0.0.0.0:0",
56 					6:"0.0.0.0:0",
57 					7:"0.0.0.0:0",
58 					8:"0.0.0.0:0",
59 					9:"0.0.0.0:0",
60 				];
61 
62 				stream.writeSysAddress(clientAddress, clientPort);
63 				stream.writeShort(sysIndex);
64 				foreach(id; internalIds.values) {
65 					string ip = split(id, ":")[0];
66 					ushort port = to!ushort(split(id, ":")[1]);
67 					stream.writeSysAddress(ip, port);
68 				}
69 				stream.writeLong(requestTime);
70 				stream.writeLong(time);
71 			}
72 			
73 			protected void _decode(ref ByteStream stream) {
74 				stream.readSysAddress(clientAddress, clientPort);
75 				sysIndex = stream.readShort();
76 				for(int i = 0; i < 10; i++) {
77 					string ip;
78 					ushort port;
79 					stream.readSysAddress(ip, port);
80 					internalIds[i] = ip ~ ":" ~ to!string(port);
81 				}
82 				requestTime = stream.readLong();
83 				time = stream.readLong();
84 			}
85 			
86 			ubyte getID() {
87 				return RakNetInfo.ONLINE_CONNECTION_REQUEST_ACCEPTED;
88 			}
89 			
90 			uint getSize() {
91 				return 96;
92 			}
93 		}
94 	}
95 }