forked from zpl-c/librg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrg.h
2643 lines (2084 loc) Β· 87 KB
/
librg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* librg - a library for building simple and elegant cross-platform mmo client-server solutions.
*
* Usage:
* #define LIBRG_IMPLEMENTATION exactly in ONE source file right BEFORE including the library, like:
*
* #define LIBRG_IMPLEMENTATION
* #include <librg.h>
*
* Credits:
* Vladyslav Hrytsenko (GitHub: inlife)
* Dominik Madarasz (GitHub: zaklaus)
*
* Dependencies:
* zpl.h
* zpl_math.h
* enet.h
*
* For the demo:
* sdl2.h
*
* Version History:
* 3.1.0
* - Removed zpl_cull and zpl_event dependencies
* - added librg_network_kick()
* - saving current librg_address_t to ctx->network
* - refactor to proper disconnection code
* - exclude local client entity from LIBRG_CONNECTION_DISCONNECT
* - moved options and some few other things to the implementation part
* - fixed issue with replacing entity control
* - fixed issue with add control queuing beign sent before create entity packet
*
* 3.0.7 - Fix for entity query dublication for player entities
* 3.0.5 - Patched librg_callback_cb arg value
* 3.0.4 - Fixed Android and iOS support
* 3.0.3 - Small fixes
* 3.0.2 - Dependency updates
* 3.0.1 - minor api patch
* 3.0.0 - contexts, major api changes, fried potato, other stuff
*
* 2.2.3 - fixed mem leak on net event
* 2.2.2 - Fixed client issue with librg_message_send_instream_except
* 2.2.1 - Fixed cpp issues with librg_data_t pointers
* 2.2.0 - Inner message system rafactor
* 2.1.0 - Inner bitstream refactors, with slight interface changes
* 2.0.2 - C++ and MSVC related fixes
* 2.0.0 - Initial C version rewrite
*
* Things TODO:
* v3.2.0?
* - DEBUG packet size validation (FEATURE)
* - refactoring librg_table_t (FEATURE)
* - remove entity ignore for target entity that was disconnected/deleted (BUG)
* - possibly adding stream_range to the query, to make it bi-sided (FEATURE)
* - tree/space node flattening (FEATURE)
*
* Copyright 2017 Vladyslav Hrytsenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIBRG_INCLUDE_H
#define LIBRG_INCLUDE_H
#define LIBRG_VERSION_MAJOR 3
#define LIBRG_VERSION_MINOR 0
#define LIBRG_VERSION_PATCH 0
#define LIBRG_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch))
#define LIBRG_VERSION_GET_MAJOR(version) (((version)>>16)&0xFF)
#define LIBRG_VERSION_GET_MINOR(version) (((version)>>8)&0xFF)
#define LIBRG_VERSION_GET_PATCH(version) ((version)&0xFF)
#define LIBRG_VERSION LIBRG_VERSION_CREATE(LIBRG_VERSION_MAJOR, LIBRG_VERSION_MINOR, LIBRG_VERSION_PATCH)
// disable asserts for release build
#if !defined(LIBRG_DEBUG)
# define ZPL_ASSERT_MSG(cond, msg, ...)
#endif
#ifndef LIBRG_CUSTOM_INCLUDES
# ifdef ZPL_SYSTEM_WINDOWS
# define _WINSOCK_DEPRECATED_NO_WARNINGS
# endif
# if defined(ZPL_SYSTEM_UNIX) && !defined(HAS_SOCKLEN_T)
# define HAS_SOCKLEN_T
# endif
# ifdef LIBRG_IMPLEMENTATION
# define ZPL_IMPLEMENTATION
# define ZPLM_IMPLEMENTATION
# define ENET_IMPLEMENTATION
# endif
# include "zpl.h"
# include "zpl_math.h"
# include "enet.h"
#endif
#ifdef LIBRG_SHARED
# if defined(_WIN32)
# define LIBRG_API ZPL_EXTERN __declspec(dllexport)
# else
# define LIBRG_API ZPL_EXTERN __attribute__((visibility("default")))
# endif
#else
# ifndef LIBRG_API
# define LIBRG_API ZPL_DEF
# endif
#endif
#ifndef LIBRG_DATA_GROW_FORMULA
# define LIBRG_DATA_GROW_FORMULA(x) (2*(x) + 16)
#endif
#define LIBRG_MESSAGE_ID u16
#define LIBRG_DATA_STREAMS_AMOUNT 4
#define librg_global zpl_global
#define librg_inline zpl_inline
#define librg_internal zpl_internal
#define librg_assert ZPL_ASSERT
#define librg_assert_msg ZPL_ASSERT_MSG
#define librg_lambda(name) name
#if !defined(librg_log)
# define librg_log zpl_printf
#endif
#if defined(LIBRG_DEBUG)
# define librg_dbg(fmt, ...) librg_log(fmt, ##__VA_ARGS__)
#else
# define librg_dbg(fmt, ...)
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
*
* OPTIONS
*
*/
typedef enum {
LIBRG_PLATFORM_ID,
LIBRG_PLATFORM_PROTOCOL,
LIBRG_PLATFORM_BUILD,
LIBRG_DEFAULT_CLIENT_TYPE,
LIBRG_DEFAULT_STREAM_RANGE,
LIBRG_DEFAULT_DATA_SIZE,
LIBRG_NETWORK_CAPACITY,
LIBRG_NETWORK_CHANNELS,
LIBRG_NETWORK_PRIMARY_CHANNEL,
LIBRG_NETWORK_SECONDARY_CHANNEL,
LIBRG_NETWORK_MESSAGE_CHANNEL,
LIBRG_MAX_ENTITIES_PER_BRANCH,
LIBRG_MAX_THREADS_PER_UPDATE,
LIBRG_OPTIONS_SIZE,
} librg_option_e;
enum {
LIBRG_MODE_SERVER,
LIBRG_MODE_CLIENT,
};
enum {
LIBRG_SPACE_2D = 2,
LIBRG_SPACE_3D = 3,
};
/**
* Default built-in events
* define your events likes this:
* enum {
* MY_NEW_EVENT_1 = LIBRG_LAST_EVENT,
* MY_NEW_EVENT_2,
* MY_NEW_EVENT_3,
* };
*/
enum {
LIBRG_CONNECTION_INIT,
LIBRG_CONNECTION_REQUEST,
LIBRG_CONNECTION_REFUSE,
LIBRG_CONNECTION_ACCEPT,
LIBRG_CONNECTION_DISCONNECT,
LIBRG_ENTITY_CREATE,
LIBRG_ENTITY_UPDATE,
LIBRG_ENTITY_REMOVE,
LIBRG_CLIENT_STREAMER_ADD,
LIBRG_CLIENT_STREAMER_REMOVE,
LIBRG_CLIENT_STREAMER_UPDATE,
LIBRG_EVENT_LAST,
};
/**
* Table for various entity bool storages
*/
ZPL_TABLE_DECLARE(extern, librg_table_t, librg_table_, u32);
/**
*
* CORE
*
*/
struct librg_ctx_t;
typedef ENetPeer librg_peer_t;
typedef ENetHost librg_host_t;
typedef ENetPacket librg_packet_t;
#define librg_entity_id u32
typedef struct {
usize capacity;
usize read_pos;
usize write_pos;
void *rawptr;
zpl_allocator_t allocator;
} librg_data_t;
/**
* Simple host address
* used to configure network on start
*/
typedef struct {
i32 port;
char *host;
} librg_address_t;
/**
* Entity flags
*/
enum {
LIBRG_ENTITY_NONE = 0, /* general flag, all destroyed/non-created entities have it */
LIBRG_ENTITY_ALIVE = (1 << 0), /* general flag, all created entities have it */
LIBRG_ENTITY_CLIENT = (1 << 1), /* flag describing entities created for client peer */
LIBRG_ENTITY_IGNORING = (1 << 2), /* flag showing that entity has ignore overrides */
LIBRG_ENTITY_QUERIED = (1 << 3), /* flag showing that entity has a cached culler query */
LIBRG_ENTITY_CONTROLLED = (1 << 4), /* flag showing if the entity is controlled(streamed) by some peer */
LIBRG_ENTITY_UNUSED = (1 << 5), /* flag showing whether the entity's space is unused */
};
/**
* Entity blob
*/
typedef struct librg_entity_t {
u32 id;
u32 type;
u64 flags;
zplm_vec3_t position;
f32 stream_range;
void *user_data;
struct librg_space_t *stream_branch;
librg_table_t ignored;
librg_table_t last_snapshot;
librg_peer_t *client_peer;
librg_peer_t *control_peer;
zpl_array_t(librg_entity_id) last_query;
} librg_entity_t;
/**
* World space structure
*/
typedef struct librg_space_t {
zpl_allocator_t allocator;
u32 max_nodes;
isize dimensions;
zplm_aabb3_t boundary;
zplm_vec3_t min_bounds;
b32 use_min_bounds;
zpl_array_t(struct librg_space_node_t) nodes;
zpl_array_t(usize) free_nodes;
zpl_array_t(struct librg_space_t) spaces;
} librg_space_t;
typedef struct librg_space_node_t {
librg_entity_t *blob;
b32 unused;
} librg_space_node_t;
/**
* Message structure
* created inside network handler
* and injected to each incoming message
*/
typedef struct {
struct librg_ctx_t *ctx;
librg_data_t *data;
librg_peer_t *peer;
librg_packet_t *packet;
void *user_data; /* optional: user information */
} librg_message_t;
typedef enum {
LIBRG_EVENT_NONE = 0, /* default empty user-created event */
LIBRG_EVENT_REJECTED = (1 << 0), /* whether or not this event was rejected */
LIBRG_EVENT_REJECTABLE = (1 << 1), /* can this event be rejected by user */
LIBRG_EVENT_REMOTE = (1 << 2), /* event was based on network message */
LIBRG_EVENT_LOCAL = (1 << 3), /* event was created locally */
} librg_event_flag_e;
/**
* Event structure
* usually created in various
*/
typedef struct {
struct librg_ctx_t *ctx; /* librg context where event has been called */
librg_data_t *data; /* optional: data is used for built-in events */
librg_peer_t *peer; /* optional: peer is used for built-in events */
librg_entity_t *entity; /* optional: entity is used for built-in events */
u64 flags; /* flags for that event */
void *user_data; /* optional: user information */
} librg_event_t;
/**
* Callbacks
*/
typedef void (librg_entity_cb)(struct librg_ctx_t *ctx, librg_entity_t *entity);
typedef void (librg_message_cb)(librg_message_t *msg);
typedef void (librg_event_cb)(librg_event_t *event);
typedef zpl_array_t(librg_event_cb *) librg_event_block;
ZPL_TABLE_DECLARE(static, librg_event_pool, librg_event_pool_, librg_event_block);
/**
* Multithreading stuff
*/
enum {
librg_thread_idle,
librg_thread_work,
librg_thread_exit,
};
typedef struct {
usize id;
usize offset;
usize count;
struct librg_ctx_t *ctx;
} librg_update_worker_si_t;
/**
* Context + config struct
*/
typedef struct librg_ctx_t {
// core
u16 mode;
u16 tick_delay;
// configuration
u16 max_connections;
u32 max_entities;
zplm_vec3_t world_size;
zplm_vec3_t min_branch_size;
f32 last_update;
void *user_data;
struct {
librg_peer_t *peer;
librg_host_t *host;
librg_table_t connected_peers;
librg_address_t last_address;
} network;
struct {
u32 count;
u32 cursor;
librg_table_t ignored;
struct librg_entity_t *list;
zpl_array_t(librg_entity_id) remove_queue;
zpl_array_t(librg_message_t *) add_control_queue;
} entity;
union {
struct {
librg_data_t stream_input;
librg_data_t stream_output;
librg_data_t stream_upd_reliable;
librg_data_t stream_upd_unreliable;
};
librg_data_t streams[LIBRG_DATA_STREAMS_AMOUNT];
};
#ifdef LIBRG_MULTITHREADED
struct {
zpl_atomic32_t signal;
zpl_atomic32_t work_count;
zpl_thread_t *update_workers;
zpl_mutex_t *send_lock;
} threading;
#endif
zpl_buffer_t(librg_message_cb *) messages;
zpl_allocator_t allocator;
zpl_timer_pool timers;
librg_event_pool events;
librg_space_t world;
} librg_ctx_t;
/**
* Set global cross-instance option for librg
*/
LIBRG_API void librg_option_set(librg_option_e option, u32 value);
/**
* Get global cross-instance option for librg
*/
LIBRG_API u32 librg_option_get(librg_option_e option);
/**
* Main initialization method
* MUST BE called in the begging of your application
*/
LIBRG_API void librg_init(librg_ctx_t *ctx);
/**
* Main tick method
* MUST BE called in your loop
* preferably w/o delays
*/
LIBRG_API void librg_tick(librg_ctx_t *ctx);
/**
* Should be called at the end of
* execution of the program
*/
LIBRG_API void librg_free(librg_ctx_t *ctx);
/**
* Frees a pointer allocated by library
* usually used in bindings.
*/
LIBRG_API void librg_release(void *ptr);
/**
* Frees an array allocated by library
* usually used in bindings.
*/
LIBRG_API void librg_release_array(void *ptr);
/**
* Is librg instance is running
* in the server mode
*/
LIBRG_API b32 librg_is_server(librg_ctx_t *ctx);
/**
* Is librg instance is running
* in the client mode
*/
LIBRG_API b32 librg_is_client(librg_ctx_t *ctx);
/**
*
* ENTITIES
*
*/
/**
* Create entity and return handle
*/
LIBRG_API librg_entity_t *librg_entity_create(librg_ctx_t *ctx, u32 type);
/**
* Check if provided entity is a valid entity
*/
LIBRG_API b32 librg_entity_valid(librg_ctx_t *ctx, librg_entity_id id);
/**
* Return entity type
*/
LIBRG_API u32 librg_entity_type(librg_ctx_t *ctx, librg_entity_id entity_id);
/**
* Return entity blob pointer
*/
LIBRG_API librg_entity_t *librg_entity_fetch(librg_ctx_t *ctx, librg_entity_id entity_id);
/**
* Destroy entity
*/
LIBRG_API void librg_entity_destroy(librg_ctx_t *ctx, librg_entity_id entity_id);
/**
* Query for entities that are in stream zone
* for current entity, and are visible to this entity
*/
LIBRG_API usize librg_entity_query(librg_ctx_t *ctx, librg_entity_id entity_id, librg_entity_id **result);
/**
* Get entity by peer
*/
LIBRG_API librg_entity_t *librg_entity_find(librg_ctx_t *ctx, librg_peer_t *peer);
/**
* Set particular entity visible or invisible
* for other entities in stream zone
*/
LIBRG_API void librg_entity_visibility_set(librg_ctx_t *ctx, librg_entity_id entity_id, b32 state);
/**
* Set particular entity visible or invisible
* for other particular entity
*/
LIBRG_API void librg_entity_visibility_set_for(librg_ctx_t *ctx, librg_entity_id entity_id, librg_entity_id target, b32 state);
/**
* Get particular entity visible or invisible
* for other entities in stream zone
*/
LIBRG_API b32 librg_entity_visibility_get(librg_ctx_t *ctx, librg_entity_id entity_id);
/**
* Get particular entity visible or invisible
* for other particular entity
*/
LIBRG_API b32 librg_entity_visibility_get_for(librg_ctx_t *ctx, librg_entity_id entity_id, librg_entity_id target);
/**
* Set some entity as client streamable
* Which means, that client will become responsive for sending
* updates about this entity
*
* And this entity wont be sent to the client, until he stops being the streamer
*
* Setting other client as streamer, will remove previous streamer from entity
*/
LIBRG_API void librg_entity_control_set(librg_ctx_t *ctx, librg_entity_id entity_id, librg_peer_t *peer);
/**
* Get controller of the entity
*/
LIBRG_API librg_peer_t *librg_entity_control_get(librg_ctx_t *ctx, librg_entity_id entity_id);
/**
* Remove some entity from stream ownership of the client
*/
LIBRG_API void librg_entity_control_remove(librg_ctx_t *ctx, librg_entity_id entity_id);
/**
* Iterate over all the entities with a flag
*/
LIBRG_API void librg_entity_iterate(librg_ctx_t *ctx, u64 flags, librg_entity_cb callback);
#define librg_entity_iteratex(ctx, cflags, cname, code) do { \
for (int _ent = 0, _valid = 0; _ent < ctx->max_entities && _valid < ctx->entity.count; ++_ent) { \
if ((ctx->entity.list[_ent].flags & (LIBRG_ENTITY_ALIVE | cflags)) == (LIBRG_ENTITY_ALIVE | cflags)) { \
_valid++; librg_entity_id cname = _ent; code; \
} \
} \
} while (0);
/**
*
* EVENTS
*
*/
/**
* Used to attach event handler
* You can bind as many event handlers onto
* single event, as you want
*
* In the callback you will need to cast event
* to type of structure that you've triggered this event with
*
* @param id usually you define event ids inside enum
* @param callback
* @return index of added event, can be used to remove particular event handler
*/
LIBRG_API u64 librg_event_add(librg_ctx_t *ctx, u64 id, librg_event_cb callback);
/**
* Used to trigger execution of all attached
* event handlers for particlar event
*
* You can provide pointer to any data, which will be
* passed inside the event callback
*
* @param id usually you define event ids inside enum
* @param event pointer onto data or NULL
*/
LIBRG_API void librg_event_trigger(librg_ctx_t *ctx, u64 id, librg_event_t *event);
/**
* Used to remove particular callback from
* event chain, so it wont be called ever again
*
* @param id usually you define event ids inside enum
* @param index returned by librg_event_add
*/
LIBRG_API void librg_event_remove(librg_ctx_t *ctx, u64 id, u64 index);
/**
* Used to reject some event from triggering from
* inside of executing callback
*/
LIBRG_API void librg_event_reject(librg_event_t *event);
/**
* Used to check if some event can be rejected
*/
LIBRG_API b32 librg_event_rejectable(librg_event_t *event);
/**
* Checks if current event was not rejected
* inside any of the callbacks
*/
LIBRG_API b32 librg_event_succeeded(librg_event_t *event);
/**
*
* BINARY DATA (BITSTREAM)
*
*/
/**
* Initialize new bitstream with default mem size
*/
LIBRG_API void librg_data_init(librg_data_t *data);
/**
* Initialize new bitstream with custom mem size
*/
LIBRG_API void librg_data_init_size(librg_data_t *data, usize size);
/**
* Free initialized bitstream
*/
LIBRG_API void librg_data_free(librg_data_t *data);
/**
* Reset initialized bitstream
* NOTE: doesnt remove any data, just resets read and write pos to 0
*/
LIBRG_API void librg_data_reset(librg_data_t *data);
/**
* Increase size of bitstream
*/
LIBRG_API void librg_data_grow(librg_data_t *data, usize min_size);
/**
* Methods for getting various parameters of bitstream
*/
LIBRG_API usize librg_data_capacity(librg_data_t *data);
LIBRG_API usize librg_data_get_rpos(librg_data_t *data);
LIBRG_API usize librg_data_get_wpos(librg_data_t *data);
LIBRG_API void librg_data_set_rpos(librg_data_t *data, usize position);
LIBRG_API void librg_data_set_wpos(librg_data_t *data, usize position);
/**
* Read and write methods for custom sized data
*/
LIBRG_API void librg_data_rptr(librg_data_t *data, void *ptr, usize size);
LIBRG_API void librg_data_wptr(librg_data_t *data, void *ptr, usize size);
/**
* Read and write methods for custom sized data
* at particular position in memory
*/
LIBRG_API void librg_data_rptr_at(librg_data_t *data, void *ptr, usize size, isize position);
LIBRG_API void librg_data_wptr_at(librg_data_t *data, void *ptr, usize size, isize position);
/**
* General one-line methods for reading/writing different types
*/
#define LIBRG_GEN_DATA_READWRITE(TYPE) \
LIBRG_API TYPE ZPL_JOIN2(librg_data_r,TYPE)(librg_data_t *data); \
LIBRG_API void ZPL_JOIN2(librg_data_w,TYPE)(librg_data_t *data, TYPE value); \
LIBRG_API TYPE ZPL_JOIN3(librg_data_r,TYPE,_at)(librg_data_t *data, isize position); \
LIBRG_API void ZPL_JOIN3(librg_data_w,TYPE,_at)(librg_data_t *data, TYPE value, isize position); \
LIBRG_GEN_DATA_READWRITE( i8);
LIBRG_GEN_DATA_READWRITE( u8);
LIBRG_GEN_DATA_READWRITE(i16);
LIBRG_GEN_DATA_READWRITE(u16);
LIBRG_GEN_DATA_READWRITE(i32);
LIBRG_GEN_DATA_READWRITE(u32);
LIBRG_GEN_DATA_READWRITE(i64);
LIBRG_GEN_DATA_READWRITE(u64);
LIBRG_GEN_DATA_READWRITE(f32);
LIBRG_GEN_DATA_READWRITE(f64);
LIBRG_GEN_DATA_READWRITE( b8);
LIBRG_GEN_DATA_READWRITE(b16);
LIBRG_GEN_DATA_READWRITE(b32);
#undef LIBRG_GEN_DATA_READWRITE
/**
* Read/write methods for entity (aliases for u32)
*/
#define librg_data_went ZPL_JOIN2(librg_data_w, librg_entity_id)
#define librg_data_rent ZPL_JOIN2(librg_data_r, librg_entity_id)
#define librg_data_wmid ZPL_JOIN2(librg_data_w, LIBRG_MESSAGE_ID)
#define librg_data_rmid ZPL_JOIN2(librg_data_r, LIBRG_MESSAGE_ID)
/**
*
* NETWORK
*
*/
/**
* Check are we connected
*/
LIBRG_API b32 librg_is_connected(librg_ctx_t *ctx);
/**
* Starts network connection
* Requires you to provide .port (if running as server)
* or both .port and .host (if running as client)
*
* For server mode - starts server
* For client mode - starts client, and connects to provided host & port
*/
LIBRG_API void librg_network_start(librg_ctx_t *ctx, librg_address_t address);
/**
* Disconnects (if connected), stops network
* and releases resources
*/
LIBRG_API void librg_network_stop(librg_ctx_t *ctx);
/**
* Forces disconnection for provided peer
* @param ctx
* @param peer
*/
LIBRG_API void librg_network_kick(librg_ctx_t *ctx, librg_peer_t *peer);
/**
* Can be used to add handler
* to a particular message id
*/
LIBRG_API void librg_network_add(librg_ctx_t *ctx, LIBRG_MESSAGE_ID id, librg_message_cb callback);
/**
* Can be used to remove a handler
* from particular message id
*/
LIBRG_API void librg_network_remove(librg_ctx_t *ctx, LIBRG_MESSAGE_ID id);
/**
* Part of message API
* Takes in initialized void of size pointer with written packet id
* and sends data to all connected peers ( or to server if its client )
*/
LIBRG_API void librg_message_send_all(librg_ctx_t *ctx, LIBRG_MESSAGE_ID id, void *data, usize size);
/**
* Part of message API
* Applies all from previous mehod
* But data will be sent only to particular provided peer
*/
LIBRG_API void librg_message_send_to(librg_ctx_t *ctx, LIBRG_MESSAGE_ID id, librg_peer_t *peer, void *data, usize size);
/**
* Part of message API
* Applies all from previous mehod
* But data will be sent to all except provided peer
*/
LIBRG_API void librg_message_send_except(librg_ctx_t *ctx, LIBRG_MESSAGE_ID id, librg_peer_t *peer, void *data, usize size);
/**
* Part of message API
* Applies all from previous mehod
* Data will be sent only to entities, which are inside streamzone
* for provided entity
*/
LIBRG_API void librg_message_send_instream(librg_ctx_t *ctx, LIBRG_MESSAGE_ID id, librg_entity_id entity_id, void *data, usize size);
/**
* Part of message API
* Applies all from previous mehod
* Data will be sent only to entities, which are inside streamzone
* for provided entity except peer
*/
LIBRG_API void librg_message_send_instream_except(librg_ctx_t *ctx, LIBRG_MESSAGE_ID id, librg_entity_id entity_id, librg_peer_t *peer, void *data, usize size);
/**
*
* EXTENSIONS
*
*/
#define librg__send_internal(CTX, ID, NAME, CALLBACK_CODE, SEND_CODE) \
librg_data_t NAME; \
librg_data_init(&NAME); \
CALLBACK_CODE; SEND_CODE; \
librg_data_free(&NAME);
#define librg_send_all(CTX, ID, NAME, CALLBACK_CODE) do { \
librg__send_internal(CTX, ID, NAME, CALLBACK_CODE, { \
librg_message_send_all(CTX, ID, NAME.rawptr, librg_data_get_wpos(&NAME)); \
}); \
} while(0);
#define librg_send_to(CTX, ID, PEER, NAME, CALLBACK_CODE) do { \
librg__send_internal(CTX, ID, NAME, CALLBACK_CODE, { \
librg_message_send_to(CTX, ID, PEER, NAME.rawptr, librg_data_get_wpos(&NAME)); \
}); \
} while(0);
#define librg_send_except(CTX, ID, PEER, NAME, CALLBACK_CODE) do { \
librg__send_internal(CTX, ID, NAME, CALLBACK_CODE, { \
librg_message_send_except(CTX, ID, PEER, NAME.rawptr, librg_data_get_wpos(&NAME)); \
}); \
} while(0);
#define librg_send_instream(CTX, ID, ENTITY, NAME, CALLBACK_CODE) do { \
librg__send_internal(CTX, ID, NAME, CALLBACK_CODE, { \
librg_message_send_instream(CTX, ID, ENTITY, NAME.rawptr, librg_data_get_wpos(&NAME)); \
}); \
} while(0);
#define librg_send_instream_except(CTX, ID, ENTITY, PEER, NAME, CALLBACK_CODE) do { \
librg__send_internal(CTX, ID, NAME, CALLBACK_CODE, { \
librg_message_send_instream(CTX, ID, ENTITY, PEER, NAME.rawptr, librg_data_get_wpos(&NAME)); \
}); \
} while(0);
#define librg_send librg_send_all
#ifdef __cplusplus
}
#endif
#if defined(LIBRG_IMPLEMENTATION) && !defined(LIBRG_IMPLEMENTATION_DONE)
#define LIBRG_IMPLEMENTATION_DONE
#ifdef __cplusplus
extern "C" {
#endif
/**
* Global option storage
*/
librg_global u32 librg_options[LIBRG_OPTIONS_SIZE] = {
/*LIBRG_PLATFORM_ID*/ 1,
/*LIBRG_PLATFORM_PROTOCOL*/ 1,
/*LIBRG_PLATFORM_BUILD*/ 1,
/*LIBRG_DEFAULT_CLIENT_TYPE*/ 0,
/*LIBRG_DEFAULT_STREAM_RANGE*/ 250,
/*LIBRG_DEFAULT_DATA_SIZE*/ 1024,
/*LIBRG_NETWORK_CAPACITY*/ 2048,
/*LIBRG_NETWORK_CHANNELS*/ 4,
/*LIBRG_NETWORK_PRIMARY_CHANNEL*/ 1,
/*LIBRG_NETWORK_SECONDARY_CHANNEL*/ 2,
/*LIBRG_NETWORK_MESSAGE_CHANNEL*/ 3,
/*LIBRG_MAX_ENTITIES_PER_BRANCH*/ 4,
/*LIBRG_MAX_THREADS_PER_UPDATE*/ 0, /* MT is disabled by default = 0 */
};
ZPL_TABLE_DEFINE(librg_event_pool, librg_event_pool_, librg_event_block);
ZPL_TABLE_DEFINE(librg_table_t, librg_table_, u32);
librg_inline void librg_option_set(librg_option_e option, u32 value) {
librg_options[option] = value;
}
librg_inline u32 librg_option_get(librg_option_e option) {
return librg_options[option];
}
/**
*
* EVENTS
*
*/
librg_inline u64 librg_event_add(librg_ctx_t *ctx, u64 id, librg_event_cb callback) {
librg_assert(ctx);
librg_event_block *block = librg_event_pool_get(&ctx->events, id);
if (!block) {
librg_event_block arr;
zpl_array_init(arr, ctx->allocator);
librg_event_pool_set(&ctx->events, id, arr);
block = librg_event_pool_get(&ctx->events, id);
}
u64 offset = zpl_array_count(block);
zpl_array_append(*block, callback);
return offset;
}
void librg_event_trigger(librg_ctx_t *ctx, u64 id, librg_event_t *event) {
librg_assert(event); event->ctx = ctx;
librg_event_block *block = librg_event_pool_get(&ctx->events, id);
if (!block) return;
for (isize i = 0; i < zpl_array_count(*block) && !(event->flags & LIBRG_EVENT_REJECTED); ++i) {
(*block)[i](event);
}
}
librg_inline void librg_event_remove(librg_ctx_t *ctx, u64 id, u64 index) {
librg_assert(ctx);
librg_event_block *block = librg_event_pool_get(&ctx->events, id);
if (block) {
zpl_array_remove_at(*block, (isize)index);
}
}
librg_inline void librg_event_reject(librg_event_t *event) {
librg_assert(event);
event->flags = (librg_event_flag_e)(event->flags | LIBRG_EVENT_REJECTED);
}
librg_inline b32 librg_event_rejectable(librg_event_t *event) {
librg_assert(event);
return (event->flags & LIBRG_EVENT_REJECTABLE);
}
librg_inline b32 librg_event_succeeded(librg_event_t *event) {
librg_assert(event);
return !(event->flags & LIBRG_EVENT_REJECTED);
}
/**
*
* DATA STREAM (BITSREAM)
*
*/
librg_inline void librg_data_init_size(librg_data_t *data, usize size) {
librg_assert(data);
data->capacity = size;
data->read_pos = 0;
data->write_pos = 0;
data->allocator = zpl_heap_allocator();
data->rawptr = zpl_alloc(data->allocator, size);
}
librg_inline void librg_data_init(librg_data_t *data) {
librg_assert(data);
librg_data_init_size(data, librg_option_get(LIBRG_DEFAULT_DATA_SIZE));
}