summaryrefslogtreecommitdiffstats
path: root/src/Network.cpp
blob: c8be7409cb954cd8cdbeb3a1339f32d8858004b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "Network.hpp"

#include <zlib.h>
#include <easylogging++.h>

#include "Socket.hpp"

Network::Network(std::string address, unsigned short port) {
	try {
		socket = new Socket(address, port);
	} catch (std::exception &e) {
		LOG(WARNING) << "Connection failed: " << e.what();
		throw;
	}

	try {
		stream = new StreamSocket(socket);
	} catch (std::exception &e) {
		LOG(WARNING) << "Stream creation failed: " << e.what();
	}


}

Network::~Network() {
	delete stream;
	delete socket;
}

std::shared_ptr<Packet> Network::ReceivePacket(ConnectionState state, bool useCompression) {
    if (useCompression) {
        int packetLength = stream->ReadVarInt();
        auto packetData = stream->ReadByteArray(packetLength);
        StreamBuffer streamBuffer(packetData.data(), packetData.size());
        
        int dataLength = streamBuffer.ReadVarInt();
        if (dataLength == 0) {
            auto packetData = streamBuffer.ReadByteArray(packetLength - streamBuffer.GetReadedLength());
            StreamBuffer streamBuffer(packetData.data(), packetData.size());
            int packetId = streamBuffer.ReadVarInt();
            auto packet = ReceivePacketByPacketId(packetId, state, streamBuffer);
            return packet;
        } else {
            std::vector<unsigned char> compressedData = streamBuffer.ReadByteArray(packetLength - streamBuffer.GetReadedLength());
            std::vector<unsigned char> uncompressedData;
            uncompressedData.resize(dataLength);

            z_stream stream;
            stream.avail_in = compressedData.size();
            stream.next_in = compressedData.data();
            stream.avail_out = uncompressedData.size();
            stream.next_out = uncompressedData.data();
            stream.zalloc = Z_NULL;
            stream.zfree = Z_NULL;
            stream.opaque = Z_NULL;
            if (inflateInit(&stream) != Z_OK)
                throw std::runtime_error("Zlib decompression initalization error");

            int status = inflate(&stream, Z_FINISH);
            switch (status) {
            case Z_STREAM_END:
                break;
            case Z_OK:                            
            case Z_STREAM_ERROR:
            case Z_BUF_ERROR:
                throw std::runtime_error("Zlib decompression error: " + std::to_string(status));
            }

            if (inflateEnd(&stream) != Z_OK)
                throw std::runtime_error("Zlib decompression end error");

            StreamBuffer streamBuffer(uncompressedData.data(), uncompressedData.size());
            int packetId = streamBuffer.ReadVarInt();
            auto packet = ReceivePacketByPacketId(packetId, state, streamBuffer);
            return packet;            
        }
    } else {
        int packetSize = stream->ReadVarInt();
        auto packetData = stream->ReadByteArray(packetSize);
        StreamBuffer streamBuffer(packetData.data(), packetData.size());
        int packetId = streamBuffer.ReadVarInt();
        auto packet = ReceivePacketByPacketId(packetId, state, streamBuffer);
        return packet;
    }	
}

void Network::SendPacket(Packet &packet, int compressionThreshold) {
    if (compressionThreshold >= 0) {
        StreamCounter packetSize;
        packetSize.WriteVarInt(packet.GetPacketId());
        packetSize.WriteVarInt(0);
        packet.ToStream(&packetSize);
        if (packetSize.GetCountedSize() < compressionThreshold) {
            stream->WriteVarInt(packetSize.GetCountedSize());
            stream->WriteVarInt(0);
            stream->WriteVarInt(packet.GetPacketId());
            packet.ToStream(stream);
        } else {
            throw std::runtime_error("Compressing send data not supported");
        }
    }
    else {
        StreamCounter packetSize;
        packetSize.WriteVarInt(packet.GetPacketId());
        packet.ToStream(&packetSize);
        stream->WriteVarInt(packetSize.GetCountedSize());
        stream->WriteVarInt(packet.GetPacketId());
        packet.ToStream(stream);
    }	
}

std::shared_ptr<Packet> Network::ReceivePacketByPacketId(int packetId, ConnectionState state, StreamInput &stream) {
	std::shared_ptr < Packet > packet(nullptr);
	switch (state) {
		case Handshaking:
            switch (packetId) {
            case PacketNameHandshakingCB::Handshake:
                packet = std::make_shared<PacketHandshake>();
                break;
            }
            break;
		case Login:
            switch (packetId) {
            case PacketNameLoginCB::LoginSuccess:
                packet = std::make_shared<PacketLoginSuccess>();
                break;
            case PacketNameLoginCB::SetCompression:
                packet = std::make_shared<PacketSetCompression>();
                break;
            case PacketNameLoginCB::Disconnect:
                packet = std::make_shared<PacketDisconnect>();
                break;
            }
			break;
		case Play:
			packet = ParsePacketPlay((PacketNamePlayCB) packetId);
			break;
		case Status:
			break;
	}
	if (packet.get() != nullptr)
		packet->FromStream(&stream);
	return packet;
}

