protocol-quic 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/ext/source/Ruby/Protocol/QUIC/Address.cpp +122 -0
  4. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Address.hpp +5 -5
  5. data/ext/source/Ruby/Protocol/QUIC/Client.cpp +161 -0
  6. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Client.hpp +3 -3
  7. data/ext/source/Ruby/Protocol/QUIC/Configuration.cpp +74 -0
  8. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Configuration.hpp +3 -3
  9. data/ext/source/Ruby/Protocol/QUIC/Connection.cpp +50 -0
  10. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Connection.hpp +4 -4
  11. data/ext/source/Ruby/Protocol/QUIC/Dispatcher.cpp +194 -0
  12. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Dispatcher.hpp +3 -3
  13. data/ext/source/Ruby/Protocol/QUIC/PacketHeader.cpp +59 -0
  14. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/PacketHeader.hpp +4 -4
  15. data/ext/source/Ruby/Protocol/QUIC/Reference.hpp +35 -0
  16. data/ext/source/Ruby/Protocol/QUIC/Server.cpp +169 -0
  17. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Server.hpp +3 -3
  18. data/ext/source/Ruby/Protocol/QUIC/Socket.cpp +103 -0
  19. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Socket.hpp +4 -4
  20. data/ext/source/Ruby/Protocol/QUIC/Stream.cpp +100 -0
  21. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Stream.hpp +4 -4
  22. data/ext/source/Ruby/Protocol/QUIC/TLS/ClientContext.cpp +61 -0
  23. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/TLS/ClientContext.hpp +3 -3
  24. data/ext/source/Ruby/Protocol/QUIC/TLS/Context.cpp +116 -0
  25. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/TLS/Context.hpp +3 -3
  26. data/ext/source/Ruby/Protocol/QUIC/TLS/ServerContext.cpp +61 -0
  27. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/TLS/ServerContext.hpp +3 -3
  28. data/ext/source/Ruby/Protocol/QUIC.cpp +36 -0
  29. data/ext/source/{Protocol → Ruby/Protocol}/QUIC.hpp +1 -1
  30. data/ext/teapot.rb +1 -1
  31. data/lib/protocol/quic/version.rb +1 -1
  32. data/lib/protocol/quic.rb +1 -1
  33. data.tar.gz.sig +0 -0
  34. metadata +28 -28
  35. metadata.gz.sig +0 -0
  36. data/ext/source/Protocol/QUIC/Address.cpp +0 -122
  37. data/ext/source/Protocol/QUIC/Client.cpp +0 -157
  38. data/ext/source/Protocol/QUIC/Configuration.cpp +0 -74
  39. data/ext/source/Protocol/QUIC/Connection.cpp +0 -50
  40. data/ext/source/Protocol/QUIC/Dispatcher.cpp +0 -190
  41. data/ext/source/Protocol/QUIC/PacketHeader.cpp +0 -59
  42. data/ext/source/Protocol/QUIC/Reference.hpp +0 -31
  43. data/ext/source/Protocol/QUIC/Server.cpp +0 -165
  44. data/ext/source/Protocol/QUIC/Socket.cpp +0 -103
  45. data/ext/source/Protocol/QUIC/Stream.cpp +0 -96
  46. data/ext/source/Protocol/QUIC/TLS/ClientContext.cpp +0 -61
  47. data/ext/source/Protocol/QUIC/TLS/Context.cpp +0 -116
  48. data/ext/source/Protocol/QUIC/TLS/ServerContext.cpp +0 -61
  49. data/ext/source/Protocol/QUIC.cpp +0 -36
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b501288cde394d030bb3d0ae6e035ee4feae69424eaec58e5dd3e94f1c85bb9
4
- data.tar.gz: 1be70108b32ee82f49f5c14e04f7db4e2205bfb2ba9344501663cf69a37f5824
3
+ metadata.gz: d4d70f75e99f4f081118f993d24a094f79481be7f33cfc6a7924f6c2f9a97e75
4
+ data.tar.gz: 583ea74199b9b2d6e4acf3bc1657aeadd61453a50bef14317c1d8d761f925e62
5
5
  SHA512:
