protocol-quic 0.0.3 → 0.0.5

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 (50) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +4 -2
  3. data/ext/ruby-protocol-quic-lock.yml +2 -2
  4. data/ext/source/Ruby/Protocol/QUIC/Address.cpp +122 -0
  5. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Address.hpp +5 -5
  6. data/ext/source/Ruby/Protocol/QUIC/Client.cpp +161 -0
  7. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Client.hpp +3 -3
  8. data/ext/source/Ruby/Protocol/QUIC/Configuration.cpp +74 -0
  9. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Configuration.hpp +3 -3
  10. data/ext/source/Ruby/Protocol/QUIC/Connection.cpp +50 -0
  11. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Connection.hpp +4 -4
  12. data/ext/source/Ruby/Protocol/QUIC/Dispatcher.cpp +194 -0
  13. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Dispatcher.hpp +3 -3
  14. data/ext/source/Ruby/Protocol/QUIC/PacketHeader.cpp +59 -0
  15. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/PacketHeader.hpp +4 -4
  16. data/ext/source/Ruby/Protocol/QUIC/Reference.hpp +35 -0
  17. data/ext/source/Ruby/Protocol/QUIC/Server.cpp +169 -0
  18. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Server.hpp +3 -3
  19. data/ext/source/Ruby/Protocol/QUIC/Socket.cpp +118 -0
  20. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Socket.hpp +4 -4
  21. data/ext/source/Ruby/Protocol/QUIC/Stream.cpp +100 -0
  22. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/Stream.hpp +4 -4
  23. data/ext/source/Ruby/Protocol/QUIC/TLS/ClientContext.cpp +61 -0
  24. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/TLS/ClientContext.hpp +3 -3
  25. data/ext/source/Ruby/Protocol/QUIC/TLS/Context.cpp +116 -0
  26. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/TLS/Context.hpp +3 -3
  27. data/ext/source/Ruby/Protocol/QUIC/TLS/ServerContext.cpp +61 -0
  28. data/ext/source/{Protocol → Ruby/Protocol}/QUIC/TLS/ServerContext.hpp +3 -3
  29. data/ext/source/Ruby/Protocol/QUIC.cpp +36 -0
  30. data/ext/source/{Protocol → Ruby/Protocol}/QUIC.hpp +1 -1
  31. data/ext/teapot.rb +1 -1
  32. data/lib/protocol/quic/version.rb +1 -1
  33. data/lib/protocol/quic.rb +1 -1
  34. data.tar.gz.sig +0 -0
  35. metadata +28 -28
  36. metadata.gz.sig +0 -0
  37. data/ext/source/Protocol/QUIC/Address.cpp +0 -122
  38. data/ext/source/Protocol/QUIC/Client.cpp +0 -157
  39. data/ext/source/Protocol/QUIC/Configuration.cpp +0 -74
  40. data/ext/source/Protocol/QUIC/Connection.cpp +0 -50
  41. data/ext/source/Protocol/QUIC/Dispatcher.cpp +0 -190
  42. data/ext/source/Protocol/QUIC/PacketHeader.cpp +0 -59
  43. data/ext/source/Protocol/QUIC/Reference.hpp +0 -31
  44. data/ext/source/Protocol/QUIC/Server.cpp +0 -165
  45. data/ext/source/Protocol/QUIC/Socket.cpp +0 -103
  46. data/ext/source/Protocol/QUIC/Stream.cpp +0 -96
  47. data/ext/source/Protocol/QUIC/TLS/ClientContext.cpp +0 -61
  48. data/ext/source/Protocol/QUIC/TLS/Context.cpp +0 -116
  49. data/ext/source/Protocol/QUIC/TLS/ServerContext.cpp +0 -61
  50. data/ext/source/Protocol/QUIC.cpp +0 -36
