1 module draklib.protocol.offline;
2 import draklib.core : RakNetInfo;
3 import draklib.bytestream : ByteStream;
4 import draklib.protocol.packet;
5 
6 class OfflineConnectionRequest1 : Packet {
7 	ubyte protocolVersion = cast(ubyte) RakNetInfo.RAKNET_PROTOCOL;
8 	ushort mtuSize;
9 
10 	override {
11 		protected void _encode(ref ByteStream stream) {
12 			stream.writeU(RakNetInfo.RAKNET_MAGIC);
13 			stream.writeUByte(protocolVersion);
14 			stream.write(new byte[mtuSize - 18]);
15 		}
16 		
17 		protected void _decode(ref ByteStream stream) {
18 			stream.skip(RakNetInfo.RAKNET_MAGIC.length);
19 			protocolVersion = stream.readUByte();
20 			mtuSize = cast(ushort) (stream.getRemainingLength() + 18);
21 		}
22 		
23 		ubyte getID() {
24 			return RakNetInfo.OFFLINE_CONNECTION_REQUEST_1;
25 		}
26 		
27 		uint getSize() {
28 			return cast(uint) 18 + mtuSize;
29 		}
30 	}
31 }
32 
33 class OfflineConnectionResponse1 : Packet {
34 	long serverGUID;
35 	ushort mtu;
36 	
37 	override {
38 		protected void _encode(ref ByteStream stream) {
39 			stream.writeU(RakNetInfo.RAKNET_MAGIC);
40 			stream.writeLong(serverGUID);
41 			stream.writeByte(0); //security
42 			stream.writeUShort(mtu);
43 		}
44 		
45 		protected void _decode(ref ByteStream stream) {
46 			stream.skip(RakNetInfo.RAKNET_MAGIC.length);
47 			serverGUID = stream.readLong();
48 			stream.readByte(); //security
49 			mtu = stream.readUShort();
50 		}
51 		
52 		ubyte getID() {
53 			return RakNetInfo.OFFLINE_CONNECTION_RESPONSE_1;
54 		}
55 		
56 		uint getSize() {
57 			return 28;
58 		}
59 	}
60 }
61 
62 class OfflineConnectionRequest2 : Packet {
63 	string serverAddress;
64 	ushort serverPort;
65 	ushort mtu;
66 	long clientGUID;
67 	
68 	override {
69 		protected void _encode(ref ByteStream stream) {
70 			stream.writeU(RakNetInfo.RAKNET_MAGIC);
71 			stream.writeSysAddress(serverAddress, serverPort);
72 			stream.writeUShort(mtu);
73 			stream.writeLong(clientGUID);
74 		}
75 		
76 		protected void _decode(ref ByteStream stream) {
77 			stream.skip(RakNetInfo.RAKNET_MAGIC.length);
78 			stream.readSysAddress(serverAddress, serverPort);
79 			mtu = stream.readUShort();
80 			clientGUID = stream.readLong();
81 		}
82 		
83 		ubyte getID() {
84 			return RakNetInfo.OFFLINE_CONNECTION_REQUEST_2;
85 		}
86 		
87 		uint getSize() {
88 			return 34;
89 		}
90 	}
91 }
92 
93 class OfflineConnectionResponse2 : Packet {
94 	long serverGUID;
95 	string clientAddress;
96 	ushort clientPort;
97 	ushort mtu;
98 	bool encryptionEnabled;
99 	
100 	override {
101 		protected void _encode(ref ByteStream stream) {
102 			stream.writeU(RakNetInfo.RAKNET_MAGIC);
103 			stream.writeLong(serverGUID);
104 			stream.writeSysAddress(clientAddress, clientPort);
105 			stream.writeUShort(mtu);
106 			stream.writeByte(encryptionEnabled ? 1 : 0);
107 		}
108 		
109 		protected void _decode(ref ByteStream stream) {
110 			stream.skip(RakNetInfo.RAKNET_MAGIC.length);
111 			serverGUID = stream.readLong();
112 			stream.readSysAddress(clientAddress, clientPort);
113 			mtu = stream.readUShort();
114 			encryptionEnabled = stream.readByte() > 0;
115 		}
116 		
117 		ubyte getID() {
118 			return RakNetInfo.OFFLINE_CONNECTION_RESPONSE_2;
119 		}
120 		
121 		uint getSize() {
122 			return 35;
123 		}
124 	}
125 }