6
- metadata.gz: 5db41a0051307b315912faf24e7a171ff8b4dee2a54181d95e35304e8d5441a63712284a847c4a89e22fb0daecc66543860e79128f2a3230bb3acf24b25cbe80
7
- data.tar.gz: 989f6ecb3e69779a9d2d88c1b3b2aab2eab212380e8094637cdba58d7b2e92c4e1cc16413ed07c6d09ee646b5b4fad4bda6a2403cf104c31600b9bbab464e6ac
6
+ metadata.gz: 51835b21c8b83076ab333a1ef061c7d504a02fd7a5cb83729fb7b21fcd367c8aaa7406330452325e5aaf99e9ffe4974fa3f88cc6405681173772099a5083a1bc
7
+ data.tar.gz: 26b668bad3fb3276f1ce4a4ae55e1546098481cbc3b684b6a480178a859c472f1228b89810f81c37a238bc38baccc1682c6346ccc3ea6074793f3a5e887143a8
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,122 @@
1
+ //
2
+ // Address.cpp
3
+ // This file is part of the "Protocol::QUIC" project and released under the MIT License.
4
+ //
5
+ // Created by Samuel Williams on 27/4/2023.
6
+ // Copyright, 2023, by Samuel Williams. All rights reserved.
7
+ //
8
+
9
+ #include "Address.hpp"
10
+ #include "ruby/backward/cxxanyargs.hpp"
11
+ #include "ruby/internal/intern/object.h"
12
+
13
+ VALUE Ruby_Protocol_QUIC_Address = Qnil;
14
+
15
+ static void Ruby_Protocol_QUIC_Address_free(void *data) {
16
+ if (data) {
17
+ delete reinterpret_cast<::Protocol::QUIC::Address *>(data);
18
+ }
19
+ }
20
+
21
+ static size_t Ruby_Protocol_QUIC_Address_size(const void *data) {
22
+ return sizeof(::Protocol::QUIC::Address);
23
+ }
24
+
25
+ const rb_data_type_t Ruby_Protocol_QUIC_Address_type = {
26
+ .wrap_struct_name = "Protocol::QUIC::Address",
27
+ .function = {
28
+ .dmark = NULL,
29
+ .dfree = Ruby_Protocol_QUIC_Address_free,
30
+ .dsize = Ruby_Protocol_QUIC_Address_size,
31
+ },
32
+ .data = NULL,
33
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
34
+ };
35
+
36
+ ::Protocol::QUIC::Address * Ruby_Protocol_QUIC_Address_get(VALUE self)
37
+ {
38
+ ::Protocol::QUIC::Address *address;
39
+
40
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Address, &Ruby_Protocol_QUIC_Address_type, address);
41
+
42
+ return address;
43
+ }
44
+
45
+ VALUE Ruby_Protocol_QUIC_Address_allocate(VALUE klass) {
46
+ return TypedData_Wrap_Struct(klass, &Ruby_Protocol_QUIC_Address_type, NULL);
47
+ }
48
+
49
+ static VALUE Ruby_Protocol_QUIC_Address_initialize(VALUE self) {
50
+ DATA_PTR(self) = new ::Protocol::QUIC::Address();
51
+
52
+ return self;
53
+ }
54
+
55
+ static VALUE Ruby_Protocol_QUIC_Address_inspect(VALUE self) {
56
+ auto address = Ruby_Protocol_QUIC_Address_get(self);
57
+ auto string = address->to_string();
58
+
59
+ VALUE class_name = rb_inspect(rb_class_of(self));
60
+
61
+ return rb_sprintf("<%" PRIsVALUE ":%p '%s'>", class_name, (void*)self, string.c_str());
62
+ }
63
+
64
+ static VALUE Ruby_Protocol_QUIC_Address_family(VALUE self) {
65
+ ::Protocol::QUIC::Address *address = Ruby_Protocol_QUIC_Address_get(self);
66
+
67
+ return RB_INT2NUM(address->family());
68
+ }
69
+
70
+ static VALUE Ruby_Protocol_QUIC_Address_data(VALUE self) {
71
+ ::Protocol::QUIC::Address *address = Ruby_Protocol_QUIC_Address_get(self);
72
+
73
+ return rb_str_new((const char *)&address->data.sa, address->length);
74
+ }
75
+
76
+ VALUE Ruby_Protocol_QUIC_Address_wrap(VALUE klass, const ::Protocol::QUIC::Address & address)
77
+ {
78
+ VALUE self = Ruby_Protocol_QUIC_Address_allocate(klass);
79
+ Ruby_Protocol_QUIC_Address_initialize(self);
80
+
81
+ ::Protocol::QUIC::Address *data = Ruby_Protocol_QUIC_Address_get(self);
82
+
83
+ *data = address;
84
+
85
+ return self;
86
+ }
87
+
88
+ VALUE Ruby_Protocol_QUIC_Address_s_resolve(VALUE klass, VALUE host, VALUE service, VALUE family, VALUE type, VALUE flags)
89
+ {
90
+ StringValue(host);
91
+ StringValue(service);
92
+
93
+ auto addresses = ::Protocol::QUIC::Address::resolve(
94
+ std::string_view(RSTRING_PTR(host), RSTRING_LEN(host)),
95
+ std::string_view(RSTRING_PTR(service), RSTRING_LEN(service)),
96
+ RB_NUM2INT(family),
97
+ RB_NUM2INT(type),
98
+ RB_NUM2INT(flags)
99
+ );
100
+
101
+ VALUE result = rb_ary_new_capa(addresses.size());
102
+
103
+ for (auto & address : addresses) {
104
+ rb_ary_push(result, Ruby_Protocol_QUIC_Address_wrap(klass, address));
105
+ }
106
+
107
+ return result;
108
+ }
109
+
110
+ void Init_Ruby_Protocol_QUIC_Address(VALUE Protocol_QUIC) {
111
+ Ruby_Protocol_QUIC_Address =
112
+ rb_define_class_under(Protocol_QUIC, "Address", rb_cObject);
113
+
114
+ rb_define_singleton_method(Ruby_Protocol_QUIC_Address, "resolve", Ruby_Protocol_QUIC_Address_s_resolve, 5);
115
+
116
+ rb_define_alloc_func(Ruby_Protocol_QUIC_Address, Ruby_Protocol_QUIC_Address_allocate);
117
+ rb_define_method(Ruby_Protocol_QUIC_Address, "initialize", Ruby_Protocol_QUIC_Address_initialize, 0);
118
+ rb_define_method(Ruby_Protocol_QUIC_Address, "inspect", Ruby_Protocol_QUIC_Address_inspect, 0);
119
+
120
+ rb_define_method(Ruby_Protocol_QUIC_Address, "family", Ruby_Protocol_QUIC_Address_family, 0);
121
+ rb_define_method(Ruby_Protocol_QUIC_Address, "data", Ruby_Protocol_QUIC_Address_data, 0);
122
+ }
@@ -16,14 +16,14 @@
16
16
  extern "C" {
17
17
  #endif
18
18
 
19
- extern VALUE Protocol_QUIC_Address;
19
+ extern VALUE Ruby_Protocol_QUIC_Address;
20
20
 
21
- void Init_Protocol_QUIC_Address(VALUE Protocol_QUIC);
21
+ void Init_Ruby_Protocol_QUIC_Address(VALUE Protocol_QUIC);
22
22
 
23
- Protocol::QUIC::Address * Protocol_QUIC_Address_get(VALUE self);
23
+ ::Protocol::QUIC::Address * Ruby_Protocol_QUIC_Address_get(VALUE self);
24
24
 
25
- VALUE Protocol_QUIC_Address_allocate(VALUE klass);
26
- VALUE Protocol_QUIC_Address_wrap(VALUE klass, const Protocol::QUIC::Address & address);
25
+ VALUE Ruby_Protocol_QUIC_Address_allocate(VALUE klass);
26
+ VALUE Ruby_Protocol_QUIC_Address_wrap(VALUE klass, const ::Protocol::QUIC::Address & address);
27
27
 
28
28
  #ifdef __cplusplus
29
29
  }
