Compression - Xenoage/RpcLib GitHub Wiki
By default, the body of the HTTP packets is uncompressed. When only small messages are sent and received, this is usually no problem. But if traffic should be reduced or if large objects are transmitted, it may be worth compressing the payload.
Compression strategies
Messages can be compressed in both directions, i.e. the requests and the responses can be compressed or not. The possible strategies are:
Disabled
: Do not compress messages (default setting)Auto
: Compress messages which are at leastCompressionThresholdBytes
bytes long (default value is 1 kB)Enabled
: Compress all messages (little effect on small messages, but higher workload for compressing and uncompressing)
The setting CompressionThresholdBytes
applies only, when the Auto
strategy is used.
Default settings
When initializing the library (both on server side, using IServiceCollection.InitRpcServer
, and client side, using RpcMain.InitRpcClient
), the default compression strategy can be given. Here is an example for the client:
RpcMain.InitRpcClient(/*...*/ new RpcSettings {
Compression = RpcCompressionStrategy.Auto,
CompressionThresholdBytes = 1500
});
Settings per method
Additionally, the used compression strategy may be changed for each method individually. For example:
interface IRemoteLogger : IRpcFunctions {
[RpcOptions(Compression = RpcCompressionStrategy.Enabled)]
Task SaveMessage(string message);
}
The CompressionDemo
project demonstrates this in the ITextRpc
interface. When running the demo (starting the client and the server simultaneously) and having a look at the HTTP network traffic (using a tool like Fiddler for example), the effects can be observed.
Compression technique
Uncompressed messages are transferred in plaintext JSON (application/json
). When a message is compressed, the whole body of the HTTP packet (the RPC object including its serialized method and parameters) is gzip
ped to binary data and the Content-Type
changes to application/gzip
. Uncompressing this data will result in the original JSON object.