sfml3-rb 0.2.2 → 0.3.0

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