@@ -0,0 +1,161 @@
1
+
2
+ #include "Client.hpp"
3
+
4
+ #include "Configuration.hpp"
5
+ #include "TLS/ClientContext.hpp"
6
+ #include "Socket.hpp"
7
+ #include "Address.hpp"
8
+ #include "Stream.hpp"
9
+
10
+ #include <unordered_map>
11
+
12
+ VALUE Ruby_Protocol_QUIC_Client = Qnil;
13
+
14
+ namespace Ruby::Protocol::QUIC {
15
+
16
+ class Client : public ::Protocol::QUIC::Client {
17
+ public:
18
+ VALUE self;
19
+
20
+ private:
21
+ VALUE _configuration;
22
+ VALUE _tls_context;
23
+ VALUE _socket;
24
+ VALUE _remote_address;
25
+ std::unordered_map<::Protocol::QUIC::StreamID, VALUE> _ruby_streams;
26
+
27
+ public:
28
+ Client(VALUE self, VALUE configuration, VALUE tls_context, VALUE socket, VALUE remote_address, VALUE chosen_version) : ::Protocol::QUIC::Client(*Ruby_Protocol_QUIC_Configuration_get(configuration), *Ruby_Protocol_QUIC_TLS_ClientContext_get(tls_context), *Ruby_Protocol_QUIC_Socket_get(socket), *Ruby_Protocol_QUIC_Address_get(remote_address), RB_NUM2UINT(chosen_version)), self(self), _configuration(configuration), _tls_context(tls_context), _socket(socket), _remote_address(remote_address) {}
29
+ virtual ~Client() {}
30
+
31
+ void handshake_completed() override
32
+ {
33
+ rb_funcall(self, rb_intern("handshake_completed"), 0);
34
+ }
35
+
36
+ ::Protocol::QUIC::Stream * create_stream(::Protocol::QUIC::StreamID stream_id) override
37
+ {
38
+ VALUE stream = rb_funcall(self, rb_intern("create_stream"), 1, RB_LL2NUM(stream_id));
39
+ _ruby_streams[stream_id] = stream;
40
+
41
+ return Ruby_Protocol_QUIC_Stream_get(stream);
42
+ }
43
+
44
+ void disconnect() override
45
+ {
46
+ ::Protocol::QUIC::Client::disconnect();
47
+ _ruby_streams.clear();
48
+ }
49
+
50
+ void stream_close(::Protocol::QUIC::Stream * stream, std::int32_t flags, std::uint64_t error_code) override
51
+ {
52
+ auto stream_id = stream->stream_id();
53
+ ::Protocol::QUIC::Client::stream_close(stream, flags, error_code);
54
+ _ruby_streams.erase(stream_id);
55
+ }
56
+
57
+ void stream_reset(::Protocol::QUIC::Stream * stream, std::size_t final_size, std::uint64_t error_code) override
58
+ {
59
+ auto stream_id = stream->stream_id();
60
+ ::Protocol::QUIC::Client::stream_reset(stream, final_size, error_code);
61
+ _ruby_streams.erase(stream_id);
62
+ }
63
+
64
+ void mark() {
65
+ rb_gc_mark_movable(self);
66
+ rb_gc_mark_movable(_configuration);
67
+ rb_gc_mark_movable(_tls_context);
68
+ rb_gc_mark_movable(_socket);
69
+ rb_gc_mark_movable(_remote_address);
70
+
71
+ for (auto & [stream_id, ruby_stream] : _ruby_streams) {
72
+ rb_gc_mark_movable(ruby_stream);
73
+ }
74
+ }
75
+
76
+ void compact() {
77
+ self = rb_gc_location(self);
78
+ _configuration = rb_gc_location(_configuration);
79
+ _tls_context = rb_gc_location(_tls_context);
80
+ _socket = rb_gc_location(_socket);
81
+ _remote_address = rb_gc_location(_remote_address);
82
+
83
+ for (auto & [stream_id, ruby_stream] : _ruby_streams) {
84
+ ruby_stream = rb_gc_location(ruby_stream);
85
+ }
86
+ }
87
+ };
88
+
89
+ }
90
+
91
+ static void Ruby_Protocol_QUIC_Client_mark(void *data)
92
+ {
93
+ if (data) {
94
+ reinterpret_cast<Ruby::Protocol::QUIC::Client *>(data)->mark();
95
+ }
96
+ }
97
+
98
+ static void Ruby_Protocol_QUIC_Client_compact(void *data)
99
+ {
100
+ if (data) {
101
+ reinterpret_cast<Ruby::Protocol::QUIC::Client *>(data)->compact();
102
+ }
103
+ }
104
+
105
+ static void Ruby_Protocol_QUIC_Client_free(void *data)
106
+ {
107
+ if (data) {
108
+ delete reinterpret_cast<::Protocol::QUIC::Client *>(data);
109
+ }
110
+ }
111
+
112
+ static size_t Ruby_Protocol_QUIC_Client_size(const void *data) {
113
+ return sizeof(Ruby::Protocol::QUIC::Client);
114
+ }
115
+
116
+ static const rb_data_type_t Ruby_Protocol_QUIC_Client_type = {
117
+ .wrap_struct_name = "Protocol::QUIC::Client",
118
+ .function = {
119
+ .dmark = Ruby_Protocol_QUIC_Client_mark,
120
+ .dfree = Ruby_Protocol_QUIC_Client_free,
121
+ .dsize = Ruby_Protocol_QUIC_Client_size,
122
+ .dcompact = Ruby_Protocol_QUIC_Client_compact,
123
+ },
124
+ .data = NULL,
125
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
126
+ };
127
+
128
+ ::Protocol::QUIC::Client * Ruby_Protocol_QUIC_Client_get(VALUE self)
129
+ {
130
+ ::Protocol::QUIC::Client *configuration;
131
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Client, &Ruby_Protocol_QUIC_Client_type, configuration);
132
+ return configuration;
133
+ }
134
+
135
+ static VALUE Ruby_Protocol_QUIC_Client_allocate(VALUE klass) {
136
+ return TypedData_Wrap_Struct(klass, &Ruby_Protocol_QUIC_Client_type, NULL);
137
+ }
138
+
139
+ static VALUE Ruby_Protocol_QUIC_Client_initialize(VALUE self, VALUE configuration, VALUE tls_context, VALUE socket, VALUE remote_address, VALUE chosen_version) {
140
+ ::Protocol::QUIC::Client *client = new Ruby::Protocol::QUIC::Client(self, configuration, tls_context, socket, remote_address, chosen_version);
141
+ DATA_PTR(self) = client;
142
+ return self;
143
+ }
144
+
145
+ static VALUE Ruby_Protocol_QUIC_Client_connect(VALUE self) {
146
+ ::Protocol::QUIC::Client *client = Ruby_Protocol_QUIC_Client_get(self);
147
+
148
+ client->connect();
149
+
150
+ return Qnil;
151
+ }
152
+
153
+ void Init_Ruby_Protocol_QUIC_Client(VALUE Protocol_QUIC) {
154
+ Ruby_Protocol_QUIC_Client =
155
+ rb_define_class_under(Protocol_QUIC, "Client", rb_cObject);
156
+
157
+ rb_define_alloc_func(Ruby_Protocol_QUIC_Client, Ruby_Protocol_QUIC_Client_allocate);
158
+ rb_define_method(Ruby_Protocol_QUIC_Client, "initialize", Ruby_Protocol_QUIC_Client_initialize, 5);
159
+
160
+ rb_define_method(Ruby_Protocol_QUIC_Client, "connect", Ruby_Protocol_QUIC_Client_connect, 0);
161
+ }
@@ -16,11 +16,11 @@
16
16
  extern "C" {
17
17
  #endif
18
18
 
19
- extern VALUE Protocol_QUIC_Client;
19
+ extern VALUE Ruby_Protocol_QUIC_Client;
20
20
 
21
- void Init_Protocol_QUIC_Client(VALUE Protocol_QUIC);
21
+ void Init_Ruby_Protocol_QUIC_Client(VALUE Protocol_QUIC);
22
22
 
23
- Protocol::QUIC::Client * Protocol_QUIC_Client_get(VALUE self);
23
+ ::Protocol::QUIC::Client * Ruby_Protocol_QUIC_Client_get(VALUE self);
24
24
 
25
25
  #ifdef __cplusplus
26
26
  }