std::shared_ptr<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) {
	switch (id) {
		case SpawnObject:
            return std::make_shared<PacketSpawnObject>();
		case SpawnExperienceOrb:
			break;
		case SpawnGlobalEntity:
			break;
		case SpawnMob:
            return std::make_shared<PacketSpawnMob>();
		case SpawnPainting:
			break;
		case SpawnPlayer:
            return std::make_shared<PacketSpawnPlayer>();
		case AnimationCB:
			break;
		case Statistics:
			break;
		case BlockBreakAnimation:
			break;
		case UpdateBlockEntity:
			break;
		case BlockAction:
			break;
		case BlockChange:
            return std::make_shared<PacketBlockChange>();
		case BossBar:
			break;
		case ServerDifficulty:
			break;
		case TabCompleteCB:
			break;
		case ChatMessageCB:
            return std::make_shared<PacketChatMessageCB>();
		case MultiBlockChange:
            return std::make_shared<PacketMultiBlockChange>();
		case ConfirmTransactionCB:
            return std::make_shared<PacketConfirmTransactionCB>();
		case CloseWindowCB:
            return std::make_shared<PacketCloseWindowCB>();
		case OpenWindow:
            return std::make_shared<PacketOpenWindow>();
		case WindowItems:
            return std::make_shared<PacketWindowItems>();
		case WindowProperty:
            return std::make_shared<PacketWindowProperty>();
		case SetSlot:
            return std::make_shared<PacketSetSlot>();
		case SetCooldown:
			break;
		case PluginMessageCB:
			break;
		case NamedSoundEffect:
			break;
		case DisconnectPlay:
			return std::make_shared<PacketDisconnectPlay>();
		case EntityStatus:
			break;
		case Explosion:
			break;
		case UnloadChunk:
            return std::make_shared<PacketUnloadChunk>();
		case ChangeGameState:
			break;
		case KeepAliveCB:
			return std::make_shared<PacketKeepAliveCB>();
		case ChunkData:
			return std::make_shared<PacketChunkData>();
		case Effect:
			break;
		case Particle:
			break;
		case JoinGame:
			return std::make_shared<PacketJoinGame>();
		case Map:
			break;
		case EntityRelativeMove:
            return std::make_shared<PacketEntityRelativeMove>();
		case EntityLookAndRelativeMove:
            return std::make_shared<PacketEntityLookAndRelativeMove>();
		case EntityLook:
            return std::make_shared<PacketEntityLook>();
		case EntityCB:
			break;
		case VehicleMove:
			break;
		case OpenSignEditor:
			break;
		case PlayerAbilitiesCB:
			break;
		case CombatEvent:
			break;
		case PlayerListItem:
			break;
		case PlayerPositionAndLookCB:
			return std::make_shared<PacketPlayerPositionAndLookCB>();
		case UseBed:
			break;
		case DestroyEntities:
            return std::make_shared<PacketDestroyEntities>();
		case RemoveEntityEffect:
			break;
		case ResourcePackSend:
			break;
		case Respawn:
			break;
		case EntityHeadLook:
			break;
		case WorldBorder:
			break;
		case Camera:
			break;
		case HeldItemChangeCB:
			break;
		case DisplayScoreboard:
			break;
		case EntityMetadata:
			break;
		case AttachEntity:
			break;
		case EntityVelocity:
            return std::make_shared<PacketEntityVelocity>();
			break;
		case EntityEquipment:
			break;
		case SetExperience:
			break;
		case UpdateHealth:
			return std::make_shared<PacketUpdateHealth>();
		case ScoreboardObjective:
			break;
		case SetPassengers:
			break;
		case Teams:
			break;
		case UpdateScore:
			break;
		case SpawnPosition:
			return std::make_shared<PacketSpawnPosition>();
		case TimeUpdate:
            return std::make_shared<PacketTimeUpdate>();
		case Title:
			break;
		case SoundEffect:
			break;
		case PlayerListHeaderAndFooter:
			break;
		case CollectItem:
			break;
		case EntityTeleport:
            return std::make_shared<PacketEntityTeleport>();
			break;
		case EntityProperties:
			break;
		case EntityEffect:
			break;
		case UnlockRecipes:
			break;
		case SelectAdvancementTab:
			break;
		case Advancements:
			break;
	}
	return nullptr;
}