-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Net compression #205
Merged
Merged
Net compression #205
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e52c8c6
Properly clean crashed clients
29c3f22
Add compression support to the protocol
643cb76
Cleanup packet compression code
ef30ce9
Remove old comment
d4c4716
Change default compression-level to 6
b9a1b79
Specify the compression-level help message more
f2f9ff5
Simplify compression buffer growth
2b15410
Simplify compression utils + Fix off by one error
9cce065
Add compression-threshold to the config
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <unistd.h> | ||
#include <vector> | ||
#include <zlib.h> | ||
|
||
#include "Server.hpp" | ||
|
||
static void add_buffer_to_vector(std::vector<uint8_t> &vector, const uint8_t *buffer, size_t length) | ||
{ | ||
for (size_t character_index = 0; character_index < length; character_index++) { | ||
char current_character = buffer[character_index]; | ||
vector.push_back(current_character); | ||
} | ||
} | ||
|
||
int compressVector(const std::vector<uint8_t> &source, std::vector<uint8_t> &destination) | ||
{ | ||
unsigned long source_length = source.size(); | ||
size_t destination_length = compressBound(source_length); | ||
|
||
uint8_t *destination_data = (uint8_t *) malloc(destination_length); | ||
emneo-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (destination_data == nullptr) { | ||
return Z_MEM_ERROR; | ||
} | ||
|
||
Bytef *source_data = (Bytef *) source.data(); | ||
int return_value = compress2((Bytef *) destination_data, &destination_length, source_data, source_length, CONFIG["compression-level"].as<int>()); | ||
add_buffer_to_vector(destination, destination_data, destination_length); | ||
free(destination_data); | ||
return return_value; | ||
} | ||
|
||
bool decompressVector(const std::vector<uint8_t> &compressedBytes, std::vector<uint8_t> &uncompressedBytes, uint32_t size) | ||
{ | ||
unsigned full_length = size; | ||
unsigned half_length = size / 2; | ||
|
||
unsigned uncompLength = full_length; | ||
char *uncomp = (char *) calloc(sizeof(char), uncompLength); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wsh There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is wrong with it? |
||
|
||
z_stream strm; | ||
strm.next_in = (Bytef *) compressedBytes.data(); | ||
strm.avail_in = size; | ||
strm.total_out = 0; | ||
strm.zalloc = Z_NULL; | ||
strm.zfree = Z_NULL; | ||
|
||
bool done = false; | ||
|
||
if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK) { | ||
free(uncomp); | ||
return false; | ||
} | ||
|
||
while (!done) { | ||
// If our output buffer is too small | ||
if (strm.total_out >= uncompLength) { | ||
// Increase size of output buffer | ||
char *uncomp2 = (char *) calloc(sizeof(char), uncompLength + half_length); | ||
memcpy(uncomp2, uncomp, uncompLength); | ||
uncompLength += half_length; | ||
free(uncomp); | ||
uncomp = uncomp2; | ||
} | ||
|
||
strm.next_out = (Bytef *) (uncomp + strm.total_out); | ||
strm.avail_out = uncompLength - strm.total_out; | ||
|
||
// Inflate another chunk. | ||
int err = inflate(&strm, Z_SYNC_FLUSH); | ||
if (err == Z_STREAM_END) | ||
done = true; | ||
else if (err != Z_OK) { | ||
break; | ||
} | ||
} | ||
|
||
if (inflateEnd(&strm) != Z_OK) { | ||
free(uncomp); | ||
return false; | ||
} | ||
|
||
for (size_t i = 0; i < strm.total_out; ++i) { | ||
uncompressedBytes.push_back(uncomp[i]); | ||
} | ||
// uncompressedBytes.insert(uncompressedBytes.begin(), uncomp, uncomp + strm.total_out); | ||
free(uncomp); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef COMPRESSION_UTILS_HPP_ | ||
#define COMPRESSION_UTILS_HPP_ | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
/** | ||
* @brief Compresses a vector using zlib | ||
* | ||
* @param source Vector to compress | ||
* @param destination Destion vector containing compressed data | ||
* @return int Whatever zlib returns when compressing | ||
* @todo Needs to be checked for lags | ||
*/ | ||
int compressVector(const std::vector<uint8_t> &source, std::vector<uint8_t> &destination); | ||
|
||
/** | ||
* @brief Decompresses a vector using zlib | ||
* | ||
* @param compressedBytes Vector to decompress | ||
* @param uncompressedBytes Destination vector with decompressed data | ||
* @param size The size of the destination vector to expect | ||
* @return true Decompression suceeded | ||
* @return false Something bad happened | ||
* @todo Needs to be heavily optimized, so much weird code | ||
*/ | ||
bool decompressVector(const std::vector<uint8_t> &compressedBytes, std::vector<uint8_t> &uncompressedBytes, uint32_t size); | ||
|
||
#endif // COMPRESSION_UTILS_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?