@@ -0,0 +1,74 @@
1
+
2
+ #include <ruby.h>
3
+
4
+ #include "Configuration.hpp"
5
+
6
+ #include <stdarg.h>
7
+ #include <stdio.h>
8
+
9
+ #include <ngtcp2/ngtcp2.h>
10
+
11
+ VALUE Ruby_Protocol_QUIC_Configuration = Qnil;
12
+
13
+ static void Ruby_Protocol_QUIC_Configuration_free(void *data)
14
+ {
15
+ if (data) {
16
+ delete reinterpret_cast<::Protocol::QUIC::Configuration *>(data);
17
+ }
18
+ }
19
+
20
+ static size_t Ruby_Protocol_QUIC_Configuration_size(const void *data) {
21
+ return sizeof(::Protocol::QUIC::Configuration);
22
+ }
23
+
24
+ static const rb_data_type_t Ruby_Protocol_QUIC_Configuration_type = {
25
+ .wrap_struct_name = "Protocol::QUIC::Configuration",
26
+ .function = {
27
+ .dmark = NULL,
28
+ .dfree = Ruby_Protocol_QUIC_Configuration_free,
29
+ .dsize = Ruby_Protocol_QUIC_Configuration_size,
30
+ },
31
+ .data = NULL,
32
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
33
+ };
34
+
35
+ static VALUE Ruby_Protocol_QUIC_Configuration_allocate(VALUE klass) {
36
+ auto configuration = new ::Protocol::QUIC::Configuration();
37
+
38
+ return TypedData_Wrap_Struct(klass, &Ruby_Protocol_QUIC_Configuration_type, configuration);
39
+ }
40
+
41
+ ::Protocol::QUIC::Configuration * Ruby_Protocol_QUIC_Configuration_get(VALUE self)
42
+ {
43
+ ::Protocol::QUIC::Configuration *configuration;
44
+
45
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Configuration, &Ruby_Protocol_QUIC_Configuration_type, configuration);
46
+
47
+ return configuration;
48
+ }
49
+
50
+ static VALUE Ruby_Protocol_QUIC_Configuration_initialize(VALUE self) {
51
+ ::Protocol::QUIC::Configuration *configuration;
52
+
53
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Configuration, &Ruby_Protocol_QUIC_Configuration_type, configuration);
54
+
55
+ return self;
56
+ }
57
+
58
+ static VALUE Ruby_Protocol_QUIC_Configuration_static_secret(VALUE self) {
59
+ ::Protocol::QUIC::Configuration *configuration;
60
+
61
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Configuration, &Ruby_Protocol_QUIC_Configuration_type, configuration);
62
+
63
+ return rb_str_new((const char *)configuration->static_secret.data(), configuration->static_secret.size());
64
+ }
65
+
66
+ void Init_Ruby_Protocol_QUIC_Configuration(VALUE Protocol_QUIC) {
67
+ Ruby_Protocol_QUIC_Configuration =
68
+ rb_define_class_under(Protocol_QUIC, "Configuration", rb_cObject);
69
+
70
+ rb_define_alloc_func(Ruby_Protocol_QUIC_Configuration, Ruby_Protocol_QUIC_Configuration_allocate);
71
+ rb_define_method(Ruby_Protocol_QUIC_Configuration, "initialize", Ruby_Protocol_QUIC_Configuration_initialize, 0);
72
+
73
+ rb_define_method(Ruby_Protocol_QUIC_Configuration, "static_secret", Ruby_Protocol_QUIC_Configuration_static_secret, 0);
74
+ }
@@ -16,11 +16,11 @@
16
16
  extern "C" {
17
17
  #endif
18
18
 
19
- extern VALUE Protocol_QUIC_Configuration;
19
+ extern VALUE Ruby_Protocol_QUIC_Configuration;
20
20
 
21
- void Init_Protocol_QUIC_Configuration(VALUE Protocol_QUIC);
21
+ void Init_Ruby_Protocol_QUIC_Configuration(VALUE Protocol_QUIC);
22
22
 
23
- Protocol::QUIC::Configuration * Protocol_QUIC_Configuration_get(VALUE self);
23
+ ::Protocol::QUIC::Configuration * Ruby_Protocol_QUIC_Configuration_get(VALUE self);
24
24
 
25
25
  #ifdef __cplusplus
26
26
  }
