1629860100
GitHub CLI 2.0 supports extensions, allowing anyone to make custom commands that build on the core functionality of GitHub CLI.
Our goal with GitHub CLI 1.0 was to build amazing tooling that allows you to more seamlessly complete the most common developer workflows end-to-end in the terminal. We continued to build on that foundation with far better support for scripting and for working with GitHub Actions. But we knew that a one-size-fits-all tool would never meet every developer’s needs.
1653475560
msgpack.php
A pure PHP implementation of the MessagePack serialization format.
The recommended way to install the library is through Composer:
composer require rybakit/msgpack
To pack values you can either use an instance of a Packer
:
$packer = new Packer();
$packed = $packer->pack($value);
or call a static method on the MessagePack
class:
$packed = MessagePack::pack($value);
In the examples above, the method pack
automatically packs a value depending on its type. However, not all PHP types can be uniquely translated to MessagePack types. For example, the MessagePack format defines map
and array
types, which are represented by a single array
type in PHP. By default, the packer will pack a PHP array as a MessagePack array if it has sequential numeric keys, starting from 0
and as a MessagePack map otherwise:
$mpArr1 = $packer->pack([1, 2]); // MP array [1, 2]
$mpArr2 = $packer->pack([0 => 1, 1 => 2]); // MP array [1, 2]
$mpMap1 = $packer->pack([0 => 1, 2 => 3]); // MP map {0: 1, 2: 3}
$mpMap2 = $packer->pack([1 => 2, 2 => 3]); // MP map {1: 2, 2: 3}
$mpMap3 = $packer->pack(['a' => 1, 'b' => 2]); // MP map {a: 1, b: 2}
However, sometimes you need to pack a sequential array as a MessagePack map. To do this, use the packMap
method:
$mpMap = $packer->packMap([1, 2]); // {0: 1, 1: 2}
Here is a list of type-specific packing methods:
$packer->packNil(); // MP nil
$packer->packBool(true); // MP bool
$packer->packInt(42); // MP int
$packer->packFloat(M_PI); // MP float (32 or 64)
$packer->packFloat32(M_PI); // MP float 32
$packer->packFloat64(M_PI); // MP float 64
$packer->packStr('foo'); // MP str
$packer->packBin("\x80"); // MP bin
$packer->packArray([1, 2]); // MP array
$packer->packMap(['a' => 1]); // MP map
$packer->packExt(1, "\xaa"); // MP ext
Check the "Custom types" section below on how to pack custom types.
The Packer
object supports a number of bitmask-based options for fine-tuning the packing process (defaults are in bold):
Name | Description |
---|---|
FORCE_STR | Forces PHP strings to be packed as MessagePack UTF-8 strings |
FORCE_BIN | Forces PHP strings to be packed as MessagePack binary data |
DETECT_STR_BIN | Detects MessagePack str/bin type automatically |
FORCE_ARR | Forces PHP arrays to be packed as MessagePack arrays |
FORCE_MAP | Forces PHP arrays to be packed as MessagePack maps |
DETECT_ARR_MAP | Detects MessagePack array/map type automatically |
FORCE_FLOAT32 | Forces PHP floats to be packed as 32-bits MessagePack floats |
FORCE_FLOAT64 | Forces PHP floats to be packed as 64-bits MessagePack floats |
The type detection mode (
DETECT_STR_BIN
/DETECT_ARR_MAP
) adds some overhead which can be noticed when you pack large (16- and 32-bit) arrays or strings. However, if you know the value type in advance (for example, you only work with UTF-8 strings or/and associative arrays), you can eliminate this overhead by forcing the packer to use the appropriate type, which will save it from running the auto-detection routine. Another option is to explicitly specify the value type. The library provides 2 auxiliary classes for this,Map
andBin
. Check the "Custom types" section below for details.
Examples:
// detect str/bin type and pack PHP 64-bit floats (doubles) to MP 32-bit floats
$packer = new Packer(PackOptions::DETECT_STR_BIN | PackOptions::FORCE_FLOAT32);
// these will throw MessagePack\Exception\InvalidOptionException
$packer = new Packer(PackOptions::FORCE_STR | PackOptions::FORCE_BIN);
$packer = new Packer(PackOptions::FORCE_FLOAT32 | PackOptions::FORCE_FLOAT64);
To unpack data you can either use an instance of a BufferUnpacker
:
$unpacker = new BufferUnpacker();
$unpacker->reset($packed);
$value = $unpacker->unpack();
or call a static method on the MessagePack
class:
$value = MessagePack::unpack($packed);
If the packed data is received in chunks (e.g. when reading from a stream), use the tryUnpack
method, which attempts to unpack data and returns an array of unpacked messages (if any) instead of throwing an InsufficientDataException
:
while ($chunk = ...) {
$unpacker->append($chunk);
if ($messages = $unpacker->tryUnpack()) {
return $messages;
}
}
If you want to unpack from a specific position in a buffer, use seek
:
$unpacker->seek(42); // set position equal to 42 bytes
$unpacker->seek(-8); // set position to 8 bytes before the end of the buffer
To skip bytes from the current position, use skip
:
$unpacker->skip(10); // set position to 10 bytes ahead of the current position
To get the number of remaining (unread) bytes in the buffer:
$unreadBytesCount = $unpacker->getRemainingCount();
To check whether the buffer has unread data:
$hasUnreadBytes = $unpacker->hasRemaining();
If needed, you can remove already read data from the buffer by calling:
$releasedBytesCount = $unpacker->release();
With the read
method you can read raw (packed) data:
$packedData = $unpacker->read(2); // read 2 bytes
Besides the above methods BufferUnpacker
provides type-specific unpacking methods, namely:
$unpacker->unpackNil(); // PHP null
$unpacker->unpackBool(); // PHP bool
$unpacker->unpackInt(); // PHP int
$unpacker->unpackFloat(); // PHP float
$unpacker->unpackStr(); // PHP UTF-8 string
$unpacker->unpackBin(); // PHP binary string
$unpacker->unpackArray(); // PHP sequential array
$unpacker->unpackMap(); // PHP associative array
$unpacker->unpackExt(); // PHP MessagePack\Type\Ext object
The BufferUnpacker
object supports a number of bitmask-based options for fine-tuning the unpacking process (defaults are in bold):
Name | Description |
---|---|
BIGINT_AS_STR | Converts overflowed integers to strings [1] |
BIGINT_AS_GMP | Converts overflowed integers to GMP objects [2] |
BIGINT_AS_DEC | Converts overflowed integers to Decimal\Decimal objects [3] |
1. The binary MessagePack format has unsigned 64-bit as its largest integer data type, but PHP does not support such integers, which means that an overflow can occur during unpacking.
2. Make sure the GMP extension is enabled.
3. Make sure the Decimal extension is enabled.
Examples:
$packedUint64 = "\xcf"."\xff\xff\xff\xff"."\xff\xff\xff\xff";
$unpacker = new BufferUnpacker($packedUint64);
var_dump($unpacker->unpack()); // string(20) "18446744073709551615"
$unpacker = new BufferUnpacker($packedUint64, UnpackOptions::BIGINT_AS_GMP);
var_dump($unpacker->unpack()); // object(GMP) {...}
$unpacker = new BufferUnpacker($packedUint64, UnpackOptions::BIGINT_AS_DEC);
var_dump($unpacker->unpack()); // object(Decimal\Decimal) {...}
In addition to the basic types, the library provides functionality to serialize and deserialize arbitrary types. This can be done in several ways, depending on your use case. Let's take a look at them.
If you need to serialize an instance of one of your classes into one of the basic MessagePack types, the best way to do this is to implement the CanBePacked interface in the class. A good example of such a class is the Map
type class that comes with the library. This type is useful when you want to explicitly specify that a given PHP array should be packed as a MessagePack map without triggering an automatic type detection routine:
$packer = new Packer();
$packedMap = $packer->pack(new Map([1, 2, 3]));
$packedArray = $packer->pack([1, 2, 3]);
More type examples can be found in the src/Type directory.
As with type objects, type transformers are only responsible for serializing values. They should be used when you need to serialize a value that does not implement the CanBePacked interface. Examples of such values could be instances of built-in or third-party classes that you don't own, or non-objects such as resources.
A transformer class must implement the CanPack interface. To use a transformer, it must first be registered in the packer. Here is an example of how to serialize PHP streams into the MessagePack bin
format type using one of the supplied transformers, StreamTransformer
:
$packer = new Packer(null, [new StreamTransformer()]);
$packedBin = $packer->pack(fopen('/path/to/file', 'r+'));
More type transformer examples can be found in the src/TypeTransformer directory.
In contrast to the cases described above, extensions are intended to handle extension types and are responsible for both serialization and deserialization of values (types).
An extension class must implement the Extension interface. To use an extension, it must first be registered in the packer and the unpacker.
The MessagePack specification divides extension types into two groups: predefined and application-specific. Currently, there is only one predefined type in the specification, Timestamp.
Timestamp
The Timestamp extension type is a predefined type. Support for this type in the library is done through the TimestampExtension
class. This class is responsible for handling Timestamp
objects, which represent the number of seconds and optional adjustment in nanoseconds:
$timestampExtension = new TimestampExtension();
$packer = new Packer();
$packer = $packer->extendWith($timestampExtension);
$unpacker = new BufferUnpacker();
$unpacker = $unpacker->extendWith($timestampExtension);
$packedTimestamp = $packer->pack(Timestamp::now());
$timestamp = $unpacker->reset($packedTimestamp)->unpack();
$seconds = $timestamp->getSeconds();
$nanoseconds = $timestamp->getNanoseconds();
When using the MessagePack
class, the Timestamp extension is already registered:
$packedTimestamp = MessagePack::pack(Timestamp::now());
$timestamp = MessagePack::unpack($packedTimestamp);
Application-specific extensions
In addition, the format can be extended with your own types. For example, to make the built-in PHP DateTime
objects first-class citizens in your code, you can create a corresponding extension, as shown in the example. Please note, that custom extensions have to be registered with a unique extension ID (an integer from 0
to 127
).
More extension examples can be found in the examples/MessagePack directory.
To learn more about how extension types can be useful, check out this article.
If an error occurs during packing/unpacking, a PackingFailedException
or an UnpackingFailedException
will be thrown, respectively. In addition, an InsufficientDataException
can be thrown during unpacking.
An InvalidOptionException
will be thrown in case an invalid option (or a combination of mutually exclusive options) is used.
Run tests as follows:
vendor/bin/phpunit
Also, if you already have Docker installed, you can run the tests in a docker container. First, create a container:
./dockerfile.sh | docker build -t msgpack -
The command above will create a container named msgpack
with PHP 8.1 runtime. You may change the default runtime by defining the PHP_IMAGE
environment variable:
PHP_IMAGE='php:8.0-cli' ./dockerfile.sh | docker build -t msgpack -
See a list of various images here.
Then run the unit tests:
docker run --rm -v $PWD:/msgpack -w /msgpack msgpack
To ensure that the unpacking works correctly with malformed/semi-malformed data, you can use a testing technique called Fuzzing. The library ships with a help file (target) for PHP-Fuzzer and can be used as follows:
php-fuzzer fuzz tests/fuzz_buffer_unpacker.php
To check performance, run:
php -n -dzend_extension=opcache.so \
-dpcre.jit=1 -dopcache.enable=1 -dopcache.enable_cli=1 \
tests/bench.php
Example output
Filter: MessagePack\Tests\Perf\Filter\ListFilter
Rounds: 3
Iterations: 100000
=============================================
Test/Target Packer BufferUnpacker
---------------------------------------------
nil .................. 0.0030 ........ 0.0139
false ................ 0.0037 ........ 0.0144
true ................. 0.0040 ........ 0.0137
7-bit uint #1 ........ 0.0052 ........ 0.0120
7-bit uint #2 ........ 0.0059 ........ 0.0114
7-bit uint #3 ........ 0.0061 ........ 0.0119
5-bit sint #1 ........ 0.0067 ........ 0.0126
5-bit sint #2 ........ 0.0064 ........ 0.0132
5-bit sint #3 ........ 0.0066 ........ 0.0135
8-bit uint #1 ........ 0.0078 ........ 0.0200
8-bit uint #2 ........ 0.0077 ........ 0.0212
8-bit uint #3 ........ 0.0086 ........ 0.0203
16-bit uint #1 ....... 0.0111 ........ 0.0271
16-bit uint #2 ....... 0.0115 ........ 0.0260
16-bit uint #3 ....... 0.0103 ........ 0.0273
32-bit uint #1 ....... 0.0116 ........ 0.0326
32-bit uint #2 ....... 0.0118 ........ 0.0332
32-bit uint #3 ....... 0.0127 ........ 0.0325
64-bit uint #1 ....... 0.0140 ........ 0.0277
64-bit uint #2 ....... 0.0134 ........ 0.0294
64-bit uint #3 ....... 0.0134 ........ 0.0281
8-bit int #1 ......... 0.0086 ........ 0.0241
8-bit int #2 ......... 0.0089 ........ 0.0225
8-bit int #3 ......... 0.0085 ........ 0.0229
16-bit int #1 ........ 0.0118 ........ 0.0280
16-bit int #2 ........ 0.0121 ........ 0.0270
16-bit int #3 ........ 0.0109 ........ 0.0274
32-bit int #1 ........ 0.0128 ........ 0.0346
32-bit int #2 ........ 0.0118 ........ 0.0339
32-bit int #3 ........ 0.0135 ........ 0.0368
64-bit int #1 ........ 0.0138 ........ 0.0276
64-bit int #2 ........ 0.0132 ........ 0.0286
64-bit int #3 ........ 0.0137 ........ 0.0274
64-bit int #4 ........ 0.0180 ........ 0.0285
64-bit float #1 ...... 0.0134 ........ 0.0284
64-bit float #2 ...... 0.0125 ........ 0.0275
64-bit float #3 ...... 0.0126 ........ 0.0283
fix string #1 ........ 0.0035 ........ 0.0133
fix string #2 ........ 0.0094 ........ 0.0216
fix string #3 ........ 0.0094 ........ 0.0222
fix string #4 ........ 0.0091 ........ 0.0241
8-bit string #1 ...... 0.0122 ........ 0.0301
8-bit string #2 ...... 0.0118 ........ 0.0304
8-bit string #3 ...... 0.0119 ........ 0.0315
16-bit string #1 ..... 0.0150 ........ 0.0388
16-bit string #2 ..... 0.1545 ........ 0.1665
32-bit string ........ 0.1570 ........ 0.1756
wide char string #1 .. 0.0091 ........ 0.0236
wide char string #2 .. 0.0122 ........ 0.0313
8-bit binary #1 ...... 0.0100 ........ 0.0302
8-bit binary #2 ...... 0.0123 ........ 0.0324
8-bit binary #3 ...... 0.0126 ........ 0.0327
16-bit binary ........ 0.0168 ........ 0.0372
32-bit binary ........ 0.1588 ........ 0.1754
fix array #1 ......... 0.0042 ........ 0.0131
fix array #2 ......... 0.0294 ........ 0.0367
fix array #3 ......... 0.0412 ........ 0.0472
16-bit array #1 ...... 0.1378 ........ 0.1596
16-bit array #2 ........... S ............. S
32-bit array .............. S ............. S
complex array ........ 0.1865 ........ 0.2283
fix map #1 ........... 0.0725 ........ 0.1048
fix map #2 ........... 0.0319 ........ 0.0405
fix map #3 ........... 0.0356 ........ 0.0665
fix map #4 ........... 0.0465 ........ 0.0497
16-bit map #1 ........ 0.2540 ........ 0.3028
16-bit map #2 ............. S ............. S
32-bit map ................ S ............. S
complex map .......... 0.2372 ........ 0.2710
fixext 1 ............. 0.0283 ........ 0.0358
fixext 2 ............. 0.0291 ........ 0.0371
fixext 4 ............. 0.0302 ........ 0.0355
fixext 8 ............. 0.0288 ........ 0.0384
fixext 16 ............ 0.0293 ........ 0.0359
8-bit ext ............ 0.0302 ........ 0.0439
16-bit ext ........... 0.0334 ........ 0.0499
32-bit ext ........... 0.1845 ........ 0.1888
32-bit timestamp #1 .. 0.0337 ........ 0.0547
32-bit timestamp #2 .. 0.0335 ........ 0.0560
64-bit timestamp #1 .. 0.0371 ........ 0.0575
64-bit timestamp #2 .. 0.0374 ........ 0.0542
64-bit timestamp #3 .. 0.0356 ........ 0.0533
96-bit timestamp #1 .. 0.0362 ........ 0.0699
96-bit timestamp #2 .. 0.0381 ........ 0.0701
96-bit timestamp #3 .. 0.0367 ........ 0.0687
=============================================
Total 2.7618 4.0820
Skipped 4 4
Failed 0 0
Ignored 0 0
With JIT:
php -n -dzend_extension=opcache.so \
-dpcre.jit=1 -dopcache.jit_buffer_size=64M -dopcache.jit=tracing -dopcache.enable=1 -dopcache.enable_cli=1 \
tests/bench.php
Example output
Filter: MessagePack\Tests\Perf\Filter\ListFilter
Rounds: 3
Iterations: 100000
=============================================
Test/Target Packer BufferUnpacker
---------------------------------------------
nil .................. 0.0005 ........ 0.0054
false ................ 0.0004 ........ 0.0059
true ................. 0.0004 ........ 0.0059
7-bit uint #1 ........ 0.0010 ........ 0.0047
7-bit uint #2 ........ 0.0010 ........ 0.0046
7-bit uint #3 ........ 0.0010 ........ 0.0046
5-bit sint #1 ........ 0.0025 ........ 0.0046
5-bit sint #2 ........ 0.0023 ........ 0.0046
5-bit sint #3 ........ 0.0024 ........ 0.0045
8-bit uint #1 ........ 0.0043 ........ 0.0081
8-bit uint #2 ........ 0.0043 ........ 0.0079
8-bit uint #3 ........ 0.0041 ........ 0.0080
16-bit uint #1 ....... 0.0064 ........ 0.0095
16-bit uint #2 ....... 0.0064 ........ 0.0091
16-bit uint #3 ....... 0.0064 ........ 0.0094
32-bit uint #1 ....... 0.0085 ........ 0.0114
32-bit uint #2 ....... 0.0077 ........ 0.0122
32-bit uint #3 ....... 0.0077 ........ 0.0120
64-bit uint #1 ....... 0.0085 ........ 0.0159
64-bit uint #2 ....... 0.0086 ........ 0.0157
64-bit uint #3 ....... 0.0086 ........ 0.0158
8-bit int #1 ......... 0.0042 ........ 0.0080
8-bit int #2 ......... 0.0042 ........ 0.0080
8-bit int #3 ......... 0.0042 ........ 0.0081
16-bit int #1 ........ 0.0065 ........ 0.0095
16-bit int #2 ........ 0.0065 ........ 0.0090
16-bit int #3 ........ 0.0056 ........ 0.0085
32-bit int #1 ........ 0.0067 ........ 0.0107
32-bit int #2 ........ 0.0066 ........ 0.0106
32-bit int #3 ........ 0.0063 ........ 0.0104
64-bit int #1 ........ 0.0072 ........ 0.0162
64-bit int #2 ........ 0.0073 ........ 0.0174
64-bit int #3 ........ 0.0072 ........ 0.0164
64-bit int #4 ........ 0.0077 ........ 0.0161
64-bit float #1 ...... 0.0053 ........ 0.0135
64-bit float #2 ...... 0.0053 ........ 0.0135
64-bit float #3 ...... 0.0052 ........ 0.0135
fix string #1 ....... -0.0002 ........ 0.0044
fix string #2 ........ 0.0035 ........ 0.0067
fix string #3 ........ 0.0035 ........ 0.0077
fix string #4 ........ 0.0033 ........ 0.0078
8-bit string #1 ...... 0.0059 ........ 0.0110
8-bit string #2 ...... 0.0063 ........ 0.0121
8-bit string #3 ...... 0.0064 ........ 0.0124
16-bit string #1 ..... 0.0099 ........ 0.0146
16-bit string #2 ..... 0.1522 ........ 0.1474
32-bit string ........ 0.1511 ........ 0.1483
wide char string #1 .. 0.0039 ........ 0.0084
wide char string #2 .. 0.0073 ........ 0.0123
8-bit binary #1 ...... 0.0040 ........ 0.0112
8-bit binary #2 ...... 0.0075 ........ 0.0123
8-bit binary #3 ...... 0.0077 ........ 0.0129
16-bit binary ........ 0.0096 ........ 0.0145
32-bit binary ........ 0.1535 ........ 0.1479
fix array #1 ......... 0.0008 ........ 0.0061
fix array #2 ......... 0.0121 ........ 0.0165
fix array #3 ......... 0.0193 ........ 0.0222
16-bit array #1 ...... 0.0607 ........ 0.0479
16-bit array #2 ........... S ............. S
32-bit array .............. S ............. S
complex array ........ 0.0749 ........ 0.0824
fix map #1 ........... 0.0329 ........ 0.0431
fix map #2 ........... 0.0161 ........ 0.0189
fix map #3 ........... 0.0205 ........ 0.0262
fix map #4 ........... 0.0252 ........ 0.0205
16-bit map #1 ........ 0.1016 ........ 0.0927
16-bit map #2 ............. S ............. S
32-bit map ................ S ............. S
complex map .......... 0.1096 ........ 0.1030
fixext 1 ............. 0.0157 ........ 0.0161
fixext 2 ............. 0.0175 ........ 0.0183
fixext 4 ............. 0.0156 ........ 0.0185
fixext 8 ............. 0.0163 ........ 0.0184
fixext 16 ............ 0.0164 ........ 0.0182
8-bit ext ............ 0.0158 ........ 0.0207
16-bit ext ........... 0.0203 ........ 0.0219
32-bit ext ........... 0.1614 ........ 0.1539
32-bit timestamp #1 .. 0.0195 ........ 0.0249
32-bit timestamp #2 .. 0.0188 ........ 0.0260
64-bit timestamp #1 .. 0.0207 ........ 0.0281
64-bit timestamp #2 .. 0.0212 ........ 0.0291
64-bit timestamp #3 .. 0.0207 ........ 0.0295
96-bit timestamp #1 .. 0.0222 ........ 0.0358
96-bit timestamp #2 .. 0.0228 ........ 0.0353
96-bit timestamp #3 .. 0.0210 ........ 0.0319
=============================================
Total 1.6432 1.9674
Skipped 4 4
Failed 0 0
Ignored 0 0
You may change default benchmark settings by defining the following environment variables:
Name | Default |
---|---|
MP_BENCH_TARGETS | pure_p,pure_u , see a list of available targets |
MP_BENCH_ITERATIONS | 100_000 |
MP_BENCH_DURATION | not set |
MP_BENCH_ROUNDS | 3 |
MP_BENCH_TESTS | -@slow , see a list of available tests |
For example:
export MP_BENCH_TARGETS=pure_p
export MP_BENCH_ITERATIONS=1000000
export MP_BENCH_ROUNDS=5
# a comma separated list of test names
export MP_BENCH_TESTS='complex array, complex map'
# or a group name
# export MP_BENCH_TESTS='-@slow' // @pecl_comp
# or a regexp
# export MP_BENCH_TESTS='/complex (array|map)/'
Another example, benchmarking both the library and the PECL extension:
MP_BENCH_TARGETS=pure_p,pure_u,pecl_p,pecl_u \
php -n -dextension=msgpack.so -dzend_extension=opcache.so \
-dpcre.jit=1 -dopcache.enable=1 -dopcache.enable_cli=1 \
tests/bench.php
Example output
Filter: MessagePack\Tests\Perf\Filter\ListFilter
Rounds: 3
Iterations: 100000
===========================================================================
Test/Target Packer BufferUnpacker msgpack_pack msgpack_unpack
---------------------------------------------------------------------------
nil .................. 0.0031 ........ 0.0141 ...... 0.0055 ........ 0.0064
false ................ 0.0039 ........ 0.0154 ...... 0.0056 ........ 0.0053
true ................. 0.0038 ........ 0.0139 ...... 0.0056 ........ 0.0044
7-bit uint #1 ........ 0.0061 ........ 0.0110 ...... 0.0059 ........ 0.0046
7-bit uint #2 ........ 0.0065 ........ 0.0119 ...... 0.0042 ........ 0.0029
7-bit uint #3 ........ 0.0054 ........ 0.0117 ...... 0.0045 ........ 0.0025
5-bit sint #1 ........ 0.0047 ........ 0.0103 ...... 0.0038 ........ 0.0022
5-bit sint #2 ........ 0.0048 ........ 0.0117 ...... 0.0038 ........ 0.0022
5-bit sint #3 ........ 0.0046 ........ 0.0102 ...... 0.0038 ........ 0.0023
8-bit uint #1 ........ 0.0063 ........ 0.0174 ...... 0.0039 ........ 0.0031
8-bit uint #2 ........ 0.0063 ........ 0.0167 ...... 0.0040 ........ 0.0029
8-bit uint #3 ........ 0.0063 ........ 0.0168 ...... 0.0039 ........ 0.0030
16-bit uint #1 ....... 0.0092 ........ 0.0222 ...... 0.0049 ........ 0.0030
16-bit uint #2 ....... 0.0096 ........ 0.0227 ...... 0.0042 ........ 0.0046
16-bit uint #3 ....... 0.0123 ........ 0.0274 ...... 0.0059 ........ 0.0051
32-bit uint #1 ....... 0.0136 ........ 0.0331 ...... 0.0060 ........ 0.0048
32-bit uint #2 ....... 0.0130 ........ 0.0336 ...... 0.0070 ........ 0.0048
32-bit uint #3 ....... 0.0127 ........ 0.0329 ...... 0.0051 ........ 0.0048
64-bit uint #1 ....... 0.0126 ........ 0.0268 ...... 0.0055 ........ 0.0049
64-bit uint #2 ....... 0.0135 ........ 0.0281 ...... 0.0052 ........ 0.0046
64-bit uint #3 ....... 0.0131 ........ 0.0274 ...... 0.0069 ........ 0.0044
8-bit int #1 ......... 0.0077 ........ 0.0236 ...... 0.0058 ........ 0.0044
8-bit int #2 ......... 0.0087 ........ 0.0244 ...... 0.0058 ........ 0.0048
8-bit int #3 ......... 0.0084 ........ 0.0241 ...... 0.0055 ........ 0.0049
16-bit int #1 ........ 0.0112 ........ 0.0271 ...... 0.0048 ........ 0.0045
16-bit int #2 ........ 0.0124 ........ 0.0292 ...... 0.0057 ........ 0.0049
16-bit int #3 ........ 0.0118 ........ 0.0270 ...... 0.0058 ........ 0.0050
32-bit int #1 ........ 0.0137 ........ 0.0366 ...... 0.0058 ........ 0.0051
32-bit int #2 ........ 0.0133 ........ 0.0366 ...... 0.0056 ........ 0.0049
32-bit int #3 ........ 0.0129 ........ 0.0350 ...... 0.0052 ........ 0.0048
64-bit int #1 ........ 0.0145 ........ 0.0254 ...... 0.0034 ........ 0.0025
64-bit int #2 ........ 0.0097 ........ 0.0214 ...... 0.0034 ........ 0.0025
64-bit int #3 ........ 0.0096 ........ 0.0287 ...... 0.0059 ........ 0.0050
64-bit int #4 ........ 0.0143 ........ 0.0277 ...... 0.0059 ........ 0.0046
64-bit float #1 ...... 0.0134 ........ 0.0281 ...... 0.0057 ........ 0.0052
64-bit float #2 ...... 0.0141 ........ 0.0281 ...... 0.0057 ........ 0.0050
64-bit float #3 ...... 0.0144 ........ 0.0282 ...... 0.0057 ........ 0.0050
fix string #1 ........ 0.0036 ........ 0.0143 ...... 0.0066 ........ 0.0053
fix string #2 ........ 0.0107 ........ 0.0222 ...... 0.0065 ........ 0.0068
fix string #3 ........ 0.0116 ........ 0.0245 ...... 0.0063 ........ 0.0069
fix string #4 ........ 0.0105 ........ 0.0253 ...... 0.0083 ........ 0.0077
8-bit string #1 ...... 0.0126 ........ 0.0318 ...... 0.0075 ........ 0.0088
8-bit string #2 ...... 0.0121 ........ 0.0295 ...... 0.0076 ........ 0.0086
8-bit string #3 ...... 0.0125 ........ 0.0293 ...... 0.0130 ........ 0.0093
16-bit string #1 ..... 0.0159 ........ 0.0368 ...... 0.0117 ........ 0.0086
16-bit string #2 ..... 0.1547 ........ 0.1686 ...... 0.1516 ........ 0.1373
32-bit string ........ 0.1558 ........ 0.1729 ...... 0.1511 ........ 0.1396
wide char string #1 .. 0.0098 ........ 0.0237 ...... 0.0066 ........ 0.0065
wide char string #2 .. 0.0128 ........ 0.0291 ...... 0.0061 ........ 0.0082
8-bit binary #1 ........... I ............. I ........... F ............. I
8-bit binary #2 ........... I ............. I ........... F ............. I
8-bit binary #3 ........... I ............. I ........... F ............. I
16-bit binary ............. I ............. I ........... F ............. I
32-bit binary ............. I ............. I ........... F ............. I
fix array #1 ......... 0.0040 ........ 0.0129 ...... 0.0120 ........ 0.0058
fix array #2 ......... 0.0279 ........ 0.0390 ...... 0.0143 ........ 0.0165
fix array #3 ......... 0.0415 ........ 0.0463 ...... 0.0162 ........ 0.0187
16-bit array #1 ...... 0.1349 ........ 0.1628 ...... 0.0334 ........ 0.0341
16-bit array #2 ........... S ............. S ........... S ............. S
32-bit array .............. S ............. S ........... S ............. S
complex array ............. I ............. I ........... F ............. F
fix map #1 ................ I ............. I ........... F ............. I
fix map #2 ........... 0.0345 ........ 0.0391 ...... 0.0143 ........ 0.0168
fix map #3 ................ I ............. I ........... F ............. I
fix map #4 ........... 0.0459 ........ 0.0473 ...... 0.0151 ........ 0.0163
16-bit map #1 ........ 0.2518 ........ 0.2962 ...... 0.0400 ........ 0.0490
16-bit map #2 ............. S ............. S ........... S ............. S
32-bit map ................ S ............. S ........... S ............. S
complex map .......... 0.2380 ........ 0.2682 ...... 0.0545 ........ 0.0579
fixext 1 .................. I ............. I ........... F ............. F
fixext 2 .................. I ............. I ........... F ............. F
fixext 4 .................. I ............. I ........... F ............. F
fixext 8 .................. I ............. I ........... F ............. F
fixext 16 ................. I ............. I ........... F ............. F
8-bit ext ................. I ............. I ........... F ............. F
16-bit ext ................ I ............. I ........... F ............. F
32-bit ext ................ I ............. I ........... F ............. F
32-bit timestamp #1 ....... I ............. I ........... F ............. F
32-bit timestamp #2 ....... I ............. I ........... F ............. F
64-bit timestamp #1 ....... I ............. I ........... F ............. F
64-bit timestamp #2 ....... I ............. I ........... F ............. F
64-bit timestamp #3 ....... I ............. I ........... F ............. F
96-bit timestamp #1 ....... I ............. I ........... F ............. F
96-bit timestamp #2 ....... I ............. I ........... F ............. F
96-bit timestamp #3 ....... I ............. I ........... F ............. F
===========================================================================
Total 1.5625 2.3866 0.7735 0.7243
Skipped 4 4 4 4
Failed 0 0 24 17
Ignored 24 24 0 7
With JIT:
MP_BENCH_TARGETS=pure_p,pure_u,pecl_p,pecl_u \
php -n -dextension=msgpack.so -dzend_extension=opcache.so \
-dpcre.jit=1 -dopcache.jit_buffer_size=64M -dopcache.jit=tracing -dopcache.enable=1 -dopcache.enable_cli=1 \
tests/bench.php
Example output
Filter: MessagePack\Tests\Perf\Filter\ListFilter
Rounds: 3
Iterations: 100000
===========================================================================
Test/Target Packer BufferUnpacker msgpack_pack msgpack_unpack
---------------------------------------------------------------------------
nil .................. 0.0001 ........ 0.0052 ...... 0.0053 ........ 0.0042
false ................ 0.0007 ........ 0.0060 ...... 0.0057 ........ 0.0043
true ................. 0.0008 ........ 0.0060 ...... 0.0056 ........ 0.0041
7-bit uint #1 ........ 0.0031 ........ 0.0046 ...... 0.0062 ........ 0.0041
7-bit uint #2 ........ 0.0021 ........ 0.0043 ...... 0.0062 ........ 0.0041
7-bit uint #3 ........ 0.0022 ........ 0.0044 ...... 0.0061 ........ 0.0040
5-bit sint #1 ........ 0.0030 ........ 0.0048 ...... 0.0062 ........ 0.0040
5-bit sint #2 ........ 0.0032 ........ 0.0046 ...... 0.0062 ........ 0.0040
5-bit sint #3 ........ 0.0031 ........ 0.0046 ...... 0.0062 ........ 0.0040
8-bit uint #1 ........ 0.0054 ........ 0.0079 ...... 0.0062 ........ 0.0050
8-bit uint #2 ........ 0.0051 ........ 0.0079 ...... 0.0064 ........ 0.0044
8-bit uint #3 ........ 0.0051 ........ 0.0082 ...... 0.0062 ........ 0.0044
16-bit uint #1 ....... 0.0077 ........ 0.0094 ...... 0.0065 ........ 0.0045
16-bit uint #2 ....... 0.0077 ........ 0.0094 ...... 0.0063 ........ 0.0045
16-bit uint #3 ....... 0.0077 ........ 0.0095 ...... 0.0064 ........ 0.0047
32-bit uint #1 ....... 0.0088 ........ 0.0119 ...... 0.0063 ........ 0.0043
32-bit uint #2 ....... 0.0089 ........ 0.0117 ...... 0.0062 ........ 0.0039
32-bit uint #3 ....... 0.0089 ........ 0.0118 ...... 0.0063 ........ 0.0044
64-bit uint #1 ....... 0.0097 ........ 0.0155 ...... 0.0063 ........ 0.0045
64-bit uint #2 ....... 0.0095 ........ 0.0153 ...... 0.0061 ........ 0.0045
64-bit uint #3 ....... 0.0096 ........ 0.0156 ...... 0.0063 ........ 0.0047
8-bit int #1 ......... 0.0053 ........ 0.0083 ...... 0.0062 ........ 0.0044
8-bit int #2 ......... 0.0052 ........ 0.0080 ...... 0.0062 ........ 0.0044
8-bit int #3 ......... 0.0052 ........ 0.0080 ...... 0.0062 ........ 0.0043
16-bit int #1 ........ 0.0089 ........ 0.0097 ...... 0.0069 ........ 0.0046
16-bit int #2 ........ 0.0075 ........ 0.0093 ...... 0.0063 ........ 0.0043
16-bit int #3 ........ 0.0075 ........ 0.0094 ...... 0.0062 ........ 0.0046
32-bit int #1 ........ 0.0086 ........ 0.0122 ...... 0.0063 ........ 0.0044
32-bit int #2 ........ 0.0087 ........ 0.0120 ...... 0.0066 ........ 0.0046
32-bit int #3 ........ 0.0086 ........ 0.0121 ...... 0.0060 ........ 0.0044
64-bit int #1 ........ 0.0096 ........ 0.0149 ...... 0.0060 ........ 0.0045
64-bit int #2 ........ 0.0096 ........ 0.0157 ...... 0.0062 ........ 0.0044
64-bit int #3 ........ 0.0096 ........ 0.0160 ...... 0.0063 ........ 0.0046
64-bit int #4 ........ 0.0097 ........ 0.0157 ...... 0.0061 ........ 0.0044
64-bit float #1 ...... 0.0079 ........ 0.0153 ...... 0.0056 ........ 0.0044
64-bit float #2 ...... 0.0079 ........ 0.0152 ...... 0.0057 ........ 0.0045
64-bit float #3 ...... 0.0079 ........ 0.0155 ...... 0.0057 ........ 0.0044
fix string #1 ........ 0.0010 ........ 0.0045 ...... 0.0071 ........ 0.0044
fix string #2 ........ 0.0048 ........ 0.0075 ...... 0.0070 ........ 0.0060
fix string #3 ........ 0.0048 ........ 0.0086 ...... 0.0068 ........ 0.0060
fix string #4 ........ 0.0050 ........ 0.0088 ...... 0.0070 ........ 0.0059
8-bit string #1 ...... 0.0081 ........ 0.0129 ...... 0.0069 ........ 0.0062
8-bit string #2 ...... 0.0086 ........ 0.0128 ...... 0.0069 ........ 0.0065
8-bit string #3 ...... 0.0086 ........ 0.0126 ...... 0.0115 ........ 0.0065
16-bit string #1 ..... 0.0105 ........ 0.0137 ...... 0.0128 ........ 0.0068
16-bit string #2 ..... 0.1510 ........ 0.1486 ...... 0.1526 ........ 0.1391
32-bit string ........ 0.1517 ........ 0.1475 ...... 0.1504 ........ 0.1370
wide char string #1 .. 0.0044 ........ 0.0085 ...... 0.0067 ........ 0.0057
wide char string #2 .. 0.0081 ........ 0.0125 ...... 0.0069 ........ 0.0063
8-bit binary #1 ........... I ............. I ........... F ............. I
8-bit binary #2 ........... I ............. I ........... F ............. I
8-bit binary #3 ........... I ............. I ........... F ............. I
16-bit binary ............. I ............. I ........... F ............. I
32-bit binary ............. I ............. I ........... F ............. I
fix array #1 ......... 0.0014 ........ 0.0059 ...... 0.0132 ........ 0.0055
fix array #2 ......... 0.0146 ........ 0.0156 ...... 0.0155 ........ 0.0148
fix array #3 ......... 0.0211 ........ 0.0229 ...... 0.0179 ........ 0.0180
16-bit array #1 ...... 0.0673 ........ 0.0498 ...... 0.0343 ........ 0.0388
16-bit array #2 ........... S ............. S ........... S ............. S
32-bit array .............. S ............. S ........... S ............. S
complex array ............. I ............. I ........... F ............. F
fix map #1 ................ I ............. I ........... F ............. I
fix map #2 ........... 0.0148 ........ 0.0180 ...... 0.0156 ........ 0.0179
fix map #3 ................ I ............. I ........... F ............. I
fix map #4 ........... 0.0252 ........ 0.0201 ...... 0.0214 ........ 0.0167
16-bit map #1 ........ 0.1027 ........ 0.0836 ...... 0.0388 ........ 0.0510
16-bit map #2 ............. S ............. S ........... S ............. S
32-bit map ................ S ............. S ........... S ............. S
complex map .......... 0.1104 ........ 0.1010 ...... 0.0556 ........ 0.0602
fixext 1 .................. I ............. I ........... F ............. F
fixext 2 .................. I ............. I ........... F ............. F
fixext 4 .................. I ............. I ........... F ............. F
fixext 8 .................. I ............. I ........... F ............. F
fixext 16 ................. I ............. I ........... F ............. F
8-bit ext ................. I ............. I ........... F ............. F
16-bit ext ................ I ............. I ........... F ............. F
32-bit ext ................ I ............. I ........... F ............. F
32-bit timestamp #1 ....... I ............. I ........... F ............. F
32-bit timestamp #2 ....... I ............. I ........... F ............. F
64-bit timestamp #1 ....... I ............. I ........... F ............. F
64-bit timestamp #2 ....... I ............. I ........... F ............. F
64-bit timestamp #3 ....... I ............. I ........... F ............. F
96-bit timestamp #1 ....... I ............. I ........... F ............. F
96-bit timestamp #2 ....... I ............. I ........... F ............. F
96-bit timestamp #3 ....... I ............. I ........... F ............. F
===========================================================================
Total 0.9642 1.0909 0.8224 0.7213
Skipped 4 4 4 4
Failed 0 0 24 17
Ignored 24 24 0 7
Note that the msgpack extension (v2.1.2) doesn't support ext, bin and UTF-8 str types.
The library is released under the MIT License. See the bundled LICENSE file for details.
Author: rybakit
Source Code: https://github.com/rybakit/msgpack.php
License: MIT License
1651147200
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution.
Has integration with high-level message protocol based on Fast Binary Encoding
Contents
Features
Requirements
Optional:
How to build?
sudo apt-get install -y binutils-dev uuid-dev libssl-dev
pip3 install gil
git clone https://github.com/chronoxor/CppServer.git
cd CppServer
gil update
cd build
./unix.sh
cd build
./unix.sh
cd build
unix.bat
cd build
mingw.bat
cd build
vs.bat
Examples
Asio service is used to host all clients/servers based on Asio C++ library. It is implemented based on Asio C++ Library and use a separate thread to perform all asynchronous IO operations and communications.
The common usecase is to instantiate one Asio service, start the service and attach TCP/UDP/WebSocket servers or/and clients to it. One Asio service can handle several servers and clients asynchronously at the same time in one I/O thread. If you want to scale your servers or clients it is possible to create and use more than one Asio services to handle your servers/clients in balance.
Also it is possible to dispatch or post your custom handler into I/O thread. Dispatch will execute the handler immediately if the current thread is I/O one. Otherwise the handler will be enqueued to the I/O queue. In opposite the post method will always enqueue the handler into the I/O queue.
Here comes an example of using custom Asio service with dispatch/post methods:
#include "server/asio/service.h"
#include "threads/thread.h"
#include <iostream>
int main(int argc, char** argv)
{
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Dispatch
std::cout << "1 - Dispatch from the main thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Dispatch([service]()
{
std::cout << "1.1 - Dispatched in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
std::cout << "1.2 - Dispatch from thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Dispatch([service]()
{
std::cout << "1.2.1 - Dispatched in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
});
std::cout << "1.3 - Post from thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Post([service]()
{
std::cout << "1.3.1 - Posted in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
});
});
// Post
std::cout << "2 - Post from the main thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Post([service]()
{
std::cout << "2.1 - Posted in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
std::cout << "2.2 - Dispatch from thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Dispatch([service]()
{
std::cout << "2.2.1 - Dispatched in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
});
std::cout << "2.3 - Post from thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
service->Post([service]()
{
std::cout << "2.3.1 - Posted in thread with Id " << CppCommon::Thread::CurrentThreadId() << std::endl;
});
});
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Output of the above example is the following:
Asio service started!
1 - Dispatch from the main thread with Id 16744
2 - Post from the main thread with Id 16744
1.1 - Dispatched in thread with Id 19920
1.2 - Dispatch from thread with Id 19920
1.2.1 - Dispatched in thread with Id 19920
1.3 - Post from thread with Id 19920
2.1 - Posted in thread with Id 19920
2.2 - Dispatch from thread with Id 19920
2.2.1 - Dispatched in thread with Id 19920
2.3 - Post from thread with Id 19920
1.3.1 - Posted in thread with Id 19920
2.3.1 - Posted in thread with Id 19920
Asio service stopped!
Here comes the example of Asio timer. It can be used to wait for some action in future with providing absolute time or relative time span. Asio timer can be used in synchronous or asynchronous modes.
#include "server/asio/timer.h"
#include "threads/thread.h"
#include <iostream>
class AsioTimer : public CppServer::Asio::Timer
{
public:
using CppServer::Asio::Timer::Timer;
protected:
void onTimer(bool canceled) override
{
std::cout << "Asio timer " << (canceled ? "canceled" : "expired") << std::endl;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Asio timer caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new Asio timer
auto timer = std::make_shared<AsioTimer>(service);
// Setup and synchronously wait for the timer
timer->Setup(CppCommon::UtcTime() + CppCommon::Timespan::seconds(1));
timer->WaitSync();
// Setup and asynchronously wait for the timer
timer->Setup(CppCommon::Timespan::seconds(1));
timer->WaitAsync();
// Wait for a while...
CppCommon::Thread::Sleep(2000);
// Setup and asynchronously wait for the timer
timer->Setup(CppCommon::Timespan::seconds(1));
timer->WaitAsync();
// Wait for a while...
CppCommon::Thread::Sleep(500);
// Cancel the timer
timer->Cancel();
// Wait for a while...
CppCommon::Thread::Sleep(500);
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Output of the above example is the following:
Asio service starting...Done!
Timer was expired
Timer was canceled
Asio service stopping...Done!
Here comes the example of the TCP chat server. It handles multiple TCP client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
#include "server/asio/tcp_server.h"
#include "threads/thread.h"
#include <iostream>
class ChatSession : public CppServer::Asio::TCPSession
{
public:
using CppServer::Asio::TCPSession::TCPSession;
protected:
void onConnected() override
{
std::cout << "Chat TCP session with Id " << id() << " connected!" << std::endl;
// Send invite message
std::string message("Hello from TCP chat! Please send a message or '!' to disconnect the client!");
SendAsync(message);
}
void onDisconnected() override
{
std::cout << "Chat TCP session with Id " << id() << " disconnected!" << std::endl;
}
void onReceived(const void* buffer, size_t size) override
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
// Multicast message to all connected sessions
server()->Multicast(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
DisconnectAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat TCP session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
class ChatServer : public CppServer::Asio::TCPServer
{
public:
using CppServer::Asio::TCPServer::TCPServer;
protected:
std::shared_ptr<CppServer::Asio::TCPSession> CreateSession(std::shared_ptr<CppServer::Asio::TCPServer> server) override
{
return std::make_shared<ChatSession>(server);
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat TCP server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// TCP server port
int port = 1111;
if (argc > 1)
port = std::atoi(argv[1]);
std::cout << "TCP server port: " << port << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new TCP chat server
auto server = std::make_shared<ChatServer>(service, port);
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server->Multicast(line);
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the TCP chat client. It connects to the TCP chat server and allows to send message to it and receive new messages.
#include "server/asio/tcp_client.h"
#include "threads/thread.h"
#include <atomic>
#include <iostream>
class ChatClient : public CppServer::Asio::TCPClient
{
public:
ChatClient(std::shared_ptr<CppServer::Asio::Service> service, const std::string& address, int port)
: CppServer::Asio::TCPClient(service, address, port)
{
_stop = false;
}
void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onConnected() override
{
std::cout << "Chat TCP client connected a new session with Id " << id() << std::endl;
}
void onDisconnected() override
{
std::cout << "Chat TCP client disconnected a session with Id " << id() << std::endl;
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
void onReceived(const void* buffer, size_t size) override
{
std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat TCP client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
private:
std::atomic<bool> _stop;
};
int main(int argc, char** argv)
{
// TCP server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
// TCP server port
int port = 1111;
if (argc > 2)
port = std::atoi(argv[2]);
std::cout << "TCP server address: " << address << std::endl;
std::cout << "TCP server port: " << port << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new TCP chat client
auto client = std::make_shared<ChatClient>(service, address, port);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Disconnect the client
if (line == "!")
{
std::cout << "Client disconnecting...";
client->DisconnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
// Send the entered text to the chat server
client->SendAsync(line);
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the SSL chat server. It handles multiple SSL client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
This example is very similar to the TCP one except the code that prepares SSL context and handshake handler.
#include "server/asio/ssl_server.h"
#include "threads/thread.h"
#include <iostream>
class ChatSession : public CppServer::Asio::SSLSession
{
public:
using CppServer::Asio::SSLSession::SSLSession;
protected:
void onConnected() override
{
std::cout << "Chat SSL session with Id " << id() << " connected!" << std::endl;
}
void onHandshaked() override
{
std::cout << "Chat SSL session with Id " << id() << " handshaked!" << std::endl;
// Send invite message
std::string message("Hello from SSL chat! Please send a message or '!' to disconnect the client!");
SendAsync(message.data(), message.size());
}
void onDisconnected() override
{
std::cout << "Chat SSL session with Id " << id() << " disconnected!" << std::endl;
}
void onReceived(const void* buffer, size_t size) override
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
// Multicast message to all connected sessions
server()->Multicast(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
DisconnectAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat SSL session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
class ChatServer : public CppServer::Asio::SSLServer
{
public:
using CppServer::Asio::SSLServer::SSLServer;
protected:
std::shared_ptr<CppServer::Asio::SSLSession> CreateSession(std::shared_ptr<CppServer::Asio::SSLServer> server) override
{
return std::make_shared<ChatSession>(server);
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat TCP server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// SSL server port
int port = 2222;
if (argc > 1)
port = std::atoi(argv[1]);
std::cout << "SSL server port: " << port << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create and prepare a new SSL server context
auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv12);
context->set_password_callback([](size_t max_length, asio::ssl::context::password_purpose purpose) -> std::string { return "qwerty"; });
context->use_certificate_chain_file("../tools/certificates/server.pem");
context->use_private_key_file("../tools/certificates/server.pem", asio::ssl::context::pem);
context->use_tmp_dh_file("../tools/certificates/dh4096.pem");
// Create a new SSL chat server
auto server = std::make_shared<ChatServer>(service, context, port);
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server->Multicast(line);
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the SSL chat client. It connects to the SSL chat server and allows to send message to it and receive new messages.
This example is very similar to the TCP one except the code that prepares SSL context and handshake handler.
#include "server/asio/ssl_client.h"
#include "threads/thread.h"
#include <atomic>
#include <iostream>
class ChatClient : public CppServer::Asio::SSLClient
{
public:
ChatClient(std::shared_ptr<CppServer::Asio::Service> service, std::shared_ptr<CppServer::Asio::SSLContext> context, const std::string& address, int port)
: CppServer::Asio::SSLClient(service, context, address, port)
{
_stop = false;
}
void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onConnected() override
{
std::cout << "Chat SSL client connected a new session with Id " << id() << std::endl;
}
void onHandshaked() override
{
std::cout << "Chat SSL client handshaked a new session with Id " << id() << std::endl;
}
void onDisconnected() override
{
std::cout << "Chat SSL client disconnected a session with Id " << id() << std::endl;
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
void onReceived(const void* buffer, size_t size) override
{
std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat SSL client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
private:
std::atomic<bool> _stop;
};
int main(int argc, char** argv)
{
// SSL server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
// SSL server port
int port = 2222;
if (argc > 2)
port = std::atoi(argv[2]);
std::cout << "SSL server address: " << address << std::endl;
std::cout << "SSL server port: " << port << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create and prepare a new SSL client context
auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv12);
context->set_default_verify_paths();
context->set_root_certs();
context->set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert);
context->load_verify_file("../tools/certificates/ca.pem");
// Create a new SSL chat client
auto client = std::make_shared<ChatClient>(service, context, address, port);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Disconnect the client
if (line == "!")
{
std::cout << "Client disconnecting...";
client->DisconnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
// Send the entered text to the chat server
client->SendAsync(line);
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the UDP echo server. It receives a datagram mesage from any UDP client and resend it back without any changes.
#include "server/asio/udp_server.h"
#include "threads/thread.h"
#include <iostream>
class EchoServer : public CppServer::Asio::UDPServer
{
public:
using CppServer::Asio::UDPServer::UDPServer;
protected:
void onStarted() override
{
// Start receive datagrams
ReceiveAsync();
}
void onReceived(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) override
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
// Echo the message back to the sender
SendAsync(endpoint, message);
}
void onSent(const asio::ip::udp::endpoint& endpoint, size_t sent) override
{
// Continue receive datagrams
ReceiveAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Echo UDP server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// UDP server port
int port = 3333;
if (argc > 1)
port = std::atoi(argv[1]);
std::cout << "UDP server port: " << port << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new UDP echo server
auto server = std::make_shared<EchoServer>(service, port);
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the UDP echo client. It sends user datagram message to UDP server and listen for response.
#include "server/asio/udp_client.h"
#include "threads/thread.h"
#include <atomic>
#include <iostream>
class EchoClient : public CppServer::Asio::UDPClient
{
public:
EchoClient(std::shared_ptr<CppServer::Asio::Service> service, const std::string& address, int port)
: CppServer::Asio::UDPClient(service, address, port)
{
_stop = false;
}
void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onConnected() override
{
std::cout << "Echo UDP client connected a new session with Id " << id() << std::endl;
// Start receive datagrams
ReceiveAsync();
}
void onDisconnected() override
{
std::cout << "Echo UDP client disconnected a session with Id " << id() << std::endl;
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
void onReceived(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) override
{
std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
// Continue receive datagrams
ReceiveAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Echo UDP client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
private:
std::atomic<bool> _stop;
};
int main(int argc, char** argv)
{
// UDP server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
// UDP server port
int port = 3333;
if (argc > 2)
port = std::atoi(argv[2]);
std::cout << "UDP server address: " << address << std::endl;
std::cout << "UDP server port: " << port << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new UDP echo client
auto client = std::make_shared<EchoClient>(service, address, port);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Disconnect the client
if (line == "!")
{
std::cout << "Client disconnecting...";
client->DisconnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
// Send the entered text to the echo server
client->SendSync(line);
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the UDP multicast server. It use multicast IP address to multicast datagram messages to all client that joined corresponding UDP multicast group.
#include "server/asio/udp_server.h"
#include "threads/thread.h"
#include <iostream>
class MulticastServer : public CppServer::Asio::UDPServer
{
public:
using CppServer::Asio::UDPServer::UDPServer;
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Multicast UDP server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// UDP multicast address
std::string multicast_address = "239.255.0.1";
if (argc > 1)
multicast_address = argv[1];
// UDP multicast port
int multicast_port = 3334;
if (argc > 2)
multicast_port = std::atoi(argv[2]);
std::cout << "UDP multicast address: " << multicast_address << std::endl;
std::cout << "UDP multicast port: " << multicast_port << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new UDP multicast server
auto server = std::make_shared<MulticastServer>(service, 0);
// Start the multicast server
std::cout << "Server starting...";
server->Start(multicast_address, multicast_port);
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server->MulticastSync(line);
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the UDP multicast client. It use multicast IP address and joins UDP multicast group in order to receive multicasted datagram messages from UDP server.
#include "server/asio/udp_client.h"
#include "threads/thread.h"
#include <atomic>
#include <iostream>
class MulticastClient : public CppServer::Asio::UDPClient
{
public:
MulticastClient(std::shared_ptr<CppServer::Asio::Service> service, const std::string& address, const std::string& multicast, int port)
: CppServer::Asio::UDPClient(service, address, port),
_multicast(multicast)
{
_stop = false;
}
void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onConnected() override
{
std::cout << "Multicast UDP client connected a new session with Id " << id() << std::endl;
// Join UDP multicast group
JoinMulticastGroupAsync(_multicast);
// Start receive datagrams
ReceiveAsync();
}
void onDisconnected() override
{
std::cout << "Multicast UDP client disconnected a session with Id " << id() << std::endl;
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
void onReceived(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) override
{
std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
// Continue receive datagrams
ReceiveAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Multicast UDP client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
private:
std::atomic<bool> _stop;
std::string _multicast;
};
int main(int argc, char** argv)
{
// UDP listen address
std::string listen_address = "0.0.0.0";
if (argc > 1)
listen_address = argv[1];
// UDP multicast address
std::string multicast_address = "239.255.0.1";
if (argc > 2)
multicast_address = argv[2];
// UDP multicast port
int multicast_port = 3334;
if (argc > 3)
multicast_port = std::atoi(argv[3]);
std::cout << "UDP listen address: " << listen_address << std::endl;
std::cout << "UDP multicast address: " << multicast_address << std::endl;
std::cout << "UDP multicast port: " << multicast_port << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new UDP multicast client
auto client = std::make_shared<MulticastClient>(service, listen_address, multicast_address, multicast_port);
client->SetupMulticast(true);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Disconnect the client
if (line == "!")
{
std::cout << "Client disconnecting...";
client->DisconnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Simple protocol is defined in simple.fbe file:
/*
Simple Fast Binary Encoding protocol for CppServer
https://github.com/chronoxor/FastBinaryEncoding
Generate protocol command: fbec --cpp --proto --input=simple.fbe --output=.
*/
// Domain declaration
domain com.chronoxor
// Package declaration
package simple
// Protocol version
version 1.0
// Simple request message
[request]
[response(SimpleResponse)]
[reject(SimpleReject)]
message SimpleRequest
{
// Request Id
uuid [id] = uuid1;
// Request message
string Message;
}
// Simple response
message SimpleResponse
{
// Response Id
uuid [id] = uuid1;
// Calculated message hash
uint32 Hash;
}
// Simple reject
message SimpleReject
{
// Reject Id
uuid [id] = uuid1;
// Error message
string Error;
}
// Simple notification
message SimpleNotify
{
// Server notification
string Notification;
}
// Disconnect request message
[request]
message DisconnectRequest
{
// Request Id
uuid [id] = uuid1;
}
Here comes the example of the simple protocol server. It process client requests, answer with corresponding responses and send server notifications back to clients.
#include "asio_service.h"
#include "server/asio/tcp_server.h"
#include "../proto/simple_protocol.h"
#include <iostream>
class SimpleProtoSession : public CppServer::Asio::TCPSession, public FBE::simple::Sender, public FBE::simple::Receiver
{
public:
using CppServer::Asio::TCPSession::TCPSession;
protected:
void onConnected() override
{
std::cout << "Simple protocol session with Id " << id() << " connected!" << std::endl;
// Send invite notification
simple::SimpleNotify notify;
notify.Notification = "Hello from Simple protocol server! Please send a message or '!' to disconnect the client!";
send(notify);
}
void onDisconnected() override
{
std::cout << "Simple protocol session with Id " << id() << " disconnected!" << std::endl;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Simple protocol session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
// Protocol handlers
void onReceive(const ::simple::DisconnectRequest& request) override { Disconnect(); }
void onReceive(const ::simple::SimpleRequest& request) override
{
std::cout << "Received: " << request << std::endl;
// Validate request
if (request.Message.empty())
{
// Send reject
simple::SimpleReject reject;
reject.id = request.id;
reject.Error = "Request message is empty!";
send(reject);
return;
}
static std::hash<std::string> hasher;
// Send response
simple::SimpleResponse response;
response.id = request.id;
response.Hash = (uint32_t)hasher(request.Message);
send(response);
}
// Protocol implementation
void onReceived(const void* buffer, size_t size) override { receive(buffer, size); }
size_t onSend(const void* data, size_t size) override { return SendAsync(data, size) ? size : 0; }
};
class SimpleProtoServer : public CppServer::Asio::TCPServer, public FBE::simple::Sender
{
public:
using CppServer::Asio::TCPServer::TCPServer;
protected:
std::shared_ptr<CppServer::Asio::TCPSession> CreateSession(const std::shared_ptr<CppServer::Asio::TCPServer>& server) override
{
return std::make_shared<SimpleProtoSession>(server);
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Simple protocol server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
// Protocol implementation
size_t onSend(const void* data, size_t size) override { Multicast(data, size); return size; }
};
int main(int argc, char** argv)
{
// Simple protocol server port
int port = 4444;
if (argc > 1)
port = std::atoi(argv[1]);
std::cout << "Simple protocol server port: " << port << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<AsioService>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new simple protocol server
auto server = std::make_shared<SimpleProtoServer>(service, port);
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
// Multicast admin notification to all sessions
simple::SimpleNotify notify;
notify.Notification = "(admin) " + line;
server->send(notify);
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the simple protocol client. It connects to the simple protocol server and allows to send requests to it and receive corresponding responses.
#include "asio_service.h"
#include "server/asio/tcp_client.h"
#include "threads/thread.h"
#include "../proto/simple_protocol.h"
#include <atomic>
#include <iostream>
class SimpleProtoClient : public CppServer::Asio::TCPClient, public FBE::simple::Client
{
public:
using CppServer::Asio::TCPClient::TCPClient;
void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onConnected() override
{
std::cout << "Simple protocol client connected a new session with Id " << id() << std::endl;
// Reset FBE protocol buffers
reset();
}
void onDisconnected() override
{
std::cout << "Simple protocol client disconnected a session with Id " << id() << std::endl;
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Simple protocol client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
// Protocol handlers
void onReceive(const ::simple::DisconnectRequest& request) override { Client::onReceive(request); std::cout << "Received: " << request << std::endl; DisconnectAsync(); }
void onReceive(const ::simple::SimpleResponse& response) override { Client::onReceive(response); std::cout << "Received: " << response << std::endl; }
void onReceive(const ::simple::SimpleReject& reject) override { Client::onReceive(reject); std::cout << "Received: " << reject << std::endl; }
void onReceive(const ::simple::SimpleNotify& notify) override { Client::onReceive(notify); std::cout << "Received: " << notify << std::endl; }
// Protocol implementation
void onReceived(const void* buffer, size_t size) override { receive(buffer, size); }
size_t onSend(const void* data, size_t size) override { return SendAsync(data, size) ? size : 0; }
private:
std::atomic<bool> _stop{false};
};
int main(int argc, char** argv)
{
// TCP server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
// Simple protocol server port
int port = 4444;
if (argc > 2)
port = std::atoi(argv[2]);
std::cout << "Simple protocol server address: " << address << std::endl;
std::cout << "Simple protocol server port: " << port << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<AsioService>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new simple protocol client
auto client = std::make_shared<SimpleProtoClient>(service, address, port);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Reconnect the client
if (line == "!")
{
std::cout << "Client reconnecting...";
client->IsConnected() ? client->ReconnectAsync() : client->ConnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
// Send request to the simple protocol server
simple::SimpleRequest request;
request.Message = line;
auto response = client->request(request).get();
// Show string hash calculation result
std::cout << "Hash of '" << line << "' = " << std::format("0x{:8X}", response.Hash) << std::endl;
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the HTTP cache server. It allows to manipulate cache data with HTTP methods (GET, POST, PUT and DELETE).
Use the following link to open Swagger OpenAPI iterative documentation: http://localhost:8080/api/index.html
#include "server/http/http_server.h"
#include "string/string_utils.h"
#include "utility/singleton.h"
#include <iostream>
#include <map>
#include <mutex>
class Cache : public CppCommon::Singleton<Cache>
{
friend CppCommon::Singleton<Cache>;
public:
std::string GetAllCache()
{
std::scoped_lock locker(_cache_lock);
std::string result;
result += "[\n";
for (const auto& item : _cache)
{
result += " {\n";
result += " \"key\": \"" + item.first + "\",\n";
result += " \"value\": \"" + item.second + "\",\n";
result += " },\n";
}
result += "]\n";
return result;
}
bool GetCacheValue(std::string_view key, std::string& value)
{
std::scoped_lock locker(_cache_lock);
auto it = _cache.find(key);
if (it != _cache.end())
{
value = it->second;
return true;
}
else
return false;
}
void PutCacheValue(std::string_view key, std::string_view value)
{
std::scoped_lock locker(_cache_lock);
auto it = _cache.emplace(key, value);
if (!it.second)
it.first->second = value;
}
bool DeleteCacheValue(std::string_view key, std::string& value)
{
std::scoped_lock locker(_cache_lock);
auto it = _cache.find(key);
if (it != _cache.end())
{
value = it->second;
_cache.erase(it);
return true;
}
else
return false;
}
private:
std::mutex _cache_lock;
std::map<std::string, std::string, std::less<>> _cache;
};
class HTTPCacheSession : public CppServer::HTTP::HTTPSession
{
public:
using CppServer::HTTP::HTTPSession::HTTPSession;
protected:
void onReceivedRequest(const CppServer::HTTP::HTTPRequest& request) override
{
// Show HTTP request content
std::cout << std::endl << request;
// Process HTTP request methods
if (request.method() == "HEAD")
SendResponseAsync(response().MakeHeadResponse());
else if (request.method() == "GET")
{
std::string key(request.url());
std::string value;
// Decode the key value
key = CppCommon::Encoding::URLDecode(key);
CppCommon::StringUtils::ReplaceFirst(key, "/api/cache", "");
CppCommon::StringUtils::ReplaceFirst(key, "?key=", "");
if (key.empty())
{
// Response with all cache values
SendResponseAsync(response().MakeGetResponse(Cache::GetInstance().GetAllCache(), "application/json; charset=UTF-8"));
}
// Get the cache value by the given key
else if (Cache::GetInstance().GetCacheValue(key, value))
{
// Response with the cache value
SendResponseAsync(response().MakeGetResponse(value));
}
else
SendResponseAsync(response().MakeErrorResponse(404, "Required cache value was not found for the key: " + key));
}
else if ((request.method() == "POST") || (request.method() == "PUT"))
{
std::string key(request.url());
std::string value(request.body());
// Decode the key value
key = CppCommon::Encoding::URLDecode(key);
CppCommon::StringUtils::ReplaceFirst(key, "/api/cache", "");
CppCommon::StringUtils::ReplaceFirst(key, "?key=", "");
// Put the cache value
Cache::GetInstance().PutCacheValue(key, value);
// Response with the cache value
SendResponseAsync(response().MakeOKResponse());
}
else if (request.method() == "DELETE")
{
std::string key(request.url());
std::string value;
// Decode the key value
key = CppCommon::Encoding::URLDecode(key);
CppCommon::StringUtils::ReplaceFirst(key, "/api/cache", "");
CppCommon::StringUtils::ReplaceFirst(key, "?key=", "");
// Delete the cache value
if (Cache::GetInstance().DeleteCacheValue(key, value))
{
// Response with the cache value
SendResponseAsync(response().MakeGetResponse(value));
}
else
SendResponseAsync(response().MakeErrorResponse(404, "Deleted cache value was not found for the key: " + key));
}
else if (request.method() == "OPTIONS")
SendResponseAsync(response().MakeOptionsResponse());
else if (request.method() == "TRACE")
SendResponseAsync(response().MakeTraceResponse(request.cache()));
else
SendResponseAsync(response().MakeErrorResponse("Unsupported HTTP method: " + std::string(request.method())));
}
void onReceivedRequestError(const CppServer::HTTP::HTTPRequest& request, const std::string& error) override
{
std::cout << "Request error: " << error << std::endl;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "HTTP session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
class HTTPCacheServer : public CppServer::HTTP::HTTPServer
{
public:
using CppServer::HTTP::HTTPServer::HTTPServer;
protected:
std::shared_ptr<CppServer::Asio::TCPSession> CreateSession(const std::shared_ptr<CppServer::Asio::TCPServer>& server) override
{
return std::make_shared<HTTPCacheSession>(std::dynamic_pointer_cast<CppServer::HTTP::HTTPServer>(server));
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "HTTP server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// HTTP server port
int port = 8080;
if (argc > 1)
port = std::atoi(argv[1]);
// HTTP server content path
std::string www = "../www/api";
if (argc > 2)
www = argv[2];
std::cout << "HTTP server port: " << port << std::endl;
std::cout << "HTTP server static content path: " << www << std::endl;
std::cout << "HTTP server website: " << "http://localhost:" << port << "/api/index.html" << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new HTTP server
auto server = std::make_shared<HTTPCacheServer>(service, port);
server->AddStaticContent(www, "/api");
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the HTTP client. It allows to send HTTP requests (GET, POST, PUT and DELETE) and receive HTTP responses.
#include "server/http/http_client.h"
#include "string/string_utils.h"
#include <iostream>
int main(int argc, char** argv)
{
// HTTP server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
std::cout << "HTTP server address: " << address << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new HTTP client
auto client = std::make_shared<CppServer::HTTP::HTTPClientEx>(service, address, "http");
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
try
{
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Reconnect the client
if (line == "!")
{
std::cout << "Client reconnecting...";
client->ReconnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
auto commands = CppCommon::StringUtils::Split(line, ' ', true);
if (commands.size() < 2)
{
std::cout << "HTTP method and URL must be entered!" << std::endl;
continue;
}
if (CppCommon::StringUtils::ToUpper(commands[0]) == "HEAD")
{
auto response = client->SendHeadRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "GET")
{
auto response = client->SendGetRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "POST")
{
if (commands.size() < 3)
{
std::cout << "HTTP method, URL and body must be entered!" << std::endl;
continue;
}
auto response = client->SendPostRequest(commands[1], commands[2]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "PUT")
{
if (commands.size() < 3)
{
std::cout << "HTTP method, URL and body must be entered!" << std::endl;
continue;
}
auto response = client->SendPutRequest(commands[1], commands[2]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "DELETE")
{
auto response = client->SendDeleteRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "OPTIONS")
{
auto response = client->SendOptionsRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "TRACE")
{
auto response = client->SendTraceRequest(commands[1]).get();
std::cout << response << std::endl;
}
else
std::cout << "Unknown HTTP method: " << commands[0] << std::endl;
}
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the HTTPS cache server. It allows to manipulate cache data with HTTP methods (GET, POST, PUT and DELETE) with secured transport protocol.
Use the following link to open Swagger OpenAPI iterative documentation: https://localhost:8443/api/index.html
#include "server/http/https_server.h"
#include "string/string_utils.h"
#include "utility/singleton.h"
#include <iostream>
#include <map>
#include <mutex>
class Cache : public CppCommon::Singleton<Cache>
{
friend CppCommon::Singleton<Cache>;
public:
std::string GetAllCache()
{
std::scoped_lock locker(_cache_lock);
std::string result;
result += "[\n";
for (const auto& item : _cache)
{
result += " {\n";
result += " \"key\": \"" + item.first + "\",\n";
result += " \"value\": \"" + item.second + "\",\n";
result += " },\n";
}
result += "]\n";
return result;
}
bool GetCacheValue(std::string_view key, std::string& value)
{
std::scoped_lock locker(_cache_lock);
auto it = _cache.find(key);
if (it != _cache.end())
{
value = it->second;
return true;
}
else
return false;
}
void PutCacheValue(std::string_view key, std::string_view value)
{
std::scoped_lock locker(_cache_lock);
auto it = _cache.emplace(key, value);
if (!it.second)
it.first->second = value;
}
bool DeleteCacheValue(std::string_view key, std::string& value)
{
std::scoped_lock locker(_cache_lock);
auto it = _cache.find(key);
if (it != _cache.end())
{
value = it->second;
_cache.erase(it);
return true;
}
else
return false;
}
private:
std::mutex _cache_lock;
std::map<std::string, std::string, std::less<>> _cache;
};
class HTTPSCacheSession : public CppServer::HTTP::HTTPSSession
{
public:
using CppServer::HTTP::HTTPSSession::HTTPSSession;
protected:
void onReceivedRequest(const CppServer::HTTP::HTTPRequest& request) override
{
// Show HTTP request content
std::cout << std::endl << request;
// Process HTTP request methods
if (request.method() == "HEAD")
SendResponseAsync(response().MakeHeadResponse());
else if (request.method() == "GET")
{
std::string key(request.url());
std::string value;
// Decode the key value
key = CppCommon::Encoding::URLDecode(key);
CppCommon::StringUtils::ReplaceFirst(key, "/api/cache", "");
CppCommon::StringUtils::ReplaceFirst(key, "?key=", "");
if (key.empty())
{
// Response with all cache values
SendResponseAsync(response().MakeGetResponse(Cache::GetInstance().GetAllCache(), "application/json; charset=UTF-8"));
}
// Get the cache value by the given key
else if (Cache::GetInstance().GetCacheValue(key, value))
{
// Response with the cache value
SendResponseAsync(response().MakeGetResponse(value));
}
else
SendResponseAsync(response().MakeErrorResponse(404, "Required cache value was not found for the key: " + key));
}
else if ((request.method() == "POST") || (request.method() == "PUT"))
{
std::string key(request.url());
std::string value(request.body());
// Decode the key value
key = CppCommon::Encoding::URLDecode(key);
CppCommon::StringUtils::ReplaceFirst(key, "/api/cache", "");
CppCommon::StringUtils::ReplaceFirst(key, "?key=", "");
// Put the cache value
Cache::GetInstance().PutCacheValue(key, value);
// Response with the cache value
SendResponseAsync(response().MakeOKResponse());
}
else if (request.method() == "DELETE")
{
std::string key(request.url());
std::string value;
// Decode the key value
key = CppCommon::Encoding::URLDecode(key);
CppCommon::StringUtils::ReplaceFirst(key, "/api/cache", "");
CppCommon::StringUtils::ReplaceFirst(key, "?key=", "");
// Delete the cache value
if (Cache::GetInstance().DeleteCacheValue(key, value))
{
// Response with the cache value
SendResponseAsync(response().MakeGetResponse(value));
}
else
SendResponseAsync(response().MakeErrorResponse(404, "Deleted cache value was not found for the key: " + key));
}
else if (request.method() == "OPTIONS")
SendResponseAsync(response().MakeOptionsResponse());
else if (request.method() == "TRACE")
SendResponseAsync(response().MakeTraceResponse(request.cache()));
else
SendResponseAsync(response().MakeErrorResponse("Unsupported HTTP method: " + std::string(request.method())));
}
void onReceivedRequestError(const CppServer::HTTP::HTTPRequest& request, const std::string& error) override
{
std::cout << "Request error: " << error << std::endl;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "HTTPS session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
class HTTPSCacheServer : public CppServer::HTTP::HTTPSServer
{
public:
using CppServer::HTTP::HTTPSServer::HTTPSServer;
protected:
std::shared_ptr<CppServer::Asio::SSLSession> CreateSession(const std::shared_ptr<CppServer::Asio::SSLServer>& server) override
{
return std::make_shared<HTTPSCacheSession>(std::dynamic_pointer_cast<CppServer::HTTP::HTTPSServer>(server));
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "HTTPS server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// HTTPS server port
int port = 8443;
if (argc > 1)
port = std::atoi(argv[1]);
// HTTPS server content path
std::string www = "../www/api";
if (argc > 2)
www = argv[2];
std::cout << "HTTPS server port: " << port << std::endl;
std::cout << "HTTPS server static content path: " << www << std::endl;
std::cout << "HTTPS server website: " << "https://localhost:" << port << "/api/index.html" << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create and prepare a new SSL server context
auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv12);
context->set_password_callback([](size_t max_length, asio::ssl::context::password_purpose purpose) -> std::string { return "qwerty"; });
context->use_certificate_chain_file("../tools/certificates/server.pem");
context->use_private_key_file("../tools/certificates/server.pem", asio::ssl::context::pem);
context->use_tmp_dh_file("../tools/certificates/dh4096.pem");
// Create a new HTTPS server
auto server = std::make_shared<HTTPSCacheServer>(service, context, port);
server->AddStaticContent(www, "/api");
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the HTTPS client. It allows to send HTTP requests (GET, POST, PUT and DELETE) and receive HTTP responses with secured transport protocol.
#include "server/http/https_client.h"
#include "string/string_utils.h"
#include <iostream>
int main(int argc, char** argv)
{
// HTTP server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
std::cout << "HTTPS server address: " << address << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create and prepare a new SSL client context
auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv12);
context->set_default_verify_paths();
context->set_root_certs();
context->set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert);
context->load_verify_file("../tools/certificates/ca.pem");
// Create a new HTTP client
auto client = std::make_shared<CppServer::HTTP::HTTPSClientEx>(service, context, address, "https");
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
try
{
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Reconnect the client
if (line == "!")
{
std::cout << "Client reconnecting...";
client->ReconnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
auto commands = CppCommon::StringUtils::Split(line, ' ', true);
if (commands.size() < 2)
{
std::cout << "HTTP method and URL must be entered!" << std::endl;
continue;
}
if (CppCommon::StringUtils::ToUpper(commands[0]) == "HEAD")
{
auto response = client->SendHeadRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "GET")
{
auto response = client->SendGetRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "POST")
{
if (commands.size() < 3)
{
std::cout << "HTTP method, URL and body must be entered!" << std::endl;
continue;
}
auto response = client->SendPostRequest(commands[1], commands[2]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "PUT")
{
if (commands.size() < 3)
{
std::cout << "HTTP method, URL and body must be entered!" << std::endl;
continue;
}
auto response = client->SendPutRequest(commands[1], commands[2]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "DELETE")
{
auto response = client->SendDeleteRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "OPTIONS")
{
auto response = client->SendOptionsRequest(commands[1]).get();
std::cout << response << std::endl;
}
else if (CppCommon::StringUtils::ToUpper(commands[0]) == "TRACE")
{
auto response = client->SendTraceRequest(commands[1]).get();
std::cout << response << std::endl;
}
else
std::cout << "Unknown HTTP method: " << commands[0] << std::endl;
}
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the WebSocket chat server. It handles multiple WebSocket client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
Use the following link to open WebSocket chat server example: http://localhost:8080/chat/index.html
#include "server/ws/ws_server.h"
#include <iostream>
class ChatSession : public CppServer::WS::WSSession
{
public:
using CppServer::WS::WSSession::WSSession;
protected:
void onWSConnected(const CppServer::HTTP::HTTPRequest& request) override
{
std::cout << "Chat WebSocket session with Id " << id() << " connected!" << std::endl;
// Send invite message
std::string message("Hello from WebSocket chat! Please send a message or '!' to disconnect the client!");
SendTextAsync(message);
}
void onWSDisconnected() override
{
std::cout << "Chat WebSocket session with Id " << id() << " disconnected!" << std::endl;
}
void onWSReceived(const void* buffer, size_t size) override
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
// Multicast message to all connected sessions
std::dynamic_pointer_cast<CppServer::WS::WSServer>(server())->MulticastText(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Close(1000);
}
void onWSPing(const void* buffer, size_t size) override
{
SendPongAsync(buffer, size);
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat WebSocket session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
class ChatServer : public CppServer::WS::WSServer
{
public:
using CppServer::WS::WSServer::WSServer;
protected:
std::shared_ptr<CppServer::Asio::TCPSession> CreateSession(std::shared_ptr<CppServer::Asio::TCPServer> server) override
{
return std::make_shared<ChatSession>(std::dynamic_pointer_cast<CppServer::WS::WSServer>(server));
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat WebSocket server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// WebSocket server port
int port = 8080;
if (argc > 1)
port = std::atoi(argv[1]);
// WebSocket server content path
std::string www = "../www/ws";
if (argc > 2)
www = argv[2];
std::cout << "WebSocket server port: " << port << std::endl;
std::cout << "WebSocket server static content path: " << www << std::endl;
std::cout << "WebSocket server website: " << "http://localhost:" << port << "/chat/index.html" << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new WebSocket chat server
auto server = std::make_shared<ChatServer>(service, port);
server->AddStaticContent(www, "/chat");
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server->MulticastText(line);
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the WebSocket chat client. It connects to the WebSocket chat server and allows to send message to it and receive new messages.
#include "server/ws/ws_client.h"
#include "threads/thread.h"
#include <atomic>
#include <iostream>
class ChatClient : public CppServer::WS::WSClient
{
public:
using CppServer::WS::WSClient::WSClient;
void DisconnectAndStop()
{
_stop = true;
CloseAsync(1000);
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onWSConnecting(CppServer::HTTP::HTTPRequest& request) override
{
request.SetBegin("GET", "/");
request.SetHeader("Host", "localhost");
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", CppCommon::Encoding::Base64Encode(ws_nonce()));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
void onWSConnected(const CppServer::HTTP::HTTPResponse& response) override
{
std::cout << "Chat WebSocket client connected a new session with Id " << id() << std::endl;
}
void onWSDisconnected() override
{
std::cout << "Chat WebSocket client disconnected a session with Id " << id() << std::endl;
}
void onWSReceived(const void* buffer, size_t size) override
{
std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
}
void onWSPing(const void* buffer, size_t size) override
{
SendPongAsync(buffer, size);
}
void onDisconnected() override
{
WSClient::onDisconnected();
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat WebSocket client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
private:
std::atomic<bool> _stop{false};
};
int main(int argc, char** argv)
{
// WebSocket server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
// WebSocket server port
int port = 8080;
if (argc > 2)
port = std::atoi(argv[2]);
std::cout << "WebSocket server address: " << address << std::endl;
std::cout << "WebSocket server port: " << port << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new WebSocket chat client
auto client = std::make_shared<ChatClient>(service, address, port);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Reconnect the client
if (line == "!")
{
std::cout << "Client reconnecting...";
client->ReconnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
// Send the entered text to the chat server
client->SendTextAsync(line);
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the WebSocket secure chat server. It handles multiple WebSocket secure client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
This example is very similar to the WebSocket one except the code that prepares WebSocket secure context and handshake handler.
Use the following link to open WebSocket secure chat server example: https://localhost:8443/chat/index.html
#include "server/ws/wss_server.h"
#include <iostream>
class ChatSession : public CppServer::WS::WSSSession
{
public:
using CppServer::WS::WSSSession::WSSSession;
protected:
void onWSConnected(const CppServer::HTTP::HTTPRequest& request) override
{
std::cout << "Chat WebSocket secure session with Id " << id() << " connected!" << std::endl;
// Send invite message
std::string message("Hello from WebSocket secure chat! Please send a message or '!' to disconnect the client!");
SendTextAsync(message);
}
void onWSDisconnected() override
{
std::cout << "Chat WebSocket secure session with Id " << id() << " disconnected!" << std::endl;
}
void onWSReceived(const void* buffer, size_t size) override
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
// Multicast message to all connected sessions
std::dynamic_pointer_cast<CppServer::WS::WSSServer>(server())->MulticastText(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Close(1000);
}
void onWSPing(const void* buffer, size_t size) override
{
SendPongAsync(buffer, size);
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat WebSocket secure session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
class ChatServer : public CppServer::WS::WSSServer
{
public:
using CppServer::WS::WSSServer::WSSServer;
protected:
std::shared_ptr<CppServer::Asio::SSLSession> CreateSession(std::shared_ptr<CppServer::Asio::SSLServer> server) override
{
return std::make_shared<ChatSession>(std::dynamic_pointer_cast<CppServer::WS::WSSServer>(server));
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat WebSocket secure server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
// WebSocket secure server port
int port = 8443;
if (argc > 1)
port = std::atoi(argv[1]);
// WebSocket secure server content path
std::string www = "../www/wss";
if (argc > 2)
www = argv[2];
std::cout << "WebSocket secure server port: " << port << std::endl;
std::cout << "WebSocket secure server static content path: " << www << std::endl;
std::cout << "WebSocket server website: " << "https://localhost:" << port << "/chat/index.html" << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create and prepare a new SSL server context
auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv12);
context->set_password_callback([](size_t max_length, asio::ssl::context::password_purpose purpose) -> std::string { return "qwerty"; });
context->use_certificate_chain_file("../tools/certificates/server.pem");
context->use_private_key_file("../tools/certificates/server.pem", asio::ssl::context::pem);
context->use_tmp_dh_file("../tools/certificates/dh4096.pem");
// Create a new WebSocket secure chat server
auto server = std::make_shared<ChatServer>(service, context, port);
server->AddStaticContent(www, "/chat");
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server->MulticastText(line);
}
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Here comes the example of the WebSocket secure chat client. It connects to the WebSocket secure chat server and allows to send message to it and receive new messages.
This example is very similar to the WebSocket one except the code that prepares WebSocket secure context and handshake handler.
#include "server/ws/wss_client.h"
#include "threads/thread.h"
#include <atomic>
#include <iostream>
class ChatClient : public CppServer::WS::WSSClient
{
public:
using CppServer::WS::WSSClient::WSSClient;
void DisconnectAndStop()
{
_stop = true;
CloseAsync(1000);
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onWSConnecting(CppServer::HTTP::HTTPRequest& request) override
{
request.SetBegin("GET", "/");
request.SetHeader("Host", "localhost");
request.SetHeader("Origin", "https://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", CppCommon::Encoding::Base64Encode(ws_nonce()));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
void onWSConnected(const CppServer::HTTP::HTTPResponse& response) override
{
std::cout << "Chat WebSocket secure client connected a new session with Id " << id() << std::endl;
}
void onWSDisconnected() override
{
std::cout << "Chat WebSocket secure client disconnected a session with Id " << id() << std::endl;
}
void onWSReceived(const void* buffer, size_t size) override
{
std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
}
void onWSPing(const void* buffer, size_t size) override
{
SendPongAsync(buffer, size);
}
void onDisconnected() override
{
WSSClient::onDisconnected();
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat WebSocket secure client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
private:
std::atomic<bool> _stop{false};
};
int main(int argc, char** argv)
{
// WebSocket server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
// WebSocket server port
int port = 8443;
if (argc > 2)
port = std::atoi(argv[2]);
std::cout << "WebSocket secure server address: " << address << std::endl;
std::cout << "WebSocket secure server port: " << port << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create and prepare a new SSL client context
auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv12);
context->set_default_verify_paths();
context->set_root_certs();
context->set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert);
context->load_verify_file("../tools/certificates/ca.pem");
// Create a new WebSocket chat client
auto client = std::make_shared<ChatClient>(service, context, address, port);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Reconnect the client
if (line == "!")
{
std::cout << "Client reconnecting...";
client->ReconnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
// Send the entered text to the chat server
client->SendTextAsync(line);
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Performance
Here comes several communication scenarios with timing measurements.
Benchmark environment is the following:
CPU architecutre: Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
CPU logical cores: 8
CPU physical cores: 4
CPU clock speed: 3.998 GHz
CPU Hyper-Threading: enabled
RAM total: 31.962 GiB
RAM free: 21.623 GiB
OS version: Microsoft Windows 8 Enterprise Edition (build 9200), 64-bit
OS bits: 64-bit
Process bits: 64-bit
Process configuaraion: release
This scenario sends lots of messages from several clients to a server. The server responses to each message and resend the similar response to the client. The benchmark measures total round-trip time to send all messages and receive all responses, messages & data throughput, count of errors.
Server address: 127.0.0.1
Server port: 1111
Working threads: 1
Working clients: 1
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.001 s
Total data: 1.692 GiB
Total messages: 56261685
Data throughput: 171.693 MiB/s
Message latency: 177 ns
Message throughput: 5625528 msg/s
Server address: 127.0.0.1
Server port: 1111
Working threads: 4
Working clients: 100
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.007 s
Total data: 1.151 GiB
Total messages: 38503396
Data throughput: 117.423 MiB/s
Message latency: 259 ns
Message throughput: 3847402 msg/s
Server address: 127.0.0.1
Server port: 2222
Working threads: 1
Working clients: 1
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.012 s
Total data: 296.350 MiB
Total messages: 9710535
Data throughput: 29.612 MiB/s
Message latency: 1.031 mcs
Message throughput: 969878 msg/s
Server address: 127.0.0.1
Server port: 2222
Working threads: 4
Working clients: 100
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.341 s
Total data: 390.660 MiB
Total messages: 12800660
Data throughput: 37.792 MiB/s
Message latency: 807 ns
Message throughput: 1237782 msg/s
Server address: 127.0.0.1
Server port: 3333
Working threads: 1
Working clients: 1
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.002 s
Total data: 46.032 MiB
Total messages: 1508355
Data throughput: 4.616 MiB/s
Message latency: 6.631 mcs
Message throughput: 150801 msg/s
Server address: 127.0.0.1
Server port: 3333
Working threads: 4
Working clients: 100
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.152 s
Total data: 32.185 MiB
Total messages: 1054512
Data throughput: 3.173 MiB/s
Message latency: 9.627 mcs
Message throughput: 103867 msg/s
Server address: 127.0.0.1
Server port: 4444
Working threads: 1
Working clients: 1
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.002 s
Total data: 497.096 MiB
Total messages: 16288783
Data throughput: 49.715 MiB/s
Message latency: 614 ns
Message throughput: 1628542 msg/s
Server address: 127.0.0.1
Server port: 4444
Working threads: 4
Working clients: 100
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.066 s
Total data: 997.384 MiB
Total messages: 32681995
Data throughput: 99.078 MiB/s
Message latency: 308 ns
Message throughput: 3246558 msg/s
Server address: 127.0.0.1
Server port: 8080
Working threads: 1
Working clients: 1
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 9.994 s
Total data: 48.958 MiB
Total messages: 1603548
Data throughput: 4.918 MiB/s
Message latency: 6.232 mcs
Message throughput: 160448 msg/s
Server address: 127.0.0.1
Server port: 8080
Working threads: 4
Working clients: 100
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 11.402 s
Total data: 206.827 MiB
Total messages: 6776702
Data throughput: 18.140 MiB/s
Message latency: 1.682 mcs
Message throughput: 594328 msg/s
Server address: 127.0.0.1
Server port: 8443
Working threads: 1
Working clients: 1
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.001 s
Total data: 62.068 MiB
Total messages: 2033811
Data throughput: 6.210 MiB/s
Message latency: 4.917 mcs
Message throughput: 203343 msg/s
Server address: 127.0.0.1
Server port: 8443
Working threads: 4
Working clients: 100
Working messages: 1000
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.011 s
Total data: 249.1023 MiB
Total messages: 8191971
Data throughput: 24.993 MiB/s
Message latency: 1.222 mcs
Message throughput: 818230 msg/s
In this scenario server multicasts messages to all connected clients. The benchmark counts total messages received by all clients for all the working time and measures messages & data throughput, count of errors.
Server address: 127.0.0.1
Server port: 1111
Working threads: 1
Working clients: 1
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.001 s
Total data: 1.907 GiB
Total messages: 63283367
Data throughput: 193.103 MiB/s
Message latency: 158 ns
Message throughput: 6327549 msg/s
Server address: 127.0.0.1
Server port: 1111
Working threads: 4
Working clients: 100
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.006 s
Total data: 1.1006 GiB
Total messages: 66535013
Data throughput: 202.930 MiB/s
Message latency: 150 ns
Message throughput: 6648899 msg/s
Server address: 127.0.0.1
Server port: 2222
Working threads: 1
Working clients: 1
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.014 s
Total data: 1.535 GiB
Total messages: 51100073
Data throughput: 155.738 MiB/s
Message latency: 195 ns
Message throughput: 5102683 msg/s
Server address: 127.0.0.1
Server port: 2222
Working threads: 4
Working clients: 100
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.691 s
Total data: 1.878 GiB
Total messages: 62334478
Data throughput: 177.954 MiB/s
Message latency: 171 ns
Message throughput: 5830473 msg/s
Server address: 239.255.0.1
Server port: 3333
Working threads: 1
Working clients: 1
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.002 s
Total data: 23.777 MiB
Total messages: 778555
Data throughput: 2.384 MiB/s
Message latency: 12.847 mcs
Message throughput: 77833 msg/s
Server address: 239.255.0.1
Server port: 3333
Working threads: 4
Working clients: 100
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.004 s
Total data: 52.457 MiB
Total messages: 1718575
Data throughput: 5.248 MiB/s
Message latency: 5.821 mcs
Message throughput: 171784 msg/s
Server address: 127.0.0.1
Server port: 8080
Working threads: 1
Working clients: 1
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.001 s
Total data: 960.902 MiB
Total messages: 31486166
Data throughput: 96.075 MiB/s
Message latency: 317 ns
Message throughput: 3148135 msg/s
Server address: 127.0.0.1
Server port: 8080
Working threads: 4
Working clients: 100
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.020 s
Total data: 986.489 MiB
Total messages: 32324898
Data throughput: 98.459 MiB/s
Message latency: 309 ns
Message throughput: 3225965 msg/s
Server address: 127.0.0.1
Server port: 8443
Working threads: 1
Working clients: 1
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.002 s
Total data: 1.041 GiB
Total messages: 34903186
Data throughput: 106.505 MiB/s
Message latency: 286 ns
Message throughput: 3489578 msg/s
Server address: 127.0.0.1
Server port: 8443
Working threads: 4
Working clients: 100
Message size: 32
Seconds to benchmarking: 10
Errors: 0
Total time: 10.013 s
Total data: 1.569 GiB
Total messages: 52225588
Data throughput: 159.172 MiB/s
Message latency: 191 ns
Message throughput: 5215639 msg/s
Server address: 127.0.0.1
Server port: 80
Working threads: 1
Working clients: 1
Working messages: 1
Seconds to benchmarking: 10
Errors: 0
Total time: 10.001 s
Total data: 58.476 MiB
Total messages: 578353
Data throughput: 5.865 MiB/s
Message latency: 17.293 mcs
Message throughput: 57825 msg/s
Server address: 127.0.0.1
Server port: 80
Working threads: 4
Working clients: 100
Working messages: 1
Seconds to benchmarking: 10
Errors: 0
Total time: 10.006 s
Total data: 310.730 MiB
Total messages: 3073650
Data throughput: 31.051 MiB/s
Message latency: 3.255 mcs
Message throughput: 307154 msg/s
Server address: 127.0.0.1
Server port: 443
Working threads: 1
Working clients: 1
Working messages: 1
Seconds to benchmarking: 10
Errors: 0
Total time: 10.003 s
Total data: 37.475 MiB
Total messages: 370602
Data throughput: 3.763 MiB/s
Message latency: 26.992 mcs
Message throughput: 37047 msg/s
Server address: 127.0.0.1
Server port: 443
Working threads: 4
Working clients: 100
Working messages: 1
Seconds to benchmarking: 10
Errors: 0
Total time: 10.035 s
Total data: 204.531 MiB
Total messages: 2023152
Data throughput: 20.389 MiB/s
Message latency: 4.960 mcs
Message throughput: 201602 msg/s
OpenSSL certificates
In order to create OpenSSL based server and client you should prepare a set of SSL certificates.
Depending on your project, you may need to purchase a traditional SSL certificate signed by a Certificate Authority. If you, for instance, want some else's web browser to talk to your WebSocket project, you'll need a traditional SSL certificate.
The commands below entered in the order they are listed will generate a self-signed certificate for development or testing purposes.
openssl genrsa -passout pass:qwerty -out ca-secret.key 4096
openssl rsa -passin pass:qwerty -in ca-secret.key -out ca.key
openssl req -new -x509 -days 3650 -subj '/C=BY/ST=Belarus/L=Minsk/O=Example root CA/OU=Example CA unit/CN=example.com' -key ca.key -out ca.crt
openssl pkcs12 -export -passout pass:qwerty -inkey ca.key -in ca.crt -out ca.pfx
openssl pkcs12 -passin pass:qwerty -passout pass:qwerty -in ca.pfx -out ca.pem
openssl genrsa -passout pass:qwerty -out server-secret.key 4096
openssl rsa -passin pass:qwerty -in server-secret.key -out server.key
openssl req -new -subj '/C=BY/ST=Belarus/L=Minsk/O=Example server/OU=Example server unit/CN=server.example.com' -key server.key -out server.csr
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt
openssl pkcs12 -export -passout pass:qwerty -inkey server.key -in server.crt -out server.pfx
openssl pkcs12 -passin pass:qwerty -passout pass:qwerty -in server.pfx -out server.pem
openssl genrsa -passout pass:qwerty -out client-secret.key 4096
openssl rsa -passin pass:qwerty -in client-secret.key -out client.key
openssl req -new -subj '/C=BY/ST=Belarus/L=Minsk/O=Example client/OU=Example client unit/CN=client.example.com' -key client.key -out client.csr
openssl x509 -req -days 3650 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt
openssl pkcs12 -export -passout pass:qwerty -inkey client.key -in client.crt -out client.pfx
openssl pkcs12 -passin pass:qwerty -passout pass:qwerty -in client.pfx -out client.pem
openssl dhparam -out dh4096.pem 4096
Author: chronoxor
Source Code: https://github.com/chronoxor/CppServer
License: MIT License
1649494800
include/indicators
.single_include/indicators
.To introduce a progress bar in your application, include indicators/progress_bar.hpp
and create a ProgressBar
object. Here's the general structure of a progress bar:
{prefix} {start} {fill} {lead} {remaining} {end} {percentage} [{elapsed}<{remaining}] {postfix}
^^^^^^^^^^^^^ Bar Width ^^^^^^^^^^^^^^^
The amount of progress in ProgressBar is maintained as a size_t
in range [0, 100]
. When progress reaches 100, the progression is complete.
From application-level code, there are two ways in which you can update this progress:
bar.tick()
You can update the progress bar using bar.tick()
which increments progress by exactly 1%
.
#include <indicators/progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
using namespace indicators;
ProgressBar bar{
option::BarWidth{50},
option::Start{"["},
option::Fill{"="},
option::Lead{">"},
option::Remainder{" "},
option::End{"]"},
option::PostfixText{"Extracting Archive"},
option::ForegroundColor{Color::green},
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return 0;
}
The above code will print a progress bar that goes from 0 to 100% at the rate of 1% every 100 ms.
bar.set_progress(value)
If you'd rather control progress of the bar in discrete steps, consider using bar.set_progress(value)
. Example:
#include <chrono>
#include <indicators/cursor_control.hpp>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
using namespace indicators;
// Hide cursor
show_console_cursor(false);
ProgressBar bar{
option::BarWidth{50},
option::Start{"["},
option::Fill{"■"},
option::Lead{"■"},
option::Remainder{"-"},
option::End{" ]"},
option::PostfixText{"Loading dependency 1/4"},
option::ForegroundColor{Color::cyan},
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}
};
// Update bar state
bar.set_progress(10); // 10% done
// do some work
std::this_thread::sleep_for(std::chrono::milliseconds(800));
bar.set_option(option::PostfixText{"Loading dependency 2/4"});
bar.set_progress(30); // 30% done
// do some more work
std::this_thread::sleep_for(std::chrono::milliseconds(700));
bar.set_option(option::PostfixText{"Loading dependency 3/4"});
bar.set_progress(65); // 65% done
// do final bit of work
std::this_thread::sleep_for(std::chrono::milliseconds(900));
bar.set_option(option::PostfixText{"Loaded dependencies!"});
bar.set_progress(100); // all done
// Show cursor
show_console_cursor(true);
return 0;
}
All progress bars and spinners in indicators
support showing time elapsed and time remaining. Inspired by python's tqdm module, the format of this meter is [{elapsed}<{remaining}]
:
#include <chrono>
#include <indicators/cursor_control.hpp>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
using namespace indicators;
// Hide cursor
show_console_cursor(false);
indicators::ProgressBar bar{
option::BarWidth{50},
option::Start{" ["},
option::Fill{"█"},
option::Lead{"█"},
option::Remainder{"-"},
option::End{"]"},
option::PrefixText{"Training Gaze Network 👀"},
option::ForegroundColor{Color::yellow},
option::ShowElapsedTime{true},
option::ShowRemainingTime{true},
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// Show cursor
show_console_cursor(true);
return 0;
}
You might have a use-case for a progress bar where the maximum amount of progress is unknown, e.g., you're downloading from a remote server that isn't advertising the total bytes.
Use an indicators::IndeterminateProgressBar
for such cases. An IndeterminateProgressBar
is similar to a regular progress bar except the total amount to progress towards is unknown. Ticking on this progress bar will happily run forever.
When you know progress is complete, simply call bar.mark_as_completed()
.
#include <chrono>
#include <indicators/indeterminate_progress_bar.hpp>
#include <indicators/cursor_control.hpp>
#include <indicators/termcolor.hpp>
#include <thread>
int main() {
indicators::IndeterminateProgressBar bar{
indicators::option::BarWidth{40},
indicators::option::Start{"["},
indicators::option::Fill{"·"},
indicators::option::Lead{"<==>"},
indicators::option::End{"]"},
indicators::option::PostfixText{"Checking for Updates"},
indicators::option::ForegroundColor{indicators::Color::yellow},
indicators::option::FontStyles{
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
};
indicators::show_console_cursor(false);
auto job = [&bar]() {
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
bar.mark_as_completed();
std::cout << termcolor::bold << termcolor::green
<< "System is up to date!\n" << termcolor::reset;
};
std::thread job_completion_thread(job);
// Update bar state
while (!bar.is_completed()) {
bar.tick();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
job_completion_thread.join();
indicators::show_console_cursor(true);
return 0;
}
Are you in need of a smooth block progress bar using unicode block elements? Use BlockProgressBar
instead of ProgressBar
. Thanks to this blog post for making BlockProgressBar
an easy addition to the library.
#include <indicators/block_progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
using namespace indicators;
// Hide cursor
show_console_cursor(false);
BlockProgressBar bar{
option::BarWidth{80},
option::Start{"["},
option::End{"]"},
option::ForegroundColor{Color::white} ,
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}
};
// Update bar state
auto progress = 0.0f;
while (true) {
bar.set_progress(progress);
progress += 0.25f;
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
// Show cursor
show_console_cursor(true);
return 0;
}
indicators
supports management of multiple progress bars with the MultiProgress
class template.
template <typename Indicator, size_t count> class MultiProgress
is a class template that holds references to multiple progress bars and provides a safe interface to update the state of each bar. MultiProgress
works with both ProgressBar
and BlockProgressBar
classes.
Use this class if you know the number of progress bars to manage at compile time.
Below is an example MultiProgress
object that manages three ProgressBar
objects.
#include <indicators/multi_progress.hpp>
#include <indicators/progress_bar.hpp>
int main() {
using namespace indicators;
// Configure first progress bar
ProgressBar bar1{
option::BarWidth{50},
option::Start{"["},
option::Fill{"■"},
option::Lead{"■"},
option::Remainder{" "},
option::End{" ]"},
option::ForegroundColor{Color::yellow},
option::ShowElapsedTime{true},
option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #1 "},
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}
};
// Configure second progress bar
ProgressBar bar2{
option::BarWidth{50},
option::Start{"["},
option::Fill{"="},
option::Lead{">"},
option::Remainder{" "},
option::End{" ]"},
option::ForegroundColor{Color::cyan},
option::ShowElapsedTime{true},
option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #2 "},
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}
};
// Configure third progress bar
indicators::ProgressBar bar3{
option::BarWidth{50},
option::Start{"["},
option::Fill{"#"},
option::Lead{"#"},
option::Remainder{" "},
option::End{" ]"},
option::ForegroundColor{Color::red},
option::ShowElapsedTime{true},
option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #3 "},
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}
};
// Construct MultiProgress object
indicators::MultiProgress<indicators::ProgressBar, 3> bars(bar1, bar2, bar3);
std::cout << "Multiple Progress Bars:\n";
auto job1 = [&bars]() {
while (true) {
bars.tick<0>();
if (bars.is_completed<0>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
};
auto job2 = [&bars]() {
while (true) {
bars.tick<1>();
if (bars.is_completed<1>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
};
auto job3 = [&bars]() {
while (true) {
bars.tick<2>();
if (bars.is_completed<2>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
};
std::thread first_job(job1);
std::thread second_job(job2);
std::thread third_job(job3);
first_job.join();
second_job.join();
third_job.join();
return 0;
}
DynamicProgress
is a container class, similar to MultiProgress
, for managing multiple progress bars. As the name suggests, with DynamicProgress
, you can dynamically add new progress bars.
To add new progress bars, call bars.push_back(new_bar)
. This call will return the index of the appended bar. You can then refer to this bar with the indexing operator, e.g., bars[4].set_progress(55)
.
Use this class if you don't know the number of progress bars at compile time.
Below is an example DynamicProgress
object that manages six ProgressBar
objects. Three of these bars are added dynamically.
#include <indicators/dynamic_progress.hpp>
#include <indicators/progress_bar.hpp>
using namespace indicators;
int main() {
ProgressBar bar1{option::BarWidth{50}, option::ForegroundColor{Color::red},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"5c90d4a2d1a8: Downloading "}};
ProgressBar bar2{option::BarWidth{50}, option::ForegroundColor{Color::yellow},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"22337bfd13a9: Downloading "}};
ProgressBar bar3{option::BarWidth{50}, option::ForegroundColor{Color::green},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"10f26c680a34: Downloading "}};
ProgressBar bar4{option::BarWidth{50}, option::ForegroundColor{Color::white},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"6364e0d7a283: Downloading "}};
ProgressBar bar5{option::BarWidth{50}, option::ForegroundColor{Color::blue},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"ff1356ba118b: Downloading "}};
ProgressBar bar6{option::BarWidth{50}, option::ForegroundColor{Color::cyan},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"5a17453338b4: Downloading "}};
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
// Construct with 3 progress bars. We'll add 3 more at a later point
DynamicProgress<ProgressBar> bars(bar1, bar2, bar3);
// Do not hide bars when completed
bars.set_option(option::HideBarWhenComplete{false});
std::thread fourth_job, fifth_job, sixth_job;
auto job4 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"6364e0d7a283: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
};
auto job5 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"ff1356ba118b: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
};
auto job6 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"5a17453338b4: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(40));
}
};
auto job1 = [&bars, &bar6, &sixth_job, &job6]() {
while (true) {
bars[0].tick();
if (bars[0].is_completed()) {
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
// bar1 is completed, adding bar6
auto i = bars.push_back(bar6);
sixth_job = std::thread(job6, i);
sixth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(140));
}
};
auto job2 = [&bars, &bar5, &fifth_job, &job5]() {
while (true) {
bars[1].tick();
if (bars[1].is_completed()) {
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
// bar2 is completed, adding bar5
auto i = bars.push_back(bar5);
fifth_job = std::thread(job5, i);
fifth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(25));
}
};
auto job3 = [&bars, &bar4, &fourth_job, &job4]() {
while (true) {
bars[2].tick();
if (bars[2].is_completed()) {
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
// bar3 is completed, adding bar4
auto i = bars.push_back(bar4);
fourth_job = std::thread(job4, i);
fourth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
};
std::thread first_job(job1);
std::thread second_job(job2);
std::thread third_job(job3);
third_job.join();
second_job.join();
first_job.join();
std::cout << termcolor::bold << termcolor::green << "✔ Downloaded image foo/bar:baz" << std::endl;
std::cout << termcolor::reset;
return 0;
}
In the above code, notice the option bars.set_option(option::HideBarWhenComplete{true});
. Yes, you can hide progress bars as and when they complete by setting this option to true
. If you do so, the above example will look like this:
To introduce a progress spinner in your application, include indicators/progress_spinner.hpp
and create a ProgressSpinner
object. Here's the general structure of a progress spinner:
{prefix} {spinner} {percentage} [{elapsed}<{remaining}] {postfix}
ProgressSpinner has a vector of strings: spinner_states
. At each update, the spinner will pick the next string from this sequence to print to the console. The spinner state can be updated similarly to ProgressBars: Using either tick()
or set_progress(value)
.
#include <indicators/progress_spinner.hpp>
int main() {
using namespace indicators;
indicators::ProgressSpinner spinner{
option::PostfixText{"Checking credentials"},
option::ForegroundColor{Color::yellow},
option::SpinnerStates{std::vector<std::string>{"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}},
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}
};
// Update spinner state
auto job = [&spinner]() {
while (true) {
if (spinner.is_completed()) {
spinner.set_option(option::ForegroundColor{Color::green});
spinner.set_option(option::PrefixText{"✔"});
spinner.set_option(option::ShowSpinner{false});
spinner.set_option(option::ShowPercentage{false});
spinner.set_option(option::PostfixText{"Authenticated!"});
spinner.mark_as_completed();
break;
} else
spinner.tick();
std::this_thread::sleep_for(std::chrono::milliseconds(40));
}
};
std::thread thread(job);
thread.join();
return 0;
}
indicators
allows you to easily control the progress direction, i.e., incremental or decremental progress by using option::ProgressType
. To program a countdown progress bar, use option::ProgressType::decremental
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
using namespace indicators;
int main() {
ProgressBar bar{option::BarWidth{50},
option::ProgressType{ProgressType::decremental},
option::Start{"["},
option::Fill{"■"},
option::Lead{"■"},
option::Remainder{"-"},
option::End{"]"},
option::PostfixText{"Reverting System Restore"},
option::ForegroundColor{Color::yellow},
option::FontStyles{std::vector<FontStyle>{FontStyle::bold}}};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << termcolor::bold << termcolor::white
<< "Task Failed Successfully\n" << termcolor::reset;
return 0;
}
If you'd like to use progress bars to indicate progress while iterating over iterables, e.g., a list of numbers, this can be achieved by using the option::MaxProgress
:
#include <chrono>
#include <indicators/block_progress_bar.hpp>
#include <indicators/cursor_control.hpp>
#include <thread>
int main() {
// Hide cursor
indicators::show_console_cursor(false);
// Random list of numbers
std::vector<size_t> numbers;
for (size_t i = 0; i < 1259438; ++i) {
numbers.push_back(i);
}
using namespace indicators;
BlockProgressBar bar{
option::BarWidth{80},
option::ForegroundColor{Color::white},
option::FontStyles{
std::vector<FontStyle>{FontStyle::bold}},
option::MaxProgress{numbers.size()}
};
std::cout << "Iterating over a list of numbers (size = "
<< numbers.size() << ")\n";
std::vector<size_t> result;
for (size_t i = 0; i < numbers.size(); ++i) {
// Perform some computation
result.push_back(numbers[i] * numbers[i]);
// Show iteration as postfix text
bar.set_option(option::PostfixText{
std::to_string(i) + "/" + std::to_string(numbers.size())
});
// update progress bar
bar.tick();
}
bar.mark_as_completed();
// Show cursor
indicators::show_console_cursor(true);
return 0;
}
indicators
supports multi-byte unicode characters in progress bars.
If the option::BarWidth
is set, the library aims to respect this setting. When filling the bar, if the next Fill
string has a display width that would exceed the bar width, then the library will fill the remainder of the bar with ' '
space characters instead.
See below an example of some progress bars, each with a bar width of 50, displaying different unicode characters:
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <indicators/indeterminate_progress_bar.hpp>
#include <indicators/cursor_control.hpp>
#include <thread>
int main() {
indicators::show_console_cursor(false);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
{
// Plain old ASCII
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"="},
indicators::option::Lead{">"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Plain-old ASCII"},
indicators::option::ForegroundColor{indicators::Color::green},
indicators::option::FontStyles{
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
{
// Unicode
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"驚くばかり"},
indicators::option::Lead{">"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Japanese"},
indicators::option::ForegroundColor{indicators::Color::yellow},
indicators::option::FontStyles{
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
{
// Russian
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"Потрясающие"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Russian"},
indicators::option::ForegroundColor{indicators::Color::red},
indicators::option::FontStyles{
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
{
// Greek
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"Φοβερός"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Greek"},
indicators::option::ForegroundColor{indicators::Color::cyan},
indicators::option::FontStyles{
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
{
// Chinese
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"太棒了"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Chinese"},
indicators::option::ForegroundColor{indicators::Color::green},
indicators::option::FontStyles{
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
{
// Emojis
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"🔥"},
indicators::option::Lead{"🔥"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Emojis"},
indicators::option::ForegroundColor{indicators::Color::white},
indicators::option::FontStyles{
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
{
// Indeterminate progress bar
indicators::IndeterminateProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"✯"},
indicators::option::Lead{"載入中"},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Loading Progress Bar"},
indicators::option::ForegroundColor{indicators::Color::yellow},
indicators::option::FontStyles{
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}
};
auto job = [&bar]() {
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
bar.mark_as_completed();
};
std::thread job_completion_thread(job);
// Update bar state
while (!bar.is_completed()) {
bar.tick();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
job_completion_thread.join();
}
indicators::show_console_cursor(true);
return 0;
}
git clone https://github.com/p-ranav/indicators
cd indicators
mkdir build && cd build
cmake -DINDICATORS_SAMPLES=ON -DINDICATORS_DEMO=ON ..
make
For Windows, if you use WinLibs like I do, the cmake command would look like this:
foo@bar:~$ mkdir build && cd build
foo@bar:~$ cmake -G "MinGW Makefiles" -DCMAKE_CXX_COMPILER="C:/WinLibs/mingw64/bin/g++.exe" -DINDICATORS_SAMPLES=ON -DINDICATORS_DEMO=ON ..
foo@bar:~$ make -j4
python3 utils/amalgamate/amalgamate.py -c single_include.json -s .
Contributions are welcome, have a look at the CONTRIBUTING.md document for more information.
Author: p-ranav
Source Code: https://github.com/p-ranav/indicators/
License: View license
1651161600
An object-oriented C++ wrapper for cURL tool
If you want to know a bit more about cURL and libcurl, you should go on the official website http://curl.haxx.se/
Donate
Help me to improve this project!
Compile and link
cd build
cmake ..
make # -j2
Note: cURL >= 7.34 is required.
Then add <curlcpp root>/build/src/
to your library path and <curlcpp root>/include/
to your include path.
When linking, link against curlcpp
(e.g.: g++ -std=c++11 example.cpp -o example -lcurlcpp -lcurl). Or if you want run from terminal,
g++ -std=c++11 example.cpp -L/home/username/path/to/build/src/ -I/home/username/path/to/include/ -lcurlcpp -lcurl
When using a git submodule and CMake-buildsystem, add the following lines to your CMakeLists.txt
:
ADD_SUBDIRECTORY(ext/curlcpp) # Change `ext/curlcpp` to a directory according to your setup
INCLUDE_DIRECTORIES(${CURLCPP_SOURCE_DIR}/include)
Simple usage example
Here are some usage examples. You will find more examples in the test folder!
Here's an example of a simple HTTP request to get google web page, using the curl_easy interface:
#include "curl_easy.h"
using curl::curl_easy;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;
/**
* This example shows how to make a simple request with curl.
*/
int main() {
// Easy object to handle the connection.
curl_easy easy;
// Add some options.
easy.add<CURLOPT_URL>("http://<your_url_here>");
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
try {
easy.perform();
} catch (curl_easy_exception &error) {
// If you want to print the last error.
std::cerr<<error.what()<<std::endl;
}
return 0;
}
If you want to get information about the current curl session, you could do:
#include "curl_easy.h"
#include "curl_ios.h"
#include "curl_exception.h"
using std::ostringstream;
using curl::curl_easy;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;
using curl::curl_ios;
/**
* This example shows how to use the easy interface and obtain
* informations about the current session.
*/
int main(int argc, const char **argv) {
// Let's declare a stream
ostringstream stream;
// We are going to put the request's output in the previously declared stream
curl_ios<ostringstream> ios(stream);
// Declaration of an easy object
curl_easy easy(ios);
// Add some option to the curl_easy object.
easy.add<CURLOPT_URL>("http://<your_url_here>");
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
try {
easy.perform();
// Retrieve information about curl current session.
auto x = easy.get_info<CURLINFO_CONTENT_TYPE>();
/**
* get_info returns a curl_easy_info object. With the get method we retrieve
* the std::pair object associated with it: the first item is the return code of the
* request. The second is the element requested by the specified libcurl macro.
*/
std::cout<<x.get()<<std::endl;
} catch (curl_easy_exception &error) {
// If you want to print the last error.
std::cerr<<error.what()<<std::endl;
// If you want to print the entire error stack you can do
error.print_traceback();
}
return 0;
}
Here's instead, the creation of an HTTPS POST login form:
#include <string>
#include "curl_easy.h"
#include "curl_pair.h"
#include "curl_form.h"
#include "curl_exception.h"
using std::string;
using curl::curl_form;
using curl::curl_easy;
using curl::curl_pair;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;
int main(int argc, const char * argv[]) {
curl_form form;
curl_easy easy;
// Forms creation
curl_pair<CURLformoption,string> name_form(CURLFORM_COPYNAME,"user");
curl_pair<CURLformoption,string> name_cont(CURLFORM_COPYCONTENTS,"you username here");
curl_pair<CURLformoption,string> pass_form(CURLFORM_COPYNAME,"passw");
curl_pair<CURLformoption,string> pass_cont(CURLFORM_COPYCONTENTS,"your password here");
try {
// Form adding
form.add(name_form,name_cont);
form.add(pass_form,pass_cont);
// Add some options to our request
easy.add<CURLOPT_URL>("http://<your_url_here>");
easy.add<CURLOPT_SSL_VERIFYPEER>(false);
easy.add<CURLOPT_HTTPPOST>(form.get());
// Execute the request.
easy.perform();
} catch (curl_easy_exception &error) {
// If you want to get the entire error stack we can do:
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
}
return 0;
}
And if we would like to put the returned content in a file? Nothing easier than:
#include <iostream>
#include <ostream>
#include <fstream>
#include "curl_easy.h"
#include "curl_ios.h"
#include "curl_exception.h"
using std::cout;
using std::endl;
using std::ostream;
using std::ofstream;
using curl::curl_easy;
using curl::curl_ios;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;
int main(int argc, const char * argv[]) {
// Create a file
ofstream myfile;
myfile.open ("/path/to/your/file");
// Create a curl_ios object to handle the stream
curl_ios<ostream> writer(myfile);
// Pass it to the easy constructor and watch the content returned in that file!
curl_easy easy(writer);
// Add some option to the easy handle
easy.add<CURLOPT_URL>("http://<your_url_here>");
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
try {
// Execute the request
easy.perform();
} catch (curl_easy_exception &error) {
// If you want to print the last error.
std::cerr<<error.what()<<std::endl;
// If you want to print the entire error stack you can do
error.print_traceback();
}
myfile.close();
return 0;
}
Not interested in files? So let's put the request's output in a variable!
#include <iostream>
#include <ostream>
#include "curl_easy.h"
#include "curl_form.h"
#include "curl_ios.h"
#include "curl_exception.h"
using std::cout;
using std::endl;
using std::ostringstream;
using curl::curl_easy;
using curl::curl_ios;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;
int main() {
// Create a stringstream object
ostringstream str;
// Create a curl_ios object, passing the stream object.
curl_ios<ostringstream> writer(str);
// Pass the writer to the easy constructor and watch the content returned in that variable!
curl_easy easy(writer);
// Add some option to the easy handle
easy.add<CURLOPT_URL>("http://<your_url_here>");
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
try {
easy.perform();
// Let's print the stream content
cout<<str.str()<<endl;
} catch (curl_easy_exception &error) {
// If you want to print the last error.
std::cerr<<error.what()<<std::endl;
// If you want to print the entire error stack you can do
error.print_traceback();
}
return 0;
}
I have implemented a sender and a receiver to make it easy to use send/receive without handling buffers. For example, a very simple send/receiver would be:
#include <iostream>
#include <string>
#include "curl_easy.h"
#include "curl_form.h"
#include "curl_pair.h"
#include "curl_receiver.h"
#include "curl_exception.h"
#include "curl_sender.h"
using std::cout;
using std::endl;
using std::string;
using curl::curl_form;
using curl::curl_easy;
using curl::curl_sender;
using curl::curl_receiver;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;
int main(int argc, const char * argv[]) {
// Simple request
string request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
// Creation of easy object.
curl_easy easy;
try {
easy.add<CURLOPT_URL>("http://<your_url_here>");
// Just connect
easy.add<CURLOPT_CONNECT_ONLY>(true);
// Execute the request.
easy.perform();
} catch (curl_easy_exception &error) {
// If you want to get the entire error stack we can do:
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
}
// Creation of a sender. You should wait here using select to check if socket is ready to send.
curl_sender<string> sender(easy);
sender.send(request);
// Prints che sent bytes number.
cout<<"Sent bytes: "<<sender.get_sent_bytes()<<endl;
for(;;) {
// You should wait here to check if socket is ready to receive
try {
// Create a receiver
curl_receiver<char, 1024> receiver;
// Receive the content on the easy handler
receiver.receive(easy);
// Prints the received bytes number.
cout<<"Receiver bytes: "<<receiver.get_received_bytes()<<endl;
} catch (curl_easy_exception &error) {
// If any errors occurs, exit from the loop
break;
}
}
return 0;
}
Author: JosephP91
Source Code: https://github.com/JosephP91/curlcpp
License: MIT License
#cpluplus #c
1648900800
I founded this project, because I wanted to publish the code I wrote in the last two years, when I tried to write enhanced checking for PostgreSQL upstream. It was not fully successful - integration into upstream requires some larger plpgsql refactoring - probably it will not be done in next years (now is Dec 2013). But written code is fully functional and can be used in production (and it is used in production). So, I created this extension to be available for all plpgsql developers.
If you like it and if you would to join to development of this extension, register yourself to postgresql extension hacking google group.
Features
I invite any ideas, patches, bugreports.
plpgsql_check is next generation of plpgsql_lint. It allows to check source code by explicit call plpgsql_check_function.
PostgreSQL PostgreSQL 10, 11, 12, 13 and 14 are supported.
The SQL statements inside PL/pgSQL functions are checked by validator for semantic errors. These errors can be found by plpgsql_check_function:
Active mode
postgres=# CREATE EXTENSION plpgsql_check;
LOAD
postgres=# CREATE TABLE t1(a int, b int);
CREATE TABLE
postgres=#
CREATE OR REPLACE FUNCTION public.f1()
RETURNS void
LANGUAGE plpgsql
AS $function$
DECLARE r record;
BEGIN
FOR r IN SELECT * FROM t1
LOOP
RAISE NOTICE '%', r.c; -- there is bug - table t1 missing "c" column
END LOOP;
END;
$function$;
CREATE FUNCTION
postgres=# select f1(); -- execution doesn't find a bug due to empty table t1
f1
────
(1 row)
postgres=# \x
Expanded display is on.
postgres=# select * from plpgsql_check_function_tb('f1()');
─[ RECORD 1 ]───────────────────────────
functionid │ f1
lineno │ 6
statement │ RAISE
sqlstate │ 42703
message │ record "r" has no field "c"
detail │ [null]
hint │ [null]
level │ error
position │ 0
query │ [null]
postgres=# \sf+ f1
CREATE OR REPLACE FUNCTION public.f1()
RETURNS void
LANGUAGE plpgsql
1 AS $function$
2 DECLARE r record;
3 BEGIN
4 FOR r IN SELECT * FROM t1
5 LOOP
6 RAISE NOTICE '%', r.c; -- there is bug - table t1 missing "c" column
7 END LOOP;
8 END;
9 $function$
Function plpgsql_check_function() has three possible formats: text, json or xml
select * from plpgsql_check_function('f1()', fatal_errors := false);
plpgsql_check_function
------------------------------------------------------------------------
error:42703:4:SQL statement:column "c" of relation "t1" does not exist
Query: update t1 set c = 30
-- ^
error:42P01:7:RAISE:missing FROM-clause entry for table "r"
Query: SELECT r.c
-- ^
error:42601:7:RAISE:too few parameters specified for RAISE
(7 rows)
postgres=# select * from plpgsql_check_function('fx()', format:='xml');
plpgsql_check_function
────────────────────────────────────────────────────────────────
<Function oid="16400"> ↵
<Issue> ↵
<Level>error</level> ↵
<Sqlstate>42P01</Sqlstate> ↵
<Message>relation "foo111" does not exist</Message> ↵
<Stmt lineno="3">RETURN</Stmt> ↵
<Query position="23">SELECT (select a from foo111)</Query>↵
</Issue> ↵
</Function>
(1 row)
You can set level of warnings via function's parameters:
'fx()'::regprocedure
or 16799::regprocedure
. Possible alternative is using a name only, when function's name is unique - like 'fx'
. When the name is not unique or the function doesn't exists it raises a error.relid DEFAULT 0
- oid of relation assigned with trigger function. It is necessary for check of any trigger function.
fatal_errors boolean DEFAULT true
- stop on first error
other_warnings boolean DEFAULT true
- show warnings like different attributes number in assignmenet on left and right side, variable overlaps function's parameter, unused variables, unwanted casting, ..
extra_warnings boolean DEFAULT true
- show warnings like missing RETURN
, shadowed variables, dead code, never read (unused) function's parameter, unmodified variables, modified auto variables, ..
performance_warnings boolean DEFAULT false
- performance related warnings like declared type with type modificator, casting, implicit casts in where clause (can be reason why index is not used), ..
security_warnings boolean DEFAULT false
- security related checks like SQL injection vulnerability detection
anyelementtype regtype DEFAULT 'int'
- a real type used instead anyelement type
anyenumtype regtype DEFAULT '-'
- a real type used instead anyenum type
anyrangetype regtype DEFAULT 'int4range'
- a real type used instead anyrange type
anycompatibletype DEFAULT 'int'
- a real type used instead anycompatible type
anycompatiblerangetype DEFAULT 'int4range'
- a real type used instead anycompatible range type
without_warnings DEFAULT false
- disable all warnings
all_warnings DEFAULT false
- enable all warnings
newtable DEFAULT NULL
, oldtable DEFAULT NULL
- the names of NEW or OLD transitive tables. These parameters are required when transitive tables are used.
When you want to check any trigger, you have to enter a relation that will be used together with trigger function
CREATE TABLE bar(a int, b int);
postgres=# \sf+ foo_trg
CREATE OR REPLACE FUNCTION public.foo_trg()
RETURNS trigger
LANGUAGE plpgsql
1 AS $function$
2 BEGIN
3 NEW.c := NEW.a + NEW.b;
4 RETURN NEW;
5 END;
6 $function$
Missing relation specification
postgres=# select * from plpgsql_check_function('foo_trg()');
ERROR: missing trigger relation
HINT: Trigger relation oid must be valid
Correct trigger checking (with specified relation)
postgres=# select * from plpgsql_check_function('foo_trg()', 'bar');
plpgsql_check_function
--------------------------------------------------------
error:42703:3:assignment:record "new" has no field "c"
(1 row)
For triggers with transitive tables you can set a oldtable
or newtable
parameters:
create or replace function footab_trig_func()
returns trigger as $$
declare x int;
begin
if false then
-- should be ok;
select count(*) from newtab into x;
-- should fail;
select count(*) from newtab where d = 10 into x;
end if;
return null;
end;
$$ language plpgsql;
select * from plpgsql_check_function('footab_trig_func','footab', newtable := 'newtab');
You can use the plpgsql_check_function for mass check functions and mass check triggers. Please, test following queries:
-- check all nontrigger plpgsql functions
SELECT p.oid, p.proname, plpgsql_check_function(p.oid)
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p ON pronamespace = n.oid
JOIN pg_catalog.pg_language l ON p.prolang = l.oid
WHERE l.lanname = 'plpgsql' AND p.prorettype <> 2279;
or
SELECT p.proname, tgrelid::regclass, cf.*
FROM pg_proc p
JOIN pg_trigger t ON t.tgfoid = p.oid
JOIN pg_language l ON p.prolang = l.oid
JOIN pg_namespace n ON p.pronamespace = n.oid,
LATERAL plpgsql_check_function(p.oid, t.tgrelid) cf
WHERE n.nspname = 'public' and l.lanname = 'plpgsql'
or
-- check all plpgsql functions (functions or trigger functions with defined triggers)
SELECT
(pcf).functionid::regprocedure, (pcf).lineno, (pcf).statement,
(pcf).sqlstate, (pcf).message, (pcf).detail, (pcf).hint, (pcf).level,
(pcf)."position", (pcf).query, (pcf).context
FROM
(
SELECT
plpgsql_check_function_tb(pg_proc.oid, COALESCE(pg_trigger.tgrelid, 0)) AS pcf
FROM pg_proc
LEFT JOIN pg_trigger
ON (pg_trigger.tgfoid = pg_proc.oid)
WHERE
prolang = (SELECT lang.oid FROM pg_language lang WHERE lang.lanname = 'plpgsql') AND
pronamespace <> (SELECT nsp.oid FROM pg_namespace nsp WHERE nsp.nspname = 'pg_catalog') AND
-- ignore unused triggers
(pg_proc.prorettype <> (SELECT typ.oid FROM pg_type typ WHERE typ.typname = 'trigger') OR
pg_trigger.tgfoid IS NOT NULL)
OFFSET 0
) ss
ORDER BY (pcf).functionid::regprocedure::text, (pcf).lineno
Passive mode
Functions should be checked on start - plpgsql_check module must be loaded.
plpgsql_check.mode = [ disabled | by_function | fresh_start | every_start ]
plpgsql_check.fatal_errors = [ yes | no ]
plpgsql_check.show_nonperformance_warnings = false
plpgsql_check.show_performance_warnings = false
Default mode is by_function, that means that the enhanced check is done only in active mode - by plpgsql_check_function. fresh_start
means cold start.
You can enable passive mode by
load 'plpgsql'; -- 1.1 and higher doesn't need it
load 'plpgsql_check';
set plpgsql_check.mode = 'every_start';
SELECT fx(10); -- run functions - function is checked before runtime starts it
Limits
plpgsql_check should find almost all errors on really static code. When developer use some PLpgSQL's dynamic features like dynamic SQL or record data type, then false positives are possible. These should be rare - in well written code - and then the affected function should be redesigned or plpgsql_check should be disabled for this function.
CREATE OR REPLACE FUNCTION f1()
RETURNS void AS $$
DECLARE r record;
BEGIN
FOR r IN EXECUTE 'SELECT * FROM t1'
LOOP
RAISE NOTICE '%', r.c;
END LOOP;
END;
$$ LANGUAGE plpgsql SET plpgsql.enable_check TO false;
A usage of plpgsql_check adds a small overhead (in enabled passive mode) and you should use it only in develop or preprod environments.
This module doesn't check queries that are assembled in runtime. It is not possible to identify results of dynamic queries - so plpgsql_check cannot to set correct type to record variables and cannot to check a dependent SQLs and expressions.
When type of record's variable is not know, you can assign it explicitly with pragma type
:
DECLARE r record;
BEGIN
EXECUTE format('SELECT * FROM %I', _tablename) INTO r;
PERFORM plpgsql_check_pragma('type: r (id int, processed bool)');
IF NOT r.processed THEN
...
Attention: The SQL injection check can detect only some SQL injection vulnerabilities. This tool cannot be used for security audit! Some issues should not be detected. This check can raise false alarms too - probably when variable is sanitized by other command or when value is of some compose type.
plpgsql_check should not to detect structure of referenced cursors. A reference on cursor in PLpgSQL is implemented as name of global cursor. In check time, the name is not known (not in all possibilities), and global cursor doesn't exist. It is significant break for any static analyse. PLpgSQL cannot to set correct type for record variables and cannot to check a dependent SQLs and expressions. A solution is same like dynamic SQL. Don't use record variable as target when you use refcursor type or disable plpgsql_check for these functions.
CREATE OR REPLACE FUNCTION foo(refcur_var refcursor)
RETURNS void AS $$
DECLARE
rec_var record;
BEGIN
FETCH refcur_var INTO rec_var; -- this is STOP for plpgsql_check
RAISE NOTICE '%', rec_var; -- record rec_var is not assigned yet error
In this case a record type should not be used (use known rowtype instead):
CREATE OR REPLACE FUNCTION foo(refcur_var refcursor)
RETURNS void AS $$
DECLARE
rec_var some_rowtype;
BEGIN
FETCH refcur_var INTO rec_var;
RAISE NOTICE '%', rec_var;
plpgsql_check cannot verify queries over temporary tables that are created in plpgsql's function runtime. For this use case it is necessary to create a fake temp table or disable plpgsql_check for this function.
In reality temp tables are stored in own (per user) schema with higher priority than persistent tables. So you can do (with following trick safetly):
CREATE OR REPLACE FUNCTION public.disable_dml()
RETURNS trigger
LANGUAGE plpgsql AS $function$
BEGIN
RAISE EXCEPTION SQLSTATE '42P01'
USING message = format('this instance of %I table doesn''t allow any DML operation', TG_TABLE_NAME),
hint = format('you should to run "CREATE TEMP TABLE %1$I(LIKE %1$I INCLUDING ALL);" statement',
TG_TABLE_NAME);
RETURN NULL;
END;
$function$;
CREATE TABLE foo(a int, b int); -- doesn't hold data ever
CREATE TRIGGER foo_disable_dml
BEFORE INSERT OR UPDATE OR DELETE ON foo
EXECUTE PROCEDURE disable_dml();
postgres=# INSERT INTO foo VALUES(10,20);
ERROR: this instance of foo table doesn't allow any DML operation
HINT: you should to run "CREATE TEMP TABLE foo(LIKE foo INCLUDING ALL);" statement
postgres=#
CREATE TABLE
postgres=# INSERT INTO foo VALUES(10,20);
INSERT 0 1
This trick emulates GLOBAL TEMP tables partially and it allows a statical validation. Other possibility is using a [template foreign data wrapper] (https://github.com/okbob/template_fdw)
You can use pragma table
and create ephemeral table:
BEGIN
CREATE TEMP TABLE xxx(a int);
PERFORM plpgsql_check_pragma('table: xxx(a int)');
INSERT INTO xxx VALUES(10);
Dependency list
A function plpgsql_show_dependency_tb can show all functions, operators and relations used inside processed function:
postgres=# select * from plpgsql_show_dependency_tb('testfunc(int,float)');
┌──────────┬───────┬────────┬─────────┬────────────────────────────┐
│ type │ oid │ schema │ name │ params │
╞══════════╪═══════╪════════╪═════════╪════════════════════════════╡
│ FUNCTION │ 36008 │ public │ myfunc1 │ (integer,double precision) │
│ FUNCTION │ 35999 │ public │ myfunc2 │ (integer,double precision) │
│ OPERATOR │ 36007 │ public │ ** │ (integer,integer) │
│ RELATION │ 36005 │ public │ myview │ │
│ RELATION │ 36002 │ public │ mytable │ │
└──────────┴───────┴────────┴─────────┴────────────────────────────┘
(4 rows)
Profiler
The plpgsql_check contains simple profiler of plpgsql functions and procedures. It can work with/without a access to shared memory. It depends on shared_preload_libraries
config. When plpgsql_check was initialized by shared_preload_libraries
, then it can allocate shared memory, and function's profiles are stored there. When plpgsql_check cannot to allocate shared momory, the profile is stored in session memory.
Due dependencies, shared_preload_libraries
should to contains plpgsql
first
postgres=# show shared_preload_libraries ;
┌──────────────────────────┐
│ shared_preload_libraries │
╞══════════════════════════╡
│ plpgsql,plpgsql_check │
└──────────────────────────┘
(1 row)
The profiler is active when GUC plpgsql_check.profiler
is on. The profiler doesn't require shared memory, but if there are not shared memory, then the profile is limmitted just to active session.
When plpgsql_check is initialized by shared_preload_libraries
, another GUC is available to configure the amount of shared memory used by the profiler: plpgsql_check.profiler_max_shared_chunks
. This defines the maximum number of statements chunk that can be stored in shared memory. For each plpgsql function (or procedure), the whole content is split into chunks of 30 statements. If needed, multiple chunks can be used to store the whole content of a single function. A single chunk is 1704 bytes. The default value for this GUC is 15000, which should be enough for big projects containing hundred of thousands of statements in plpgsql, and will consume about 24MB of memory. If your project doesn't require that much number of chunks, you can set this parameter to a smaller number in order to decrease the memory usage. The minimum value is 50 (which should consume about 83kB of memory), and the maximum value is 100000 (which should consume about 163MB of memory). Changing this parameter requires a PostgreSQL restart.
The profiler will also retrieve the query identifier for each instruction that contains an expression or optimizable statement. Note that this requires pg_stat_statements, or another similar third-party extension), to be installed. There are some limitations to the query identifier retrieval:
Attention: A update of shared profiles can decrease performance on servers under higher load.
The profile can be displayed by function plpgsql_profiler_function_tb
:
postgres=# select lineno, avg_time, source from plpgsql_profiler_function_tb('fx(int)');
┌────────┬──────────┬───────────────────────────────────────────────────────────────────┐
│ lineno │ avg_time │ source │
╞════════╪══════════╪═══════════════════════════════════════════════════════════════════╡
│ 1 │ │ │
│ 2 │ │ declare result int = 0; │
│ 3 │ 0.075 │ begin │
│ 4 │ 0.202 │ for i in 1..$1 loop │
│ 5 │ 0.005 │ select result + i into result; select result + i into result; │
│ 6 │ │ end loop; │
│ 7 │ 0 │ return result; │
│ 8 │ │ end; │
└────────┴──────────┴───────────────────────────────────────────────────────────────────┘
(9 rows)
The profile per statements (not per line) can be displayed by function plpgsql_profiler_function_statements_tb:
CREATE OR REPLACE FUNCTION public.fx1(a integer)
RETURNS integer
LANGUAGE plpgsql
1 AS $function$
2 begin
3 if a > 10 then
4 raise notice 'ahoj';
5 return -1;
6 else
7 raise notice 'nazdar';
8 return 1;
9 end if;
10 end;
11 $function$
postgres=# select stmtid, parent_stmtid, parent_note, lineno, exec_stmts, stmtname
from plpgsql_profiler_function_statements_tb('fx1');
┌────────┬───────────────┬─────────────┬────────┬────────────┬─────────────────┐
│ stmtid │ parent_stmtid │ parent_note │ lineno │ exec_stmts │ stmtname │
╞════════╪═══════════════╪═════════════╪════════╪════════════╪═════════════════╡
│ 0 │ ∅ │ ∅ │ 2 │ 0 │ statement block │
│ 1 │ 0 │ body │ 3 │ 0 │ IF │
│ 2 │ 1 │ then body │ 4 │ 0 │ RAISE │
│ 3 │ 1 │ then body │ 5 │ 0 │ RETURN │
│ 4 │ 1 │ else body │ 7 │ 0 │ RAISE │
│ 5 │ 1 │ else body │ 8 │ 0 │ RETURN │
└────────┴───────────────┴─────────────┴────────┴────────────┴─────────────────┘
(6 rows)
All stored profiles can be displayed by calling function plpgsql_profiler_functions_all
:
postgres=# select * from plpgsql_profiler_functions_all();
┌───────────────────────┬────────────┬────────────┬──────────┬─────────────┬──────────┬──────────┐
│ funcoid │ exec_count │ total_time │ avg_time │ stddev_time │ min_time │ max_time │
╞═══════════════════════╪════════════╪════════════╪══════════╪═════════════╪══════════╪══════════╡
│ fxx(double precision) │ 1 │ 0.01 │ 0.01 │ 0.00 │ 0.01 │ 0.01 │
└───────────────────────┴────────────┴────────────┴──────────┴─────────────┴──────────┴──────────┘
(1 row)
There are two functions for cleaning stored profiles: plpgsql_profiler_reset_all()
and plpgsql_profiler_reset(regprocedure)
.
plpgsql_check provides two functions:
plpgsql_coverage_statements(name)
plpgsql_coverage_branches(name)
There is another very good PLpgSQL profiler - https://bitbucket.org/openscg/plprofiler
My extension is designed to be simple for use and practical. Nothing more or less.
plprofiler is more complex. It build call graphs and from this graph it can creates flame graph of execution times.
Both extensions can be used together with buildin PostgreSQL's feature - tracking functions.
set track_functions to 'pl';
...
select * from pg_stat_user_functions;
Tracer
plpgsql_check provides a tracing possibility - in this mode you can see notices on start or end functions (terse and default verbosity) and start or end statements (verbose verbosity). For default and verbose verbosity the content of function arguments is displayed. The content of related variables are displayed when verbosity is verbose.
postgres=# do $$ begin perform fx(10,null, 'now', e'stěhule'); end; $$;
NOTICE: #0 ->> start of inline_code_block (Oid=0)
NOTICE: #2 ->> start of function fx(integer,integer,date,text) (Oid=16405)
NOTICE: #2 call by inline_code_block line 1 at PERFORM
NOTICE: #2 "a" => '10', "b" => null, "c" => '2020-08-03', "d" => 'stěhule'
NOTICE: #4 ->> start of function fx(integer) (Oid=16404)
NOTICE: #4 call by fx(integer,integer,date,text) line 1 at PERFORM
NOTICE: #4 "a" => '10'
NOTICE: #4 <<- end of function fx (elapsed time=0.098 ms)
NOTICE: #2 <<- end of function fx (elapsed time=0.399 ms)
NOTICE: #0 <<- end of block (elapsed time=0.754 ms)
The number after #
is a execution frame counter (this number is related to deep of error context stack). It allows to pair start end and of function.
Tracing is enabled by setting plpgsql_check.tracer
to on
. Attention - enabling this behaviour has significant negative impact on performance (unlike the profiler). You can set a level for output used by tracer plpgsql_check.tracer_errlevel
(default is notice
). The output content is limited by length specified by plpgsql_check.tracer_variable_max_length
configuration variable.
In terse verbose mode the output is reduced:
postgres=# set plpgsql_check.tracer_verbosity TO terse;
SET
postgres=# do $$ begin perform fx(10,null, 'now', e'stěhule'); end; $$;
NOTICE: #0 start of inline code block (oid=0)
NOTICE: #2 start of fx (oid=16405)
NOTICE: #4 start of fx (oid=16404)
NOTICE: #4 end of fx
NOTICE: #2 end of fx
NOTICE: #0 end of inline code block
In verbose mode the output is extended about statement details:
postgres=# do $$ begin perform fx(10,null, 'now', e'stěhule'); end; $$;
NOTICE: #0 ->> start of block inline_code_block (oid=0)
NOTICE: #0.1 1 --> start of PERFORM
NOTICE: #2 ->> start of function fx(integer,integer,date,text) (oid=16405)
NOTICE: #2 call by inline_code_block line 1 at PERFORM
NOTICE: #2 "a" => '10', "b" => null, "c" => '2020-08-04', "d" => 'stěhule'
NOTICE: #2.1 1 --> start of PERFORM
NOTICE: #2.1 "a" => '10'
NOTICE: #4 ->> start of function fx(integer) (oid=16404)
NOTICE: #4 call by fx(integer,integer,date,text) line 1 at PERFORM
NOTICE: #4 "a" => '10'
NOTICE: #4.1 6 --> start of assignment
NOTICE: #4.1 "a" => '10', "b" => '20'
NOTICE: #4.1 <-- end of assignment (elapsed time=0.076 ms)
NOTICE: #4.1 "res" => '130'
NOTICE: #4.2 7 --> start of RETURN
NOTICE: #4.2 "res" => '130'
NOTICE: #4.2 <-- end of RETURN (elapsed time=0.054 ms)
NOTICE: #4 <<- end of function fx (elapsed time=0.373 ms)
NOTICE: #2.1 <-- end of PERFORM (elapsed time=0.589 ms)
NOTICE: #2 <<- end of function fx (elapsed time=0.727 ms)
NOTICE: #0.1 <-- end of PERFORM (elapsed time=1.147 ms)
NOTICE: #0 <<- end of block (elapsed time=1.286 ms)
Special feature of tracer is tracing of ASSERT
statement when plpgsql_check.trace_assert
is on
. When plpgsql_check.trace_assert_verbosity
is DEFAULT
, then all function's or procedure's variables are displayed when assert expression is false. When this configuration is VERBOSE
then all variables from all plpgsql frames are displayed. This behaviour is independent on plpgsql.check_asserts
value. It can be used, although the assertions are disabled in plpgsql runtime.
postgres=# set plpgsql_check.tracer to off;
postgres=# set plpgsql_check.trace_assert_verbosity TO verbose;
postgres=# do $$ begin perform fx(10,null, 'now', e'stěhule'); end; $$;
NOTICE: #4 PLpgSQL assert expression (false) on line 12 of fx(integer) is false
NOTICE: "a" => '10', "res" => null, "b" => '20'
NOTICE: #2 PL/pgSQL function fx(integer,integer,date,text) line 1 at PERFORM
NOTICE: "a" => '10', "b" => null, "c" => '2020-08-05', "d" => 'stěhule'
NOTICE: #0 PL/pgSQL function inline_code_block line 1 at PERFORM
ERROR: assertion failed
CONTEXT: PL/pgSQL function fx(integer) line 12 at ASSERT
SQL statement "SELECT fx(a)"
PL/pgSQL function fx(integer,integer,date,text) line 1 at PERFORM
SQL statement "SELECT fx(10,null, 'now', e'stěhule')"
PL/pgSQL function inline_code_block line 1 at PERFORM
postgres=# set plpgsql.check_asserts to off;
SET
postgres=# do $$ begin perform fx(10,null, 'now', e'stěhule'); end; $$;
NOTICE: #4 PLpgSQL assert expression (false) on line 12 of fx(integer) is false
NOTICE: "a" => '10', "res" => null, "b" => '20'
NOTICE: #2 PL/pgSQL function fx(integer,integer,date,text) line 1 at PERFORM
NOTICE: "a" => '10', "b" => null, "c" => '2020-08-05', "d" => 'stěhule'
NOTICE: #0 PL/pgSQL function inline_code_block line 1 at PERFORM
DO
Tracer prints content of variables or function arguments. For security definer function, this content can hold security sensitive data. This is reason why tracer is disabled by default and should be enabled only with super user rights plpgsql_check.enable_tracer
.
Pragma
You can configure plpgsql_check behave inside checked function with "pragma" function. This is a analogy of PL/SQL or ADA language of PRAGMA feature. PLpgSQL doesn't support PRAGMA, but plpgsql_check detects function named plpgsql_check_pragma
and get options from parameters of this function. These plpgsql_check options are valid to end of group of statements.
CREATE OR REPLACE FUNCTION test()
RETURNS void AS $$
BEGIN
...
-- for following statements disable check
PERFORM plpgsql_check_pragma('disable:check');
...
-- enable check again
PERFORM plpgsql_check_pragma('enable:check');
...
END;
$$ LANGUAGE plpgsql;
The function plpgsql_check_pragma
is immutable function that returns one. It is defined by plpgsql_check
extension. You can declare alternative plpgsql_check_pragma
function like:
CREATE OR REPLACE FUNCTION plpgsql_check_pragma(VARIADIC args[])
RETURNS int AS $$
SELECT 1
$$ LANGUAGE sql IMMUTABLE;
Using pragma function in declaration part of top block sets options on function level too.
CREATE OR REPLACE FUNCTION test()
RETURNS void AS $$
DECLARE
aux int := plpgsql_check_pragma('disable:extra_warnings');
...
Shorter syntax for pragma is supported too:
CREATE OR REPLACE FUNCTION test()
RETURNS void AS $$
DECLARE r record;
BEGIN
PERFORM 'PRAGMA:TYPE:r (a int, b int)';
PERFORM 'PRAGMA:TABLE: x (like pg_class)';
...
echo:str
- print string (for testing)
status:check
,status:tracer
, status:other_warnings
, status:performance_warnings
, status:extra_warnings
,status:security_warnings
enable:check
,enable:tracer
, enable:other_warnings
, enable:performance_warnings
, enable:extra_warnings
,enable:security_warnings
disable:check
,disable:tracer
, disable:other_warnings
, disable:performance_warnings
, disable:extra_warnings
,disable:security_warnings
type:varname typename
or type:varname (fieldname type, ...)
- set type to variable of record type
table: name (column_name type, ...)
or table: name (like tablename)
- create ephereal table
Pragmas enable:tracer
and disable:tracer
are active for Postgres 12 and higher
Compilation
You need a development environment for PostgreSQL extensions:
make clean
make install
result:
[pavel@localhost plpgsql_check]$ make USE_PGXS=1 clean
rm -f plpgsql_check.so libplpgsql_check.a libplpgsql_check.pc
rm -f plpgsql_check.o
rm -rf results/ regression.diffs regression.out tmp_check/ log/
[pavel@localhost plpgsql_check]$ make USE_PGXS=1 all
clang -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I/usr/local/pgsql/lib/pgxs/src/makefiles/../../src/pl/plpgsql/src -I. -I./ -I/usr/local/pgsql/include/server -I/usr/local/pgsql/include/internal -D_GNU_SOURCE -c -o plpgsql_check.o plpgsql_check.c
clang -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I/usr/local/pgsql/lib/pgxs/src/makefiles/../../src/pl/plpgsql/src -shared -o plpgsql_check.so plpgsql_check.o -L/usr/local/pgsql/lib -Wl,--as-needed -Wl,-rpath,'/usr/local/pgsql/lib',--enable-new-dtags
[pavel@localhost plpgsql_check]$ su root
Password: *******
[root@localhost plpgsql_check]# make USE_PGXS=1 install
/usr/bin/mkdir -p '/usr/local/pgsql/lib'
/usr/bin/mkdir -p '/usr/local/pgsql/share/extension'
/usr/bin/mkdir -p '/usr/local/pgsql/share/extension'
/usr/bin/install -c -m 755 plpgsql_check.so '/usr/local/pgsql/lib/plpgsql_check.so'
/usr/bin/install -c -m 644 plpgsql_check.control '/usr/local/pgsql/share/extension/'
/usr/bin/install -c -m 644 plpgsql_check--0.9.sql '/usr/local/pgsql/share/extension/'
[root@localhost plpgsql_check]# exit
[pavel@localhost plpgsql_check]$ make USE_PGXS=1 installcheck
/usr/local/pgsql/lib/pgxs/src/makefiles/../../src/test/regress/pg_regress --inputdir=./ --psqldir='/usr/local/pgsql/bin' --dbname=pl_regression --load-language=plpgsql --dbname=contrib_regression plpgsql_check_passive plpgsql_check_active plpgsql_check_active-9.5
(using postmaster on Unix socket, default port)
============== dropping database "contrib_regression" ==============
DROP DATABASE
============== creating database "contrib_regression" ==============
CREATE DATABASE
ALTER DATABASE
============== installing plpgsql ==============
CREATE LANGUAGE
============== running regression test queries ==============
test plpgsql_check_passive ... ok
test plpgsql_check_active ... ok
test plpgsql_check_active-9.5 ... ok
=====================
All 3 tests passed.
=====================
Sometimes successful compilation can require libicu-dev package (PostgreSQL 10 and higher - when pg was compiled with ICU support)
sudo apt install libicu-dev
You can check precompiled dll libraries http://okbob.blogspot.cz/2015/02/plpgsqlcheck-is-available-for-microsoft.html
or compile by self:
plpgsql_check.dll
to PostgreSQL\14\lib
plpgsql_check.control
and plpgsql_check--2.1.sql
to PostgreSQL\14\share\extension
Compilation against PostgreSQL 10 requires libICU!
Licence
Copyright (c) Pavel Stehule (pavel.stehule@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Note
If you like it, send a postcard to address
Pavel Stehule
Skalice 12
256 01 Benesov u Prahy
Czech Republic
I invite any questions, comments, bug reports, patches on mail address pavel.stehule@gmail.com
Author: okbob
Source Code: https://github.com/okbob/plpgsql_check
License: View license