@@ -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
+ }
@@ -16,11 +16,11 @@
16
16
  extern "C" {
17
17
  #endif
18
18
 
19
- extern VALUE Protocol_QUIC_Dispatcher;
19
+ extern VALUE Ruby_Protocol_QUIC_Dispatcher;
20
20
 
21
- void Init_Protocol_QUIC_Dispatcher(VALUE Protocol_QUIC);
21
+ void Init_Ruby_Protocol_QUIC_Dispatcher(VALUE Protocol_QUIC);
22
22
 
23
- Protocol::QUIC::Dispatcher * Protocol_QUIC_Dispatcher_get(VALUE self);
23
+ ::Protocol::QUIC::Dispatcher * Ruby_Protocol_QUIC_Dispatcher_get(VALUE self);
24
24
 
25
25
  #ifdef __cplusplus
26
26
  }
@@ -0,0 +1,59 @@
1
+ //
2
+ // PacketHeader.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 "PacketHeader.hpp"
10
+
11
+ VALUE Ruby_Protocol_QUIC_PacketHeader = Qnil;
12
+
13
+ static void Ruby_Protocol_QUIC_PacketHeader_free(void *data) {
14
+ if (data) {
15
+ delete reinterpret_cast<ngtcp2_pkt_hd *>(data);
16
+ }
17
+ }
18
+
19
+ static size_t Ruby_Protocol_QUIC_PacketHeader_size(const void *data) {
20
+ return sizeof(ngtcp2_pkt_hd);
21
+ }
22
+
23
+ const rb_data_type_t Ruby_Protocol_QUIC_PacketHeader_type = {
24
+ .wrap_struct_name = "Protocol::QUIC::PacketHeader",
25
+ .function = {
26
+ .dmark = NULL,
27
+ .dfree = Ruby_Protocol_QUIC_PacketHeader_free,
28
+ .dsize = Ruby_Protocol_QUIC_PacketHeader_size,
29
+ },
30
+ .data = NULL,
31
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
32
+ };
33
+
34
+ ngtcp2_pkt_hd * Ruby_Protocol_QUIC_PacketHeader_get(VALUE self)
35
+ {
36
+ ngtcp2_pkt_hd *address;
37
+
38
+ TypedData_Get_Struct(self, ngtcp2_pkt_hd, &Ruby_Protocol_QUIC_PacketHeader_type, address);
39
+
40
+ return address;
41
+ }
42
+
43
+ VALUE Ruby_Protocol_QUIC_PacketHeader_allocate(VALUE klass) {
44
+ return TypedData_Wrap_Struct(klass, &Ruby_Protocol_QUIC_PacketHeader_type, NULL);
45
+ }
46
+
47
+ static VALUE Ruby_Protocol_QUIC_PacketHeader_initialize(VALUE self) {
48
+ DATA_PTR(self) = new ngtcp2_pkt_hd();
49
+
50
+ return self;
51
+ }
52
+
53
+ void Init_Ruby_Protocol_QUIC_PacketHeader(VALUE Protocol_QUIC) {
54
+ Ruby_Protocol_QUIC_PacketHeader =
55
+ rb_define_class_under(Protocol_QUIC, "PacketHeader", rb_cObject);
56
+
57
+ rb_define_alloc_func(Ruby_Protocol_QUIC_PacketHeader, Ruby_Protocol_QUIC_PacketHeader_allocate);
58
+ rb_define_method(Ruby_Protocol_QUIC_PacketHeader, "initialize", Ruby_Protocol_QUIC_PacketHeader_initialize, 0);
59
+ }
@@ -16,13 +16,13 @@
16
16
  extern "C" {
17
17
  #endif
18
18
 
19
- extern VALUE Protocol_QUIC_PacketHeader;
19
+ extern VALUE Ruby_Protocol_QUIC_PacketHeader;
20
20
 
21
- void Init_Protocol_QUIC_PacketHeader(VALUE Protocol_QUIC);
21
+ void Init_Ruby_Protocol_QUIC_PacketHeader(VALUE Protocol_QUIC);
22
22
 
23
- ngtcp2_pkt_hd * Protocol_QUIC_PacketHeader_get(VALUE self);
23
+ ngtcp2_pkt_hd * Ruby_Protocol_QUIC_PacketHeader_get(VALUE self);
24
24
 
25
- VALUE Protocol_QUIC_PacketHeader_allocate(VALUE klass);
25
+ VALUE Ruby_Protocol_QUIC_PacketHeader_allocate(VALUE klass);
26
26
 
27
27
  #ifdef __cplusplus
28
28
  }