@@ -0,0 +1,50 @@
1
+
2
+ #include <ruby.h>
3
+
4
+ #include "Connection.hpp"
5
+ #include "ruby/internal/intern/vm.h"
6
+
7
+ #include <stdarg.h>
8
+ #include <stdio.h>
9
+
10
+ #include <ngtcp2/ngtcp2.h>
11
+
12
+ VALUE Ruby_Protocol_QUIC_Connection = Qnil;
13
+
14
+ static void Ruby_Protocol_QUIC_Connection_free(void *data)
15
+ {
16
+ if (data) {
17
+ delete reinterpret_cast<::Protocol::QUIC::Connection *>(data);
18
+ }
19
+ }
20
+
21
+ static size_t Ruby_Protocol_QUIC_Connection_size(const void *data) {
22
+ return sizeof(::Protocol::QUIC::Connection);
23
+ }
24
+
25
+ const rb_data_type_t Ruby_Protocol_QUIC_Connection_type = {
26
+ .wrap_struct_name = "Protocol::QUIC::Connection",
27
+ .function = {
28
+ .dmark = NULL,
29
+ .dfree = Ruby_Protocol_QUIC_Connection_free,
30
+ .dsize = Ruby_Protocol_QUIC_Connection_size,
31
+ },
32
+ .data = NULL,
33
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
34
+ };
35
+
36
+ ::Protocol::QUIC::Connection * Ruby_Protocol_QUIC_Connection_get(VALUE self)
37
+ {
38
+ ::Protocol::QUIC::Connection *connection;
39
+
40
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Connection, &Ruby_Protocol_QUIC_Connection_type, connection);
41
+
42
+ return connection;
43
+ }
44
+
45
+ void Init_Ruby_Protocol_QUIC_Connection(VALUE Protocol_QUIC) {
46
+ Ruby_Protocol_QUIC_Connection =
47
+ rb_define_class_under(Protocol_QUIC, "Connection", rb_cObject);
48
+
49
+ rb_undef_alloc_func(Ruby_Protocol_QUIC_Connection);
50
+ }
@@ -16,13 +16,13 @@
16
16
  extern "C" {
17
17
  #endif
18
18
 
19
- extern VALUE Protocol_QUIC_Connection;
19
+ extern VALUE Ruby_Protocol_QUIC_Connection;
20
20
 
21
- extern const rb_data_type_t Protocol_QUIC_Connection_type;
21
+ extern const rb_data_type_t Ruby_Protocol_QUIC_Connection_type;
22
22
 
23
- void Init_Protocol_QUIC_Connection(VALUE Protocol_QUIC);
23
+ void Init_Ruby_Protocol_QUIC_Connection(VALUE Protocol_QUIC);
24
24
 
25
- Protocol::QUIC::Connection * Protocol_QUIC_Connection_get(VALUE self);
25
+ ::Protocol::QUIC::Connection * Ruby_Protocol_QUIC_Connection_get(VALUE self);
26
26
 
27
27
  #ifdef __cplusplus
28
28
  }
