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 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
|
// $Id: Proactor_UDP_Test.cpp 94132 2011-06-01 05:53:39Z msmit $
// ============================================================================
/**
* @file Proactor_UDP_Test.cpp
*
* $Id: Proactor_UDP_Test.cpp 94132 2011-06-01 05:53:39Z msmit $
*
* This program illustrates how the ACE_Proactor can be used to
* implement an application that uses UDP/IP communications.
*
* @author Steve Huston <shuston@riverace.com>, based on Proactor_Test.cpp
*/
// ============================================================================
#include "test_config.h"
#if defined (ACE_HAS_THREADS) && (defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS))
// This only works on Win32 platforms and on Unix platforms
// supporting POSIX aio calls.
#include "ace/Signal.h"
#include "ace/Service_Config.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_CODgram.h"
#include "ace/SOCK_Dgram.h"
#include "ace/Object_Manager.h"
#include "ace/Get_Opt.h"
#include "ace/Proactor.h"
#include "ace/Task.h"
#include "ace/Thread_Semaphore.h"
#include "ace/OS_NS_ctype.h"
#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_signal.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_socket.h"
#include "ace/Sock_Connect.h"
#include "ace/os_include/netinet/os_tcp.h"
#include "ace/Atomic_Op.h"
#include "ace/Synch_Traits.h"
#if defined (ACE_WIN32)
# include "ace/WIN32_Proactor.h"
#elif defined (ACE_HAS_AIO_CALLS)
# include "ace/POSIX_Proactor.h"
# include "ace/POSIX_CB_Proactor.h"
# include "ace/SUN_Proactor.h"
#endif /* ACE_WIN32 */
// Proactor Type (UNIX only, Win32 ignored)
typedef enum { DEFAULT = 0, AIOCB, SIG, SUN, CB } ProactorType;
static ProactorType proactor_type = DEFAULT;
// POSIX : > 0 max number aio operations proactor,
static size_t max_aio_operations = 0;
// both: 0 run client or server / depends on host
// != 0 run client and server
static int both = 0;
// Host that we're connecting to.
static const ACE_TCHAR *host = 0;
// number of Client instances
static int clients = 1;
const int MAX_CLIENTS = 1000;
const int MAX_SERVERS = 1000;
// duplex mode: == 0 half-duplex
// != 0 full duplex
static int duplex = 0;
// number threads in the Proactor thread pool
static int threads = 1;
// Port that we're receiving session initiations on.
static u_short port = ACE_DEFAULT_SERVER_PORT;
// Log options
static int loglevel; // 0 full , 1 only errors
static size_t xfer_limit; // Number of bytes for Client to send.
static char complete_message[] =
"GET / HTTP/1.1\r\n"
"Accept: */*\r\n"
"Accept-Language: C++\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Proactor_Test/1.0 (non-compatible)\r\n"
"Connection: Keep-Alive\r\n"
"\r\n";
static char close_req_msg[] = "CLOSE";
static char close_ack_msg[] = "CLOSE-ACK";
class LogLocker
{
public:
LogLocker () { ACE_LOG_MSG->acquire (); }
virtual ~LogLocker () { ACE_LOG_MSG->release (); }
};
// Function to remove signals from the signal mask.
static int
disable_signal (int sigmin, int sigmax)
{
#if !defined (ACE_LACKS_UNIX_SIGNALS)
sigset_t signal_set;
if (ACE_OS::sigemptyset (&signal_set) == - 1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Error: (%P|%t):%p\n"),
ACE_TEXT ("sigemptyset failed")));
for (int i = sigmin; i <= sigmax; i++)
ACE_OS::sigaddset (&signal_set, i);
// Put the <signal_set>.
# if defined (ACE_LACKS_PTHREAD_THR_SIGSETMASK)
// In multi-threaded application this is not POSIX compliant
// but let's leave it just in case.
if (ACE_OS::sigprocmask (SIG_BLOCK, &signal_set, 0) != 0)
# else
if (ACE_OS::thr_sigsetmask (SIG_BLOCK, &signal_set, 0) != 0)
# endif /* ACE_LACKS_PTHREAD_THR_SIGSETMASK */
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Error: (%P|%t): %p\n"),
ACE_TEXT ("SIG_BLOCK failed")),
-1);
#else
ACE_UNUSED_ARG (sigmin);
ACE_UNUSED_ARG (sigmax);
#endif /* ACE_LACKS_UNIX_SIGNALS */
return 0;
}
// *************************************************************
// MyTask is ACE_Task resposible for :
// 1. creation and deletion of
// Proactor and Proactor thread pool
// 2. running Proactor event loop
// *************************************************************
/**
* @class MyTask
*
* MyTask plays role for Proactor threads pool
*
* MyTask is ACE_Task resposible for:
* 1. Creation and deletion of Proactor and Proactor thread pool
* 2. Running Proactor event loop
*/
class MyTask : public ACE_Task<ACE_MT_SYNCH>
{
public:
MyTask (void):
lock_ (),
sem_ ((unsigned int) 0),
proactor_(0) {}
virtual ~MyTask()
{
(void) this->stop ();
(void) this->delete_proactor();
}
virtual int svc (void);
int start (int num_threads,
ProactorType type_proactor,
size_t max_op );
int stop (void);
private:
int create_proactor (ProactorType type_proactor,
size_t max_op);
int delete_proactor (void);
ACE_SYNCH_RECURSIVE_MUTEX lock_;
ACE_Thread_Semaphore sem_;
ACE_Proactor * proactor_;
};
int
MyTask::create_proactor (ProactorType type_proactor, size_t max_op)
{
ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX,
monitor,
this->lock_,
-1);
ACE_TEST_ASSERT (this->proactor_ == 0);
#if defined (ACE_WIN32)
ACE_UNUSED_ARG (type_proactor);
ACE_UNUSED_ARG (max_op);
ACE_WIN32_Proactor *proactor_impl = 0;
ACE_NEW_RETURN (proactor_impl,
ACE_WIN32_Proactor,
-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%t) Create Proactor Type = WIN32\n")));
#elif defined (ACE_HAS_AIO_CALLS)
ACE_POSIX_Proactor * proactor_impl = 0;
switch (type_proactor)
{
case AIOCB:
ACE_NEW_RETURN (proactor_impl,
ACE_POSIX_AIOCB_Proactor (max_op),
-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Create Proactor Type = AIOCB\n")));
break;
#if defined(ACE_HAS_POSIX_REALTIME_SIGNALS)
case SIG:
ACE_NEW_RETURN (proactor_impl,
ACE_POSIX_SIG_Proactor (max_op),
-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Create Proactor Type = SIG\n")));
break;
#endif /* ACE_HAS_POSIX_REALTIME_SIGNALS */
# if defined (sun)
case SUN:
ACE_NEW_RETURN (proactor_impl,
ACE_SUN_Proactor (max_op),
-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%t) Create Proactor Type = SUN\n")));
break;
# endif /* sun */
# if !defined(ACE_HAS_BROKEN_SIGEVENT_STRUCT)
case CB:
ACE_NEW_RETURN (proactor_impl,
ACE_POSIX_CB_Proactor (max_op),
-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Create Proactor Type = CB\n")));
break;
# endif /* !ACE_HAS_BROKEN_SIGEVENT_STRUCT */
default:
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Create Proactor Type = DEFAULT\n")));
break;
}
#endif /* ACE_WIN32 */
// always delete implementation 1 , not !(proactor_impl == 0)
ACE_NEW_RETURN (this->proactor_,
ACE_Proactor (proactor_impl, 1 ),
-1);
// Set new singleton and delete it in close_singleton()
ACE_Proactor::instance (this->proactor_, 1);
return 0;
}
int
MyTask::delete_proactor (void)
{
ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX,
monitor,
this->lock_,
-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Delete Proactor\n")));
ACE_Proactor::close_singleton ();
this->proactor_ = 0;
return 0;
}
int
MyTask::start (int num_threads,
ProactorType type_proactor,
size_t max_op)
{
if (this->create_proactor (type_proactor, max_op) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("unable to create proactor")),
-1);
if (this->activate (THR_NEW_LWP, num_threads) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("unable to activate thread pool")),
-1);
for (; num_threads > 0; num_threads--)
{
sem_.acquire ();
}
return 0;
}
int
MyTask::stop ()
{
if (this->proactor_ != 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Calling End Proactor event loop\n")));
this->proactor_->proactor_end_event_loop ();
}
if (this->wait () == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("unable to stop thread pool")));
return 0;
}
int
MyTask::svc (void)
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) MyTask started\n")));
disable_signal (ACE_SIGRTMIN, ACE_SIGRTMAX);
disable_signal (SIGPIPE, SIGPIPE);
// signal that we are ready
sem_.release (1);
this->proactor_->proactor_run_event_loop ();
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) MyTask finished\n")));
return 0;
}
// forward declaration
class TestData;
// "Server" is one side of a session. It's the same idea as in TCP, but
// there's no acceptor in UDP; sessions are started by the client sending
// a "start" datagram to a well-known UDP port. The start datagram tells
// which port number the client is receiving on. The server then sends an
// "ack" datagram to indicate the session is set up successfully and to say
// which port the server is listening on. Thus, a unique pairing of server
// and client port numbers is established. Each session will require a
// separate server-side socket as well as the client. Note that experienced
// UDP programmers will be quivering at this point knowing that there's no
// reason to have multiple server-side sockets, and no real reason to
// pre-register the client ports either since all the addressing info is
// available on normal UDP programming. However, this is all necessary in
// the POSIX case since the POSIX aio functions were not designed with UDP
// in mind, and the addressing information is not available in UDP receive
// completion callbacks; thus, each socket needs to be fully connected before
// running session data. The addressing information needed to run this
// use-case in the "normal" way is available on Windows, but this test runs
// across many platforms, so can't rely on that information.
class Server : public ACE_Handler
{
public:
Server ();
Server (TestData *tester, int id);
~Server (void);
int id (void) { return this->id_; }
size_t get_total_snd (void) { return this->total_snd_; }
size_t get_total_rcv (void) { return this->total_rcv_; }
long get_total_w (void) { return this->total_w_; }
long get_total_r (void) { return this->total_r_; }
/// This is called after the new session has been established.
void go (ACE_HANDLE handle, const ACE_INET_Addr &client);
void cancel ();
protected:
/**
* @name AIO callback handling
*
* These methods are called by the framework
*/
/// This is called when asynchronous <read> operation from the
/// socket completes.
virtual void handle_read_dgram (const ACE_Asynch_Read_Dgram::Result &result);
/// This is called when an asynchronous <write> to the socket
/// completes.
virtual void handle_write_dgram (const ACE_Asynch_Write_Dgram::Result &result);
private:
int initiate_read (void);
int initiate_write (ACE_Message_Block *mb, size_t nbytes);
TestData *tester_;
int id_;
ACE_INET_Addr client_addr_;
ACE_Asynch_Read_Dgram rs_;
ACE_Asynch_Write_Dgram ws_;
ACE_SYNCH_MUTEX lock_;
int io_count_; // Number of currently outstanding I/O requests
bool flg_cancel_;
bool flg_closing_;
size_t total_snd_; // Number of bytes successfully sent
size_t total_rcv_; // Number of bytes successfully received
int total_w_; // Number of write operations
int total_r_; // Number of read operations
};
// *******************************************
// Client
// *******************************************
class Client : public ACE_Handler
{
public:
Client ();
Client (TestData *tester, int id);
~Client (void);
void go (ACE_HANDLE h, const ACE_INET_Addr &server);
int id (void) { return this->id_; }
size_t get_total_snd (void) { return this->total_snd_; }
size_t get_total_rcv (void) { return this->total_rcv_; }
int get_total_w (void) { return this->total_w_; }
int get_total_r (void) { return this->total_r_; }
// This is called when asynchronous reads from the socket complete
virtual void handle_read_dgram (const ACE_Asynch_Read_Dgram::Result &result);
// This is called when asynchronous writes from the socket complete
virtual void handle_write_dgram (const ACE_Asynch_Write_Dgram::Result &result);
void cancel (void);
private:
int initiate_read (void);
int initiate_write (void);
// FUZZ: disable check_for_lack_ACE_OS
void close (void);
// FUZZ: enable check_for_lack_ACE_OS
TestData *tester_;
int id_;
ACE_INET_Addr server_addr_;
ACE_Asynch_Read_Dgram rs_;
ACE_Asynch_Write_Dgram ws_;
ACE_SYNCH_MUTEX lock_;
int io_count_;
int stop_writing_; // Writes are shut down; just read.
bool flg_cancel_;
size_t total_snd_;
size_t total_rcv_;
int total_w_;
int total_r_;
};
// TestData collects and reports on test-related transfer and connection
// statistics.
class TestData
{
public:
TestData ();
bool testing_done (void);
Server *server_up (void);
Client *client_up (void);
void server_done (Server *s);
void client_done (Client *c);
void stop_all (void);
void report (void);
private:
struct Local_Stats
{
// Track number of sessions that report start, and those that report
// their end (and stats).
ACE_Atomic_Op<ACE_SYNCH_MUTEX, int> sessions_up_;
ACE_Atomic_Op<ACE_SYNCH_MUTEX, int> sessions_down_;
// Total read and write bytes for all sessions.
ACE_Atomic_Op<ACE_SYNCH_MUTEX, size_t> w_cnt_;
ACE_Atomic_Op<ACE_SYNCH_MUTEX, size_t> r_cnt_;
// Total read and write operations issues for all sessions.
ACE_Atomic_Op<ACE_SYNCH_MUTEX, size_t> w_ops_;
ACE_Atomic_Op<ACE_SYNCH_MUTEX, size_t> r_ops_;
} servers_, clients_;
ACE_SYNCH_MUTEX list_lock_;
Server *server_list_[MAX_SERVERS];
Client *client_list_[MAX_CLIENTS];
};
TestData::TestData ()
{
int i;
for (i = 0; i < MAX_SERVERS; ++i)
this->server_list_[i] = 0;
for (i = 0; i < MAX_CLIENTS; ++i)
this->client_list_[i] = 0;
}
bool
TestData::testing_done (void)
{
int svr_up = this->servers_.sessions_up_.value ();
int svr_dn = this->servers_.sessions_down_.value ();
int clt_up = this->clients_.sessions_up_.value ();
int clt_dn = this->clients_.sessions_down_.value ();
if (svr_up == 0 && clt_up == 0) // No connections up yet
return false;
return (svr_dn >= svr_up && clt_dn >= clt_up);
}
Server *
TestData::server_up (void)
{
++this->servers_.sessions_up_;
ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, monitor, this->list_lock_, 0);
for (int i = 0; i < MAX_SERVERS; ++i)
{
if (this->server_list_[i] == 0)
{
ACE_NEW_RETURN (this->server_list_[i], Server (this, i), 0);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Server %d up; now %d up, %d down.\n"),
i,
this->servers_.sessions_up_.value (),
this->servers_.sessions_down_.value ()));
return this->server_list_[i];
}
}
return 0;
}
Client *
TestData::client_up (void)
{
++this->clients_.sessions_up_;
ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, monitor, this->list_lock_, 0);
for (int i = 0; i < MAX_CLIENTS; ++i)
{
if (this->client_list_[i] == 0)
{
ACE_NEW_RETURN (this->client_list_[i], Client (this, i), 0);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d up; now %d up, %d down.\n"),
i,
this->clients_.sessions_up_.value (),
this->clients_.sessions_down_.value ()));
return this->client_list_[i];
}
}
return 0;
}
void
TestData::server_done (Server *s)
{
this->servers_.w_cnt_ += s->get_total_snd ();
this->servers_.r_cnt_ += s->get_total_rcv ();
this->servers_.w_ops_ += s->get_total_w ();
this->servers_.r_ops_ += s->get_total_r ();
++this->servers_.sessions_down_;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Server %d gone; now %d up, %d down\n"),
s->id (),
this->servers_.sessions_up_.value (),
this->servers_.sessions_down_.value ()));
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->list_lock_);
int i;
for (i = 0; i < MAX_SERVERS; ++i)
{
if (this->server_list_[i] == s)
{
if (s->id () != i)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Server %d is pos %d in list\n"),
s->id (),
i));
this->server_list_[i] = 0;
break;
}
}
if (i >= MAX_SERVERS)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Server %@ done but not listed\n"), s));
return;
}
void
TestData::client_done (Client *c)
{
this->clients_.w_cnt_ += c->get_total_snd ();
this->clients_.r_cnt_ += c->get_total_rcv ();
this->clients_.w_ops_ += c->get_total_w ();
this->clients_.r_ops_ += c->get_total_r ();
++this->clients_.sessions_down_;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d gone; now %d up, %d down\n"),
c->id (),
this->clients_.sessions_up_.value (),
this->clients_.sessions_down_.value ()));
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->list_lock_);
int i;
for (i = 0; i < MAX_CLIENTS; ++i)
{
if (this->client_list_[i] == c)
{
if (c->id () != i)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Client %d is pos %d in list\n"),
c->id (),
i));
this->client_list_[i] = 0;
break;
}
}
if (i >= MAX_CLIENTS)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Client %@ done but not listed\n"), c));
return;
}
void
TestData::stop_all (void)
{
int i;
// Lock and cancel everything. Then release the lock, possibly allowing
// cleanups, then grab it again and delete all Servers and Clients.
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->list_lock_);
for (i = 0; i < MAX_CLIENTS; ++i)
{
if (this->client_list_[i] != 0)
this->client_list_[i]->cancel ();
}
for (i = 0; i < MAX_SERVERS; ++i)
{
if (this->server_list_[i] != 0)
this->server_list_[i]->cancel ();
}
}
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->list_lock_);
for (i = 0; i < MAX_CLIENTS; ++i)
{
if (this->client_list_[i] != 0)
delete this->client_list_[i];
}
for (i = 0; i < MAX_SERVERS; ++i)
{
if (this->server_list_[i] != 0)
delete this->server_list_[i];
}
}
}
void
TestData::report (void)
{
// Print statistics
ACE_TCHAR bufs [256];
ACE_TCHAR bufr [256];
ACE_OS::sprintf (bufs,
ACE_SIZE_T_FORMAT_SPECIFIER
ACE_TEXT ("(") ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT (")"),
this->clients_.w_cnt_.value (),
this->clients_.w_ops_.value ());
ACE_OS::sprintf (bufr,
ACE_SIZE_T_FORMAT_SPECIFIER
ACE_TEXT ("(") ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT (")"),
this->clients_.r_cnt_.value (),
this->clients_.r_ops_.value ());
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Clients total bytes (ops): snd=%s rcv=%s\n"),
bufs,
bufr));
ACE_OS::sprintf (bufs,
ACE_SIZE_T_FORMAT_SPECIFIER
ACE_TEXT ("(") ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT (")"),
this->servers_.w_cnt_.value (),
this->servers_.w_ops_.value ());
ACE_OS::sprintf (bufr,
ACE_SIZE_T_FORMAT_SPECIFIER
ACE_TEXT ("(") ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT (")"),
this->servers_.r_cnt_.value (),
this->servers_.r_ops_.value ());
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Servers total bytes (ops): snd=%s rcv=%s\n"),
bufs,
bufr));
if (this->clients_.w_cnt_.value () == 0 ||
this->clients_.r_cnt_.value () == 0 ||
this->servers_.w_cnt_.value () == 0 ||
this->servers_.r_cnt_.value () == 0 )
ACE_ERROR ((LM_ERROR, ACE_TEXT ("It appears that this test didn't ")
ACE_TEXT ("really do anything. Something is very wrong.\n")));
}
// Session set-up struct.
struct Session_Data
{
ACE_INT32 direction_; // 0 == Start, 1 == Ack
ACE_INT32 addr_; // Network byte order, must be IPv4
ACE_UINT16 port_; // UDP port, network byte order
Session_Data() { ACE_OS::memset (this, 0, sizeof(*this)); }
};
// Master is the server-side receiver of session establishment requests.
// For each "start" dgram received, instantiates a new Server object,
// indicating the addressing info for the client.
// Master is initialized with a count of the number of expected sessions. After
// this number are set up, Master will stop listening for session requests.
// This is a bit fragile but is necessary because on HP-UX, AIX, et al., it
// is impossible to close/cancel a socket with an outstanding UDP recieve
// (on AIX the process is so wedged the machine needs to be rebooted to
// clear it!). So, this bit of messiness is necessary for portability.
// When the Master is destroyed, it will try to stop establishing sessions
// but this will only work on Windows.
class Master : public ACE_Handler
{
public:
Master (TestData *tester, const ACE_INET_Addr &recv_addr, int expected);
~Master (void);
// Called when dgram receive operation completes.
virtual void handle_read_dgram (const ACE_Asynch_Read_Dgram::Result &result);
private:
void start_recv (void);
TestData *tester_;
ACE_INET_Addr recv_addr_;
ACE_SOCK_Dgram sock_;
ACE_Asynch_Read_Dgram rd_;
ACE_Message_Block *mb_;
ACE_Atomic_Op<ACE_SYNCH_MUTEX, int> sessions_expected_;
volatile bool recv_in_progress_;
};
// *************************************************************
Master::Master (TestData *tester, const ACE_INET_Addr &recv_addr, int expected)
: tester_ (tester),
recv_addr_ (recv_addr),
mb_ (0),
sessions_expected_ (expected),
recv_in_progress_ (false)
{
if (this->sock_.open (recv_addr) == -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Master socket %p\n"), ACE_TEXT ("open")));
else
{
if (this->rd_.open (*this, this->sock_.get_handle ()) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Master reader %p\n"),
ACE_TEXT ("open")));
this->mb_ = new ACE_Message_Block (sizeof (Session_Data));
start_recv ();
}
}
Master::~Master (void)
{
if (this->recv_in_progress_)
this->rd_.cancel ();
this->sock_.close ();
if (this->mb_ != 0)
{
this->mb_->release ();
this->mb_ = 0;
}
}
void
Master::handle_read_dgram (const ACE_Asynch_Read_Dgram::Result &result)
{
// We should only receive Start datagrams with valid addresses to reply to.
if (result.success ())
{
if (result.bytes_transferred () != sizeof (Session_Data))
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) Master session data expected %B bytes; ")
ACE_TEXT ("received %B\n"),
sizeof (Session_Data),
result.bytes_transferred ()));
else
{
ACE_Message_Block *mb = result.message_block ();
Session_Data *session =
reinterpret_cast<Session_Data*>(mb->rd_ptr ());
if (session->direction_ == 0)
{
ACE_INET_Addr client_addr, me_addr;
ACE_TCHAR client_str[80], me_str[80];
client_addr.set ((u_short)session->port_, session->addr_, 0);
client_addr.addr_to_string (client_str, 80);
// Set up the local and remote addresses - need fully-specified
// addresses to use UDP aio on Linux. This is the socket that
// the session will run over. The addressing info to be sent
// back to the Client will be sent over the receive socket
// to ensure it goes back to the client initiating the session.
ACE_SOCK_CODgram sock;
if (sock.open (client_addr) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) Master new socket for ")
ACE_TEXT ("client %s: %p\n"),
client_str,
ACE_TEXT ("open")));
}
else
{
sock.get_local_addr (me_addr);
me_addr.addr_to_string (me_str, 80);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Master setting up server for ")
ACE_TEXT ("local %s, peer %s\n"),
me_str,
client_str));
Session_Data session;
session.direction_ = 1; // Ack
session.addr_ = ACE_HTONL (me_addr.get_ip_address ());
session.port_ = ACE_HTONS (me_addr.get_port_number ());
if (this->sock_.send (&session,
sizeof (session),
client_addr) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) Master reply %p\n"),
ACE_TEXT ("send")));
sock.close ();
}
else
{
Server *server = this->tester_->server_up ();
server->go (sock.get_handle (), client_addr);
}
}
if (--this->sessions_expected_ == 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("All expected sessions are up\n")));
}
}
else
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) Badly formed Session request\n")));
}
}
}
else
{
ACE_Log_Priority prio = LM_ERROR;
#if defined (ACE_WIN32)
if (result.error () == ERROR_OPERATION_ABORTED)
prio = LM_DEBUG;
#else
if (result.error () == ECANCELED)
prio = LM_DEBUG;
#endif /* ACE_WIN32 */
// Multiple steps to log the error without squashing errno.
ACE_LOG_MSG->conditional_set (__FILE__,
__LINE__,
-1,
(int)(result.error ()));
ACE_LOG_MSG->log (prio,
ACE_TEXT ("(%t) Master %p\n"),
ACE_TEXT ("recv"));
// If canceled, don't try to restart.
if (prio == LM_DEBUG)
return;
}
this->start_recv ();
}
void
Master::start_recv (void)
{
if (this->mb_ == 0)
return;
size_t unused = 0;
this->mb_->reset ();
if (this->rd_.recv (this->mb_, unused, 0) == -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) Master %p\n"), ACE_TEXT ("recv")));
else
this->recv_in_progress_ = true;
}
// ***************************************************
Server::Server ()
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Shouldn't use this constructor!\n")));
}
Server::Server (TestData *tester, int id)
: tester_ (tester),
id_ (id),
io_count_ (0),
flg_cancel_(false),
flg_closing_ (false),
total_snd_(0),
total_rcv_(0),
total_w_ (0),
total_r_ (0)
{
}
Server::~Server (void)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Server %d dtor; %d sends (%B bytes); ")
ACE_TEXT ("%d recvs (%B bytes)\n"),
this->id_,
this->total_w_, this->total_snd_,
this->total_r_, this->total_rcv_));
if (this->io_count_ != 0)
ACE_ERROR ((LM_WARNING,
ACE_TEXT ("(%t) Server %d deleted with ")
ACE_TEXT ("%d I/O outstanding\n"),
this->id_,
this->io_count_));
// This test bounces data back and forth between Clients and Servers.
// Therefore, if there was significantly more data in one direction, that's
// a problem. Remember, the byte counts are unsigned values.
int issue_data_warning = 0;
if (this->total_snd_ > this->total_rcv_)
{
if (this->total_rcv_ == 0)
issue_data_warning = 1;
else if (this->total_snd_ / this->total_rcv_ > 2)
issue_data_warning = 1;
}
else
{
if (this->total_snd_ == 0)
issue_data_warning = 1;
else if (this->total_rcv_ / this->total_snd_ > 2)
issue_data_warning = 1;
}
if (issue_data_warning)
ACE_DEBUG ((LM_WARNING,
ACE_TEXT ("(%t) Above byte counts look odd; need review\n")));
if (this->tester_ != 0)
this->tester_->server_done (this);
if (this->handle () != ACE_INVALID_HANDLE)
ACE_OS::closesocket (this->handle ());
this->id_ = -1;
this->handle (ACE_INVALID_HANDLE);
}
void
Server::cancel ()
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_);
this->flg_cancel_ = true;
this->ws_.cancel ();
this->rs_.cancel ();
return;
}
void
Server::go (ACE_HANDLE handle, const ACE_INET_Addr &client)
{
this->handle (handle);
this->client_addr_.set (client);
// Lock this before initiating I/O, else it may complete while we're
// still setting up.
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_);
if (this->ws_.open (*this, this->handle ()) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Server::ACE_Asynch_Write_Dgram::open")));
else if (this->rs_.open (*this, this->handle ()) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Server::ACE_Asynch_Read_Dgram::open")));
else
this->initiate_read ();
}
if (this->io_count_ > 0)
return;
delete this; // Error setting up I/O factories
}
int
Server::initiate_read (void)
{
if (this->flg_cancel_ || this->handle () == ACE_INVALID_HANDLE)
return -1;
ACE_Message_Block *mb = 0;
ACE_NEW_RETURN (mb,
ACE_Message_Block (1024), //BUFSIZ + 1),
-1);
// Inititiate receive
size_t unused = 0;
if (this->rs_.recv (mb, unused, 0) == -1)
{
mb->release ();
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) Server %d, %p\n"),
this->id_,
ACE_TEXT ("read")),
-1);
}
this->io_count_++;
this->total_r_++;
return 0;
}
int
Server::initiate_write (ACE_Message_Block *mb, size_t nbytes)
{
if (this->flg_cancel_ || this->handle () == ACE_INVALID_HANDLE)
{
mb->release ();
return -1;
}
if (nbytes == 0)
{
mb->release ();
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT ("(%t) Server %d write nbytes == 0\n"),
this->id_),
-1);
}
if (this->ws_.send (mb, nbytes, 0, this->client_addr_) == -1)
{
mb->release ();
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT ("(%t) Server %d, %p\n"),
this->id_,
ACE_TEXT ("write")),
-1);
}
this->io_count_++;
this->total_w_++;
return 0;
}
void
Server::handle_read_dgram (const ACE_Asynch_Read_Dgram::Result &result)
{
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_ );
ACE_Message_Block *mb = result.message_block ();
// Reset pointers.
mb->rd_ptr ()[result.bytes_transferred ()] = '\0';
if (loglevel > 1)
{
LogLocker log_lock;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) **** Server %d: handle_read_dgram() ****\n"),
this->id_));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %B\n"),
ACE_TEXT ("bytes_to_read"),
result.bytes_to_read ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("handle"),
result.handle ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %B\n"),
ACE_TEXT ("bytes_transfered"),
result.bytes_transferred ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %@\n"),
ACE_TEXT ("act"),
result.act ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("success"),
result.success ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %@\n"),
ACE_TEXT ("completion_key"),
result.completion_key ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("error"),
result.error ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %s\n"),
ACE_TEXT ("message_block"),
mb->rd_ptr ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("**** end of message ****************\n")));
}
else if (result.error () != 0)
{
ACE_Log_Priority prio;
#if defined (ACE_WIN32)
if (result.error () == ERROR_OPERATION_ABORTED)
prio = LM_DEBUG;
#else
if (result.error () == ECANCELED)
prio = LM_DEBUG;
#endif /* ACE_WIN32 */
else
prio = LM_ERROR;
ACE_LOG_MSG->errnum (result.error ());
ACE_LOG_MSG->log (prio,
ACE_TEXT ("(%t) Server %d; %p\n"),
this->id_,
ACE_TEXT ("read"));
}
else if (loglevel > 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Server %d: read %B bytes\n"),
this->id_,
result.bytes_transferred ()));
}
if (result.error () == 0 && result.bytes_transferred () > 0)
{
this->total_rcv_ += result.bytes_transferred ();
// If client says we're done, ack it; we're done reading.
size_t to_send = result.bytes_transferred ();
if (ACE_OS::strcmp (mb->rd_ptr (), close_req_msg) == 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Server %d saw close request; ack\n"),
this->id_));
this->flg_closing_ = true;
mb->reset ();
mb->copy (close_ack_msg);
to_send = mb->length ();
}
if (this->initiate_write (mb, to_send) == 0)
{
if (duplex != 0 && !this->flg_closing_)
this->initiate_read ();
}
}
else
mb->release ();
--this->io_count_;
if (this->io_count_ > 0)
return;
}
delete this;
}
void
Server::handle_write_dgram (const ACE_Asynch_Write_Dgram::Result &result)
{
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_);
ACE_Message_Block *mb = result.message_block ();
if (loglevel > 1)
{
LogLocker log_lock;
//mb.rd_ptr () [0] = '\0';
mb->rd_ptr (mb->rd_ptr () - result.bytes_transferred ());
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) **** Server %d: handle_write_dgram() ****\n"),
this->id_));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %B\n"),
ACE_TEXT ("bytes_to_write"),
result.bytes_to_write ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("handle"),
result.handle ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %B\n"),
ACE_TEXT ("bytes_transfered"),
result.bytes_transferred ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %@\n"),
ACE_TEXT ("act"),
result.act ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("success"),
result.success ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %@\n"),
ACE_TEXT ("completion_key"),
result.completion_key ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("error"),
result.error ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %s\n"),
ACE_TEXT ("message_block"),
mb->rd_ptr ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("**** end of message ****************\n")));
}
else if (result.error () != 0)
{
ACE_Log_Priority prio;
#if defined (ACE_WIN32)
if (result.error () == ERROR_OPERATION_ABORTED)
prio = LM_DEBUG;
#else
if (result.error () == ECANCELED)
prio = LM_DEBUG;
#endif /* ACE_WIN32 */
else
prio = LM_ERROR;
ACE_LOG_MSG->errnum (result.error ());
ACE_LOG_MSG->log (prio,
ACE_TEXT ("(%t) Server %d; %p\n"),
this->id_,
ACE_TEXT ("write"));
}
else if (loglevel > 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Server %d: wrote %B bytes ok\n"),
this->id_,
result.bytes_transferred ()));
}
mb->release ();
if (result.error () == 0 && result.bytes_transferred () > 0)
{
this->total_snd_ += result.bytes_transferred ();
if (duplex == 0 && !this->flg_closing_)
this->initiate_read ();
}
--this->io_count_;
if (this->io_count_ > 0)
return;
}
delete this;
}
// *******************************************
// Connector
//
// Connector creates the proper number of Clients and initiates
// sessions on them.
// *******************************************
class Connector
{
public:
Connector (TestData *tester);
int start (const ACE_INET_Addr &addr, int num);
private:
TestData *tester_;
};
// *************************************************************
Connector::Connector (TestData *tester)
: tester_ (tester)
{
}
int
Connector::start (const ACE_INET_Addr& addr, int num)
{
ACE_OS::sleep(3); // Let Master get going
if (num > MAX_CLIENTS)
num = MAX_CLIENTS;
if (num < 0)
num = 1;
int rc = 0;
for (; rc < num; rc++)
{
ACE_SOCK_CODgram sock;
if (sock.open (addr) == -1)
ACE_ERROR_BREAK ((LM_ERROR,
ACE_TEXT ("(%t) Starting client %d: %p\n"),
rc,
ACE_TEXT ("open")));
ACE_INET_Addr me;
sock.get_local_addr (me);
u_short my_port = ACE_HTONS (me.get_port_number ());
ACE_INT32 my_addr = ACE_HTONL (me.get_ip_address ());
Session_Data session;
session.direction_ = 0; // Start
session.addr_ = my_addr;
session.port_ = my_port;
if (sock.send (&session, sizeof (session)) == -1)
ACE_ERROR_BREAK ((LM_ERROR,
ACE_TEXT ("(%t) Starting client %d: %p\n"),
rc,
ACE_TEXT ("send")));
if (sock.recv (&session, sizeof (session)) == -1)
ACE_ERROR_BREAK ((LM_ERROR,
ACE_TEXT ("(%t) Starting client %d: %p\n"),
rc,
ACE_TEXT ("recv")));
ACE_INET_Addr server;
server.set (session.port_, session.addr_, 0);
Client *client = this->tester_->client_up ();
ACE_TCHAR me_str[80], server_str[80];
me.addr_to_string (me_str, 80);
server.addr_to_string (server_str, 80);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d setting up local %s, peer %s\n"),
client->id(),
me_str,
server_str));
sock.close ();
if (sock.open (server, me) == -1)
ACE_ERROR_BREAK ((LM_ERROR,
ACE_TEXT ("(%t) Re-opening %p\n"),
ACE_TEXT ("client")));
client->go (sock.get_handle (), server);
sock.set_handle (ACE_INVALID_HANDLE);
}
return rc;
}
Client::Client ()
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Shouldn't use this constructor!\n")));
}
Client::Client (TestData *tester, int id)
: tester_ (tester),
id_ (id),
io_count_ (0),
stop_writing_ (0),
flg_cancel_ (false),
total_snd_ (0),
total_rcv_ (0),
total_w_ (0),
total_r_ (0)
{
}
Client::~Client (void)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d dtor; %d sends (%B bytes); ")
ACE_TEXT ("%d recvs (%B bytes)\n"),
this->id_,
this->total_w_, this->total_snd_,
this->total_r_, this->total_rcv_));
if (this->io_count_ != 0)
ACE_ERROR ((LM_WARNING,
ACE_TEXT ("(%t) Client %d deleted with %d I/O outstanding\n"),
this->id_,
this->io_count_));
// This test bounces data back and forth between Clients and Servers.
// Therefore, if there was significantly more data in one direction, that's
// a problem. Remember, the byte counts are unsigned values.
int issue_data_warning = 0;
if (this->total_snd_ > this->total_rcv_)
{
if (this->total_rcv_ == 0)
issue_data_warning = 1;
else if (this->total_snd_ / this->total_rcv_ > 2)
issue_data_warning = 1;
}
else
{
if (this->total_snd_ == 0)
issue_data_warning = 1;
else if (this->total_rcv_ / this->total_snd_ > 2)
issue_data_warning = 1;
}
if (issue_data_warning)
ACE_DEBUG ((LM_WARNING,
ACE_TEXT ("(%t) Above byte counts look odd; need review\n")));
if (this->tester_ != 0)
this->tester_->client_done (this);
this->id_ = -1;
if (this->handle () != ACE_INVALID_HANDLE)
{
ACE_OS::closesocket (this->handle ());
}
this->handle (ACE_INVALID_HANDLE);
}
void
Client::cancel ()
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_);
this->flg_cancel_ = true;
this->ws_.cancel ();
this->rs_.cancel ();
return;
}
void
Client::close ()
{
// This must be called with the lock_ held.
++this->stop_writing_;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Closing Client %d writes; %d I/O outstanding\n"),
this->id_, this->io_count_));
return;
}
void
Client::go (ACE_HANDLE handle, const ACE_INET_Addr &server)
{
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_);
this->handle (handle);
this->server_addr_.set (server);
// Open send and receive factories.
if (this->ws_.open (*this, handle) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) Client %d: %p\n"),
this->id_,
ACE_TEXT ("ACE_Asynch_Write_Dgram::open")));
else if (this->rs_.open (*this, handle) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) Client %d: %p\n"),
this->id_,
ACE_TEXT ("ACE_Asynch_Read_Dgram::open")));
else if (this->initiate_write () == 0)
{
if (duplex != 0) // Start an asynchronous read
this->initiate_read ();
}
if (this->io_count_ > 0)
return;
}
delete this;
}
int
Client::initiate_write (void)
{
if (this->flg_cancel_ || this->handle () == ACE_INVALID_HANDLE)
return -1;
// stop_writing_ is set to 1 to say "stop". To avoid repeating the
// close datagram for every echo, only send it once. Sure, there's a risk
// it will get lost, but since this is most often intra-host, don't
// worry about that very small risk.
if (this->stop_writing_ > 0) // Need to tell server to "close"
{
if (this->stop_writing_ > 1) // Already told server to close
return 0;
++this->stop_writing_;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d requesting close\n"),
this->id_));
ACE_Message_Block *mb =
new ACE_Message_Block (ACE_OS::strlen (close_req_msg) + 1);
mb->copy (close_req_msg);
size_t unused; // Number of bytes sent
if (this->ws_.send (mb, unused, 0, this->server_addr_) == -1)
{
mb->release ();
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) Client %d, %p\n"),
this->id_,
ACE_TEXT ("initiating closing send")),
-1);
}
this->io_count_++;
this->total_w_++;
return 0;
}
static const size_t complete_message_length =
ACE_OS::strlen (complete_message);
#if defined (ACE_WIN32)
ACE_Message_Block *mb1 = 0,
*mb2 = 0,
*mb3 = 0;
// No need to allocate +1 for proper printing - the memory includes it already
ACE_NEW_RETURN (mb1,
ACE_Message_Block ((char *)complete_message,
complete_message_length),
-1);
ACE_NEW_RETURN (mb2,
ACE_Message_Block ((char *)complete_message,
complete_message_length),
-1);
ACE_NEW_RETURN (mb3,
ACE_Message_Block ((char *)complete_message,
complete_message_length),
-1);
mb1->wr_ptr (complete_message_length);
mb2->wr_ptr (complete_message_length);
mb3->wr_ptr (complete_message_length);
// chain them together
mb1->cont (mb2);
mb2->cont (mb3);
size_t unused; // Number of bytes sent
if (this->ws_.send (mb1, unused, 0, this->server_addr_) == -1)
{
mb1->release ();
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Client::ACE_Asynch_Write_Dgram::send")),
-1);
}
#else /* ACE_WIN32 */
ACE_Message_Block *mb = 0;
// No need to allocate +1 for proper printing - the memory includes
// it already
ACE_NEW_RETURN (mb,
ACE_Message_Block (complete_message,
complete_message_length),
-1);
mb->wr_ptr (complete_message_length);
size_t unused; // Number of bytes sent
if (this->ws_.send (mb, unused, 0, this->server_addr_) == -1)
{
mb->release ();
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT ("(%t) Client %d, %p\n"),
this->id_,
ACE_TEXT ("send")),
-1);
}
#endif /* ACE_WIN32 */
this->io_count_++;
this->total_w_++;
return 0;
}
int
Client::initiate_read (void)
{
if (this->flg_cancel_ || this->handle_ == ACE_INVALID_HANDLE)
return -1;
static const size_t complete_message_length =
ACE_OS::strlen (complete_message);
#if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
ACE_Message_Block *mb1 = 0,
*mb2 = 0,
*mb3 = 0,
*mb4 = 0,
*mb5 = 0,
*mb6 = 0;
// We allocate +1 only for proper printing - we can just set the last byte
// to '\0' before printing out
ACE_NEW_RETURN (mb1, ACE_Message_Block (complete_message_length + 1), -1);
ACE_NEW_RETURN (mb2, ACE_Message_Block (complete_message_length + 1), -1);
ACE_NEW_RETURN (mb3, ACE_Message_Block (complete_message_length + 1), -1);
// Let allocate memory for one more triplet,
// This improves performance
// as we can receive more the than one block at once
// Generally, we can receive more triplets ....
ACE_NEW_RETURN (mb4, ACE_Message_Block (complete_message_length + 1), -1);
ACE_NEW_RETURN (mb5, ACE_Message_Block (complete_message_length + 1), -1);
ACE_NEW_RETURN (mb6, ACE_Message_Block (complete_message_length + 1), -1);
mb1->cont (mb2);
mb2->cont (mb3);
mb3->cont (mb4);
mb4->cont (mb5);
mb5->cont (mb6);
// hide last byte in each message block, reserving it for later to set '\0'
// for proper printouts
mb1->size (mb1->size () - 1);
mb2->size (mb2->size () - 1);
mb3->size (mb3->size () - 1);
mb4->size (mb4->size () - 1);
mb5->size (mb5->size () - 1);
mb6->size (mb6->size () - 1);
// Inititiate read
size_t unused = 0;
if (this->rs_.recv (mb1, unused, 0) == -1)
{
mb1->release ();
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("Client::ACE_Asynch_Read_Stream::readv")),
-1);
}
#else /* ACE_HAS_WIN32_OVERLAPPED_IO */
// Try to read more chunks
size_t blksize = ( complete_message_length > BUFSIZ ) ?
complete_message_length : BUFSIZ;
ACE_Message_Block *mb = 0;
// We allocate +1 only for proper printing - we can just set the last byte
// to '\0' before printing out
ACE_NEW_RETURN (mb,
ACE_Message_Block (blksize + 1),
-1);
// Inititiate read
size_t unused = 0;
if (this->rs_.recv (mb, unused, 0) == -1)
{
mb->release ();
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%t) Client %d, %p\n"),
this->id_,
ACE_TEXT ("read")),
-1);
}
#endif /* ACE_HAS_WIN32_OVERLAPPED_IO */
this->io_count_++;
this->total_r_++;
return 0;
}
void
Client::handle_write_dgram (const ACE_Asynch_Write_Dgram::Result &result)
{
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_);
ACE_Message_Block *mb = result.message_block ();
if (loglevel > 1)
{
LogLocker log_lock;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) **** Client %d: handle_write_dgram() ****\n"),
this->id_));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %B\n"),
ACE_TEXT ("bytes_to_write"),
result.bytes_to_write ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("handle"),
result.handle ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %B\n"),
ACE_TEXT ("bytes_transfered"),
result.bytes_transferred ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %@\n"),
ACE_TEXT ("act"),
result.act ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("success"),
result.success ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %@\n"),
ACE_TEXT ("completion_key"),
result.completion_key ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("error"),
result.error ()));
#if defined (ACE_WIN32)
size_t bytes_transferred = result.bytes_transferred ();
char index = 0;
for (ACE_Message_Block* mb_i = mb;
(mb_i != 0) && (bytes_transferred > 0);
mb_i = mb_i->cont ())
{
// write 0 at string end for proper printout (if end of mb,
// it's 0 already)
mb_i->rd_ptr()[0] = '\0';
size_t len = mb_i->rd_ptr () - mb_i->base ();
// move rd_ptr backwards as required for printout
if (len >= bytes_transferred)
{
mb_i->rd_ptr (0 - bytes_transferred);
bytes_transferred = 0;
}
else
{
mb_i->rd_ptr (0 - len);
bytes_transferred -= len;
}
++index;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s%d = %s\n"),
ACE_TEXT ("message_block, part "),
index,
mb_i->rd_ptr ()));
}
#else /* ACE_WIN32 */
// write 0 at string end for proper printout (if end of mb, it's 0 already)
mb->rd_ptr()[0] = '\0';
// move rd_ptr backwards as required for printout
mb->rd_ptr (- result.bytes_transferred ());
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %s\n"),
ACE_TEXT ("message_block"),
mb->rd_ptr ()));
#endif /* ACE_WIN32 */
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("**** end of message ****************\n")));
}
else if (result.error () != 0)
{
ACE_Log_Priority prio;
#if defined (ACE_WIN32)
if (result.error () == ERROR_OPERATION_ABORTED)
prio = LM_DEBUG;
#else
if (result.error () == ECANCELED)
prio = LM_DEBUG;
#endif /* ACE_WIN32 */
else
prio = LM_ERROR;
ACE_LOG_MSG->errnum (result.error ());
ACE_LOG_MSG->log (prio,
ACE_TEXT ("(%t) Client %d; %p\n"),
this->id_,
ACE_TEXT ("write"));
}
else if (loglevel > 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d: wrote %B bytes ok\n"),
this->id_,
result.bytes_transferred ()));
}
mb->release ();
if (result.error () == 0 && result.bytes_transferred () > 0)
{
this->total_snd_ += result.bytes_transferred ();
if (this->total_snd_ >= xfer_limit)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d sent %B, limit %B\n"),
this->id_, this->total_snd_, xfer_limit));
this->close ();
}
if (duplex != 0) // full duplex, continue write
{
if ((this->total_snd_- this->total_rcv_) < 1024*32 ) //flow control
this->initiate_write ();
}
else // half-duplex read reply, after read we will start write
this->initiate_read ();
}
--this->io_count_;
if (this->io_count_ > 0)
return;
}
delete this;
}
void
Client::handle_read_dgram (const ACE_Asynch_Read_Dgram::Result &result)
{
{
ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->lock_);
ACE_Message_Block *mb = result.message_block ();
if (loglevel > 1)
{
LogLocker log_lock;
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) **** Client %d: handle_read_dgram() ****\n"),
this->id_));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %B\n"),
ACE_TEXT ("bytes_to_read"),
result.bytes_to_read ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("handle"),
result.handle ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %B\n"),
ACE_TEXT ("bytes_transfered"),
result.bytes_transferred ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %@\n"),
ACE_TEXT ("act"),
result.act ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("success"),
result.success ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %@\n"),
ACE_TEXT ("completion_key"),
result.completion_key ()));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %d\n"),
ACE_TEXT ("error"),
result.error ()));
#if defined (ACE_WIN32)
char index = 0;
for (ACE_Message_Block* mb_i = mb;
mb_i != 0;
mb_i = mb_i->cont ())
{
++index;
// write 0 at string end for proper printout
mb_i->wr_ptr()[0] = '\0';
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s%d = %s\n"),
ACE_TEXT ("message_block, part "),
index,
mb_i->rd_ptr ()));
}
#else /* ACE_WIN32 */
// write 0 at string end for proper printout
mb->rd_ptr()[result.bytes_transferred ()] = '\0'; // for proper printout
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("%s = %s\n"),
ACE_TEXT ("message_block"),
mb->rd_ptr ()));
#endif /* ACE_WIN32 */
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("**** end of message ****************\n")));
}
else if (result.error () != 0)
{
ACE_Log_Priority prio;
#if defined (ACE_WIN32)
if (result.error () == ERROR_OPERATION_ABORTED)
prio = LM_DEBUG;
#else
if (result.error () == ECANCELED)
prio = LM_DEBUG;
#endif /* ACE_WIN32 */
else
prio = LM_ERROR;
ACE_Log_Msg::instance ()->errnum (result.error ());
ACE_Log_Msg::instance ()->log (prio,
ACE_TEXT ("(%t) Client %d; %p\n"),
this->id_,
ACE_TEXT ("read"));
}
else if (loglevel > 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d: read %B bytes ok\n"),
this->id_,
result.bytes_transferred ()));
}
if (result.error () == 0 && result.bytes_transferred () > 0)
{
this->total_rcv_ += result.bytes_transferred ();
// If we've closed and the server acked, we're done.
if (this->stop_writing_ &&
ACE_OS::strcmp (mb->rd_ptr (), close_ack_msg) == 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Client %d recvd close-ack\n"),
this->id_));
}
else
{
if (duplex != 0)
this->initiate_read ();
else // half-duplex write, after write we will start read
this->initiate_write ();
}
}
mb->release ();
--this->io_count_;
if (this->io_count_ > 0)
return;
}
delete this;
}
// *************************************************************
// Configuration helpers
// *************************************************************
int
print_usage (int /* argc */, ACE_TCHAR *argv[])
{
ACE_ERROR
((LM_ERROR,
ACE_TEXT ("\nusage: %s")
ACE_TEXT ("\n-o <max number of started aio operations for Proactor>")
ACE_TEXT ("\n-t <Proactor type> UNIX-only, Win32-default always:")
ACE_TEXT ("\n a AIOCB")
ACE_TEXT ("\n i SIG")
ACE_TEXT ("\n c CB")
ACE_TEXT ("\n s SUN")
ACE_TEXT ("\n d default")
ACE_TEXT ("\n-d <duplex mode 1-on/0-off>")
ACE_TEXT ("\n-h <host> for Client mode")
ACE_TEXT ("\n-n <number threads for Proactor pool>")
ACE_TEXT ("\n-p <port to listen/connect>")
ACE_TEXT ("\n-c <number of client instances>")
ACE_TEXT ("\n-b run client and server at the same time")
ACE_TEXT ("\n f file")
ACE_TEXT ("\n c console")
ACE_TEXT ("\n-v log level")
ACE_TEXT ("\n 0 - log errors and highlights")
ACE_TEXT ("\n 1 - log level 0 plus progress information")
ACE_TEXT ("\n 2 - log level 1 plus operation parameters and results")
ACE_TEXT ("\n-x max transfer byte count per Client")
ACE_TEXT ("\n-u show this message")
ACE_TEXT ("\n"),
argv[0]
));
return -1;
}
static int
set_proactor_type (const ACE_TCHAR *ptype)
{
if (!ptype)
return 0;
switch (ACE_OS::ace_toupper (*ptype))
{
case 'D':
proactor_type = DEFAULT;
return 1;
case 'A':
proactor_type = AIOCB;
return 1;
case 'I':
proactor_type = SIG;
return 1;
#if defined (sun)
case 'S':
proactor_type = SUN;
return 1;
#endif /* sun */
#if !defined (ACE_HAS_BROKEN_SIGEVENT_STRUCT)
case 'C':
proactor_type = CB;
return 1;
#endif /* !ACE_HAS_BROKEN_SIGEVENT_STRUCT */
default:
break;
}
return 0;
}
static int
parse_args (int argc, ACE_TCHAR *argv[])
{
// First, set up all the defaults then let any args change them.
both = 1; // client and server simultaneosly
duplex = 1; // full duplex is on
host = ACE_LOCALHOST; // server to connect
port = ACE_DEFAULT_SERVER_PORT; // port to connect/listen
max_aio_operations = 512; // POSIX Proactor params
proactor_type = DEFAULT; // Proactor type = default
threads = 3; // size of Proactor thread pool
clients = 10; // number of clients
loglevel = 0; // log level : only errors and highlights
// Default transfer limit 50 messages per Sender
xfer_limit = 50 * ACE_OS::strlen (complete_message);
// Linux kernels up to at least 2.6.9 (RHEL 4) can't do full duplex aio.
# if defined (linux)
duplex = 0;
#endif
if (argc == 1) // no arguments , so one button test
return 0;
ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("x:t:o:n:p:d:h:c:v:ub"));
int c;
while ((c = get_opt ()) != EOF)
{
switch (c)
{
case 'x': // xfer limit
xfer_limit = static_cast<size_t> (ACE_OS::atoi (get_opt.opt_arg ()));
if (xfer_limit == 0)
xfer_limit = 1; // Bare minimum.
break;
case 'b': // both client and server
both = 1;
break;
case 'v': // log level
loglevel = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'd': // duplex
duplex = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'h': // host for sender
host = get_opt.opt_arg ();
break;
case 'p': // port number
port = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'n': // thread pool size
threads = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 'c': // number of clients
clients = ACE_OS::atoi (get_opt.opt_arg ());
if (clients > MAX_CLIENTS)
clients = MAX_CLIENTS;
break;
case 'o': // max number of aio for proactor
max_aio_operations = ACE_OS::atoi (get_opt.opt_arg ());
break;
case 't': // Proactor Type
if (set_proactor_type (get_opt.opt_arg ()))
break;
return print_usage (argc, argv);
case 'u':
default:
return print_usage (argc, argv);
} // switch
} // while
if (proactor_type == SUN && threads > 1)
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Sun aiowait is not thread-safe; ")
ACE_TEXT ("changing to 1 thread\n")));
threads = 1;
}
return 0;
}
int
run_main (int argc, ACE_TCHAR *argv[])
{
ACE_START_TEST (ACE_TEXT ("Proactor_UDP_Test"));
if (::parse_args (argc, argv) == -1)
return -1;
disable_signal (ACE_SIGRTMIN, ACE_SIGRTMAX);
disable_signal (SIGPIPE, SIGPIPE);
MyTask task1;
TestData test;
if (task1.start (threads, proactor_type, max_aio_operations) == 0)
{
// NOTE - there's no real reason this test is limited to IPv4 other
// than the way Session_Data is set up - to expand this test to work
// on IPv6 as well as IPv4, you need to do some work on passing the
// Session_Data address differently.
ACE_INET_Addr addr (port, ACE_LOCALHOST, AF_INET);
Master master (&test, addr, clients);
Connector connector (&test);
int rc = 0;
if (both != 0 || host == 0) // Acceptor
{
// Already running; if not needed will be deleted soon.
rc = 1;
}
if (both != 0 || host != 0)
{
if (host == 0)
host = ACE_LOCALHOST;
if (addr.set (port, host, 1, addr.get_type ()) == -1)
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), host));
else
rc += connector.start (addr, clients);
}
// Wait a few seconds to let things get going, then poll til
// all sessions are done. Note that when we exit this scope, the
// Acceptor and Connector will be destroyed, which should prevent
// further connections and also test how well destroyed handlers
// are handled.
ACE_OS::sleep (3);
}
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Sleeping til sessions run down.\n")));
while (!test.testing_done ())
ACE_OS::sleep (1);
test.stop_all ();
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Stop Thread Pool Task\n")));
task1.stop ();
ACE_END_TEST;
return 0;
}
#else
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Proactor_UDP_Test"));
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("Threads or Asynchronous IO is unsupported.\n")
ACE_TEXT ("Proactor_UDP_Test will not be run.")));
ACE_END_TEST;
return 0;
}
#endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */
|