Class: BasicSocket
- Defined in:
- mrbgems/mruby-socket/src/socket.c,
mrbgems/mruby-socket/mrblib/socket.rb
Direct Known Subclasses
Constant Summary collapse
- @@do_not_reverse_lookup =
true
Constants inherited from IO
IO::BUF_SIZE, IO::SEEK_CUR, IO::SEEK_END, IO::SEEK_SET
Instance Attribute Summary collapse
-
#do_not_reverse_lookup ⇒ Object
Returns the value of attribute do_not_reverse_lookup.
Class Method Summary collapse
Instance Method Summary collapse
-
#_is_socket= ⇒ Object
-
#_recvfrom ⇒ Object
-
#_setnonblock ⇒ Object
-
#close ⇒ Object
-
#getpeereid ⇒ Object
-
#getpeername ⇒ Object
-
#getsockname ⇒ Object
-
#getsockopt ⇒ Object
-
#initialize(*args) ⇒ BasicSocket
constructor
A new instance of BasicSocket.
-
#local_address ⇒ Object
def connect_address.
-
#recv ⇒ Object
-
#recv_nonblock(maxlen, flags = 0) ⇒ Object
-
#remote_address ⇒ Object
-
#send ⇒ Object
recvmsg(maxlen, flags=0).
-
#setsockopt ⇒ Object
sendmsg_nonblock.
-
#shutdown ⇒ Object
-
#sysread ⇒ Object
-
#sysseek ⇒ Object
-
#syswrite ⇒ Object
Methods inherited from IO
#<<, #_check_readable, #_read_buf, #close_on_exec=, #close_on_exec?, #close_write, #closed?, #each, #each_byte, #eof?, #fileno, #flush, #getc, #gets, #hash, #initialize_copy, #isatty, open, #pid, pipe, popen, #pos, #pos=, #print, #printf, #puts, read, #read, #readchar, #readline, #readlines, #rewind, #seek, #sync, #sync=, #ungetc, #write
Constructor Details
#initialize(*args) ⇒ BasicSocket
Returns a new instance of BasicSocket
179 180 181 182 183 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 179 def initialize(*args) super(*args) self._is_socket = true @do_not_reverse_lookup = @@do_not_reverse_lookup end |
Instance Attribute Details
#do_not_reverse_lookup ⇒ Object
Returns the value of attribute do_not_reverse_lookup
208 209 210 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 208 def do_not_reverse_lookup @do_not_reverse_lookup end |
Class Method Details
.do_not_reverse_lookup ⇒ Object
171 172 173 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 171 def self.do_not_reverse_lookup @@do_not_reverse_lookup end |
.do_not_reverse_lookup=(val) ⇒ Object
175 176 177 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 175 def self.do_not_reverse_lookup=(val) @@do_not_reverse_lookup = val ? true : false end |
.for_fd(fd) ⇒ Object
185 186 187 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 185 def self.for_fd(fd) super(fd, "r+") end |
Instance Method Details
#_is_socket= ⇒ Object
499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'mrbgems/mruby-socket/src/socket.c', line 499 static mrb_value mrb_basicsocket_set_is_socket(mrb_state *mrb, mrb_value self) { mrb_bool b; struct mrb_io *io_p; mrb_get_args(mrb, "b", &b); io_p = (struct mrb_io*)DATA_PTR(self); if (io_p) { io_p->is_socket = b; } return mrb_bool_value(b); } |
#_recvfrom ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'mrbgems/mruby-socket/src/socket.c', line 372 static mrb_value mrb_basicsocket_recvfrom(mrb_state *mrb, mrb_value self) { ssize_t n; mrb_int maxlen, flags = 0; mrb_value ary, buf, sa; socklen_t socklen; mrb_get_args(mrb, "i|i", &maxlen, &flags); buf = mrb_str_buf_new(mrb, maxlen); socklen = sizeof(struct sockaddr_storage); sa = mrb_str_buf_new(mrb, socklen); n = recvfrom(socket_fd(mrb, self), RSTRING_PTR(buf), (fsize_t)maxlen, (int)flags, (struct sockaddr *)RSTRING_PTR(sa), &socklen); if (n == -1) mrb_sys_fail(mrb, "recvfrom"); mrb_str_resize(mrb, buf, (mrb_int)n); mrb_str_resize(mrb, sa, (mrb_int)socklen); ary = mrb_ary_new_capa(mrb, 2); mrb_ary_push(mrb, ary, buf); mrb_ary_push(mrb, ary, sa); return ary; } |
#_setnonblock ⇒ Object
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'mrbgems/mruby-socket/src/socket.c', line 414 static mrb_value mrb_basicsocket_setnonblock(mrb_state *mrb, mrb_value self) { int fd, flags; mrb_bool nonblocking; #ifdef _WIN32 u_long mode = 1; #endif mrb_get_args(mrb, "b", &nonblocking); fd = socket_fd(mrb, self); #ifdef _WIN32 flags = ioctlsocket(fd, FIONBIO, &mode); if (flags != NO_ERROR) mrb_sys_fail(mrb, "ioctlsocket"); #else flags = fcntl(fd, F_GETFL, 0); if (flags == 1) mrb_sys_fail(mrb, "fcntl"); if (nonblocking) flags |= O_NONBLOCK; else flags &= ~O_NONBLOCK; if (fcntl(fd, F_SETFL, flags) == -1) mrb_sys_fail(mrb, "fcntl"); #endif return mrb_nil_value(); } |
#close ⇒ Object
773 774 775 776 777 778 779 |
# File 'mrbgems/mruby-socket/src/socket.c', line 773 static mrb_value mrb_win32_basicsocket_close(mrb_state *mrb, mrb_value self) { if (closesocket(socket_fd(mrb, self)) != NO_ERROR) mrb_raise(mrb, E_SOCKET_ERROR, "closesocket unsuccessful"); return mrb_nil_value(); } |
#getpeereid ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'mrbgems/mruby-socket/src/socket.c', line 287 static mrb_value mrb_basicsocket_getpeereid(mrb_state *mrb, mrb_value self) { #ifdef HAVE_GETPEEREID mrb_value ary; gid_t egid; uid_t euid; int s; s = socket_fd(mrb, self); if (getpeereid(s, &euid, &egid) != 0) mrb_sys_fail(mrb, "getpeereid"); ary = mrb_ary_new_capa(mrb, 2); mrb_ary_push(mrb, ary, mrb_fixnum_value((mrb_int)euid)); mrb_ary_push(mrb, ary, mrb_fixnum_value((mrb_int)egid)); return ary; #else mrb_raise(mrb, E_RUNTIME_ERROR, "getpeereid is not available on this system"); return mrb_nil_value(); #endif } |
#getpeername ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'mrbgems/mruby-socket/src/socket.c', line 310 static mrb_value mrb_basicsocket_getpeername(mrb_state *mrb, mrb_value self) { struct sockaddr_storage ss; socklen_t salen; salen = sizeof(ss); if (getpeername(socket_fd(mrb, self), (struct sockaddr *)&ss, &salen) != 0) mrb_sys_fail(mrb, "getpeername"); return mrb_str_new(mrb, (char*)&ss, salen); } |
#getsockname ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'mrbgems/mruby-socket/src/socket.c', line 323 static mrb_value mrb_basicsocket_getsockname(mrb_state *mrb, mrb_value self) { struct sockaddr_storage ss; socklen_t salen; salen = sizeof(ss); if (getsockname(socket_fd(mrb, self), (struct sockaddr *)&ss, &salen) != 0) mrb_sys_fail(mrb, "getsockname"); return mrb_str_new(mrb, (char*)&ss, salen); } |
#getsockopt ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'mrbgems/mruby-socket/src/socket.c', line 336 static mrb_value mrb_basicsocket_getsockopt(mrb_state *mrb, mrb_value self) { char opt[8]; int s; mrb_int family, level, optname; mrb_value c, data; socklen_t optlen; mrb_get_args(mrb, "ii", &level, &optname); s = socket_fd(mrb, self); optlen = sizeof(opt); if (getsockopt(s, (int)level, (int)optname, opt, &optlen) == -1) mrb_sys_fail(mrb, "getsockopt"); c = mrb_const_get(mrb, mrb_obj_value(mrb_class_get(mrb, "Socket")), mrb_intern_lit(mrb, "Option")); family = socket_family(s); data = mrb_str_new(mrb, opt, optlen); return mrb_funcall(mrb, c, "new", 4, mrb_fixnum_value(family), mrb_fixnum_value(level), mrb_fixnum_value(optname), data); } |
#local_address ⇒ Object
def connect_address
191 192 193 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 191 def local_address Addrinfo.new self.getsockname end |
#recv ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'mrbgems/mruby-socket/src/socket.c', line 356 static mrb_value mrb_basicsocket_recv(mrb_state *mrb, mrb_value self) { ssize_t n; mrb_int maxlen, flags = 0; mrb_value buf; mrb_get_args(mrb, "i|i", &maxlen, &flags); buf = mrb_str_buf_new(mrb, maxlen); n = recv(socket_fd(mrb, self), RSTRING_PTR(buf), (fsize_t)maxlen, (int)flags); if (n == -1) mrb_sys_fail(mrb, "recv"); mrb_str_resize(mrb, buf, (mrb_int)n); return buf; } |
#recv_nonblock(maxlen, flags = 0) ⇒ Object
195 196 197 198 199 200 201 202 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 195 def recv_nonblock(maxlen, flags=0) begin _setnonblock(true) recv(maxlen, flags) ensure _setnonblock(false) end end |
#remote_address ⇒ Object
204 205 206 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 204 def remote_address Addrinfo.new self.getpeername end |
#send ⇒ Object
recvmsg(maxlen, flags=0)
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'mrbgems/mruby-socket/src/socket.c', line 395 static mrb_value mrb_basicsocket_send(mrb_state *mrb, mrb_value self) { ssize_t n; mrb_int flags; mrb_value dest, mesg; dest = mrb_nil_value(); mrb_get_args(mrb, "Si|S", &mesg, &flags, &dest); if (mrb_nil_p(dest)) { n = send(socket_fd(mrb, self), RSTRING_PTR(mesg), (fsize_t)RSTRING_LEN(mesg), (int)flags); } else { n = sendto(socket_fd(mrb, self), RSTRING_PTR(mesg), (fsize_t)RSTRING_LEN(mesg), (int)flags, (const struct sockaddr*)RSTRING_PTR(dest), (fsize_t)RSTRING_LEN(dest)); } if (n == -1) mrb_sys_fail(mrb, "send"); return mrb_fixnum_value((mrb_int)n); } |
#setsockopt ⇒ Object
sendmsg_nonblock
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'mrbgems/mruby-socket/src/socket.c', line 443 static mrb_value mrb_basicsocket_setsockopt(mrb_state *mrb, mrb_value self) { int s; mrb_int argc, level = 0, optname; mrb_value optval, so; argc = mrb_get_args(mrb, "o|io", &so, &optname, &optval); if (argc == 3) { if (!mrb_fixnum_p(so)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "level is not an integer"); } level = mrb_fixnum(so); if (mrb_string_p(optval)) { /* that's good */ } else if (mrb_true_p(optval) || mrb_false_p(optval)) { mrb_int i = mrb_test(optval) ? 1 : 0; optval = mrb_str_new(mrb, (char*)&i, sizeof(i)); } else if (mrb_fixnum_p(optval)) { if (optname == IP_MULTICAST_TTL || optname == IP_MULTICAST_LOOP) { char uc = (char)mrb_fixnum(optval); optval = mrb_str_new(mrb, &uc, sizeof(uc)); } else { mrb_int i = mrb_fixnum(optval); optval = mrb_str_new(mrb, (char*)&i, sizeof(i)); } } else { mrb_raise(mrb, E_ARGUMENT_ERROR, "optval should be true, false, an integer, or a string"); } } else if (argc == 1) { if (strcmp(mrb_obj_classname(mrb, so), "Socket::Option") != 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "not an instance of Socket::Option"); level = mrb_fixnum(mrb_funcall(mrb, so, "level", 0)); optname = mrb_fixnum(mrb_funcall(mrb, so, "optname", 0)); optval = mrb_funcall(mrb, so, "data", 0); } else { mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%i for 3)", argc); } s = socket_fd(mrb, self); if (setsockopt(s, (int)level, (int)optname, RSTRING_PTR(optval), (socklen_t)RSTRING_LEN(optval)) == -1) mrb_sys_fail(mrb, "setsockopt"); return mrb_fixnum_value(0); } |
#shutdown ⇒ Object
488 489 490 491 492 493 494 495 496 497 |
# File 'mrbgems/mruby-socket/src/socket.c', line 488 static mrb_value mrb_basicsocket_shutdown(mrb_state *mrb, mrb_value self) { mrb_int how = SHUT_RDWR; mrb_get_args(mrb, "|i", &how); if (shutdown(socket_fd(mrb, self), (int)how) != 0) mrb_sys_fail(mrb, "shutdown"); return mrb_fixnum_value(0); } |
#sysread ⇒ Object
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 |
# File 'mrbgems/mruby-socket/src/socket.c', line 782 static mrb_value mrb_win32_basicsocket_sysread(mrb_state *mrb, mrb_value self) { int sd, ret; mrb_value buf = mrb_nil_value(); mrb_int maxlen; mrb_get_args(mrb, "i|S", &maxlen, &buf); if (maxlen < 0) { return mrb_nil_value(); } if (mrb_nil_p(buf)) { buf = mrb_str_new(mrb, NULL, maxlen); } if (RSTRING_LEN(buf) != maxlen) { buf = mrb_str_resize(mrb, buf, maxlen); } sd = socket_fd(mrb, self); ret = recv(sd, RSTRING_PTR(buf), (int)maxlen, 0); switch (ret) { case 0: /* EOF */ if (maxlen == 0) { buf = mrb_str_new_cstr(mrb, ""); } else { mrb_raise(mrb, E_EOF_ERROR, "sysread failed: End of File"); } break; case SOCKET_ERROR: /* Error */ mrb_sys_fail(mrb, "recv"); break; default: if (RSTRING_LEN(buf) != ret) { buf = mrb_str_resize(mrb, buf, ret); } break; } return buf; } |
#sysseek ⇒ Object
825 826 827 828 829 830 |
# File 'mrbgems/mruby-socket/src/socket.c', line 825 static mrb_value mrb_win32_basicsocket_sysseek(mrb_state *mrb, mrb_value self) { mrb_raise(mrb, E_NOTIMP_ERROR, "sysseek not implemented for windows sockets"); return mrb_nil_value(); } |
#syswrite ⇒ Object
832 833 834 835 836 837 838 839 840 841 842 843 844 845 |
# File 'mrbgems/mruby-socket/src/socket.c', line 832 static mrb_value mrb_win32_basicsocket_syswrite(mrb_state *mrb, mrb_value self) { int n; SOCKET sd; mrb_value str; sd = socket_fd(mrb, self); mrb_get_args(mrb, "S", &str); n = send(sd, RSTRING_PTR(str), (int)RSTRING_LEN(str), 0); if (n == SOCKET_ERROR) mrb_sys_fail(mrb, "send"); return mrb_fixnum_value(n); } |