1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 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 442 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 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 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 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
|
/*****************************************************************************
$Id$
File: rubymain.cpp
Date: 06Apr06
Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
Gmail: blackhedd
This program is free software; you can redistribute it and/or modify
it under the terms of either: 1) the GNU General Public License
as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version; or 2) Ruby's License.
See the file COPYING for complete licensing information.
*****************************************************************************/
#include "project.h"
#include "eventmachine.h"
#include <ruby.h>
/* Adapted from NUM2BSIG / BSIG2NUM in ext/fiddle/conversions.h,
* we'll call it a BSIG for Binding Signature here. */
#if SIZEOF_VOIDP == SIZEOF_LONG
# define BSIG2NUM(x) (ULONG2NUM((unsigned long)(x)))
# define NUM2BSIG(x) (NUM2ULONG(x))
# ifdef OS_WIN32
# define PRIFBSIG "I32u"
# else
# define PRIFBSIG "lu"
# endif
#else
# define BSIG2NUM(x) (ULL2NUM((unsigned long long)(x)))
# define NUM2BSIG(x) (NUM2ULL(x))
# ifdef OS_WIN32
# define PRIFBSIG "I64u"
# else
# define PRIFBSIG "llu"
# endif
#endif
/* Adapted from SWIG's changes for Ruby 2.7 compatibility.
* Before Ruby 2.7, rb_rescue takes (VALUE (*))(ANYARGS)
* whereas in Ruby 2.7, rb_rescue takes (VALUE (*))(VALUE)
* */
#if defined(__cplusplus) && !defined(RB_METHOD_DEFINITION_DECL)
# define VALUEFUNC(f) ((VALUE (*)(ANYARGS)) f)
#else
# define VALUEFUNC(f) (f)
#endif
/*******
Statics
*******/
static VALUE EmModule;
static VALUE EmConnection;
static VALUE EmConnsHash;
static VALUE EmTimersHash;
static VALUE EM_eConnectionError;
static VALUE EM_eUnknownTimerFired;
static VALUE EM_eConnectionNotBound;
static VALUE EM_eUnsupported;
static VALUE EM_eInvalidSignature;
static VALUE EM_eInvalidPrivateKey;
static VALUE Intern_at_signature;
static VALUE Intern_at_timers;
static VALUE Intern_at_conns;
static VALUE Intern_at_error_handler;
static VALUE Intern_event_callback;
static VALUE Intern_run_deferred_callbacks;
static VALUE Intern_delete;
static VALUE Intern_call;
static VALUE Intern_at;
static VALUE Intern_receive_data;
static VALUE Intern_ssl_handshake_completed;
static VALUE Intern_ssl_verify_peer;
static VALUE Intern_notify_readable;
static VALUE Intern_notify_writable;
static VALUE Intern_proxy_target_unbound;
static VALUE Intern_proxy_completed;
static VALUE Intern_connection_completed;
static VALUE rb_cProcessStatus;
#ifdef IS_RUBY_3_OR_LATER
/* Structure definition from MRI Ruby 3.0 process.c */
struct rb_process_status {
rb_pid_t pid;
int status;
int error;
};
#endif
struct em_event {
uintptr_t signature;
int event;
const char *data_str;
unsigned long data_num;
};
static inline VALUE ensure_conn(const uintptr_t signature)
{
VALUE conn = rb_hash_aref (EmConnsHash, BSIG2NUM (signature));
if (conn == Qnil)
rb_raise (EM_eConnectionNotBound, "unknown connection: %" PRIFBSIG, signature);
return conn;
}
/****************
t_event_callback
****************/
static inline VALUE event_callback (VALUE e_value)
{
struct em_event *e = (struct em_event *)e_value;
const uintptr_t signature = e->signature;
int event = e->event;
const char *data_str = e->data_str;
const unsigned long data_num = e->data_num;
switch (event) {
case EM_CONNECTION_READ:
{
VALUE conn = rb_hash_aref (EmConnsHash, BSIG2NUM (signature));
if (conn == Qnil)
rb_raise (EM_eConnectionNotBound, "received %lu bytes of data for unknown signature: %" PRIFBSIG, data_num, signature);
rb_funcall (conn, Intern_receive_data, 1, rb_str_new (data_str, data_num));
return Qnil;
}
case EM_CONNECTION_ACCEPTED:
{
rb_funcall (EmModule, Intern_event_callback, 3, BSIG2NUM(signature), INT2FIX(event), ULONG2NUM(data_num));
return Qnil;
}
case EM_CONNECTION_UNBOUND:
{
rb_funcall (EmModule, Intern_event_callback, 3, BSIG2NUM(signature), INT2FIX(event), ULONG2NUM(data_num));
return Qnil;
}
case EM_CONNECTION_COMPLETED:
{
VALUE conn = ensure_conn(signature);
rb_funcall (conn, Intern_connection_completed, 0);
return Qnil;
}
case EM_CONNECTION_NOTIFY_READABLE:
{
VALUE conn = ensure_conn(signature);
rb_funcall (conn, Intern_notify_readable, 0);
return Qnil;
}
case EM_CONNECTION_NOTIFY_WRITABLE:
{
VALUE conn = ensure_conn(signature);
rb_funcall (conn, Intern_notify_writable, 0);
return Qnil;
}
case EM_LOOPBREAK_SIGNAL:
{
rb_funcall (EmModule, Intern_run_deferred_callbacks, 0);
return Qnil;
}
case EM_TIMER_FIRED:
{
VALUE timer = rb_funcall (EmTimersHash, Intern_delete, 1, ULONG2NUM (data_num));
if (timer == Qnil) {
rb_raise (EM_eUnknownTimerFired, "no such timer: %lu", data_num);
} else if (timer == Qfalse) {
/* Timer Canceled */
} else {
rb_funcall (timer, Intern_call, 0);
}
return Qnil;
}
#ifdef WITH_SSL
case EM_SSL_HANDSHAKE_COMPLETED:
{
VALUE conn = ensure_conn(signature);
rb_funcall (conn, Intern_ssl_handshake_completed, 0);
return Qnil;
}
case EM_SSL_VERIFY:
{
VALUE conn = ensure_conn(signature);
VALUE should_accept = rb_funcall (conn, Intern_ssl_verify_peer, 1, rb_str_new(data_str, data_num));
if (RTEST(should_accept))
evma_accept_ssl_peer (signature);
return Qnil;
}
#endif
case EM_PROXY_TARGET_UNBOUND:
{
VALUE conn = ensure_conn(signature);
rb_funcall (conn, Intern_proxy_target_unbound, 0);
return Qnil;
}
case EM_PROXY_COMPLETED:
{
VALUE conn = ensure_conn(signature);
rb_funcall (conn, Intern_proxy_completed, 0);
return Qnil;
}
}
return Qnil;
}
/*******************
event_error_handler
*******************/
static VALUE event_error_handler(VALUE self UNUSED, VALUE err)
{
VALUE error_handler = rb_ivar_get(EmModule, Intern_at_error_handler);
rb_funcall (error_handler, Intern_call, 1, err);
return Qnil;
}
/**********************
event_callback_wrapper
**********************/
static void event_callback_wrapper (const uintptr_t signature, int event, const char *data_str, const unsigned long data_num)
{
struct em_event e;
e.signature = signature;
e.event = event;
e.data_str = data_str;
e.data_num = data_num;
if (!rb_ivar_defined(EmModule, Intern_at_error_handler))
event_callback((VALUE)&e);
else
rb_rescue(VALUEFUNC(event_callback), (VALUE)&e, VALUEFUNC(event_error_handler), Qnil);
}
/**************************
t_initialize_event_machine
**************************/
static VALUE t_initialize_event_machine (VALUE self UNUSED)
{
EmConnsHash = rb_ivar_get (EmModule, Intern_at_conns);
EmTimersHash = rb_ivar_get (EmModule, Intern_at_timers);
assert(EmConnsHash != Qnil);
assert(EmTimersHash != Qnil);
evma_initialize_library ((EMCallback)event_callback_wrapper);
return Qnil;
}
/******************
t_run_machine_once
******************/
static VALUE t_run_machine_once (VALUE self UNUSED)
{
return evma_run_machine_once () ? Qtrue : Qfalse;
}
/*************
t_run_machine
*************/
static VALUE t_run_machine (VALUE self UNUSED)
{
evma_run_machine();
return Qnil;
}
/*****************************
t_get_timer_count
*****************************/
static VALUE t_get_timer_count ()
{
return SIZET2NUM (evma_get_timer_count ());
}
/*******************
t_add_oneshot_timer
*******************/
static VALUE t_add_oneshot_timer (VALUE self UNUSED, VALUE interval)
{
const uintptr_t f = evma_install_oneshot_timer (FIX2LONG (interval));
if (!f)
rb_raise (rb_eRuntimeError, "%s", "ran out of timers; use #set_max_timers to increase limit");
return BSIG2NUM (f);
}
/**************
t_start_server
**************/
static VALUE t_start_server (VALUE self UNUSED, VALUE server, VALUE port)
{
const uintptr_t f = evma_create_tcp_server (StringValueCStr(server), FIX2INT(port));
if (!f)
rb_raise (rb_eRuntimeError, "%s", "no acceptor (port is in use or requires root privileges)");
return BSIG2NUM (f);
}
/*************
t_stop_server
*************/
static VALUE t_stop_server (VALUE self UNUSED, VALUE signature)
{
evma_stop_tcp_server (NUM2BSIG (signature));
return Qnil;
}
/*******************
t_start_unix_server
*******************/
static VALUE t_start_unix_server (VALUE self UNUSED, VALUE filename)
{
const uintptr_t f = evma_create_unix_domain_server (StringValueCStr(filename));
if (!f)
rb_raise (rb_eRuntimeError, "%s", "no unix-domain acceptor");
return BSIG2NUM (f);
}
/********************
t_attach_sd
********************/
static VALUE t_attach_sd(VALUE self UNUSED, VALUE sd)
{
const uintptr_t f = evma_attach_sd(FIX2INT(sd));
if (!f)
rb_raise (rb_eRuntimeError, "%s", "no socket descriptor acceptor");
return BSIG2NUM (f);
}
/***********
t_send_data
***********/
static VALUE t_send_data (VALUE self UNUSED, VALUE signature, VALUE data, VALUE data_length)
{
int b = evma_send_data_to_connection (NUM2BSIG (signature), StringValuePtr (data), FIX2INT (data_length));
return INT2NUM (b);
}
/***********
t_start_tls
***********/
static VALUE t_start_tls (VALUE self UNUSED, VALUE signature)
{
try {
evma_start_tls (NUM2BSIG (signature));
} catch (const std::runtime_error& e) {
rb_raise (EM_eInvalidPrivateKey, e.what(), signature);
}
return Qnil;
}
/***************
t_set_tls_parms
***************/
static VALUE t_set_tls_parms (VALUE self UNUSED, VALUE signature, VALUE privkeyfile, VALUE privkey, VALUE privkeypass, VALUE certchainfile, VALUE cert, VALUE verify_peer, VALUE fail_if_no_peer_cert, VALUE snihostname, VALUE cipherlist, VALUE ecdh_curve, VALUE dhparam, VALUE ssl_version)
{
/* set_tls_parms takes a series of positional arguments for specifying such things
* as private keys and certificate chains.
* It's expected that the parameter list will grow as we add more supported features.
* ALL of these parameters are optional, and can be specified as empty or NULL strings.
*/
evma_set_tls_parms (NUM2BSIG (signature), StringValueCStr (privkeyfile), StringValueCStr (privkey), StringValueCStr (privkeypass), StringValueCStr (certchainfile), StringValueCStr (cert), (verify_peer == Qtrue ? 1 : 0), (fail_if_no_peer_cert == Qtrue ? 1 : 0), StringValueCStr (snihostname), StringValueCStr (cipherlist), StringValueCStr (ecdh_curve), StringValueCStr (dhparam), NUM2INT (ssl_version));
return Qnil;
}
/***************
t_get_peer_cert
***************/
#ifdef WITH_SSL
static VALUE t_get_peer_cert (VALUE self UNUSED, VALUE signature)
{
VALUE ret = Qnil;
X509 *cert = NULL;
BUF_MEM *buf;
BIO *out;
cert = evma_get_peer_cert (NUM2BSIG (signature));
if (cert != NULL) {
out = BIO_new(BIO_s_mem());
PEM_write_bio_X509(out, cert);
BIO_get_mem_ptr(out, &buf);
ret = rb_str_new(buf->data, buf->length);
X509_free(cert);
BIO_free(out);
}
return ret;
}
#else
static VALUE t_get_peer_cert (VALUE self UNUSED, VALUE signature UNUSED)
{
return Qnil;
}
#endif
/***************
t_get_cipher_bits
***************/
#ifdef WITH_SSL
static VALUE t_get_cipher_bits (VALUE self UNUSED, VALUE signature)
{
int bits = evma_get_cipher_bits (NUM2BSIG (signature));
if (bits == -1)
return Qnil;
return INT2NUM (bits);
}
#else
static VALUE t_get_cipher_bits (VALUE self UNUSED, VALUE signature UNUSED)
{
return Qnil;
}
#endif
/***************
t_get_cipher_name
***************/
#ifdef WITH_SSL
static VALUE t_get_cipher_name (VALUE self UNUSED, VALUE signature)
{
const char *protocol = evma_get_cipher_name (NUM2BSIG (signature));
if (protocol)
return rb_str_new2 (protocol);
return Qnil;
}
#else
static VALUE t_get_cipher_name (VALUE self UNUSED, VALUE signature UNUSED)
{
return Qnil;
}
#endif
/***************
t_get_cipher_protocol
***************/
#ifdef WITH_SSL
static VALUE t_get_cipher_protocol (VALUE self UNUSED, VALUE signature)
{
const char *cipher = evma_get_cipher_protocol (NUM2BSIG (signature));
if (cipher)
return rb_str_new2 (cipher);
return Qnil;
}
#else
static VALUE t_get_cipher_protocol (VALUE self UNUSED, VALUE signature UNUSED)
{
return Qnil;
}
#endif
/***************
t_get_sni_hostname
***************/
#ifdef WITH_SSL
static VALUE t_get_sni_hostname (VALUE self UNUSED, VALUE signature)
{
const char *sni_hostname = evma_get_sni_hostname (NUM2BSIG (signature));
if (sni_hostname)
return rb_str_new2 (sni_hostname);
return Qnil;
}
#else
static VALUE t_get_sni_hostname (VALUE self UNUSED, VALUE signature UNUSED)
{
return Qnil;
}
#endif
/**************
t_get_peername
**************/
static VALUE t_get_peername (VALUE self UNUSED, VALUE signature)
{
char buf[1024];
socklen_t len = sizeof buf;
try {
if (evma_get_peername (NUM2BSIG (signature), (struct sockaddr*)buf, &len)) {
return rb_str_new (buf, len);
}
} catch (std::runtime_error e) {
rb_raise (rb_eRuntimeError, "%s", e.what());
}
return Qnil;
}
/**************
t_get_sockname
**************/
static VALUE t_get_sockname (VALUE self UNUSED, VALUE signature)
{
char buf[1024];
socklen_t len = sizeof buf;
try {
if (evma_get_sockname (NUM2BSIG (signature), (struct sockaddr*)buf, &len)) {
return rb_str_new (buf, len);
}
} catch (std::runtime_error e) {
rb_raise (rb_eRuntimeError, "%s", e.what());
}
return Qnil;
}
/********************
t_get_subprocess_pid
********************/
static VALUE t_get_subprocess_pid (VALUE self UNUSED, VALUE signature)
{
pid_t pid;
if (evma_get_subprocess_pid (NUM2BSIG (signature), &pid)) {
return INT2NUM (pid);
}
return Qnil;
}
/***********************
t_get_subprocess_status
***********************/
static VALUE t_get_subprocess_status (VALUE self UNUSED, VALUE signature)
{
VALUE proc_status = Qnil;
int status;
pid_t pid;
if (evma_get_subprocess_status (NUM2BSIG (signature), &status)) {
if (evma_get_subprocess_pid (NUM2BSIG (signature), &pid)) {
#ifdef IS_RUBY_3_OR_LATER
struct rb_process_status *data = NULL;
/* Defined to match static definition from MRI Ruby 3.0 process.c
*
* Older C++ compilers before GCC 8 don't allow static initialization of a
* struct without every field specified, so the definition here is at runtime
*/
static rb_data_type_t rb_process_status_type;
rb_process_status_type.wrap_struct_name = "Process::Status";
rb_process_status_type.function.dfree = RUBY_DEFAULT_FREE;
rb_process_status_type.flags = RUBY_TYPED_FREE_IMMEDIATELY;
proc_status = TypedData_Make_Struct(rb_cProcessStatus, struct rb_process_status, &rb_process_status_type, data);
data->pid = pid;
data->status = status;
#else
proc_status = rb_obj_alloc(rb_cProcessStatus);
/* MRI Ruby uses hidden instance vars */
rb_ivar_set(proc_status, rb_intern_const("status"), INT2FIX(status));
rb_ivar_set(proc_status, rb_intern_const("pid"), INT2FIX(pid));
#endif
#ifdef RUBINIUS
/* Rubinius uses standard instance vars */
rb_iv_set(proc_status, "@pid", INT2FIX(pid));
if (WIFEXITED(status)) {
rb_iv_set(proc_status, "@status", INT2FIX(WEXITSTATUS(status)));
} else if (WIFSIGNALED(status)) {
rb_iv_set(proc_status, "@termsig", INT2FIX(WTERMSIG(status)));
} else if (WIFSTOPPED(status)) {
rb_iv_set(proc_status, "@stopsig", INT2FIX(WSTOPSIG(status)));
}
#endif
}
}
rb_obj_freeze(proc_status);
return proc_status;
}
/**********************
t_get_connection_count
**********************/
static VALUE t_get_connection_count (VALUE self UNUSED)
{
return INT2NUM(evma_get_connection_count());
}
/*****************************
t_get_comm_inactivity_timeout
*****************************/
static VALUE t_get_comm_inactivity_timeout (VALUE self UNUSED, VALUE signature)
{
return rb_float_new(evma_get_comm_inactivity_timeout(NUM2BSIG (signature)));
}
/*****************************
t_set_comm_inactivity_timeout
*****************************/
static VALUE t_set_comm_inactivity_timeout (VALUE self UNUSED, VALUE signature, VALUE timeout)
{
float ti = RFLOAT_VALUE(timeout);
if (evma_set_comm_inactivity_timeout(NUM2BSIG(signature), ti)) {
return Qtrue;
}
return Qfalse;
}
/*****************************
t_get_pending_connect_timeout
*****************************/
static VALUE t_get_pending_connect_timeout (VALUE self UNUSED, VALUE signature)
{
return rb_float_new(evma_get_pending_connect_timeout(NUM2BSIG (signature)));
}
/*****************************
t_set_pending_connect_timeout
*****************************/
static VALUE t_set_pending_connect_timeout (VALUE self UNUSED, VALUE signature, VALUE timeout)
{
float ti = RFLOAT_VALUE(timeout);
if (evma_set_pending_connect_timeout(NUM2BSIG(signature), ti)) {
return Qtrue;
}
return Qfalse;
}
/***************
t_send_datagram
***************/
static VALUE t_send_datagram (VALUE self UNUSED, VALUE signature, VALUE data, VALUE data_length, VALUE address, VALUE port)
{
int b = evma_send_datagram (NUM2BSIG (signature), StringValuePtr (data), FIX2INT (data_length), StringValueCStr(address), FIX2INT(port));
if (b < 0)
rb_raise (EM_eConnectionError, "%s", "error in sending datagram"); // FIXME: this could be more specific.
return INT2NUM (b);
}
/******************
t_close_connection
******************/
static VALUE t_close_connection (VALUE self UNUSED, VALUE signature, VALUE after_writing)
{
evma_close_connection (NUM2BSIG (signature), ((after_writing == Qtrue) ? 1 : 0));
return Qnil;
}
/********************************
t_report_connection_error_status
********************************/
static VALUE t_report_connection_error_status (VALUE self UNUSED, VALUE signature)
{
int b = evma_report_connection_error_status (NUM2BSIG (signature));
return INT2NUM (b);
}
/****************
t_connect_server
****************/
static VALUE t_connect_server (VALUE self UNUSED, VALUE server, VALUE port)
{
// Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
// Specifically, if the value of port comes in as a string rather than an integer,
// NUM2INT will throw a type error, but FIX2INT will generate garbage.
try {
const uintptr_t f = evma_connect_to_server (NULL, 0, StringValueCStr(server), NUM2INT(port));
if (!f)
rb_raise (EM_eConnectionError, "%s", "no connection");
return BSIG2NUM (f);
} catch (std::runtime_error e) {
rb_raise (EM_eConnectionError, "%s", e.what());
}
return Qnil;
}
/*********************
t_bind_connect_server
*********************/
static VALUE t_bind_connect_server (VALUE self UNUSED, VALUE bind_addr, VALUE bind_port, VALUE server, VALUE port)
{
// Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
// Specifically, if the value of port comes in as a string rather than an integer,
// NUM2INT will throw a type error, but FIX2INT will generate garbage.
try {
const uintptr_t f = evma_connect_to_server (StringValueCStr(bind_addr), NUM2INT(bind_port), StringValueCStr(server), NUM2INT(port));
if (!f)
rb_raise (EM_eConnectionError, "%s", "no connection");
return BSIG2NUM (f);
} catch (std::runtime_error e) {
rb_raise (EM_eConnectionError, "%s", e.what());
}
return Qnil;
}
/*********************
t_connect_unix_server
*********************/
static VALUE t_connect_unix_server (VALUE self UNUSED, VALUE serversocket)
{
const uintptr_t f = evma_connect_to_unix_server (StringValueCStr(serversocket));
if (!f)
rb_raise (rb_eRuntimeError, "%s", "no connection");
return BSIG2NUM (f);
}
/***********
t_attach_fd
***********/
static VALUE t_attach_fd (VALUE self UNUSED, VALUE file_descriptor, VALUE watch_mode)
{
const uintptr_t f = evma_attach_fd (NUM2INT(file_descriptor), watch_mode == Qtrue);
if (!f)
rb_raise (rb_eRuntimeError, "%s", "no connection");
return BSIG2NUM (f);
}
/***********
t_detach_fd
***********/
static VALUE t_detach_fd (VALUE self UNUSED, VALUE signature)
{
return INT2NUM(evma_detach_fd (NUM2BSIG (signature)));
}
/*********************
t_get_file_descriptor
*********************/
static VALUE t_get_file_descriptor (VALUE self UNUSED, VALUE signature)
{
return INT2NUM(evma_get_file_descriptor (NUM2BSIG (signature)));
}
/**************
t_get_sock_opt
**************/
static VALUE t_get_sock_opt (VALUE self UNUSED, VALUE signature, VALUE lev, VALUE optname)
{
int fd = evma_get_file_descriptor (NUM2BSIG (signature));
int level = NUM2INT(lev), option = NUM2INT(optname);
socklen_t len = 128;
char buf[128];
if (getsockopt(fd, level, option, buf, &len) < 0)
rb_sys_fail("getsockopt");
return rb_str_new(buf, len);
}
/**************
t_set_sock_opt
**************/
static VALUE t_set_sock_opt (VALUE self UNUSED, VALUE signature, VALUE lev, VALUE optname, VALUE optval)
{
int fd = evma_get_file_descriptor (NUM2BSIG (signature));
int level = NUM2INT(lev), option = NUM2INT(optname);
int i;
const void *v;
socklen_t len;
switch (TYPE(optval)) {
case T_FIXNUM:
i = FIX2INT(optval);
goto numval;
case T_FALSE:
i = 0;
goto numval;
case T_TRUE:
i = 1;
numval:
v = (void*)&i; len = sizeof(i);
break;
default:
StringValue(optval);
v = RSTRING_PTR(optval);
len = RSTRING_LENINT(optval);
break;
}
if (setsockopt(fd, level, option, (char *)v, len) < 0)
rb_sys_fail("setsockopt");
return INT2FIX(0);
}
/********************
t_is_notify_readable
********************/
static VALUE t_is_notify_readable (VALUE self UNUSED, VALUE signature)
{
return evma_is_notify_readable(NUM2BSIG (signature)) ? Qtrue : Qfalse;
}
/*********************
t_set_notify_readable
*********************/
static VALUE t_set_notify_readable (VALUE self UNUSED, VALUE signature, VALUE mode)
{
evma_set_notify_readable(NUM2BSIG(signature), mode == Qtrue);
return Qnil;
}
/********************
t_is_notify_readable
********************/
static VALUE t_is_notify_writable (VALUE self UNUSED, VALUE signature)
{
return evma_is_notify_writable(NUM2BSIG (signature)) ? Qtrue : Qfalse;
}
/*********************
t_set_notify_writable
*********************/
static VALUE t_set_notify_writable (VALUE self UNUSED, VALUE signature, VALUE mode)
{
evma_set_notify_writable(NUM2BSIG (signature), mode == Qtrue);
return Qnil;
}
/*******
t_pause
*******/
static VALUE t_pause (VALUE self UNUSED, VALUE signature)
{
return evma_pause(NUM2BSIG (signature)) ? Qtrue : Qfalse;
}
/********
t_resume
********/
static VALUE t_resume (VALUE self UNUSED, VALUE signature)
{
return evma_resume(NUM2BSIG (signature)) ? Qtrue : Qfalse;
}
/**********
t_paused_p
**********/
static VALUE t_paused_p (VALUE self UNUSED, VALUE signature)
{
return evma_is_paused(NUM2BSIG (signature)) ? Qtrue : Qfalse;
}
/*********************
t_num_close_scheduled
*********************/
static VALUE t_num_close_scheduled (VALUE self UNUSED)
{
return INT2FIX(evma_num_close_scheduled());
}
/*****************
t_open_udp_socket
*****************/
static VALUE t_open_udp_socket (VALUE self UNUSED, VALUE server, VALUE port)
{
const uintptr_t f = evma_open_datagram_socket (StringValueCStr(server), FIX2INT(port));
if (!f)
rb_raise (rb_eRuntimeError, "%s", "no datagram socket");
return BSIG2NUM(f);
}
/*****************
t_release_machine
*****************/
static VALUE t_release_machine (VALUE self UNUSED)
{
evma_release_library();
return Qnil;
}
/******
t_stop
******/
static VALUE t_stop (VALUE self UNUSED)
{
evma_stop_machine();
return Qnil;
}
/******************
t_signal_loopbreak
******************/
static VALUE t_signal_loopbreak (VALUE self UNUSED)
{
evma_signal_loopbreak();
return Qnil;
}
/**************
t_library_type
**************/
static VALUE t_library_type (VALUE self UNUSED)
{
return rb_eval_string (":extension");
}
/*******************
t_set_timer_quantum
*******************/
static VALUE t_set_timer_quantum (VALUE self UNUSED, VALUE interval)
{
evma_set_timer_quantum (FIX2INT (interval));
return Qnil;
}
/********************
t_get_max_timer_count
********************/
static VALUE t_get_max_timer_count (VALUE self UNUSED)
{
return INT2FIX (evma_get_max_timer_count());
}
/********************
t_set_max_timer_count
********************/
static VALUE t_set_max_timer_count (VALUE self UNUSED, VALUE ct)
{
evma_set_max_timer_count (FIX2INT (ct));
return Qnil;
}
/********************
t_get/set_simultaneous_accept_count
********************/
static VALUE t_get_simultaneous_accept_count (VALUE self UNUSED)
{
return INT2FIX (evma_get_simultaneous_accept_count());
}
static VALUE t_set_simultaneous_accept_count (VALUE self UNUSED, VALUE ct)
{
evma_set_simultaneous_accept_count (FIX2INT (ct));
return Qnil;
}
/***************
t_setuid_string
***************/
static VALUE t_setuid_string (VALUE self UNUSED, VALUE username)
{
evma_setuid_string (StringValueCStr (username));
return Qnil;
}
/**************
t_invoke_popen
**************/
static VALUE t_invoke_popen (VALUE self UNUSED, VALUE cmd)
{
#ifdef OS_WIN32
rb_raise (EM_eUnsupported, "popen is not available on this platform");
#endif
int len = RARRAY_LEN(cmd);
if (len >= 2048)
rb_raise (rb_eRuntimeError, "%s", "too many arguments to popen");
char *strings [2048];
for (int i=0; i < len; i++) {
VALUE ix = INT2FIX (i);
VALUE s = rb_ary_aref (1, &ix, cmd);
strings[i] = StringValueCStr (s);
}
strings[len] = NULL;
uintptr_t f = 0;
try {
f = evma_popen (strings);
} catch (std::runtime_error e) {
rb_raise (rb_eRuntimeError, "%s", e.what());
}
if (!f) {
char *err = strerror (errno);
char buf[100];
memset (buf, 0, sizeof(buf));
snprintf (buf, sizeof(buf)-1, "no popen: %s", (err?err:"???"));
rb_raise (rb_eRuntimeError, "%s", buf);
}
return BSIG2NUM (f);
}
/***************
t_read_keyboard
***************/
static VALUE t_read_keyboard (VALUE self UNUSED)
{
const uintptr_t f = evma_open_keyboard();
if (!f)
rb_raise (rb_eRuntimeError, "%s", "no keyboard reader");
return BSIG2NUM (f);
}
/****************
t_watch_filename
****************/
static VALUE t_watch_filename (VALUE self UNUSED, VALUE fname)
{
try {
return BSIG2NUM(evma_watch_filename(StringValueCStr(fname)));
} catch (std::runtime_error e) {
rb_raise (EM_eUnsupported, "%s", e.what());
}
return Qnil;
}
/******************
t_unwatch_filename
******************/
static VALUE t_unwatch_filename (VALUE self UNUSED, VALUE sig)
{
try {
evma_unwatch_filename(NUM2BSIG (sig));
} catch (std::runtime_error e) {
rb_raise (EM_eInvalidSignature, "%s", e.what());
}
return Qnil;
}
/***********
t_watch_pid
***********/
static VALUE t_watch_pid (VALUE self UNUSED, VALUE pid)
{
try {
return BSIG2NUM(evma_watch_pid(NUM2INT(pid)));
} catch (std::runtime_error e) {
rb_raise (EM_eUnsupported, "%s", e.what());
}
return Qnil;
}
/*************
t_unwatch_pid
*************/
static VALUE t_unwatch_pid (VALUE self UNUSED, VALUE sig)
{
evma_unwatch_pid(NUM2BSIG (sig));
return Qnil;
}
/*************
t_watch_only_p
*************/
static VALUE t_watch_only_p (VALUE self UNUSED, VALUE signature)
{
return evma_is_watch_only(NUM2BSIG (signature)) ? Qtrue : Qfalse;
}
/**********
t__epoll_p
**********/
static VALUE t__epoll_p (VALUE self UNUSED)
{
#ifdef HAVE_EPOLL
return Qtrue;
#else
return Qfalse;
#endif
}
/********
t__epoll
********/
static VALUE t__epoll (VALUE self UNUSED)
{
if (t__epoll_p(self) == Qfalse)
return Qfalse;
evma_set_epoll (1);
return Qtrue;
}
/***********
t__epoll_set
***********/
static VALUE t__epoll_set (VALUE self, VALUE val)
{
if (t__epoll_p(self) == Qfalse && val == Qtrue)
rb_raise (EM_eUnsupported, "%s", "epoll is not supported on this platform");
evma_set_epoll (val == Qtrue ? 1 : 0);
return val;
}
/***********
t__kqueue_p
***********/
static VALUE t__kqueue_p (VALUE self UNUSED)
{
#ifdef HAVE_KQUEUE
return Qtrue;
#else
return Qfalse;
#endif
}
/*********
t__kqueue
*********/
static VALUE t__kqueue (VALUE self UNUSED)
{
if (t__kqueue_p(self) == Qfalse)
return Qfalse;
evma_set_kqueue (1);
return Qtrue;
}
/*************
t__kqueue_set
*************/
static VALUE t__kqueue_set (VALUE self, VALUE val)
{
if (t__kqueue_p(self) == Qfalse && val == Qtrue)
rb_raise (EM_eUnsupported, "%s", "kqueue is not supported on this platform");
evma_set_kqueue (val == Qtrue ? 1 : 0);
return val;
}
/********
t__ssl_p
********/
static VALUE t__ssl_p (VALUE self UNUSED)
{
#ifdef WITH_SSL
return Qtrue;
#else
return Qfalse;
#endif
}
/********
t_stopping
********/
static VALUE t_stopping ()
{
if (evma_stopping()) {
return Qtrue;
} else {
return Qfalse;
}
}
/****************
t_send_file_data
****************/
static VALUE t_send_file_data (VALUE self UNUSED, VALUE signature, VALUE filename)
{
/* The current implementation of evma_send_file_data_to_connection enforces a strict
* upper limit on the file size it will transmit (currently 32K). The function returns
* zero on success, -1 if the requested file exceeds its size limit, and a positive
* number for other errors.
* TODO: Positive return values are actually errno's, which is probably the wrong way to
* do this. For one thing it's ugly. For another, we can't be sure zero is never a real errno.
*/
int b = evma_send_file_data_to_connection (NUM2BSIG (signature), StringValueCStr(filename));
if (b == -1)
rb_raise(rb_eRuntimeError, "%s", "File too large. send_file_data() supports files under 32k.");
if (b > 0) {
char *err = strerror (b);
char buf[1024];
memset (buf, 0, sizeof(buf));
snprintf (buf, sizeof(buf)-1, ": %s %s", StringValueCStr(filename),(err?err:"???"));
rb_raise (rb_eIOError, "%s", buf);
}
return INT2NUM (0);
}
/*******************
t_set_rlimit_nofile
*******************/
static VALUE t_set_rlimit_nofile (VALUE self UNUSED, VALUE arg)
{
int arg_int = (NIL_P(arg)) ? -1 : NUM2INT (arg);
return INT2NUM (evma_set_rlimit_nofile (arg_int));
}
/***************************
conn_get_outbound_data_size
***************************/
static VALUE conn_get_outbound_data_size (VALUE self)
{
VALUE sig = rb_ivar_get (self, Intern_at_signature);
return INT2NUM (evma_get_outbound_data_size (NUM2BSIG (sig)));
}
/******************************
conn_associate_callback_target
******************************/
static VALUE conn_associate_callback_target (VALUE self UNUSED, VALUE sig UNUSED)
{
// No-op for the time being.
return Qnil;
}
/******************
t_enable_keepalive
******************/
static VALUE t_enable_keepalive (int argc, VALUE *argv, VALUE self)
{
VALUE idle, intvl, cnt;
rb_scan_args(argc, argv, "03", &idle, &intvl, &cnt);
// In ed.cpp, skip 0 values before calling setsockopt
int i_idle = NIL_P(idle) ? 0 : NUM2INT(idle);
int i_intvl = NIL_P(intvl) ? 0 : NUM2INT(intvl);
int i_cnt = NIL_P(cnt) ? 0 : NUM2INT(cnt);
VALUE sig = rb_ivar_get (self, Intern_at_signature);
try {
return INT2NUM (evma_enable_keepalive(NUM2ULONG(sig), i_idle, i_intvl, i_cnt));
} catch (std::runtime_error e) {
rb_raise (rb_eRuntimeError, "%s", e.what());
}
}
/******************
t_disable_keepalive
******************/
static VALUE t_disable_keepalive (VALUE self)
{
VALUE sig = rb_ivar_get (self, Intern_at_signature);
try {
return INT2NUM (evma_disable_keepalive(NUM2ULONG(sig)));
} catch (std::runtime_error e) {
rb_raise (rb_eRuntimeError, "%s", e.what());
}
}
/***************
t_get_loop_time
****************/
static VALUE t_get_loop_time (VALUE self UNUSED)
{
uint64_t current_time = evma_get_current_loop_time();
if (current_time == 0) {
return Qnil;
}
// Generally the industry has moved to 64-bit time_t, this is just in case we're 32-bit time_t.
if (sizeof(time_t) < 8 && current_time > INT_MAX) {
return rb_funcall(rb_cTime, Intern_at, 2, INT2NUM(current_time / 1000000), INT2NUM(current_time % 1000000));
} else {
return rb_time_new(current_time / 1000000, current_time % 1000000);
}
}
/*************
t_start_proxy
**************/
static VALUE t_start_proxy (VALUE self UNUSED, VALUE from, VALUE to, VALUE bufsize, VALUE length)
{
try {
evma_start_proxy(NUM2BSIG (from), NUM2BSIG (to), NUM2ULONG(bufsize), NUM2ULONG(length));
} catch (std::runtime_error e) {
rb_raise (EM_eConnectionError, "%s", e.what());
}
return Qnil;
}
/************
t_stop_proxy
*************/
static VALUE t_stop_proxy (VALUE self UNUSED, VALUE from)
{
try{
evma_stop_proxy(NUM2BSIG (from));
} catch (std::runtime_error e) {
rb_raise (EM_eConnectionError, "%s", e.what());
}
return Qnil;
}
/***************
t_proxied_bytes
****************/
static VALUE t_proxied_bytes (VALUE self UNUSED, VALUE from)
{
try{
return BSIG2NUM(evma_proxied_bytes(NUM2BSIG (from)));
} catch (std::runtime_error e) {
rb_raise (EM_eConnectionError, "%s", e.what());
}
return Qnil;
}
/***************
t_get_idle_time
****************/
static VALUE t_get_idle_time (VALUE self UNUSED, VALUE from)
{
try{
uint64_t current_time = evma_get_current_loop_time();
uint64_t time = evma_get_last_activity_time(NUM2BSIG (from));
if (current_time != 0 && time != 0) {
if (time >= current_time)
return BSIG2NUM(0);
else {
uint64_t diff = current_time - time;
float seconds = diff / (1000.0*1000.0);
return rb_float_new(seconds);
}
return Qnil;
}
} catch (std::runtime_error e) {
rb_raise (EM_eConnectionError, "%s", e.what());
}
return Qnil;
}
/************************
t_get_heartbeat_interval
*************************/
static VALUE t_get_heartbeat_interval (VALUE self UNUSED)
{
return rb_float_new(evma_get_heartbeat_interval());
}
/************************
t_set_heartbeat_interval
*************************/
static VALUE t_set_heartbeat_interval (VALUE self UNUSED, VALUE interval)
{
float iv = RFLOAT_VALUE(interval);
if (evma_set_heartbeat_interval(iv))
return Qtrue;
return Qfalse;
}
/*********************
Init_rubyeventmachine
*********************/
extern "C" void Init_rubyeventmachine()
{
// Lookup Process::Status for get_subprocess_status
VALUE rb_mProcess = rb_const_get(rb_cObject, rb_intern("Process"));
rb_cProcessStatus = rb_const_get(rb_mProcess, rb_intern("Status"));
// Tuck away some symbol values so we don't have to look 'em up every time we need 'em.
Intern_at_signature = rb_intern ("@signature");
Intern_at_timers = rb_intern ("@timers");
Intern_at_conns = rb_intern ("@conns");
Intern_at_error_handler = rb_intern("@error_handler");
Intern_event_callback = rb_intern ("event_callback");
Intern_run_deferred_callbacks = rb_intern ("run_deferred_callbacks");
Intern_delete = rb_intern ("delete");
Intern_call = rb_intern ("call");
Intern_at = rb_intern("at");
Intern_receive_data = rb_intern ("receive_data");
Intern_ssl_handshake_completed = rb_intern ("ssl_handshake_completed");
Intern_ssl_verify_peer = rb_intern ("ssl_verify_peer");
Intern_notify_readable = rb_intern ("notify_readable");
Intern_notify_writable = rb_intern ("notify_writable");
Intern_proxy_target_unbound = rb_intern ("proxy_target_unbound");
Intern_proxy_completed = rb_intern ("proxy_completed");
Intern_connection_completed = rb_intern ("connection_completed");
// INCOMPLETE, we need to define class Connections inside module EventMachine
// run_machine and run_machine_without_threads are now identical.
// Must deprecate the without_threads variant.
EmModule = rb_define_module ("EventMachine");
EmConnection = rb_define_class_under (EmModule, "Connection", rb_cObject);
rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eRuntimeError);
EM_eConnectionError = rb_define_class_under (EmModule, "ConnectionError", rb_eRuntimeError);
EM_eConnectionNotBound = rb_define_class_under (EmModule, "ConnectionNotBound", rb_eRuntimeError);
EM_eUnknownTimerFired = rb_define_class_under (EmModule, "UnknownTimerFired", rb_eRuntimeError);
EM_eUnsupported = rb_define_class_under (EmModule, "Unsupported", rb_eRuntimeError);
EM_eInvalidSignature = rb_define_class_under (EmModule, "InvalidSignature", rb_eRuntimeError);
EM_eInvalidPrivateKey = rb_define_class_under (EmModule, "InvalidPrivateKey", rb_eRuntimeError);
rb_define_module_function (EmModule, "initialize_event_machine", (VALUE(*)(...))t_initialize_event_machine, 0);
rb_define_module_function (EmModule, "run_machine_once", (VALUE(*)(...))t_run_machine_once, 0);
rb_define_module_function (EmModule, "run_machine", (VALUE(*)(...))t_run_machine, 0);
rb_define_module_function (EmModule, "run_machine_without_threads", (VALUE(*)(...))t_run_machine, 0);
rb_define_module_function (EmModule, "get_timer_count", (VALUE(*)(...))t_get_timer_count, 0);
rb_define_module_function (EmModule, "add_oneshot_timer", (VALUE(*)(...))t_add_oneshot_timer, 1);
rb_define_module_function (EmModule, "start_tcp_server", (VALUE(*)(...))t_start_server, 2);
rb_define_module_function (EmModule, "stop_tcp_server", (VALUE(*)(...))t_stop_server, 1);
rb_define_module_function (EmModule, "start_unix_server", (VALUE(*)(...))t_start_unix_server, 1);
rb_define_module_function (EmModule, "attach_sd", (VALUE(*)(...))t_attach_sd, 1);
rb_define_module_function (EmModule, "set_tls_parms", (VALUE(*)(...))t_set_tls_parms, 13);
rb_define_module_function (EmModule, "start_tls", (VALUE(*)(...))t_start_tls, 1);
rb_define_module_function (EmModule, "get_peer_cert", (VALUE(*)(...))t_get_peer_cert, 1);
rb_define_module_function (EmModule, "get_cipher_bits", (VALUE(*)(...))t_get_cipher_bits, 1);
rb_define_module_function (EmModule, "get_cipher_name", (VALUE(*)(...))t_get_cipher_name, 1);
rb_define_module_function (EmModule, "get_cipher_protocol", (VALUE(*)(...))t_get_cipher_protocol, 1);
rb_define_module_function (EmModule, "get_sni_hostname", (VALUE(*)(...))t_get_sni_hostname, 1);
rb_define_module_function (EmModule, "send_data", (VALUE(*)(...))t_send_data, 3);
rb_define_module_function (EmModule, "send_datagram", (VALUE(*)(...))t_send_datagram, 5);
rb_define_module_function (EmModule, "close_connection", (VALUE(*)(...))t_close_connection, 2);
rb_define_module_function (EmModule, "report_connection_error_status", (VALUE(*)(...))t_report_connection_error_status, 1);
rb_define_module_function (EmModule, "connect_server", (VALUE(*)(...))t_connect_server, 2);
rb_define_module_function (EmModule, "bind_connect_server", (VALUE(*)(...))t_bind_connect_server, 4);
rb_define_module_function (EmModule, "connect_unix_server", (VALUE(*)(...))t_connect_unix_server, 1);
rb_define_module_function (EmModule, "attach_fd", (VALUE (*)(...))t_attach_fd, 2);
rb_define_module_function (EmModule, "detach_fd", (VALUE (*)(...))t_detach_fd, 1);
rb_define_module_function (EmModule, "get_file_descriptor", (VALUE (*)(...))t_get_file_descriptor, 1);
rb_define_module_function (EmModule, "get_sock_opt", (VALUE (*)(...))t_get_sock_opt, 3);
rb_define_module_function (EmModule, "set_sock_opt", (VALUE (*)(...))t_set_sock_opt, 4);
rb_define_module_function (EmModule, "set_notify_readable", (VALUE (*)(...))t_set_notify_readable, 2);
rb_define_module_function (EmModule, "set_notify_writable", (VALUE (*)(...))t_set_notify_writable, 2);
rb_define_module_function (EmModule, "is_notify_readable", (VALUE (*)(...))t_is_notify_readable, 1);
rb_define_module_function (EmModule, "is_notify_writable", (VALUE (*)(...))t_is_notify_writable, 1);
rb_define_module_function (EmModule, "pause_connection", (VALUE (*)(...))t_pause, 1);
rb_define_module_function (EmModule, "resume_connection", (VALUE (*)(...))t_resume, 1);
rb_define_module_function (EmModule, "connection_paused?", (VALUE (*)(...))t_paused_p, 1);
rb_define_module_function (EmModule, "num_close_scheduled", (VALUE (*)(...))t_num_close_scheduled, 0);
rb_define_module_function (EmModule, "start_proxy", (VALUE (*)(...))t_start_proxy, 4);
rb_define_module_function (EmModule, "stop_proxy", (VALUE (*)(...))t_stop_proxy, 1);
rb_define_module_function (EmModule, "get_proxied_bytes", (VALUE (*)(...))t_proxied_bytes, 1);
rb_define_module_function (EmModule, "watch_filename", (VALUE (*)(...))t_watch_filename, 1);
rb_define_module_function (EmModule, "unwatch_filename", (VALUE (*)(...))t_unwatch_filename, 1);
rb_define_module_function (EmModule, "watch_pid", (VALUE (*)(...))t_watch_pid, 1);
rb_define_module_function (EmModule, "unwatch_pid", (VALUE (*)(...))t_unwatch_pid, 1);
rb_define_module_function (EmModule, "watch_only?", (VALUE (*)(...))t_watch_only_p, 1);
rb_define_module_function (EmModule, "current_time", (VALUE(*)(...))t_get_loop_time, 0);
rb_define_module_function (EmModule, "open_udp_socket", (VALUE(*)(...))t_open_udp_socket, 2);
rb_define_module_function (EmModule, "read_keyboard", (VALUE(*)(...))t_read_keyboard, 0);
rb_define_module_function (EmModule, "release_machine", (VALUE(*)(...))t_release_machine, 0);
rb_define_module_function (EmModule, "stop", (VALUE(*)(...))t_stop, 0);
rb_define_module_function (EmModule, "signal_loopbreak", (VALUE(*)(...))t_signal_loopbreak, 0);
rb_define_module_function (EmModule, "library_type", (VALUE(*)(...))t_library_type, 0);
rb_define_module_function (EmModule, "set_timer_quantum", (VALUE(*)(...))t_set_timer_quantum, 1);
rb_define_module_function (EmModule, "get_max_timer_count", (VALUE(*)(...))t_get_max_timer_count, 0);
rb_define_module_function (EmModule, "set_max_timer_count", (VALUE(*)(...))t_set_max_timer_count, 1);
rb_define_module_function (EmModule, "get_simultaneous_accept_count", (VALUE(*)(...))t_get_simultaneous_accept_count, 0);
rb_define_module_function (EmModule, "set_simultaneous_accept_count", (VALUE(*)(...))t_set_simultaneous_accept_count, 1);
rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1);
rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1);
rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 2);
rb_define_module_function (EmModule, "get_heartbeat_interval", (VALUE(*)(...))t_get_heartbeat_interval, 0);
rb_define_module_function (EmModule, "set_heartbeat_interval", (VALUE(*)(...))t_set_heartbeat_interval, 1);
rb_define_module_function (EmModule, "get_idle_time", (VALUE(*)(...))t_get_idle_time, 1);
rb_define_module_function (EmModule, "get_peername", (VALUE(*)(...))t_get_peername, 1);
rb_define_module_function (EmModule, "get_sockname", (VALUE(*)(...))t_get_sockname, 1);
rb_define_module_function (EmModule, "get_subprocess_pid", (VALUE(*)(...))t_get_subprocess_pid, 1);
rb_define_module_function (EmModule, "get_subprocess_status", (VALUE(*)(...))t_get_subprocess_status, 1);
rb_define_module_function (EmModule, "get_comm_inactivity_timeout", (VALUE(*)(...))t_get_comm_inactivity_timeout, 1);
rb_define_module_function (EmModule, "set_comm_inactivity_timeout", (VALUE(*)(...))t_set_comm_inactivity_timeout, 2);
rb_define_module_function (EmModule, "get_pending_connect_timeout", (VALUE(*)(...))t_get_pending_connect_timeout, 1);
rb_define_module_function (EmModule, "set_pending_connect_timeout", (VALUE(*)(...))t_set_pending_connect_timeout, 2);
rb_define_module_function (EmModule, "set_rlimit_nofile", (VALUE(*)(...))t_set_rlimit_nofile, 1);
rb_define_module_function (EmModule, "get_connection_count", (VALUE(*)(...))t_get_connection_count, 0);
rb_define_module_function (EmModule, "epoll", (VALUE(*)(...))t__epoll, 0);
rb_define_module_function (EmModule, "epoll=", (VALUE(*)(...))t__epoll_set, 1);
rb_define_module_function (EmModule, "epoll?", (VALUE(*)(...))t__epoll_p, 0);
rb_define_module_function (EmModule, "kqueue", (VALUE(*)(...))t__kqueue, 0);
rb_define_module_function (EmModule, "kqueue=", (VALUE(*)(...))t__kqueue_set, 1);
rb_define_module_function (EmModule, "kqueue?", (VALUE(*)(...))t__kqueue_p, 0);
rb_define_module_function (EmModule, "ssl?", (VALUE(*)(...))t__ssl_p, 0);
rb_define_module_function(EmModule, "stopping?",(VALUE(*)(...))t_stopping, 0);
rb_define_method (EmConnection, "get_outbound_data_size", (VALUE(*)(...))conn_get_outbound_data_size, 0);
rb_define_method (EmConnection, "associate_callback_target", (VALUE(*)(...))conn_associate_callback_target, 1);
rb_define_method (EmConnection, "enable_keepalive", (VALUE(*)(...))t_enable_keepalive, -1);
rb_define_method (EmConnection, "disable_keepalive", (VALUE(*)(...))t_disable_keepalive, 0);
// Connection states
rb_define_const (EmModule, "TimerFired", INT2NUM(EM_TIMER_FIRED ));
rb_define_const (EmModule, "ConnectionData", INT2NUM(EM_CONNECTION_READ ));
rb_define_const (EmModule, "ConnectionUnbound", INT2NUM(EM_CONNECTION_UNBOUND ));
rb_define_const (EmModule, "ConnectionAccepted", INT2NUM(EM_CONNECTION_ACCEPTED ));
rb_define_const (EmModule, "ConnectionCompleted", INT2NUM(EM_CONNECTION_COMPLETED ));
rb_define_const (EmModule, "LoopbreakSignalled", INT2NUM(EM_LOOPBREAK_SIGNAL ));
rb_define_const (EmModule, "ConnectionNotifyReadable", INT2NUM(EM_CONNECTION_NOTIFY_READABLE));
rb_define_const (EmModule, "ConnectionNotifyWritable", INT2NUM(EM_CONNECTION_NOTIFY_WRITABLE));
rb_define_const (EmModule, "SslHandshakeCompleted", INT2NUM(EM_SSL_HANDSHAKE_COMPLETED ));
rb_define_const (EmModule, "SslVerify", INT2NUM(EM_SSL_VERIFY ));
// EM_PROXY_TARGET_UNBOUND = 110,
// EM_PROXY_COMPLETED = 111
// SSL Protocols
rb_define_const (EmModule, "EM_PROTO_SSLv2", INT2NUM(EM_PROTO_SSLv2 ));
rb_define_const (EmModule, "EM_PROTO_SSLv3", INT2NUM(EM_PROTO_SSLv3 ));
rb_define_const (EmModule, "EM_PROTO_TLSv1", INT2NUM(EM_PROTO_TLSv1 ));
rb_define_const (EmModule, "EM_PROTO_TLSv1_1", INT2NUM(EM_PROTO_TLSv1_1));
rb_define_const (EmModule, "EM_PROTO_TLSv1_2", INT2NUM(EM_PROTO_TLSv1_2));
#ifdef TLS1_3_VERSION
rb_define_const (EmModule, "EM_PROTO_TLSv1_3", INT2NUM(EM_PROTO_TLSv1_3));
#endif
#ifdef OPENSSL_NO_SSL3
/* True if SSL3 is not available */
rb_define_const (EmModule, "OPENSSL_NO_SSL3", Qtrue);
rb_define_const (EmModule, "OPENSSL_NO_SSL2", Qtrue);
#else
rb_define_const (EmModule, "OPENSSL_NO_SSL3", Qfalse);
#ifdef OPENSSL_NO_SSL2
rb_define_const (EmModule, "OPENSSL_NO_SSL2", Qtrue);
#else
rb_define_const (EmModule, "OPENSSL_NO_SSL2", Qfalse);
#endif
#endif
// OpenSSL Build / Runtime/Load versions
/* Version of OpenSSL that EventMachine was compiled with */
rb_define_const(EmModule, "OPENSSL_VERSION", rb_str_new2(OPENSSL_VERSION_TEXT));
#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000
/* Version of OpenSSL that EventMachine loaded with */
rb_define_const(EmModule, "OPENSSL_LIBRARY_VERSION", rb_str_new2(OpenSSL_version(OPENSSL_VERSION)));
#else
rb_define_const(EmModule, "OPENSSL_LIBRARY_VERSION", rb_str_new2(SSLeay_version(SSLEAY_VERSION)));
#endif
}
|