@@ -0,0 +1,35 @@
1
+ //
2
+ // Reference.hpp
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
+ #pragma once
10
+
11
+ #include <ruby.h>
12
+
13
+ namespace Ruby::Protocol::QUIC {
14
+
15
+ struct ValueReference {
16
+ VALUE _value;
17
+
18
+ template <typename ReferenceType>
19
+ ValueReference(VALUE value, ReferenceType & reference) : _value(value)
20
+ {
21
+ DATA_PTR(value) = reinterpret_cast<void*>(&reference);
22
+ }
23
+
24
+ template <typename ReferenceType>
25
+ ValueReference(VALUE value, const ReferenceType & reference) : ValueReference(value, const_cast<ReferenceType&>(reference))
26
+ {
27
+ }
28
+
29
+ ~ValueReference()
30
+ {
31
+ DATA_PTR(_value) = NULL;
32
+ }
33
+ };
34
+
35
+ }
@@ -0,0 +1,169 @@
1
+
2
+ #include "Server.hpp"
3
+
4
+ #include "Dispatcher.hpp"
5
+ #include "Configuration.hpp"
6
+ #include "Connection.hpp"
7
+ #include "TLS/ServerContext.hpp"
8
+ #include "Socket.hpp"
9
+ #include "Address.hpp"
10
+ #include "PacketHeader.hpp"
11
+
12
+ #include "Stream.hpp"
13
+
14
+ #include <unordered_map>
15
+
16
+ VALUE Ruby_Protocol_QUIC_Server = Qnil;
17
+
18
+ namespace Ruby::Protocol::QUIC {
19
+
20
+ class Server : public ::Protocol::QUIC::Server {
21
+ public:
22
+ VALUE self;
23
+
24
+ private:
25
+ VALUE _dispatcher;
26
+ VALUE _configuration;
27
+ VALUE _tls_context;
28
+ VALUE _socket;
29
+ VALUE _remote_address;
30
+ std::unordered_map<::Protocol::QUIC::StreamID, VALUE> _ruby_streams;
31
+
32
+ public:
33
+ Server(VALUE self, VALUE dispatcher, VALUE configuration, VALUE tls_context, VALUE socket, VALUE remote_address, VALUE packet_header, VALUE ocid) : ::Protocol::QUIC::Server(*Ruby_Protocol_QUIC_Dispatcher_get(dispatcher), *Ruby_Protocol_QUIC_Configuration_get(configuration), *Ruby_Protocol_QUIC_TLS_ServerContext_get(tls_context), *Ruby_Protocol_QUIC_Socket_get(socket), *Ruby_Protocol_QUIC_Address_get(remote_address), *Ruby_Protocol_QUIC_PacketHeader_get(packet_header), nullptr), self(self), _dispatcher(dispatcher), _configuration(configuration), _tls_context(tls_context), _socket(socket), _remote_address(remote_address) {}
34
+ virtual ~Server() {}
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::Server::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::Server::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::Server::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(_dispatcher);
67
+ rb_gc_mark_movable(_configuration);
68
+ rb_gc_mark_movable(_tls_context);
69
+ rb_gc_mark_movable(_socket);
70
+ rb_gc_mark_movable(_remote_address);
71
+
72
+ for (auto & [stream_id, ruby_stream] : _ruby_streams) {
73
+ rb_gc_mark_movable(ruby_stream);
74
+ }
75
+ }
76
+
77
+ void compact() {
78
+ self = rb_gc_location(self);
79
+ _dispatcher = rb_gc_location(_dispatcher);
80
+ _configuration = rb_gc_location(_configuration);
81
+ _tls_context = rb_gc_location(_tls_context);
82
+ _socket = rb_gc_location(_socket);
83
+ _remote_address = rb_gc_location(_remote_address);
84
+
85
+ for (auto & [stream_id, ruby_stream] : _ruby_streams) {
86
+ ruby_stream = rb_gc_location(ruby_stream);
87
+ }
88
+ }
89
+ };
90
+
91
+ }
92
+
93
+ static void Ruby_Protocol_QUIC_Server_mark(void *data)
94
+ {
95
+ if (data) {
96
+ reinterpret_cast<Ruby::Protocol::QUIC::Server *>(data)->mark();
97
+ }
98
+ }
99
+
100
+ static void Ruby_Protocol_QUIC_Server_compact(void *data)
101
+ {
102
+ if (data) {
103
+ reinterpret_cast<Ruby::Protocol::QUIC::Server *>(data)->compact();
104
+ }
105
+ }
106
+
107
+ static void Ruby_Protocol_QUIC_Server_free(void *data)
108
+ {
109
+ if (data) {
110
+ delete reinterpret_cast<::Protocol::QUIC::Server *>(data);
111
+ }
112
+ }
113
+
114
+ static size_t Ruby_Protocol_QUIC_Server_size(const void *data) {
115
+ return sizeof(Ruby::Protocol::QUIC::Server);
116
+ }
117
+
118
+ static const rb_data_type_t Ruby_Protocol_QUIC_Server_type = {
119
+ .wrap_struct_name = "Protocol::QUIC::Server",
120
+ .function = {
121
+ .dmark = Ruby_Protocol_QUIC_Server_mark,
122
+ .dfree = Ruby_Protocol_QUIC_Server_free,
123
+ .dsize = Ruby_Protocol_QUIC_Server_size,
124
+ .dcompact = Ruby_Protocol_QUIC_Server_compact,
125
+ },
126
+ .parent = &Ruby_Protocol_QUIC_Connection_type,
127
+ .data = NULL,
128
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
129
+ };
130
+
131
+ ::Protocol::QUIC::Server * Ruby_Protocol_QUIC_Server_get(VALUE self)
132
+ {
133
+ ::Protocol::QUIC::Server *server;
134
+
135
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Server, &Ruby_Protocol_QUIC_Server_type, server);
136
+
137
+ return server;
138
+ }
139
+
140
+ static VALUE Ruby_Protocol_QUIC_Server_allocate(VALUE klass) {
141
+ return TypedData_Wrap_Struct(klass, &Ruby_Protocol_QUIC_Server_type, NULL);
142
+ }
143
+
144
+ // Dispatcher & dispatcher, Configuration & configuration, TLS::ServerContext & tls_context, Socket & socket, const Address & remote_address, const ngtcp2_pkt_hd & packet_header, ngtcp2_cid *ocid = nullptr
145
+ static VALUE Ruby_Protocol_QUIC_Server_initialize(VALUE self, VALUE dispatcher, VALUE configuration, VALUE tls_context, VALUE socket, VALUE remote_address, VALUE packet_header, VALUE ocid) {
146
+ ::Protocol::QUIC::Server *server = new Ruby::Protocol::QUIC::Server(self, dispatcher, configuration, tls_context, socket, remote_address, packet_header, ocid);
147
+
148
+ DATA_PTR(self) = server;
149
+
150
+ return self;
151
+ }
152
+
153
+ static VALUE Ruby_Protocol_QUIC_Server_send_packets(VALUE self) {
154
+ ::Protocol::QUIC::Server *server = Ruby_Protocol_QUIC_Server_get(self);
155
+
156
+ server->send_packets();
157
+
158
+ return Qnil;
159
+ }
160
+
161
+ void Init_Ruby_Protocol_QUIC_Server(VALUE Protocol_QUIC) {
162
+ Ruby_Protocol_QUIC_Server =
163
+ rb_define_class_under(Protocol_QUIC, "Server", rb_cObject);
164
+
165
+ rb_define_alloc_func(Ruby_Protocol_QUIC_Server, Ruby_Protocol_QUIC_Server_allocate);
166
+ rb_define_method(Ruby_Protocol_QUIC_Server, "initialize", Ruby_Protocol_QUIC_Server_initialize, 7);
167
+
168
+ rb_define_method(Ruby_Protocol_QUIC_Server, "send_packets", Ruby_Protocol_QUIC_Server_send_packets, 0);
169
+ }
@@ -16,11 +16,11 @@
16
16
  extern "C" {
17
17
  #endif
18
18
 
19
- extern VALUE Protocol_QUIC_Server;
19
+ extern VALUE Ruby_Protocol_QUIC_Server;
20
20
 
21
- void Init_Protocol_QUIC_Server(VALUE Protocol_QUIC);
21
+ void Init_Ruby_Protocol_QUIC_Server(VALUE Protocol_QUIC);
22
22
 
23
- Protocol::QUIC::Server * Protocol_QUIC_Server_get(VALUE self);
23
+ ::Protocol::QUIC::Server * Ruby_Protocol_QUIC_Server_get(VALUE self);
24
24
 
25
25
  #ifdef __cplusplus
26
26
  }
