sfml3-rb 0.2.2 → 0.3.1
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.
- checksums.yaml +4 -4
- data/.yardopts +9 -0
- data/CHANGELOG.md +67 -16
- data/README.md +123 -32
- data/TODO.md +18 -11
- data/ext/audio/audio_enums.c +58 -20
- data/ext/audio/listener.c +89 -6
- data/ext/audio/music.c +212 -5
- data/ext/audio/sound.c +218 -9
- data/ext/audio/sound_buffer.c +197 -47
- data/ext/audio/sound_buffer_recorder.c +90 -18
- data/ext/audio/sound_recorder.c +188 -58
- data/ext/audio/sound_source_cone.c +127 -33
- data/ext/audio/sound_stream.c +204 -21
- data/ext/core/exceptions.c +10 -8
- data/ext/core/unicode.c +12 -2
- data/ext/ext.c +8 -0
- data/ext/graphics/blend_mode.c +218 -60
- data/ext/graphics/circle.c +300 -16
- data/ext/graphics/color.c +253 -94
- data/ext/graphics/drawable.c +16 -4
- data/ext/graphics/font.c +144 -25
- data/ext/graphics/glyph.c +38 -8
- data/ext/graphics/image.c +205 -41
- data/ext/graphics/polygon.c +317 -30
- data/ext/graphics/rect.c +225 -60
- data/ext/graphics/rectangle.c +307 -35
- data/ext/graphics/render_state.c +124 -12
- data/ext/graphics/render_texture.c +191 -14
- data/ext/graphics/render_window.c +55 -0
- data/ext/graphics/render_window.h +10 -0
- data/ext/graphics/shader.c +289 -17
- data/ext/graphics/shape.c +265 -32
- data/ext/graphics/sprite.c +245 -30
- data/ext/graphics/stencil_mode.c +163 -46
- data/ext/graphics/target.c +121 -43
- data/ext/graphics/target.h +31 -17
- data/ext/graphics/text.c +339 -19
- data/ext/graphics/texture.c +291 -6
- data/ext/graphics/transform.c +178 -5
- data/ext/graphics/transformable.c +127 -12
- data/ext/graphics/vertex.c +98 -6
- data/ext/graphics/vertex_array.c +142 -21
- data/ext/graphics/vertex_buffer.c +154 -12
- data/ext/graphics/view.c +140 -6
- data/ext/network/ftp.c +278 -12
- data/ext/network/http.c +179 -44
- data/ext/network/ip_address.c +127 -35
- data/ext/network/network_enums.c +248 -141
- data/ext/network/packet.c +280 -41
- data/ext/network/socket_selector.c +98 -21
- data/ext/network/tcp_listener.c +82 -18
- data/ext/network/tcp_socket.c +135 -27
- data/ext/network/udp_socket.c +129 -28
- data/ext/ports.rb +7 -0
- data/ext/system/buffer.c +47 -16
- data/ext/system/clock.c +77 -12
- data/ext/system/input_stream.c +113 -51
- data/ext/system/sleep.c +10 -2
- data/ext/system/time.c +182 -40
- data/ext/system/vec2.c +166 -39
- data/ext/system/vec3.c +177 -39
- data/ext/window/clipboard.c +47 -17
- data/ext/window/context.c +66 -13
- data/ext/window/context_settings.c +132 -42
- data/ext/window/cursor.c +45 -9
- data/ext/window/event.c +164 -53
- data/ext/window/joystick.c +69 -3
- data/ext/window/keyboard.c +168 -116
- data/ext/window/mouse.c +64 -13
- data/ext/window/sensor.c +28 -2
- data/ext/window/touch.c +32 -12
- data/ext/window/video_mode.c +93 -31
- data/ext/window/vulkan.c +34 -6
- data/ext/window/window.c +228 -229
- data/ext/window/window.h +4 -4
- data/ext/window/window_base.c +251 -0
- data/ext/window/window_base.h +43 -0
- data/ext/window/window_base.inc +236 -0
- data/lib/sfml/version.rb +1 -1
- data/sig/audio/audio_enums.rbs +30 -0
- data/sig/audio/listener.rbs +16 -0
- data/sig/audio/music.rbs +56 -0
- data/sig/audio/sound.rbs +51 -0
- data/sig/audio/sound_buffer.rbs +17 -0
- data/sig/audio/sound_buffer_recorder.rbs +14 -0
- data/sig/audio/sound_recorder.rbs +17 -0
- data/sig/audio/sound_source_cone.rbs +14 -0
- data/sig/audio/sound_stream.rbs +51 -0
- data/sig/graphics/blend_mode.rbs +29 -0
- data/sig/graphics/circle.rbs +44 -0
- data/sig/graphics/color.rbs +39 -0
- data/sig/graphics/drawable.rbs +5 -0
- data/sig/graphics/font.rbs +20 -0
- data/sig/graphics/glyph.rbs +7 -0
- data/sig/graphics/image.rbs +22 -0
- data/sig/graphics/polygon.rbs +42 -0
- data/sig/graphics/rect.rbs +33 -0
- data/sig/graphics/rectangle.rbs +42 -0
- data/sig/graphics/render_state.rbs +21 -0
- data/sig/graphics/render_target.rbs +18 -0
- data/sig/graphics/render_texture.rbs +39 -0
- data/sig/graphics/render_window.rbs +7 -0
- data/sig/graphics/shader.rbs +39 -0
- data/sig/graphics/shape.rbs +38 -0
- data/sig/graphics/sprite.rbs +34 -0
- data/sig/graphics/stencil_mode.rbs +18 -0
- data/sig/graphics/target.rbs +8 -0
- data/sig/graphics/text.rbs +48 -0
- data/sig/graphics/texture.rbs +34 -0
- data/sig/graphics/transform.rbs +37 -0
- data/sig/graphics/transformable.rbs +25 -0
- data/sig/graphics/vertex.rbs +17 -0
- data/sig/graphics/vertex_array.rbs +17 -0
- data/sig/graphics/vertex_buffer.rbs +22 -0
- data/sig/graphics/view.rbs +23 -0
- data/sig/network/ftp.rbs +46 -0
- data/sig/network/http.rbs +27 -0
- data/sig/network/ip_address.rbs +22 -0
- data/sig/network/network_enums.rbs +95 -0
- data/sig/network/packet.rbs +40 -0
- data/sig/network/socket_selector.rbs +14 -0
- data/sig/network/tcp_listener.rbs +13 -0
- data/sig/network/tcp_socket.rbs +18 -0
- data/sig/network/udp_socket.rbs +17 -0
- data/sig/sfml/version.rbs +3 -0
- data/sig/system/buffer.rbs +11 -0
- data/sig/system/clock.rbs +13 -0
- data/sig/system/input_stream.rbs +7 -0
- data/sig/system/sleep.rbs +3 -0
- data/sig/system/time.rbs +28 -0
- data/sig/system/vec2.rbs +26 -0
- data/sig/system/vec3.rbs +27 -0
- data/sig/window/clipboard.rbs +8 -0
- data/sig/window/context.rbs +11 -0
- data/sig/window/context_settings.rbs +20 -0
- data/sig/window/cursor.rbs +6 -0
- data/sig/window/event.rbs +20 -0
- data/sig/window/joystick.rbs +15 -0
- data/sig/window/keyboard.rbs +10 -0
- data/sig/window/mouse.rbs +9 -0
- data/sig/window/sensor.rbs +8 -0
- data/sig/window/touch.rbs +6 -0
- data/sig/window/video_mode.rbs +16 -0
- data/sig/window/vulkan.rbs +7 -0
- data/sig/window/window.rbs +34 -0
- data/sig/window/window_base.rbs +33 -0
- metadata +76 -3
data/ext/network/http.c
CHANGED
|
@@ -8,37 +8,37 @@
|
|
|
8
8
|
#include "system/time.h"
|
|
9
9
|
|
|
10
10
|
typedef struct {
|
|
11
|
-
sfHttp
|
|
11
|
+
sfHttp* handle;
|
|
12
12
|
} Http;
|
|
13
13
|
|
|
14
14
|
typedef struct {
|
|
15
|
-
sfHttpRequest
|
|
15
|
+
sfHttpRequest* handle;
|
|
16
16
|
} HttpRequest;
|
|
17
17
|
|
|
18
18
|
typedef struct {
|
|
19
|
-
sfHttpResponse
|
|
19
|
+
sfHttpResponse* handle;
|
|
20
20
|
} HttpResponse;
|
|
21
21
|
|
|
22
22
|
static VALUE rb_cHttp;
|
|
23
23
|
static VALUE rb_cHttpRequest;
|
|
24
24
|
static VALUE rb_cHttpResponse;
|
|
25
25
|
|
|
26
|
-
static void Http_free(void
|
|
27
|
-
Http
|
|
26
|
+
static void Http_free(void* ptr) {
|
|
27
|
+
Http* http = ptr;
|
|
28
28
|
|
|
29
29
|
sfHttp_destroy(http->handle);
|
|
30
30
|
free(http);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
static void HttpRequest_free(void
|
|
34
|
-
HttpRequest
|
|
33
|
+
static void HttpRequest_free(void* ptr) {
|
|
34
|
+
HttpRequest* request = ptr;
|
|
35
35
|
|
|
36
36
|
sfHttpRequest_destroy(request->handle);
|
|
37
37
|
free(request);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
static void HttpResponse_free(void
|
|
41
|
-
HttpResponse
|
|
40
|
+
static void HttpResponse_free(void* ptr) {
|
|
41
|
+
HttpResponse* response = ptr;
|
|
42
42
|
|
|
43
43
|
sfHttpResponse_destroy(response->handle);
|
|
44
44
|
free(response);
|
|
@@ -47,23 +47,20 @@ static void HttpResponse_free(void *ptr) {
|
|
|
47
47
|
static const rb_data_type_t Http_data_type = {
|
|
48
48
|
.wrap_struct_name = "SFML::Http",
|
|
49
49
|
.function = {.dmark = NULL, .dfree = Http_free, .dsize = NULL},
|
|
50
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
51
|
-
};
|
|
50
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY};
|
|
52
51
|
|
|
53
52
|
static const rb_data_type_t HttpRequest_data_type = {
|
|
54
53
|
.wrap_struct_name = "SFML::HttpRequest",
|
|
55
54
|
.function = {.dmark = NULL, .dfree = HttpRequest_free, .dsize = NULL},
|
|
56
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
57
|
-
};
|
|
55
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY};
|
|
58
56
|
|
|
59
57
|
static const rb_data_type_t HttpResponse_data_type = {
|
|
60
58
|
.wrap_struct_name = "SFML::HttpResponse",
|
|
61
59
|
.function = {.dmark = NULL, .dfree = HttpResponse_free, .dsize = NULL},
|
|
62
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
63
|
-
};
|
|
60
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY};
|
|
64
61
|
|
|
65
|
-
static VALUE Http_wrap(VALUE klass, sfHttp
|
|
66
|
-
Http
|
|
62
|
+
static VALUE Http_wrap(VALUE klass, sfHttp* handle) {
|
|
63
|
+
Http* ptr;
|
|
67
64
|
|
|
68
65
|
if (handle == NULL) {
|
|
69
66
|
rb_raise(rb_eRuntimeError, "failed to create HTTP client");
|
|
@@ -75,8 +72,8 @@ static VALUE Http_wrap(VALUE klass, sfHttp *handle) {
|
|
|
75
72
|
return TypedData_Wrap_Struct(klass, &Http_data_type, ptr);
|
|
76
73
|
}
|
|
77
74
|
|
|
78
|
-
static VALUE HttpRequest_wrap(VALUE klass, sfHttpRequest
|
|
79
|
-
HttpRequest
|
|
75
|
+
static VALUE HttpRequest_wrap(VALUE klass, sfHttpRequest* handle) {
|
|
76
|
+
HttpRequest* ptr;
|
|
80
77
|
|
|
81
78
|
if (handle == NULL) {
|
|
82
79
|
rb_raise(rb_eRuntimeError, "failed to create HTTP request");
|
|
@@ -88,8 +85,8 @@ static VALUE HttpRequest_wrap(VALUE klass, sfHttpRequest *handle) {
|
|
|
88
85
|
return TypedData_Wrap_Struct(klass, &HttpRequest_data_type, ptr);
|
|
89
86
|
}
|
|
90
87
|
|
|
91
|
-
static VALUE HttpResponse_wrap(sfHttpResponse
|
|
92
|
-
HttpResponse
|
|
88
|
+
static VALUE HttpResponse_wrap(sfHttpResponse* handle) {
|
|
89
|
+
HttpResponse* ptr;
|
|
93
90
|
|
|
94
91
|
if (handle == NULL) {
|
|
95
92
|
rb_raise(rb_eRuntimeError, "HTTP request failed");
|
|
@@ -101,16 +98,56 @@ static VALUE HttpResponse_wrap(sfHttpResponse *handle) {
|
|
|
101
98
|
return TypedData_Wrap_Struct(rb_cHttpResponse, &HttpResponse_data_type, ptr);
|
|
102
99
|
}
|
|
103
100
|
|
|
104
|
-
static VALUE
|
|
101
|
+
static VALUE Http_alloc(VALUE klass) {
|
|
105
102
|
return Http_wrap(klass, sfHttp_create());
|
|
106
103
|
}
|
|
107
104
|
|
|
105
|
+
static VALUE HttpRequest_alloc(VALUE klass) {
|
|
106
|
+
return HttpRequest_wrap(klass, sfHttpRequest_create());
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
static VALUE HttpResponse_alloc(VALUE klass) {
|
|
110
|
+
(void)klass;
|
|
111
|
+
rb_raise(rb_eNotImpError, "HTTP responses are returned by Http#send_request and cannot be "
|
|
112
|
+
"constructed directly");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* call-seq:
|
|
116
|
+
* Http.new -> Http
|
|
117
|
+
*
|
|
118
|
+
* Creates a new HTTP client with no host set.
|
|
119
|
+
*
|
|
120
|
+
* @return [Http]
|
|
121
|
+
*/
|
|
122
|
+
static VALUE Http_initialize(VALUE self) {
|
|
123
|
+
return self;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/* call-seq:
|
|
127
|
+
* set_host(host, port = 0) -> self
|
|
128
|
+
*
|
|
129
|
+
* Sets the target host of subsequent requests. +port+ of 0 means the
|
|
130
|
+
* standard port for the scheme (80 for HTTP, 443 for HTTPS) is used;
|
|
131
|
+
* +host+ may include a scheme, e.g. +"https://example.com"+.
|
|
132
|
+
*
|
|
133
|
+
* @return [self]
|
|
134
|
+
*/
|
|
108
135
|
static VALUE Http_set_host(VALUE self, VALUE rb_host, VALUE rb_port) {
|
|
109
|
-
sfHttp_setHost(Get_Http_Struct(self), StringValueCStr(rb_host),
|
|
136
|
+
sfHttp_setHost(Get_Http_Struct(self), StringValueCStr(rb_host),
|
|
137
|
+
(unsigned short)NUM2INT(rb_port));
|
|
110
138
|
return self;
|
|
111
139
|
}
|
|
112
140
|
|
|
113
|
-
|
|
141
|
+
/* call-seq:
|
|
142
|
+
* send_request(request, timeout = Time.zero) -> HttpResponse
|
|
143
|
+
*
|
|
144
|
+
* Sends +request+ to the host set with #set_host, blocking until the
|
|
145
|
+
* response arrives or +timeout+ elapses.
|
|
146
|
+
*
|
|
147
|
+
* @return [HttpResponse]
|
|
148
|
+
* @raise [TypeError] if +request+ is not an SFML::HttpRequest
|
|
149
|
+
*/
|
|
150
|
+
static VALUE Http_send_request(int argc, VALUE* argv, VALUE self) {
|
|
114
151
|
VALUE rb_request, rb_timeout;
|
|
115
152
|
sfTime timeout = sfTime_Zero;
|
|
116
153
|
|
|
@@ -128,72 +165,170 @@ static VALUE Http_send_request(int argc, VALUE *argv, VALUE self) {
|
|
|
128
165
|
sfHttp_sendRequest(Get_Http_Struct(self), Get_HttpRequest_Struct(rb_request), timeout));
|
|
129
166
|
}
|
|
130
167
|
|
|
131
|
-
|
|
132
|
-
|
|
168
|
+
/* call-seq:
|
|
169
|
+
* HttpRequest.new -> HttpRequest
|
|
170
|
+
*
|
|
171
|
+
* Creates a new HTTP request.
|
|
172
|
+
*
|
|
173
|
+
* @return [HttpRequest] a new request, defaulting to a GET of +"/"+ over
|
|
174
|
+
* HTTP/1.0 with no fields or body
|
|
175
|
+
*/
|
|
176
|
+
static VALUE HttpRequest_initialize(VALUE self) {
|
|
177
|
+
return self;
|
|
133
178
|
}
|
|
134
179
|
|
|
180
|
+
/* call-seq:
|
|
181
|
+
* set_field(field, value) -> value
|
|
182
|
+
*
|
|
183
|
+
* Sets the value of a header field, e.g. +"Content-Type"+. Field names are
|
|
184
|
+
* case-insensitive. A few fields (Content-Length, Connection, User-Agent,
|
|
185
|
+
* Host) are managed automatically and overwritten before sending.
|
|
186
|
+
*
|
|
187
|
+
* @return [String] +value+
|
|
188
|
+
*/
|
|
135
189
|
static VALUE HttpRequest_set_field(VALUE self, VALUE rb_field, VALUE rb_value) {
|
|
136
190
|
sfHttpRequest_setField(Get_HttpRequest_Struct(self), StringValueCStr(rb_field),
|
|
137
191
|
StringValueCStr(rb_value));
|
|
138
192
|
return rb_value;
|
|
139
193
|
}
|
|
140
194
|
|
|
195
|
+
/* call-seq:
|
|
196
|
+
* method=(value) -> value
|
|
197
|
+
*
|
|
198
|
+
* +value+ is an HttpMethod name or value.
|
|
199
|
+
*
|
|
200
|
+
* @return [Symbol, Integer] +value+
|
|
201
|
+
*/
|
|
141
202
|
static VALUE HttpRequest_set_method(VALUE self, VALUE rb_method) {
|
|
142
203
|
sfHttpRequest_setMethod(Get_HttpRequest_Struct(self), http_method_from_rb(rb_method));
|
|
143
204
|
return rb_method;
|
|
144
205
|
}
|
|
145
206
|
|
|
207
|
+
/* call-seq:
|
|
208
|
+
* uri=(value) -> value
|
|
209
|
+
*
|
|
210
|
+
* Sets the request URI, e.g. +"/index.html"+.
|
|
211
|
+
*
|
|
212
|
+
* @return [String] +value+
|
|
213
|
+
*/
|
|
146
214
|
static VALUE HttpRequest_set_uri(VALUE self, VALUE rb_uri) {
|
|
147
215
|
sfHttpRequest_setUri(Get_HttpRequest_Struct(self), StringValueCStr(rb_uri));
|
|
148
216
|
return rb_uri;
|
|
149
217
|
}
|
|
150
218
|
|
|
219
|
+
/* call-seq:
|
|
220
|
+
* set_http_version(major, minor) -> [major, minor]
|
|
221
|
+
*
|
|
222
|
+
* Sets the HTTP protocol version used by the request.
|
|
223
|
+
*
|
|
224
|
+
* @return [Array(Integer, Integer)] +[major, minor]+
|
|
225
|
+
*/
|
|
151
226
|
static VALUE HttpRequest_set_http_version(VALUE self, VALUE rb_major, VALUE rb_minor) {
|
|
152
|
-
sfHttpRequest_setHttpVersion(Get_HttpRequest_Struct(self), (unsigned int)
|
|
153
|
-
(unsigned int)
|
|
227
|
+
sfHttpRequest_setHttpVersion(Get_HttpRequest_Struct(self), (unsigned int)NUM2INT(rb_major),
|
|
228
|
+
(unsigned int)NUM2INT(rb_minor));
|
|
154
229
|
|
|
155
230
|
return rb_ary_new_from_args(2, rb_major, rb_minor);
|
|
156
231
|
}
|
|
157
232
|
|
|
233
|
+
/* call-seq:
|
|
234
|
+
* body=(value) -> value
|
|
235
|
+
*
|
|
236
|
+
* Sets the request body, used by POST/PUT requests; ignored for GET/HEAD.
|
|
237
|
+
*
|
|
238
|
+
* @return [String] +value+
|
|
239
|
+
*/
|
|
158
240
|
static VALUE HttpRequest_set_body(VALUE self, VALUE rb_body) {
|
|
159
241
|
sfHttpRequest_setBody(Get_HttpRequest_Struct(self), StringValueCStr(rb_body));
|
|
160
242
|
return rb_body;
|
|
161
243
|
}
|
|
162
244
|
|
|
245
|
+
/* call-seq:
|
|
246
|
+
* field(name) -> String or nil
|
|
247
|
+
*
|
|
248
|
+
* Returns the value of the response header field named +name+.
|
|
249
|
+
*
|
|
250
|
+
* @return [String, nil] the value of response header field +name+
|
|
251
|
+
* (case-insensitive), or +nil+ if it wasn't sent
|
|
252
|
+
*/
|
|
163
253
|
static VALUE HttpResponse_field(VALUE self, VALUE rb_field) {
|
|
164
|
-
const char
|
|
165
|
-
|
|
254
|
+
const char* field =
|
|
255
|
+
sfHttpResponse_getField(Get_HttpResponse_Struct(self), StringValueCStr(rb_field));
|
|
166
256
|
|
|
167
257
|
return field != NULL ? rb_str_new_cstr(field) : Qnil;
|
|
168
258
|
}
|
|
169
259
|
|
|
260
|
+
/* call-seq: status -> Integer
|
|
261
|
+
*
|
|
262
|
+
* Returns the numeric HTTP status code.
|
|
263
|
+
*
|
|
264
|
+
* @return [Integer] the raw HTTP status code
|
|
265
|
+
*/
|
|
170
266
|
static VALUE HttpResponse_status(VALUE self) {
|
|
171
267
|
return INT2NUM(sfHttpResponse_getStatus(Get_HttpResponse_Struct(self)));
|
|
172
268
|
}
|
|
173
269
|
|
|
270
|
+
/* call-seq: status_name -> Symbol
|
|
271
|
+
*
|
|
272
|
+
* Returns the status as an HttpStatus name.
|
|
273
|
+
*
|
|
274
|
+
* @return [Symbol] an HttpStatus name for #status
|
|
275
|
+
*/
|
|
174
276
|
static VALUE HttpResponse_status_name(VALUE self) {
|
|
175
|
-
return ID2SYM(
|
|
277
|
+
return ID2SYM(
|
|
278
|
+
rb_intern(http_status_name(sfHttpResponse_getStatus(Get_HttpResponse_Struct(self)))));
|
|
176
279
|
}
|
|
177
280
|
|
|
281
|
+
/* call-seq: major_version -> Integer
|
|
282
|
+
*
|
|
283
|
+
* Returns the major component of the HTTP protocol version.
|
|
284
|
+
*
|
|
285
|
+
* @return [Integer] the major version of the HTTP protocol used by the server
|
|
286
|
+
*/
|
|
178
287
|
static VALUE HttpResponse_major_version(VALUE self) {
|
|
179
288
|
return UINT2NUM(sfHttpResponse_getMajorVersion(Get_HttpResponse_Struct(self)));
|
|
180
289
|
}
|
|
181
290
|
|
|
291
|
+
/* call-seq: minor_version -> Integer
|
|
292
|
+
*
|
|
293
|
+
* Returns the minor component of the HTTP protocol version.
|
|
294
|
+
*
|
|
295
|
+
* @return [Integer] the minor version of the HTTP protocol used by the server
|
|
296
|
+
*/
|
|
182
297
|
static VALUE HttpResponse_minor_version(VALUE self) {
|
|
183
298
|
return UINT2NUM(sfHttpResponse_getMinorVersion(Get_HttpResponse_Struct(self)));
|
|
184
299
|
}
|
|
185
300
|
|
|
301
|
+
/* call-seq: body -> String
|
|
302
|
+
*
|
|
303
|
+
* Returns the body of the response.
|
|
304
|
+
*
|
|
305
|
+
* @return [String] the body of the response, possibly an error message
|
|
306
|
+
* when the server sent something other than valid HTML
|
|
307
|
+
*/
|
|
186
308
|
static VALUE HttpResponse_body(VALUE self) {
|
|
187
309
|
return rb_str_new_cstr(sfHttpResponse_getBody(Get_HttpResponse_Struct(self)));
|
|
188
310
|
}
|
|
189
311
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
312
|
+
/* Document-class: SFML::Http
|
|
313
|
+
* A basic HTTP client for performing requests against one host at a time
|
|
314
|
+
* (set with #set_host).
|
|
315
|
+
*/
|
|
316
|
+
void Init_Http(VALUE rb_mSFML) {
|
|
317
|
+
rb_cHttp = rb_define_class_under(rb_mSFML, "Http", rb_cObject);
|
|
318
|
+
rb_define_alloc_func(rb_cHttp, Http_alloc);
|
|
319
|
+
/* Document-class: SFML::HttpRequest
|
|
320
|
+
* An HTTP request to send via Http#send_request.
|
|
321
|
+
*/
|
|
322
|
+
rb_cHttpRequest = rb_define_class_under(rb_mSFML, "HttpRequest", rb_cObject);
|
|
323
|
+
rb_define_alloc_func(rb_cHttpRequest, HttpRequest_alloc);
|
|
324
|
+
/* Document-class: SFML::HttpResponse
|
|
325
|
+
* The status, headers and body returned by Http#send_request.
|
|
326
|
+
*/
|
|
327
|
+
rb_cHttpResponse = rb_define_class_under(rb_mSFML, "HttpResponse", rb_cObject);
|
|
328
|
+
rb_define_alloc_func(rb_cHttpResponse, HttpResponse_alloc);
|
|
329
|
+
|
|
330
|
+
rb_define_method(rb_cHttp, "initialize", Http_initialize, 0);
|
|
331
|
+
rb_define_method(rb_cHttpRequest, "initialize", HttpRequest_initialize, 0);
|
|
197
332
|
|
|
198
333
|
rb_define_method(rb_cHttp, "set_host", Http_set_host, 2);
|
|
199
334
|
rb_define_method(rb_cHttp, "send_request", Http_send_request, -1);
|
|
@@ -224,20 +359,20 @@ VALUE Get_Klass_HttpResponse(void) {
|
|
|
224
359
|
return rb_cHttpResponse;
|
|
225
360
|
}
|
|
226
361
|
|
|
227
|
-
void
|
|
228
|
-
Http
|
|
362
|
+
void* Get_Http_Struct(VALUE self) {
|
|
363
|
+
Http* ptr;
|
|
229
364
|
TypedData_Get_Struct(self, Http, &Http_data_type, ptr);
|
|
230
365
|
return ptr->handle;
|
|
231
366
|
}
|
|
232
367
|
|
|
233
|
-
void
|
|
234
|
-
HttpRequest
|
|
368
|
+
void* Get_HttpRequest_Struct(VALUE self) {
|
|
369
|
+
HttpRequest* ptr;
|
|
235
370
|
TypedData_Get_Struct(self, HttpRequest, &HttpRequest_data_type, ptr);
|
|
236
371
|
return ptr->handle;
|
|
237
372
|
}
|
|
238
373
|
|
|
239
|
-
void
|
|
240
|
-
HttpResponse
|
|
374
|
+
void* Get_HttpResponse_Struct(VALUE self) {
|
|
375
|
+
HttpResponse* ptr;
|
|
241
376
|
TypedData_Get_Struct(self, HttpResponse, &HttpResponse_data_type, ptr);
|
|
242
377
|
return ptr->handle;
|
|
243
378
|
}
|
data/ext/network/ip_address.c
CHANGED
|
@@ -13,60 +13,117 @@ typedef struct {
|
|
|
13
13
|
|
|
14
14
|
static VALUE rb_cIpAddress;
|
|
15
15
|
|
|
16
|
-
static void IpAddress_free(void
|
|
16
|
+
static void IpAddress_free(void* ptr) {
|
|
17
17
|
free(ptr);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
static const rb_data_type_t IpAddress_data_type = {
|
|
21
21
|
.wrap_struct_name = "SFML::IpAddress",
|
|
22
22
|
.function = {.dmark = NULL, .dfree = IpAddress_free, .dsize = NULL},
|
|
23
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
24
|
-
};
|
|
23
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY};
|
|
25
24
|
|
|
26
25
|
VALUE ip_address_to_rb(sfIpAddress address) {
|
|
27
|
-
IpAddress
|
|
26
|
+
IpAddress* ptr = malloc(sizeof(IpAddress));
|
|
28
27
|
|
|
29
28
|
ptr->address = address;
|
|
30
29
|
|
|
31
30
|
return TypedData_Wrap_Struct(rb_cIpAddress, &IpAddress_data_type, ptr);
|
|
32
31
|
}
|
|
33
32
|
|
|
33
|
+
/* call-seq:
|
|
34
|
+
* IpAddress.from_string(address) -> IpAddress
|
|
35
|
+
*
|
|
36
|
+
* Builds an address from a string, which may be a dotted decimal
|
|
37
|
+
* (+"192.168.1.1"+) or a network name (+"localhost"+).
|
|
38
|
+
*
|
|
39
|
+
* @return [IpAddress]
|
|
40
|
+
*/
|
|
34
41
|
static VALUE IpAddress_from_string(VALUE klass, VALUE rb_address) {
|
|
35
42
|
return ip_address_to_rb(sfIpAddress_fromString(StringValueCStr(rb_address)));
|
|
36
43
|
}
|
|
37
44
|
|
|
45
|
+
/* call-seq:
|
|
46
|
+
* IpAddress.from_bytes(byte0, byte1, byte2, byte3) -> IpAddress
|
|
47
|
+
*
|
|
48
|
+
* Builds an address from its four decimal byte components (e.g.
|
|
49
|
+
* +192, 168, 1, 1+).
|
|
50
|
+
*
|
|
51
|
+
* @return [IpAddress]
|
|
52
|
+
*/
|
|
38
53
|
static VALUE IpAddress_from_bytes(VALUE klass, VALUE rb0, VALUE rb1, VALUE rb2, VALUE rb3) {
|
|
39
|
-
return ip_address_to_rb(sfIpAddress_fromBytes((uint8_t)
|
|
40
|
-
(uint8_t)
|
|
54
|
+
return ip_address_to_rb(sfIpAddress_fromBytes((uint8_t)NUM2INT(rb0), (uint8_t)NUM2INT(rb1),
|
|
55
|
+
(uint8_t)NUM2INT(rb2), (uint8_t)NUM2INT(rb3)));
|
|
41
56
|
}
|
|
42
57
|
|
|
58
|
+
/* call-seq:
|
|
59
|
+
* IpAddress.from_integer(value) -> IpAddress
|
|
60
|
+
*
|
|
61
|
+
* Builds an address from its 32-bit representation, in host byte order.
|
|
62
|
+
*
|
|
63
|
+
* @return [IpAddress]
|
|
64
|
+
*/
|
|
43
65
|
static VALUE IpAddress_from_integer(VALUE klass, VALUE rb_integer) {
|
|
44
|
-
return ip_address_to_rb(sfIpAddress_fromInteger((uint32_t)
|
|
66
|
+
return ip_address_to_rb(sfIpAddress_fromInteger((uint32_t)NUM2UINT(rb_integer)));
|
|
45
67
|
}
|
|
46
68
|
|
|
47
|
-
static VALUE
|
|
48
|
-
|
|
49
|
-
return IpAddress_from_integer(klass, rb_address);
|
|
50
|
-
}
|
|
69
|
+
static VALUE IpAddress_alloc(VALUE klass) {
|
|
70
|
+
IpAddress* ptr = malloc(sizeof(IpAddress));
|
|
51
71
|
|
|
52
|
-
if (
|
|
53
|
-
|
|
72
|
+
if (ptr == NULL) {
|
|
73
|
+
rb_raise(rb_eNoMemError, "failed to allocate IP address");
|
|
54
74
|
}
|
|
55
75
|
|
|
56
|
-
|
|
57
|
-
return ip_address_to_rb(((IpAddress *) Get_IpAddress_Struct(rb_address))->address);
|
|
58
|
-
}
|
|
76
|
+
ptr->address = sfIpAddress_None;
|
|
59
77
|
|
|
60
|
-
|
|
78
|
+
return TypedData_Wrap_Struct(klass, &IpAddress_data_type, ptr);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* call-seq:
|
|
82
|
+
* IpAddress.new(address) -> IpAddress
|
|
83
|
+
*
|
|
84
|
+
* +address+ may be an Integer (32-bit representation), a String (dotted
|
|
85
|
+
* decimal or network name), or another IpAddress to copy.
|
|
86
|
+
*
|
|
87
|
+
* @return [IpAddress]
|
|
88
|
+
* @raise [TypeError] if +address+ is none of the above
|
|
89
|
+
*/
|
|
90
|
+
static VALUE IpAddress_initialize(VALUE self, VALUE rb_address) {
|
|
91
|
+
IpAddress* ptr = (IpAddress*)Get_IpAddress_Struct(self);
|
|
92
|
+
|
|
93
|
+
if (RB_INTEGER_TYPE_P(rb_address)) {
|
|
94
|
+
ptr->address = sfIpAddress_fromInteger((uint32_t)NUM2UINT(rb_address));
|
|
95
|
+
} else if (RB_TYPE_P(rb_address, T_STRING)) {
|
|
96
|
+
ptr->address = sfIpAddress_fromString(StringValueCStr(rb_address));
|
|
97
|
+
} else if (rb_obj_is_kind_of(rb_address, rb_cIpAddress)) {
|
|
98
|
+
ptr->address = ((IpAddress*)Get_IpAddress_Struct(rb_address))->address;
|
|
99
|
+
} else {
|
|
100
|
+
raise_invalid_argument_class(rb_cIpAddress);
|
|
101
|
+
}
|
|
61
102
|
|
|
62
|
-
return
|
|
103
|
+
return self;
|
|
63
104
|
}
|
|
64
105
|
|
|
106
|
+
/* call-seq:
|
|
107
|
+
* IpAddress.local_address -> IpAddress
|
|
108
|
+
*
|
|
109
|
+
* Returns the address of the local computer on its local network.
|
|
110
|
+
*
|
|
111
|
+
* @return [IpAddress] the address of the local computer on the local
|
|
112
|
+
* network
|
|
113
|
+
*/
|
|
65
114
|
static VALUE IpAddress_local_address(VALUE klass) {
|
|
66
115
|
return ip_address_to_rb(sfIpAddress_getLocalAddress());
|
|
67
116
|
}
|
|
68
117
|
|
|
69
|
-
|
|
118
|
+
/* call-seq:
|
|
119
|
+
* IpAddress.public_address(timeout = Time.zero) -> IpAddress
|
|
120
|
+
*
|
|
121
|
+
* Blocks while querying an external web service for the computer's
|
|
122
|
+
* public address, as seen from outside the local network.
|
|
123
|
+
*
|
|
124
|
+
* @return [IpAddress] +IpAddress::NONE+ if the request timed out or failed
|
|
125
|
+
*/
|
|
126
|
+
static VALUE IpAddress_public_address(int argc, VALUE* argv, VALUE klass) {
|
|
70
127
|
VALUE rb_timeout;
|
|
71
128
|
sfTime timeout = sfTime_Zero;
|
|
72
129
|
|
|
@@ -79,39 +136,68 @@ static VALUE IpAddress_public_address(int argc, VALUE *argv, VALUE klass) {
|
|
|
79
136
|
return ip_address_to_rb(sfIpAddress_getPublicAddress(timeout));
|
|
80
137
|
}
|
|
81
138
|
|
|
139
|
+
/* call-seq: to_s -> String
|
|
140
|
+
*
|
|
141
|
+
* Returns the address in dotted decimal notation.
|
|
142
|
+
*
|
|
143
|
+
* @return [String] dotted decimal representation, e.g. +"192.168.1.1"+
|
|
144
|
+
*/
|
|
82
145
|
static VALUE IpAddress_to_s(VALUE self) {
|
|
83
146
|
char buffer[16];
|
|
84
147
|
|
|
85
|
-
sfIpAddress_toString(((IpAddress
|
|
148
|
+
sfIpAddress_toString(((IpAddress*)Get_IpAddress_Struct(self))->address, buffer);
|
|
86
149
|
|
|
87
150
|
return rb_str_new_cstr(buffer);
|
|
88
151
|
}
|
|
89
152
|
|
|
153
|
+
/* call-seq: to_integer -> Integer
|
|
154
|
+
*
|
|
155
|
+
* Returns the address as a 32-bit integer.
|
|
156
|
+
*
|
|
157
|
+
* @return [Integer] the 32-bit representation, in host byte order
|
|
158
|
+
*/
|
|
90
159
|
static VALUE IpAddress_to_integer(VALUE self) {
|
|
91
|
-
return UINT2NUM(sfIpAddress_toInteger(((IpAddress
|
|
160
|
+
return UINT2NUM(sfIpAddress_toInteger(((IpAddress*)Get_IpAddress_Struct(self))->address));
|
|
92
161
|
}
|
|
93
162
|
|
|
163
|
+
/* call-seq:
|
|
164
|
+
* self == other -> true or false
|
|
165
|
+
*
|
|
166
|
+
* Returns +true+ if +other+ is an IpAddress with the same value.
|
|
167
|
+
*
|
|
168
|
+
* @return [Boolean]
|
|
169
|
+
*/
|
|
94
170
|
static VALUE IpAddress_eql(VALUE self, VALUE rb_other) {
|
|
95
|
-
sfIpAddress a = ((IpAddress
|
|
171
|
+
sfIpAddress a = ((IpAddress*)Get_IpAddress_Struct(self))->address;
|
|
96
172
|
sfIpAddress b;
|
|
97
173
|
|
|
98
174
|
if (!rb_obj_is_kind_of(rb_other, rb_cIpAddress)) {
|
|
99
175
|
return Qfalse;
|
|
100
176
|
}
|
|
101
177
|
|
|
102
|
-
b = ((IpAddress
|
|
178
|
+
b = ((IpAddress*)Get_IpAddress_Struct(rb_other))->address;
|
|
103
179
|
|
|
104
180
|
return BOOL2RB(sfIpAddress_toInteger(a) == sfIpAddress_toInteger(b));
|
|
105
181
|
}
|
|
106
182
|
|
|
183
|
+
/* call-seq: hash -> Integer
|
|
184
|
+
*
|
|
185
|
+
* Returns a hash value consistent with #==, so addresses work as Hash keys.
|
|
186
|
+
*
|
|
187
|
+
* @return [Integer] a hash suitable for use as a Hash key, consistent with #==
|
|
188
|
+
*/
|
|
107
189
|
static VALUE IpAddress_hash(VALUE self) {
|
|
108
|
-
return UINT2NUM(sfIpAddress_toInteger(((IpAddress
|
|
190
|
+
return UINT2NUM(sfIpAddress_toInteger(((IpAddress*)Get_IpAddress_Struct(self))->address));
|
|
109
191
|
}
|
|
110
192
|
|
|
111
|
-
|
|
112
|
-
|
|
193
|
+
/* Document-class: SFML::IpAddress
|
|
194
|
+
* An IPv4 network address.
|
|
195
|
+
*/
|
|
196
|
+
void Init_IpAddress(VALUE rb_mSFML) {
|
|
197
|
+
rb_cIpAddress = rb_define_class_under(rb_mSFML, "IpAddress", rb_cObject);
|
|
198
|
+
rb_define_alloc_func(rb_cIpAddress, IpAddress_alloc);
|
|
113
199
|
|
|
114
|
-
|
|
200
|
+
rb_define_method(rb_cIpAddress, "initialize", IpAddress_initialize, 1);
|
|
115
201
|
rb_define_singleton_method(rb_cIpAddress, "from_string", IpAddress_from_string, 1);
|
|
116
202
|
rb_define_singleton_method(rb_cIpAddress, "from_bytes", IpAddress_from_bytes, 4);
|
|
117
203
|
rb_define_singleton_method(rb_cIpAddress, "from_integer", IpAddress_from_integer, 1);
|
|
@@ -125,18 +211,24 @@ void Init_IpAddress(VALUE rb_module) {
|
|
|
125
211
|
rb_define_method(rb_cIpAddress, "==", IpAddress_eql, 1);
|
|
126
212
|
rb_define_method(rb_cIpAddress, "eql?", IpAddress_eql, 1);
|
|
127
213
|
|
|
128
|
-
|
|
129
|
-
rb_define_const(rb_cIpAddress, "
|
|
130
|
-
|
|
131
|
-
rb_define_const(rb_cIpAddress, "
|
|
214
|
+
/* An invalid/unspecified address. */
|
|
215
|
+
rb_define_const(rb_cIpAddress, "NONE", rb_obj_freeze(ip_address_to_rb(sfIpAddress_None)));
|
|
216
|
+
/* Any address, 0.0.0.0, e.g. to bind a listener to all network interfaces. */
|
|
217
|
+
rb_define_const(rb_cIpAddress, "ANY", rb_obj_freeze(ip_address_to_rb(sfIpAddress_Any)));
|
|
218
|
+
/* The local host address, 127.0.0.1. */
|
|
219
|
+
rb_define_const(rb_cIpAddress, "LOCAL_HOST",
|
|
220
|
+
rb_obj_freeze(ip_address_to_rb(sfIpAddress_LocalHost)));
|
|
221
|
+
/* The broadcast address, 255.255.255.255. */
|
|
222
|
+
rb_define_const(rb_cIpAddress, "BROADCAST",
|
|
223
|
+
rb_obj_freeze(ip_address_to_rb(sfIpAddress_Broadcast)));
|
|
132
224
|
}
|
|
133
225
|
|
|
134
226
|
VALUE Get_Klass_IpAddress(void) {
|
|
135
227
|
return rb_cIpAddress;
|
|
136
228
|
}
|
|
137
229
|
|
|
138
|
-
void
|
|
139
|
-
IpAddress
|
|
230
|
+
void* Get_IpAddress_Struct(VALUE self) {
|
|
231
|
+
IpAddress* ptr;
|
|
140
232
|
TypedData_Get_Struct(self, IpAddress, &IpAddress_data_type, ptr);
|
|
141
233
|
return ptr;
|
|
142
234
|
}
|
|
@@ -147,7 +239,7 @@ sfIpAddress ip_address_from_rb(VALUE rb_address, sfIpAddress fallback) {
|
|
|
147
239
|
}
|
|
148
240
|
|
|
149
241
|
if (rb_obj_is_kind_of(rb_address, rb_cIpAddress)) {
|
|
150
|
-
return ((IpAddress
|
|
242
|
+
return ((IpAddress*)Get_IpAddress_Struct(rb_address))->address;
|
|
151
243
|
}
|
|
152
244
|
|
|
153
245
|
if (RB_TYPE_P(rb_address, T_STRING)) {
|
|
@@ -155,7 +247,7 @@ sfIpAddress ip_address_from_rb(VALUE rb_address, sfIpAddress fallback) {
|
|
|
155
247
|
}
|
|
156
248
|
|
|
157
249
|
if (RB_INTEGER_TYPE_P(rb_address)) {
|
|
158
|
-
return sfIpAddress_fromInteger((uint32_t)
|
|
250
|
+
return sfIpAddress_fromInteger((uint32_t)NUM2UINT(rb_address));
|
|
159
251
|
}
|
|
160
252
|
|
|
161
253
|
raise_invalid_argument_class(rb_cIpAddress);
|