@@ -0,0 +1,194 @@
1
+
2
+ #include "Dispatcher.hpp"
3
+
4
+ #include "Socket.hpp"
5
+ #include "Address.hpp"
6
+ #include "PacketHeader.hpp"
7
+
8
+ #include "Configuration.hpp"
9
+ #include "TLS/ServerContext.hpp"
10
+
11
+ #include "Server.hpp"
12
+
13
+ #include "Reference.hpp"
14
+
15
+ #include <unordered_map>
16
+
17
+ namespace Ruby::Protocol::QUIC {
18
+
19
+ class Dispatcher final : public ::Protocol::QUIC::Dispatcher {
20
+ public:
21
+ VALUE self;
22
+
23
+ private:
24
+ VALUE _ruby_configuration;
25
+ VALUE _ruby_tls_context;
26
+ std::unordered_map<::Protocol::QUIC::Server *, VALUE> _ruby_servers;
27
+ std::unordered_map<::Protocol::QUIC::Socket *, VALUE> _ruby_sockets;
28
+
29
+ public:
30
+ Dispatcher(VALUE self, VALUE configuration, VALUE tls_context) : ::Protocol::QUIC::Dispatcher(*Ruby_Protocol_QUIC_Configuration_get(configuration), *Ruby_Protocol_QUIC_TLS_ServerContext_get(tls_context)), self(self), _ruby_configuration(configuration), _ruby_tls_context(tls_context)
31
+ {
32
+ }
33
+
34
+ ~Dispatcher() {
35
+ }
36
+
37
+ VALUE ruby_configuration() noexcept {return _ruby_configuration;}
38
+ VALUE ruby_tls_context() noexcept {return _ruby_tls_context;}
39
+
40
+ ::Protocol::QUIC::Server * listen(VALUE ruby_socket)
41
+ {
42
+ auto socket = Ruby_Protocol_QUIC_Socket_get(ruby_socket);
43
+
44
+ _ruby_sockets[socket] = ruby_socket;
45
+
46
+ return ::Protocol::QUIC::Dispatcher::listen(*socket);
47
+ }
48
+
49
+ ::Protocol::QUIC::Server * create_server(::Protocol::QUIC::Socket &socket, const ::Protocol::QUIC::Address &address, const ngtcp2_pkt_hd &packet_header) override
50
+ {
51
+ auto iterator = _ruby_sockets.find(&socket);
52
+
53
+ if (iterator == _ruby_sockets.end()) {
54
+ rb_raise(rb_eRuntimeError, "Could not find Ruby socket wrapper for native socket.");
55
+ }
56
+
57
+ VALUE ruby_socket = iterator->second;
58
+ VALUE ruby_address = Ruby_Protocol_QUIC_Address_wrap(Ruby_Protocol_QUIC_Address, address);
59
+
60
+ VALUE ruby_packet_header = Ruby_Protocol_QUIC_PacketHeader_allocate(Ruby_Protocol_QUIC_PacketHeader);
61
+ ValueReference ruby_packet_header_reference(ruby_packet_header, packet_header);
62
+
63
+ VALUE server = rb_funcall(self, rb_intern("create_server"), 3, ruby_socket, ruby_address, ruby_packet_header);
64
+ auto native_server = Ruby_Protocol_QUIC_Server_get(server);
65
+
66
+ _ruby_servers[native_server] = server;
67
+
68
+ return native_server;
69
+ }
70
+
71
+ void remove(::Protocol::QUIC::Server * server) override
72
+ {
73
+ ::Protocol::QUIC::Dispatcher::remove(server);
74
+ _ruby_servers.erase(server);
75
+ }
76
+
77
+ void mark() {
78
+ rb_gc_mark_movable(self);
79
+ rb_gc_mark_movable(_ruby_configuration);
80
+ rb_gc_mark_movable(_ruby_tls_context);
81
+
82
+ for (auto & [server, ruby_server] : _ruby_servers) {
83
+ rb_gc_mark_movable(ruby_server);
84
+ }
85
+
86
+ for (auto & [socket, ruby_socket] : _ruby_sockets) {
87
+ rb_gc_mark_movable(ruby_socket);
88
+ }
89
+ }
90
+
91
+ void compact() {
92
+ self = rb_gc_location(self);
93
+ _ruby_configuration = rb_gc_location(_ruby_configuration);
94
+ _ruby_tls_context = rb_gc_location(_ruby_tls_context);
95
+
96
+ for (auto & [server, ruby_server] : _ruby_servers) {
97
+ ruby_server = rb_gc_location(ruby_server);
98
+ }
99
+
100
+ for (auto & [socket, ruby_socket] : _ruby_sockets) {
101
+ ruby_socket = rb_gc_location(ruby_socket);
102
+ }
103
+ }
104
+ };
105
+
106
+ }
107
+
108
+ VALUE Ruby_Protocol_QUIC_Dispatcher = Qnil;
109
+
110
+ static void Ruby_Protocol_QUIC_Dispatcher_mark(void *data) {
111
+ if (data) {
112
+ reinterpret_cast<Ruby::Protocol::QUIC::Dispatcher *>(data)->mark();
113
+ }
114
+ }
115
+
116
+ static void Ruby_Protocol_QUIC_Dispatcher_compact(void *data) {
117
+ if (data) {
118
+ reinterpret_cast<Ruby::Protocol::QUIC::Dispatcher *>(data)->compact();
119
+ }
120
+ }
121
+
122
+ static void Ruby_Protocol_QUIC_Dispatcher_free(void *data) {
123
+ if (data) {
124
+ delete reinterpret_cast<::Protocol::QUIC::Dispatcher *>(data);
125
+ }
126
+ }
127
+
128
+ static size_t Ruby_Protocol_QUIC_Dispatcher_size(const void *data) {
129
+ return sizeof(Ruby::Protocol::QUIC::Dispatcher);
130
+ }
131
+
132
+ static const rb_data_type_t Ruby_Protocol_QUIC_Dispatcher_type = {
133
+ .wrap_struct_name = "Protocol::QUIC::Dispatcher",
134
+ .function = {
135
+ .dmark = Ruby_Protocol_QUIC_Dispatcher_mark,
136
+ .dfree = Ruby_Protocol_QUIC_Dispatcher_free,
137
+ .dsize = Ruby_Protocol_QUIC_Dispatcher_size,
138
+ .dcompact = Ruby_Protocol_QUIC_Dispatcher_compact,
139
+ },
140
+ .data = NULL,
141
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
142
+ };
143
+
144
+ ::Protocol::QUIC::Dispatcher * Ruby_Protocol_QUIC_Dispatcher_get(VALUE self)
145
+ {
146
+ ::Protocol::QUIC::Dispatcher *dispatcher;
147
+
148
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Dispatcher, &Ruby_Protocol_QUIC_Dispatcher_type, dispatcher);
149
+
150
+ return dispatcher;
151
+ }
152
+
153
+ static VALUE Ruby_Protocol_QUIC_Dispatcher_allocate(VALUE klass) {
154
+ return TypedData_Wrap_Struct(klass, &Ruby_Protocol_QUIC_Dispatcher_type, NULL);
155
+ }
156
+
157
+ static VALUE Ruby_Protocol_QUIC_Dispatcher_initialize(VALUE self, VALUE configuration, VALUE tls_context) {
158
+ auto dispatcher = new Ruby::Protocol::QUIC::Dispatcher(self, configuration, tls_context);
159
+ DATA_PTR(self) = dispatcher;
160
+ return self;
161
+ }
162
+
163
+ static VALUE Ruby_Protocol_QUIC_Dispatcher_configuration(VALUE self) {
164
+ auto dispatcher = dynamic_cast<Ruby::Protocol::QUIC::Dispatcher*>(Ruby_Protocol_QUIC_Dispatcher_get(self));
165
+
166
+ return dispatcher->ruby_configuration();
167
+ }
168
+
169
+ static VALUE Ruby_Protocol_QUIC_Dispatcher_tls_context(VALUE self) {
170
+ auto dispatcher = dynamic_cast<Ruby::Protocol::QUIC::Dispatcher*>(Ruby_Protocol_QUIC_Dispatcher_get(self));
171
+
172
+ return dispatcher->ruby_tls_context();
173
+ }
174
+
175
+ static VALUE Ruby_Protocol_QUIC_Dispatcher_listen(VALUE self, VALUE socket) {
176
+ auto dispatcher = dynamic_cast<Ruby::Protocol::QUIC::Dispatcher*>(Ruby_Protocol_QUIC_Dispatcher_get(self));
177
+
178
+ dispatcher->listen(socket);
179
+
180
+ return Qnil;
181
+ }
182
+
183
+ void Init_Ruby_Protocol_QUIC_Dispatcher(VALUE Protocol_QUIC) {
184
+ Ruby_Protocol_QUIC_Dispatcher =
185
+ rb_define_class_under(Protocol_QUIC, "Dispatcher", rb_cObject);
186
+
187
+ rb_define_alloc_func(Ruby_Protocol_QUIC_Dispatcher, Ruby_Protocol_QUIC_Dispatcher_allocate);
188
+ rb_define_method(Ruby_Protocol_QUIC_Dispatcher, "initialize", Ruby_Protocol_QUIC_Dispatcher_initialize, 2);
189
+
190
+ rb_define_method(Ruby_Protocol_QUIC_Dispatcher, "configuration", Ruby_Protocol_QUIC_Dispatcher_configuration, 0);
191
+ rb_define_method(Ruby_Protocol_QUIC_Dispatcher, "tls_context", Ruby_Protocol_QUIC_Dispatcher_tls_context, 0);
192
+
193
+ rb_define_method(Ruby_Protocol_QUIC_Dispatcher, "listen", Ruby_Protocol_QUIC_Dispatcher_listen, 1);
194
+ }