@@ -0,0 +1,118 @@
1
+ //
2
+ // Socket.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 "Socket.hpp"
10
+ #include "Address.hpp"
11
+
12
+ VALUE Ruby_Protocol_QUIC_Socket = Qnil;
13
+
14
+ static void Ruby_Protocol_QUIC_Socket_mark(void *data) {
15
+ if (data) {
16
+ auto socket = reinterpret_cast<::Protocol::QUIC::Socket *>(data);
17
+ socket->monitor().mark();
18
+ }
19
+ }
20
+
21
+ static void Ruby_Protocol_QUIC_Socket_compact(void *data) {
22
+ if (data) {
23
+ auto socket = reinterpret_cast<::Protocol::QUIC::Socket *>(data);
24
+ socket->monitor().compact();
25
+ }
26
+ }
27
+
28
+ static void Ruby_Protocol_QUIC_Socket_free(void *data) {
29
+ if (data) {
30
+ delete reinterpret_cast<::Protocol::QUIC::Socket *>(data);
31
+ }
32
+ }
33
+
34
+ static size_t Ruby_Protocol_QUIC_Socket_size(const void *data) {
35
+ return sizeof(::Protocol::QUIC::Socket);
36
+ }
37
+
38
+ const rb_data_type_t Ruby_Protocol_QUIC_Socket_type = {
39
+ .wrap_struct_name = "Protocol::QUIC::Socket",
40
+ .function = {
41
+ .dmark = Ruby_Protocol_QUIC_Socket_mark,
42
+ .dfree = Ruby_Protocol_QUIC_Socket_free,
43
+ .dsize = Ruby_Protocol_QUIC_Socket_size,
44
+ .dcompact = Ruby_Protocol_QUIC_Socket_compact,
45
+ },
46
+ .data = NULL,
47
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
48
+ };
49
+
50
+ ::Protocol::QUIC::Socket * Ruby_Protocol_QUIC_Socket_get(VALUE self)
51
+ {
52
+ ::Protocol::QUIC::Socket *socket;
53
+
54
+ TypedData_Get_Struct(self, ::Protocol::QUIC::Socket, &Ruby_Protocol_QUIC_Socket_type, socket);
55
+
56
+ return socket;
57
+ }
58
+
59
+ VALUE Ruby_Protocol_QUIC_Socket_allocate(VALUE klass) {
60
+ return TypedData_Wrap_Struct(klass, &Ruby_Protocol_QUIC_Socket_type, NULL);
61
+ }
62
+
63
+ static VALUE Ruby_Protocol_QUIC_Socket_initialize(VALUE self, VALUE domain, VALUE type, VALUE protocol) {
64
+ DATA_PTR(self) = new ::Protocol::QUIC::Socket(RB_NUM2INT(domain), RB_NUM2INT(type), RB_NUM2INT(protocol));
65
+
66
+ return self;
67
+ }
68
+
69
+ static VALUE Ruby_Protocol_QUIC_Socket_local_address(VALUE self) {
70
+ auto socket = Ruby_Protocol_QUIC_Socket_get(self);
71
+ auto &address = socket->local_address();
72
+
73
+ if (address) {
74
+ return Ruby_Protocol_QUIC_Address_wrap(Ruby_Protocol_QUIC_Address, address);
75
+ } else {
76
+ return Qnil;
77
+ }
78
+ }
79
+
80
+ static VALUE Ruby_Protocol_QUIC_Socket_remote_address(VALUE self) {
81
+ auto socket = Ruby_Protocol_QUIC_Socket_get(self);
82
+ auto &address = socket->remote_address();
83
+
84
+ if (address) {
85
+ return Ruby_Protocol_QUIC_Address_wrap(Ruby_Protocol_QUIC_Address, address);
86
+ } else {
87
+ return Qnil;
88
+ }
89
+ }
90
+
91
+ static VALUE Ruby_Protocol_QUIC_Socket_bind(VALUE self, VALUE address) {
92
+ auto socket = Ruby_Protocol_QUIC_Socket_get(self);
93
+
94
+ return RTEST(
95
+ socket->bind(*Ruby_Protocol_QUIC_Address_get(address))
96
+ );
97
+ }
98
+
99
+ static VALUE Ruby_Protocol_QUIC_Socket_connect(VALUE self, VALUE address) {
100
+ auto socket = Ruby_Protocol_QUIC_Socket_get(self);
101
+
102
+ return RTEST(
103
+ socket->connect(*Ruby_Protocol_QUIC_Address_get(address))
104
+ );
105
+ }
106
+
107
+ void Init_Ruby_Protocol_QUIC_Socket(VALUE Protocol_QUIC) {
108
+ Ruby_Protocol_QUIC_Socket = rb_define_class_under(Protocol_QUIC, "Socket", rb_cObject);
109
+
110
+ rb_define_alloc_func(Ruby_Protocol_QUIC_Socket, Ruby_Protocol_QUIC_Socket_allocate);
111
+ rb_define_method(Ruby_Protocol_QUIC_Socket, "initialize", Ruby_Protocol_QUIC_Socket_initialize, 3);
112
+
113
+ rb_define_method(Ruby_Protocol_QUIC_Socket, "local_address", Ruby_Protocol_QUIC_Socket_local_address, 0);
114
+ rb_define_method(Ruby_Protocol_QUIC_Socket, "remote_address", Ruby_Protocol_QUIC_Socket_remote_address, 0);
115
+
116
+ rb_define_method(Ruby_Protocol_QUIC_Socket, "bind", Ruby_Protocol_QUIC_Socket_bind, 1);
117
+ rb_define_method(Ruby_Protocol_QUIC_Socket, "connect", Ruby_Protocol_QUIC_Socket_connect, 1);
118
+ }
@@ -16,13 +16,13 @@
16
16
  extern "C" {
17
17
  #endif
18
18
 
19
- extern VALUE Protocol_QUIC_Socket;
19
+ extern VALUE Ruby_Protocol_QUIC_Socket;
20
20
 
21
- void Init_Protocol_QUIC_Socket(VALUE Protocol_QUIC);
21
+ void Init_Ruby_Protocol_QUIC_Socket(VALUE Protocol_QUIC);
22
22
 
23
- Protocol::QUIC::Socket * Protocol_QUIC_Socket_get(VALUE self);
23
+ ::Protocol::QUIC::Socket * Ruby_Protocol_QUIC_Socket_get(VALUE self);
24
24
 
25
- VALUE Protocol_QUIC_Socket_allocate(VALUE klass);
25
+ VALUE Ruby_Protocol_QUIC_Socket_allocate(VALUE klass);
26
26
 
27
27
  #ifdef __cplusplus
28
28
  }