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
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/physicalsocketserver.h"
#if defined(_MSC_VER) && _MSC_VER < 1300
#pragma warning(disable:4786)
#endif
#ifdef MEMORY_SANITIZER
#include <sanitizer/msan_interface.h>
#endif
#if defined(WEBRTC_POSIX)
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#if defined(WEBRTC_USE_EPOLL)
// "poll" will be used to wait for the signal dispatcher.
#include <poll.h>
#endif
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/select.h>
#include <unistd.h>
#include <signal.h>
#endif
#if defined(WEBRTC_WIN)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#undef SetPort
#endif
#include <algorithm>
#include <map>
#include "rtc_base/arraysize.h"
#include "rtc_base/basictypes.h"
#include "rtc_base/byteorder.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/networkmonitor.h"
#include "rtc_base/nullsocketserver.h"
#include "rtc_base/timeutils.h"
#include "rtc_base/win32socketinit.h"
#if defined(WEBRTC_POSIX)
#include <netinet/tcp.h> // for TCP_NODELAY
#define IP_MTU 14 // Until this is integrated from linux/in.h to netinet/in.h
typedef void* SockOptArg;
#endif // WEBRTC_POSIX
#if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) && !defined(WEBRTC_BSD) && !defined(__native_client__)
#if defined(WEBRTC_LINUX)
#include <linux/sockios.h>
#endif
int64_t GetSocketRecvTimestamp(int socket) {
struct timeval tv_ioctl;
int ret = ioctl(socket, SIOCGSTAMP, &tv_ioctl);
if (ret != 0)
return -1;
int64_t timestamp =
rtc::kNumMicrosecsPerSec * static_cast<int64_t>(tv_ioctl.tv_sec) +
static_cast<int64_t>(tv_ioctl.tv_usec);
return timestamp;
}
#else
int64_t GetSocketRecvTimestamp(int socket) {
return -1;
}
#endif
#if defined(WEBRTC_WIN)
typedef char* SockOptArg;
#endif
#if defined(WEBRTC_USE_EPOLL)
// POLLRDHUP / EPOLLRDHUP are only defined starting with Linux 2.6.17.
#if !defined(POLLRDHUP)
#define POLLRDHUP 0x2000
#endif
#if !defined(EPOLLRDHUP)
#define EPOLLRDHUP 0x2000
#endif
#endif
namespace rtc {
std::unique_ptr<SocketServer> SocketServer::CreateDefault() {
#if defined(__native_client__)
return std::unique_ptr<SocketServer>(new rtc::NullSocketServer);
#else
return std::unique_ptr<SocketServer>(new rtc::PhysicalSocketServer);
#endif
}
#if defined(WEBRTC_WIN)
// Standard MTUs, from RFC 1191
const uint16_t PACKET_MAXIMUMS[] = {
65535, // Theoretical maximum, Hyperchannel
32000, // Nothing
17914, // 16Mb IBM Token Ring
8166, // IEEE 802.4
// 4464, // IEEE 802.5 (4Mb max)
4352, // FDDI
// 2048, // Wideband Network
2002, // IEEE 802.5 (4Mb recommended)
// 1536, // Expermental Ethernet Networks
// 1500, // Ethernet, Point-to-Point (default)
1492, // IEEE 802.3
1006, // SLIP, ARPANET
// 576, // X.25 Networks
// 544, // DEC IP Portal
// 512, // NETBIOS
508, // IEEE 802/Source-Rt Bridge, ARCNET
296, // Point-to-Point (low delay)
68, // Official minimum
0, // End of list marker
};
static const int IP_HEADER_SIZE = 20u;
static const int IPV6_HEADER_SIZE = 40u;
static const int ICMP_HEADER_SIZE = 8u;
static const int ICMP_PING_TIMEOUT_MILLIS = 10000u;
#endif
PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s)
: ss_(ss), s_(s), error_(0),
state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED),
resolver_(nullptr) {
#if defined(WEBRTC_WIN)
// EnsureWinsockInit() ensures that winsock is initialized. The default
// version of this function doesn't do anything because winsock is
// initialized by constructor of a static object. If neccessary libjingle
// users can link it with a different version of this function by replacing
// win32socketinit.cc. See win32socketinit.cc for more details.
EnsureWinsockInit();
#endif
if (s_ != INVALID_SOCKET) {
SetEnabledEvents(DE_READ | DE_WRITE);
int type = SOCK_STREAM;
socklen_t len = sizeof(type);
const int res =
getsockopt(s_, SOL_SOCKET, SO_TYPE, (SockOptArg)&type, &len);
RTC_DCHECK_EQ(0, res);
udp_ = (SOCK_DGRAM == type);
}
}
PhysicalSocket::~PhysicalSocket() {
Close();
}
bool PhysicalSocket::Create(int family, int type) {
Close();
s_ = ::socket(family, type, 0);
udp_ = (SOCK_DGRAM == type);
UpdateLastError();
if (udp_) {
SetEnabledEvents(DE_READ | DE_WRITE);
}
return s_ != INVALID_SOCKET;
}
SocketAddress PhysicalSocket::GetLocalAddress() const {
sockaddr_storage addr_storage = {0};
socklen_t addrlen = sizeof(addr_storage);
sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
int result = ::getsockname(s_, addr, &addrlen);
SocketAddress address;
if (result >= 0) {
SocketAddressFromSockAddrStorage(addr_storage, &address);
} else {
RTC_LOG(LS_WARNING) << "GetLocalAddress: unable to get local addr, socket="
<< s_;
}
return address;
}
SocketAddress PhysicalSocket::GetRemoteAddress() const {
sockaddr_storage addr_storage = {0};
socklen_t addrlen = sizeof(addr_storage);
sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
int result = ::getpeername(s_, addr, &addrlen);
SocketAddress address;
if (result >= 0) {
SocketAddressFromSockAddrStorage(addr_storage, &address);
} else {
RTC_LOG(LS_WARNING)
<< "GetRemoteAddress: unable to get remote addr, socket=" << s_;
}
return address;
}
int PhysicalSocket::Bind(const SocketAddress& bind_addr) {
SocketAddress copied_bind_addr = bind_addr;
// If a network binder is available, use it to bind a socket to an interface
// instead of bind(), since this is more reliable on an OS with a weak host
// model.
if (ss_->network_binder() && !bind_addr.IsAnyIP()) {
NetworkBindingResult result =
ss_->network_binder()->BindSocketToNetwork(s_, bind_addr.ipaddr());
if (result == NetworkBindingResult::SUCCESS) {
// Since the network binder handled binding the socket to the desired
// network interface, we don't need to (and shouldn't) include an IP in
// the bind() call; bind() just needs to assign a port.
copied_bind_addr.SetIP(GetAnyIP(copied_bind_addr.ipaddr().family()));
} else if (result == NetworkBindingResult::NOT_IMPLEMENTED) {
RTC_LOG(LS_INFO) << "Can't bind socket to network because "
"network binding is not implemented for this OS.";
} else {
if (bind_addr.IsLoopbackIP()) {
// If we couldn't bind to a loopback IP (which should only happen in
// test scenarios), continue on. This may be expected behavior.
RTC_LOG(LS_VERBOSE) << "Binding socket to loopback address "
<< bind_addr.ipaddr().ToString()
<< " failed; result: " << static_cast<int>(result);
} else {
RTC_LOG(LS_WARNING) << "Binding socket to network address "
<< bind_addr.ipaddr().ToString()
<< " failed; result: " << static_cast<int>(result);
// If a network binding was attempted and failed, we should stop here
// and not try to use the socket. Otherwise, we may end up sending
// packets with an invalid source address.
// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7026
return -1;
}
}
}
sockaddr_storage addr_storage;
size_t len = copied_bind_addr.ToSockAddrStorage(&addr_storage);
sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
int err = ::bind(s_, addr, static_cast<int>(len));
UpdateLastError();
#if !defined(NDEBUG)
if (0 == err) {
dbg_addr_ = "Bound @ ";
dbg_addr_.append(GetLocalAddress().ToString());
}
#endif
return err;
}
int PhysicalSocket::Connect(const SocketAddress& addr) {
// TODO(pthatcher): Implicit creation is required to reconnect...
// ...but should we make it more explicit?
if (state_ != CS_CLOSED) {
SetError(EALREADY);
return SOCKET_ERROR;
}
if (addr.IsUnresolvedIP()) {
RTC_LOG(LS_VERBOSE) << "Resolving addr in PhysicalSocket::Connect";
resolver_ = new AsyncResolver();
resolver_->SignalDone.connect(this, &PhysicalSocket::OnResolveResult);
resolver_->Start(addr);
state_ = CS_CONNECTING;
return 0;
}
return DoConnect(addr);
}
int PhysicalSocket::DoConnect(const SocketAddress& connect_addr) {
if ((s_ == INVALID_SOCKET) &&
!Create(connect_addr.family(), SOCK_STREAM)) {
return SOCKET_ERROR;
}
sockaddr_storage addr_storage;
size_t len = connect_addr.ToSockAddrStorage(&addr_storage);
sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
int err = ::connect(s_, addr, static_cast<int>(len));
UpdateLastError();
uint8_t events = DE_READ | DE_WRITE;
if (err == 0) {
state_ = CS_CONNECTED;
} else if (IsBlockingError(GetError())) {
state_ = CS_CONNECTING;
events |= DE_CONNECT;
} else {
return SOCKET_ERROR;
}
EnableEvents(events);
return 0;
}
int PhysicalSocket::GetError() const {
CritScope cs(&crit_);
return error_;
}
void PhysicalSocket::SetError(int error) {
CritScope cs(&crit_);
error_ = error;
}
AsyncSocket::ConnState PhysicalSocket::GetState() const {
return state_;
}
int PhysicalSocket::GetOption(Option opt, int* value) {
int slevel;
int sopt;
if (TranslateOption(opt, &slevel, &sopt) == -1)
return -1;
socklen_t optlen = sizeof(*value);
int ret = ::getsockopt(s_, slevel, sopt, (SockOptArg)value, &optlen);
if (ret != -1 && opt == OPT_DONTFRAGMENT) {
#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
*value = (*value != IP_PMTUDISC_DONT) ? 1 : 0;
#endif
}
return ret;
}
int PhysicalSocket::SetOption(Option opt, int value) {
int slevel;
int sopt;
if (TranslateOption(opt, &slevel, &sopt) == -1)
return -1;
if (opt == OPT_DONTFRAGMENT) {
#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
value = (value) ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
#endif
}
return ::setsockopt(s_, slevel, sopt, (SockOptArg)&value, sizeof(value));
}
int PhysicalSocket::Send(const void* pv, size_t cb) {
int sent = DoSend(s_, reinterpret_cast<const char *>(pv),
static_cast<int>(cb),
#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
// Suppress SIGPIPE. Without this, attempting to send on a socket whose
// other end is closed will result in a SIGPIPE signal being raised to
// our process, which by default will terminate the process, which we
// don't want. By specifying this flag, we'll just get the error EPIPE
// instead and can handle the error gracefully.
MSG_NOSIGNAL
#else
0
#endif
);
UpdateLastError();
MaybeRemapSendError();
// We have seen minidumps where this may be false.
RTC_DCHECK(sent <= static_cast<int>(cb));
if ((sent > 0 && sent < static_cast<int>(cb)) ||
(sent < 0 && IsBlockingError(GetError()))) {
EnableEvents(DE_WRITE);
}
return sent;
}
int PhysicalSocket::SendTo(const void* buffer,
size_t length,
const SocketAddress& addr) {
sockaddr_storage saddr;
size_t len = addr.ToSockAddrStorage(&saddr);
int sent = DoSendTo(
s_, static_cast<const char *>(buffer), static_cast<int>(length),
#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
// Suppress SIGPIPE. See above for explanation.
MSG_NOSIGNAL,
#else
0,
#endif
reinterpret_cast<sockaddr*>(&saddr), static_cast<int>(len));
UpdateLastError();
MaybeRemapSendError();
// We have seen minidumps where this may be false.
RTC_DCHECK(sent <= static_cast<int>(length));
if ((sent > 0 && sent < static_cast<int>(length)) ||
(sent < 0 && IsBlockingError(GetError()))) {
EnableEvents(DE_WRITE);
}
return sent;
}
int PhysicalSocket::Recv(void* buffer, size_t length, int64_t* timestamp) {
int received = ::recv(s_, static_cast<char*>(buffer),
static_cast<int>(length), 0);
if ((received == 0) && (length != 0)) {
// Note: on graceful shutdown, recv can return 0. In this case, we
// pretend it is blocking, and then signal close, so that simplifying
// assumptions can be made about Recv.
RTC_LOG(LS_WARNING) << "EOF from socket; deferring close event";
// Must turn this back on so that the select() loop will notice the close
// event.
EnableEvents(DE_READ);
SetError(EWOULDBLOCK);
return SOCKET_ERROR;
}
if (timestamp) {
*timestamp = GetSocketRecvTimestamp(s_);
}
UpdateLastError();
int error = GetError();
bool success = (received >= 0) || IsBlockingError(error);
if (udp_ || success) {
EnableEvents(DE_READ);
}
if (!success) {
RTC_LOG_F(LS_VERBOSE) << "Error = " << error;
}
return received;
}
int PhysicalSocket::RecvFrom(void* buffer,
size_t length,
SocketAddress* out_addr,
int64_t* timestamp) {
sockaddr_storage addr_storage;
socklen_t addr_len = sizeof(addr_storage);
sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
int received = ::recvfrom(s_, static_cast<char*>(buffer),
static_cast<int>(length), 0, addr, &addr_len);
if (timestamp) {
*timestamp = GetSocketRecvTimestamp(s_);
}
UpdateLastError();
if ((received >= 0) && (out_addr != nullptr))
SocketAddressFromSockAddrStorage(addr_storage, out_addr);
int error = GetError();
bool success = (received >= 0) || IsBlockingError(error);
if (udp_ || success) {
EnableEvents(DE_READ);
}
if (!success) {
RTC_LOG_F(LS_VERBOSE) << "Error = " << error;
}
return received;
}
int PhysicalSocket::Listen(int backlog) {
int err = ::listen(s_, backlog);
UpdateLastError();
if (err == 0) {
state_ = CS_CONNECTING;
EnableEvents(DE_ACCEPT);
#if !defined(NDEBUG)
dbg_addr_ = "Listening @ ";
dbg_addr_.append(GetLocalAddress().ToString());
#endif
}
return err;
}
AsyncSocket* PhysicalSocket::Accept(SocketAddress* out_addr) {
// Always re-subscribe DE_ACCEPT to make sure new incoming connections will
// trigger an event even if DoAccept returns an error here.
EnableEvents(DE_ACCEPT);
sockaddr_storage addr_storage;
socklen_t addr_len = sizeof(addr_storage);
sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
SOCKET s = DoAccept(s_, addr, &addr_len);
UpdateLastError();
if (s == INVALID_SOCKET)
return nullptr;
if (out_addr != nullptr)
SocketAddressFromSockAddrStorage(addr_storage, out_addr);
return ss_->WrapSocket(s);
}
int PhysicalSocket::Close() {
if (s_ == INVALID_SOCKET)
return 0;
int err = ::closesocket(s_);
UpdateLastError();
s_ = INVALID_SOCKET;
state_ = CS_CLOSED;
SetEnabledEvents(0);
if (resolver_) {
resolver_->Destroy(false);
resolver_ = nullptr;
}
return err;
}
SOCKET PhysicalSocket::DoAccept(SOCKET socket,
sockaddr* addr,
socklen_t* addrlen) {
return ::accept(socket, addr, addrlen);
}
int PhysicalSocket::DoSend(SOCKET socket, const char* buf, int len, int flags) {
return ::send(socket, buf, len, flags);
}
int PhysicalSocket::DoSendTo(SOCKET socket,
const char* buf,
int len,
int flags,
const struct sockaddr* dest_addr,
socklen_t addrlen) {
return ::sendto(socket, buf, len, flags, dest_addr, addrlen);
}
void PhysicalSocket::OnResolveResult(AsyncResolverInterface* resolver) {
if (resolver != resolver_) {
return;
}
int error = resolver_->GetError();
if (error == 0) {
error = DoConnect(resolver_->address());
} else {
Close();
}
if (error) {
SetError(error);
SignalCloseEvent(this, error);
}
}
void PhysicalSocket::UpdateLastError() {
SetError(RTC_LAST_SYSTEM_ERROR);
}
void PhysicalSocket::MaybeRemapSendError() {
#if defined(WEBRTC_MAC)
// https://developer.apple.com/library/mac/documentation/Darwin/
// Reference/ManPages/man2/sendto.2.html
// ENOBUFS - The output queue for a network interface is full.
// This generally indicates that the interface has stopped sending,
// but may be caused by transient congestion.
if (GetError() == ENOBUFS) {
SetError(EWOULDBLOCK);
}
#endif
}
void PhysicalSocket::SetEnabledEvents(uint8_t events) {
enabled_events_ = events;
}
void PhysicalSocket::EnableEvents(uint8_t events) {
enabled_events_ |= events;
}
void PhysicalSocket::DisableEvents(uint8_t events) {
enabled_events_ &= ~events;
}
int PhysicalSocket::TranslateOption(Option opt, int* slevel, int* sopt) {
switch (opt) {
case OPT_DONTFRAGMENT:
#if defined(WEBRTC_WIN)
*slevel = IPPROTO_IP;
*sopt = IP_DONTFRAGMENT;
break;
#elif defined(WEBRTC_MAC) || defined(WEBRTC_BSD) || defined(__native_client__)
RTC_LOG(LS_WARNING) << "Socket::OPT_DONTFRAGMENT not supported.";
return -1;
#elif defined(WEBRTC_POSIX)
*slevel = IPPROTO_IP;
*sopt = IP_MTU_DISCOVER;
break;
#endif
case OPT_RCVBUF:
*slevel = SOL_SOCKET;
*sopt = SO_RCVBUF;
break;
case OPT_SNDBUF:
*slevel = SOL_SOCKET;
*sopt = SO_SNDBUF;
break;
case OPT_NODELAY:
*slevel = IPPROTO_TCP;
*sopt = TCP_NODELAY;
break;
case OPT_DSCP:
RTC_LOG(LS_WARNING) << "Socket::OPT_DSCP not supported.";
return -1;
case OPT_RTP_SENDTIME_EXTN_ID:
return -1; // No logging is necessary as this not a OS socket option.
default:
RTC_NOTREACHED();
return -1;
}
return 0;
}
SocketDispatcher::SocketDispatcher(PhysicalSocketServer *ss)
#if defined(WEBRTC_WIN)
: PhysicalSocket(ss), id_(0), signal_close_(false)
#else
: PhysicalSocket(ss)
#endif
{
}
SocketDispatcher::SocketDispatcher(SOCKET s, PhysicalSocketServer *ss)
#if defined(WEBRTC_WIN)
: PhysicalSocket(ss, s), id_(0), signal_close_(false)
#else
: PhysicalSocket(ss, s)
#endif
{
}
SocketDispatcher::~SocketDispatcher() {
Close();
}
bool SocketDispatcher::Initialize() {
RTC_DCHECK(s_ != INVALID_SOCKET);
// Must be a non-blocking
#if defined(WEBRTC_WIN)
u_long argp = 1;
ioctlsocket(s_, FIONBIO, &argp);
#elif defined(WEBRTC_POSIX)
fcntl(s_, F_SETFL, fcntl(s_, F_GETFL, 0) | O_NONBLOCK);
#endif
#if defined(WEBRTC_IOS)
// iOS may kill sockets when the app is moved to the background
// (specifically, if the app doesn't use the "voip" UIBackgroundMode). When
// we attempt to write to such a socket, SIGPIPE will be raised, which by
// default will terminate the process, which we don't want. By specifying
// this socket option, SIGPIPE will be disabled for the socket.
int value = 1;
::setsockopt(s_, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value));
#endif
ss_->Add(this);
return true;
}
bool SocketDispatcher::Create(int type) {
return Create(AF_INET, type);
}
bool SocketDispatcher::Create(int family, int type) {
// Change the socket to be non-blocking.
if (!PhysicalSocket::Create(family, type))
return false;
if (!Initialize())
return false;
#if defined(WEBRTC_WIN)
do { id_ = ++next_id_; } while (id_ == 0);
#endif
return true;
}
#if defined(WEBRTC_WIN)
WSAEVENT SocketDispatcher::GetWSAEvent() {
return WSA_INVALID_EVENT;
}
SOCKET SocketDispatcher::GetSocket() {
return s_;
}
bool SocketDispatcher::CheckSignalClose() {
if (!signal_close_)
return false;
char ch;
if (recv(s_, &ch, 1, MSG_PEEK) > 0)
return false;
state_ = CS_CLOSED;
signal_close_ = false;
SignalCloseEvent(this, signal_err_);
return true;
}
int SocketDispatcher::next_id_ = 0;
#elif defined(WEBRTC_POSIX)
int SocketDispatcher::GetDescriptor() {
return s_;
}
bool SocketDispatcher::IsDescriptorClosed() {
if (udp_) {
// The MSG_PEEK trick doesn't work for UDP, since (at least in some
// circumstances) it requires reading an entire UDP packet, which would be
// bad for performance here. So, just check whether |s_| has been closed,
// which should be sufficient.
return s_ == INVALID_SOCKET;
}
// We don't have a reliable way of distinguishing end-of-stream
// from readability. So test on each readable call. Is this
// inefficient? Probably.
char ch;
ssize_t res = ::recv(s_, &ch, 1, MSG_PEEK);
if (res > 0) {
// Data available, so not closed.
return false;
} else if (res == 0) {
// EOF, so closed.
return true;
} else { // error
switch (errno) {
// Returned if we've already closed s_.
case EBADF:
// Returned during ungraceful peer shutdown.
case ECONNRESET:
return true;
// The normal blocking error; don't log anything.
case EWOULDBLOCK:
// Interrupted system call.
case EINTR:
return false;
default:
// Assume that all other errors are just blocking errors, meaning the
// connection is still good but we just can't read from it right now.
// This should only happen when connecting (and at most once), because
// in all other cases this function is only called if the file
// descriptor is already known to be in the readable state. However,
// it's not necessary a problem if we spuriously interpret a
// "connection lost"-type error as a blocking error, because typically
// the next recv() will get EOF, so we'll still eventually notice that
// the socket is closed.
RTC_LOG_ERR(LS_WARNING) << "Assuming benign blocking error";
return false;
}
}
}
#endif // WEBRTC_POSIX
uint32_t SocketDispatcher::GetRequestedEvents() {
return enabled_events();
}
void SocketDispatcher::OnPreEvent(uint32_t ff) {
if ((ff & DE_CONNECT) != 0)
state_ = CS_CONNECTED;
#if defined(WEBRTC_WIN)
// We set CS_CLOSED from CheckSignalClose.
#elif defined(WEBRTC_POSIX)
if ((ff & DE_CLOSE) != 0)
state_ = CS_CLOSED;
#endif
}
#if defined(WEBRTC_WIN)
void SocketDispatcher::OnEvent(uint32_t ff, int err) {
int cache_id = id_;
// Make sure we deliver connect/accept first. Otherwise, consumers may see
// something like a READ followed by a CONNECT, which would be odd.
if (((ff & DE_CONNECT) != 0) && (id_ == cache_id)) {
if (ff != DE_CONNECT)
RTC_LOG(LS_VERBOSE) << "Signalled with DE_CONNECT: " << ff;
DisableEvents(DE_CONNECT);
#if !defined(NDEBUG)
dbg_addr_ = "Connected @ ";
dbg_addr_.append(GetRemoteAddress().ToString());
#endif
SignalConnectEvent(this);
}
if (((ff & DE_ACCEPT) != 0) && (id_ == cache_id)) {
DisableEvents(DE_ACCEPT);
SignalReadEvent(this);
}
if ((ff & DE_READ) != 0) {
DisableEvents(DE_READ);
SignalReadEvent(this);
}
if (((ff & DE_WRITE) != 0) && (id_ == cache_id)) {
DisableEvents(DE_WRITE);
SignalWriteEvent(this);
}
if (((ff & DE_CLOSE) != 0) && (id_ == cache_id)) {
signal_close_ = true;
signal_err_ = err;
}
}
#elif defined(WEBRTC_POSIX)
void SocketDispatcher::OnEvent(uint32_t ff, int err) {
#if defined(WEBRTC_USE_EPOLL)
// Remember currently enabled events so we can combine multiple changes
// into one update call later.
// The signal handlers might re-enable events disabled here, so we can't
// keep a list of events to disable at the end of the method. This list
// would not be updated with the events enabled by the signal handlers.
StartBatchedEventUpdates();
#endif
// Make sure we deliver connect/accept first. Otherwise, consumers may see
// something like a READ followed by a CONNECT, which would be odd.
if ((ff & DE_CONNECT) != 0) {
DisableEvents(DE_CONNECT);
SignalConnectEvent(this);
}
if ((ff & DE_ACCEPT) != 0) {
DisableEvents(DE_ACCEPT);
SignalReadEvent(this);
}
if ((ff & DE_READ) != 0) {
DisableEvents(DE_READ);
SignalReadEvent(this);
}
if ((ff & DE_WRITE) != 0) {
DisableEvents(DE_WRITE);
SignalWriteEvent(this);
}
if ((ff & DE_CLOSE) != 0) {
// The socket is now dead to us, so stop checking it.
SetEnabledEvents(0);
SignalCloseEvent(this, err);
}
#if defined(WEBRTC_USE_EPOLL)
FinishBatchedEventUpdates();
#endif
}
#endif // WEBRTC_POSIX
#if defined(WEBRTC_USE_EPOLL)
static int GetEpollEvents(uint32_t ff) {
int events = 0;
if (ff & (DE_READ | DE_ACCEPT)) {
events |= EPOLLIN;
}
if (ff & (DE_WRITE | DE_CONNECT)) {
events |= EPOLLOUT;
}
return events;
}
void SocketDispatcher::StartBatchedEventUpdates() {
RTC_DCHECK_EQ(saved_enabled_events_, -1);
saved_enabled_events_ = enabled_events();
}
void SocketDispatcher::FinishBatchedEventUpdates() {
RTC_DCHECK_NE(saved_enabled_events_, -1);
uint8_t old_events = static_cast<uint8_t>(saved_enabled_events_);
saved_enabled_events_ = -1;
MaybeUpdateDispatcher(old_events);
}
void SocketDispatcher::MaybeUpdateDispatcher(uint8_t old_events) {
if (GetEpollEvents(enabled_events()) != GetEpollEvents(old_events) &&
saved_enabled_events_ == -1) {
ss_->Update(this);
}
}
void SocketDispatcher::SetEnabledEvents(uint8_t events) {
uint8_t old_events = enabled_events();
PhysicalSocket::SetEnabledEvents(events);
MaybeUpdateDispatcher(old_events);
}
void SocketDispatcher::EnableEvents(uint8_t events) {
uint8_t old_events = enabled_events();
PhysicalSocket::EnableEvents(events);
MaybeUpdateDispatcher(old_events);
}
void SocketDispatcher::DisableEvents(uint8_t events) {
uint8_t old_events = enabled_events();
PhysicalSocket::DisableEvents(events);
MaybeUpdateDispatcher(old_events);
}
#endif // WEBRTC_USE_EPOLL
int SocketDispatcher::Close() {
if (s_ == INVALID_SOCKET)
return 0;
#if defined(WEBRTC_WIN)
id_ = 0;
signal_close_ = false;
#endif
ss_->Remove(this);
return PhysicalSocket::Close();
}
#if defined(WEBRTC_POSIX)
class EventDispatcher : public Dispatcher {
public:
EventDispatcher(PhysicalSocketServer* ss) : ss_(ss), fSignaled_(false) {
if (pipe(afd_) < 0)
RTC_LOG(LERROR) << "pipe failed";
ss_->Add(this);
}
~EventDispatcher() override {
ss_->Remove(this);
close(afd_[0]);
close(afd_[1]);
}
virtual void Signal() {
CritScope cs(&crit_);
if (!fSignaled_) {
const uint8_t b[1] = {0};
const ssize_t res = write(afd_[1], b, sizeof(b));
RTC_DCHECK_EQ(1, res);
fSignaled_ = true;
}
}
uint32_t GetRequestedEvents() override { return DE_READ; }
void OnPreEvent(uint32_t ff) override {
// It is not possible to perfectly emulate an auto-resetting event with
// pipes. This simulates it by resetting before the event is handled.
CritScope cs(&crit_);
if (fSignaled_) {
uint8_t b[4]; // Allow for reading more than 1 byte, but expect 1.
const ssize_t res = read(afd_[0], b, sizeof(b));
RTC_DCHECK_EQ(1, res);
fSignaled_ = false;
}
}
void OnEvent(uint32_t ff, int err) override { RTC_NOTREACHED(); }
int GetDescriptor() override { return afd_[0]; }
bool IsDescriptorClosed() override { return false; }
private:
PhysicalSocketServer *ss_;
int afd_[2];
bool fSignaled_;
CriticalSection crit_;
};
// These two classes use the self-pipe trick to deliver POSIX signals to our
// select loop. This is the only safe, reliable, cross-platform way to do
// non-trivial things with a POSIX signal in an event-driven program (until
// proper pselect() implementations become ubiquitous).
class PosixSignalHandler {
public:
// POSIX only specifies 32 signals, but in principle the system might have
// more and the programmer might choose to use them, so we size our array
// for 128.
static const int kNumPosixSignals = 128;
// There is just a single global instance. (Signal handlers do not get any
// sort of user-defined void * parameter, so they can't access anything that
// isn't global.)
static PosixSignalHandler* Instance() {
RTC_DEFINE_STATIC_LOCAL(PosixSignalHandler, instance, ());
return &instance;
}
// Returns true if the given signal number is set.
bool IsSignalSet(int signum) const {
RTC_DCHECK(signum < static_cast<int>(arraysize(received_signal_)));
if (signum < static_cast<int>(arraysize(received_signal_))) {
return received_signal_[signum];
} else {
return false;
}
}
// Clears the given signal number.
void ClearSignal(int signum) {
RTC_DCHECK(signum < static_cast<int>(arraysize(received_signal_)));
if (signum < static_cast<int>(arraysize(received_signal_))) {
received_signal_[signum] = false;
}
}
// Returns the file descriptor to monitor for signal events.
int GetDescriptor() const {
return afd_[0];
}
// This is called directly from our real signal handler, so it must be
// signal-handler-safe. That means it cannot assume anything about the
// user-level state of the process, since the handler could be executed at any
// time on any thread.
void OnPosixSignalReceived(int signum) {
if (signum >= static_cast<int>(arraysize(received_signal_))) {
// We don't have space in our array for this.
return;
}
// Set a flag saying we've seen this signal.
received_signal_[signum] = true;
// Notify application code that we got a signal.
const uint8_t b[1] = {0};
if (-1 == write(afd_[1], b, sizeof(b))) {
// Nothing we can do here. If there's an error somehow then there's
// nothing we can safely do from a signal handler.
// No, we can't even safely log it.
// But, we still have to check the return value here. Otherwise,
// GCC 4.4.1 complains ignoring return value. Even (void) doesn't help.
return;
}
}
private:
PosixSignalHandler() {
if (pipe(afd_) < 0) {
RTC_LOG_ERR(LS_ERROR) << "pipe failed";
return;
}
if (fcntl(afd_[0], F_SETFL, O_NONBLOCK) < 0) {
RTC_LOG_ERR(LS_WARNING) << "fcntl #1 failed";
}
if (fcntl(afd_[1], F_SETFL, O_NONBLOCK) < 0) {
RTC_LOG_ERR(LS_WARNING) << "fcntl #2 failed";
}
memset(const_cast<void *>(static_cast<volatile void *>(received_signal_)),
0,
sizeof(received_signal_));
}
~PosixSignalHandler() {
int fd1 = afd_[0];
int fd2 = afd_[1];
// We clobber the stored file descriptor numbers here or else in principle
// a signal that happens to be delivered during application termination
// could erroneously write a zero byte to an unrelated file handle in
// OnPosixSignalReceived() if some other file happens to be opened later
// during shutdown and happens to be given the same file descriptor number
// as our pipe had. Unfortunately even with this precaution there is still a
// race where that could occur if said signal happens to be handled
// concurrently with this code and happens to have already read the value of
// afd_[1] from memory before we clobber it, but that's unlikely.
afd_[0] = -1;
afd_[1] = -1;
close(fd1);
close(fd2);
}
int afd_[2];
// These are boolean flags that will be set in our signal handler and read
// and cleared from Wait(). There is a race involved in this, but it is
// benign. The signal handler sets the flag before signaling the pipe, so
// we'll never end up blocking in select() while a flag is still true.
// However, if two of the same signal arrive close to each other then it's
// possible that the second time the handler may set the flag while it's still
// true, meaning that signal will be missed. But the first occurrence of it
// will still be handled, so this isn't a problem.
// Volatile is not necessary here for correctness, but this data _is_ volatile
// so I've marked it as such.
volatile uint8_t received_signal_[kNumPosixSignals];
};
class PosixSignalDispatcher : public Dispatcher {
public:
PosixSignalDispatcher(PhysicalSocketServer *owner) : owner_(owner) {
owner_->Add(this);
}
~PosixSignalDispatcher() override {
owner_->Remove(this);
}
uint32_t GetRequestedEvents() override { return DE_READ; }
void OnPreEvent(uint32_t ff) override {
// Events might get grouped if signals come very fast, so we read out up to
// 16 bytes to make sure we keep the pipe empty.
uint8_t b[16];
ssize_t ret = read(GetDescriptor(), b, sizeof(b));
if (ret < 0) {
RTC_LOG_ERR(LS_WARNING) << "Error in read()";
} else if (ret == 0) {
RTC_LOG(LS_WARNING) << "Should have read at least one byte";
}
}
void OnEvent(uint32_t ff, int err) override {
for (int signum = 0; signum < PosixSignalHandler::kNumPosixSignals;
++signum) {
if (PosixSignalHandler::Instance()->IsSignalSet(signum)) {
PosixSignalHandler::Instance()->ClearSignal(signum);
HandlerMap::iterator i = handlers_.find(signum);
if (i == handlers_.end()) {
// This can happen if a signal is delivered to our process at around
// the same time as we unset our handler for it. It is not an error
// condition, but it's unusual enough to be worth logging.
RTC_LOG(LS_INFO) << "Received signal with no handler: " << signum;
} else {
// Otherwise, execute our handler.
(*i->second)(signum);
}
}
}
}
int GetDescriptor() override {
return PosixSignalHandler::Instance()->GetDescriptor();
}
bool IsDescriptorClosed() override { return false; }
void SetHandler(int signum, void (*handler)(int)) {
handlers_[signum] = handler;
}
void ClearHandler(int signum) {
handlers_.erase(signum);
}
bool HasHandlers() {
return !handlers_.empty();
}
private:
typedef std::map<int, void (*)(int)> HandlerMap;
HandlerMap handlers_;
// Our owner.
PhysicalSocketServer *owner_;
};
#endif // WEBRTC_POSIX
#if defined(WEBRTC_WIN)
static uint32_t FlagsToEvents(uint32_t events) {
uint32_t ffFD = FD_CLOSE;
if (events & DE_READ)
ffFD |= FD_READ;
if (events & DE_WRITE)
ffFD |= FD_WRITE;
if (events & DE_CONNECT)
ffFD |= FD_CONNECT;
if (events & DE_ACCEPT)
ffFD |= FD_ACCEPT;
return ffFD;
}
class EventDispatcher : public Dispatcher {
public:
EventDispatcher(PhysicalSocketServer *ss) : ss_(ss) {
hev_ = WSACreateEvent();
if (hev_) {
ss_->Add(this);
}
}
~EventDispatcher() override {
if (hev_ != nullptr) {
ss_->Remove(this);
WSACloseEvent(hev_);
hev_ = nullptr;
}
}
virtual void Signal() {
if (hev_ != nullptr)
WSASetEvent(hev_);
}
uint32_t GetRequestedEvents() override { return 0; }
void OnPreEvent(uint32_t ff) override { WSAResetEvent(hev_); }
void OnEvent(uint32_t ff, int err) override {}
WSAEVENT GetWSAEvent() override { return hev_; }
SOCKET GetSocket() override { return INVALID_SOCKET; }
bool CheckSignalClose() override { return false; }
private:
PhysicalSocketServer* ss_;
WSAEVENT hev_;
};
#endif // WEBRTC_WIN
// Sets the value of a boolean value to false when signaled.
class Signaler : public EventDispatcher {
public:
Signaler(PhysicalSocketServer* ss, bool* pf)
: EventDispatcher(ss), pf_(pf) {
}
~Signaler() override { }
void OnEvent(uint32_t ff, int err) override {
if (pf_)
*pf_ = false;
}
private:
bool *pf_;
};
PhysicalSocketServer::PhysicalSocketServer()
: fWait_(false) {
#if defined(WEBRTC_USE_EPOLL)
// Since Linux 2.6.8, the size argument is ignored, but must be greater than
// zero. Before that the size served as hint to the kernel for the amount of
// space to initially allocate in internal data structures.
epoll_fd_ = epoll_create(FD_SETSIZE);
if (epoll_fd_ == -1) {
// Not an error, will fall back to "select" below.
RTC_LOG_E(LS_WARNING, EN, errno) << "epoll_create";
epoll_fd_ = INVALID_SOCKET;
}
#endif
signal_wakeup_ = new Signaler(this, &fWait_);
#if defined(WEBRTC_WIN)
socket_ev_ = WSACreateEvent();
#endif
}
PhysicalSocketServer::~PhysicalSocketServer() {
#if defined(WEBRTC_WIN)
WSACloseEvent(socket_ev_);
#endif
#if defined(WEBRTC_POSIX)
signal_dispatcher_.reset();
#endif
delete signal_wakeup_;
#if defined(WEBRTC_USE_EPOLL)
if (epoll_fd_ != INVALID_SOCKET) {
close(epoll_fd_);
}
#endif
RTC_DCHECK(dispatchers_.empty());
}
void PhysicalSocketServer::WakeUp() {
signal_wakeup_->Signal();
}
Socket* PhysicalSocketServer::CreateSocket(int type) {
return CreateSocket(AF_INET, type);
}
Socket* PhysicalSocketServer::CreateSocket(int family, int type) {
PhysicalSocket* socket = new PhysicalSocket(this);
if (socket->Create(family, type)) {
return socket;
} else {
delete socket;
return nullptr;
}
}
AsyncSocket* PhysicalSocketServer::CreateAsyncSocket(int type) {
return CreateAsyncSocket(AF_INET, type);
}
AsyncSocket* PhysicalSocketServer::CreateAsyncSocket(int family, int type) {
SocketDispatcher* dispatcher = new SocketDispatcher(this);
if (dispatcher->Create(family, type)) {
return dispatcher;
} else {
delete dispatcher;
return nullptr;
}
}
AsyncSocket* PhysicalSocketServer::WrapSocket(SOCKET s) {
SocketDispatcher* dispatcher = new SocketDispatcher(s, this);
if (dispatcher->Initialize()) {
return dispatcher;
} else {
delete dispatcher;
return nullptr;
}
}
void PhysicalSocketServer::Add(Dispatcher *pdispatcher) {
CritScope cs(&crit_);
if (processing_dispatchers_) {
// A dispatcher is being added while a "Wait" call is processing the
// list of socket events.
// Defer adding to "dispatchers_" set until processing is done to avoid
// invalidating the iterator in "Wait".
pending_remove_dispatchers_.erase(pdispatcher);
pending_add_dispatchers_.insert(pdispatcher);
} else {
dispatchers_.insert(pdispatcher);
}
#if defined(WEBRTC_USE_EPOLL)
if (epoll_fd_ != INVALID_SOCKET) {
AddEpoll(pdispatcher);
}
#endif // WEBRTC_USE_EPOLL
}
void PhysicalSocketServer::Remove(Dispatcher *pdispatcher) {
CritScope cs(&crit_);
if (processing_dispatchers_) {
// A dispatcher is being removed while a "Wait" call is processing the
// list of socket events.
// Defer removal from "dispatchers_" set until processing is done to avoid
// invalidating the iterator in "Wait".
if (!pending_add_dispatchers_.erase(pdispatcher) &&
dispatchers_.find(pdispatcher) == dispatchers_.end()) {
RTC_LOG(LS_WARNING) << "PhysicalSocketServer asked to remove a unknown "
<< "dispatcher, potentially from a duplicate call to "
<< "Add.";
return;
}
pending_remove_dispatchers_.insert(pdispatcher);
} else if (!dispatchers_.erase(pdispatcher)) {
RTC_LOG(LS_WARNING)
<< "PhysicalSocketServer asked to remove a unknown "
<< "dispatcher, potentially from a duplicate call to Add.";
return;
}
#if defined(WEBRTC_USE_EPOLL)
if (epoll_fd_ != INVALID_SOCKET) {
RemoveEpoll(pdispatcher);
}
#endif // WEBRTC_USE_EPOLL
}
void PhysicalSocketServer::Update(Dispatcher* pdispatcher) {
#if defined(WEBRTC_USE_EPOLL)
if (epoll_fd_ == INVALID_SOCKET) {
return;
}
CritScope cs(&crit_);
if (dispatchers_.find(pdispatcher) == dispatchers_.end()) {
return;
}
UpdateEpoll(pdispatcher);
#endif
}
void PhysicalSocketServer::AddRemovePendingDispatchers() {
if (!pending_add_dispatchers_.empty()) {
for (Dispatcher* pdispatcher : pending_add_dispatchers_) {
dispatchers_.insert(pdispatcher);
}
pending_add_dispatchers_.clear();
}
if (!pending_remove_dispatchers_.empty()) {
for (Dispatcher* pdispatcher : pending_remove_dispatchers_) {
dispatchers_.erase(pdispatcher);
}
pending_remove_dispatchers_.clear();
}
}
#if defined(WEBRTC_POSIX)
bool PhysicalSocketServer::Wait(int cmsWait, bool process_io) {
#if defined(WEBRTC_USE_EPOLL)
// We don't keep a dedicated "epoll" descriptor containing only the non-IO
// (i.e. signaling) dispatcher, so "poll" will be used instead of the default
// "select" to support sockets larger than FD_SETSIZE.
if (!process_io) {
return WaitPoll(cmsWait, signal_wakeup_);
} else if (epoll_fd_ != INVALID_SOCKET) {
return WaitEpoll(cmsWait);
}
#endif
return WaitSelect(cmsWait, process_io);
}
static void ProcessEvents(Dispatcher* dispatcher,
bool readable,
bool writable,
bool check_error) {
int errcode = 0;
// TODO(pthatcher): Should we set errcode if getsockopt fails?
if (check_error) {
socklen_t len = sizeof(errcode);
::getsockopt(dispatcher->GetDescriptor(), SOL_SOCKET, SO_ERROR, &errcode,
&len);
}
uint32_t ff = 0;
// Check readable descriptors. If we're waiting on an accept, signal
// that. Otherwise we're waiting for data, check to see if we're
// readable or really closed.
// TODO(pthatcher): Only peek at TCP descriptors.
if (readable) {
if (dispatcher->GetRequestedEvents() & DE_ACCEPT) {
ff |= DE_ACCEPT;
} else if (errcode || dispatcher->IsDescriptorClosed()) {
ff |= DE_CLOSE;
} else {
ff |= DE_READ;
}
}
// Check writable descriptors. If we're waiting on a connect, detect
// success versus failure by the reaped error code.
if (writable) {
if (dispatcher->GetRequestedEvents() & DE_CONNECT) {
if (!errcode) {
ff |= DE_CONNECT;
} else {
ff |= DE_CLOSE;
}
} else {
ff |= DE_WRITE;
}
}
// Tell the descriptor about the event.
if (ff != 0) {
dispatcher->OnPreEvent(ff);
dispatcher->OnEvent(ff, errcode);
}
}
bool PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) {
// Calculate timing information
struct timeval* ptvWait = nullptr;
struct timeval tvWait;
struct timeval tvStop;
if (cmsWait != kForever) {
// Calculate wait timeval
tvWait.tv_sec = cmsWait / 1000;
tvWait.tv_usec = (cmsWait % 1000) * 1000;
ptvWait = &tvWait;
// Calculate when to return in a timeval
gettimeofday(&tvStop, nullptr);
tvStop.tv_sec += tvWait.tv_sec;
tvStop.tv_usec += tvWait.tv_usec;
if (tvStop.tv_usec >= 1000000) {
tvStop.tv_usec -= 1000000;
tvStop.tv_sec += 1;
}
}
// Zero all fd_sets. Don't need to do this inside the loop since
// select() zeros the descriptors not signaled
fd_set fdsRead;
FD_ZERO(&fdsRead);
fd_set fdsWrite;
FD_ZERO(&fdsWrite);
// Explicitly unpoison these FDs on MemorySanitizer which doesn't handle the
// inline assembly in FD_ZERO.
// http://crbug.com/344505
#ifdef MEMORY_SANITIZER
__msan_unpoison(&fdsRead, sizeof(fdsRead));
__msan_unpoison(&fdsWrite, sizeof(fdsWrite));
#endif
fWait_ = true;
while (fWait_) {
int fdmax = -1;
{
CritScope cr(&crit_);
// TODO(jbauch): Support re-entrant waiting.
RTC_DCHECK(!processing_dispatchers_);
for (Dispatcher* pdispatcher : dispatchers_) {
// Query dispatchers for read and write wait state
RTC_DCHECK(pdispatcher);
if (!process_io && (pdispatcher != signal_wakeup_))
continue;
int fd = pdispatcher->GetDescriptor();
// "select"ing a file descriptor that is equal to or larger than
// FD_SETSIZE will result in undefined behavior.
RTC_CHECK_LT(fd, FD_SETSIZE);
if (fd > fdmax)
fdmax = fd;
uint32_t ff = pdispatcher->GetRequestedEvents();
if (ff & (DE_READ | DE_ACCEPT))
FD_SET(fd, &fdsRead);
if (ff & (DE_WRITE | DE_CONNECT))
FD_SET(fd, &fdsWrite);
}
}
// Wait then call handlers as appropriate
// < 0 means error
// 0 means timeout
// > 0 means count of descriptors ready
int n = select(fdmax + 1, &fdsRead, &fdsWrite, nullptr, ptvWait);
// If error, return error.
if (n < 0) {
if (errno != EINTR) {
RTC_LOG_E(LS_ERROR, EN, errno) << "select";
return false;
}
// Else ignore the error and keep going. If this EINTR was for one of the
// signals managed by this PhysicalSocketServer, the
// PosixSignalDeliveryDispatcher will be in the signaled state in the next
// iteration.
} else if (n == 0) {
// If timeout, return success
return true;
} else {
// We have signaled descriptors
CritScope cr(&crit_);
processing_dispatchers_ = true;
for (Dispatcher* pdispatcher : dispatchers_) {
int fd = pdispatcher->GetDescriptor();
bool readable = FD_ISSET(fd, &fdsRead);
if (readable) {
FD_CLR(fd, &fdsRead);
}
bool writable = FD_ISSET(fd, &fdsWrite);
if (writable) {
FD_CLR(fd, &fdsWrite);
}
// The error code can be signaled through reads or writes.
ProcessEvents(pdispatcher, readable, writable, readable || writable);
}
processing_dispatchers_ = false;
// Process deferred dispatchers that have been added/removed while the
// events were handled above.
AddRemovePendingDispatchers();
}
// Recalc the time remaining to wait. Doing it here means it doesn't get
// calced twice the first time through the loop
if (ptvWait) {
ptvWait->tv_sec = 0;
ptvWait->tv_usec = 0;
struct timeval tvT;
gettimeofday(&tvT, nullptr);
if ((tvStop.tv_sec > tvT.tv_sec)
|| ((tvStop.tv_sec == tvT.tv_sec)
&& (tvStop.tv_usec > tvT.tv_usec))) {
ptvWait->tv_sec = tvStop.tv_sec - tvT.tv_sec;
ptvWait->tv_usec = tvStop.tv_usec - tvT.tv_usec;
if (ptvWait->tv_usec < 0) {
RTC_DCHECK(ptvWait->tv_sec > 0);
ptvWait->tv_usec += 1000000;
ptvWait->tv_sec -= 1;
}
}
}
}
return true;
}
#if defined(WEBRTC_USE_EPOLL)
// Initial number of events to process with one call to "epoll_wait".
static const size_t kInitialEpollEvents = 128;
// Maximum number of events to process with one call to "epoll_wait".
static const size_t kMaxEpollEvents = 8192;
void PhysicalSocketServer::AddEpoll(Dispatcher* pdispatcher) {
RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
int fd = pdispatcher->GetDescriptor();
RTC_DCHECK(fd != INVALID_SOCKET);
if (fd == INVALID_SOCKET) {
return;
}
struct epoll_event event = {0};
event.events = GetEpollEvents(pdispatcher->GetRequestedEvents());
event.data.ptr = pdispatcher;
int err = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event);
RTC_DCHECK_EQ(err, 0);
if (err == -1) {
RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_ADD";
}
}
void PhysicalSocketServer::RemoveEpoll(Dispatcher* pdispatcher) {
RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
int fd = pdispatcher->GetDescriptor();
RTC_DCHECK(fd != INVALID_SOCKET);
if (fd == INVALID_SOCKET) {
return;
}
struct epoll_event event = {0};
int err = epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, &event);
RTC_DCHECK(err == 0 || errno == ENOENT);
if (err == -1) {
if (errno == ENOENT) {
// Socket has already been closed.
RTC_LOG_E(LS_VERBOSE, EN, errno) << "epoll_ctl EPOLL_CTL_DEL";
} else {
RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_DEL";
}
}
}
void PhysicalSocketServer::UpdateEpoll(Dispatcher* pdispatcher) {
RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
int fd = pdispatcher->GetDescriptor();
RTC_DCHECK(fd != INVALID_SOCKET);
if (fd == INVALID_SOCKET) {
return;
}
struct epoll_event event = {0};
event.events = GetEpollEvents(pdispatcher->GetRequestedEvents());
event.data.ptr = pdispatcher;
int err = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, fd, &event);
RTC_DCHECK_EQ(err, 0);
if (err == -1) {
RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_MOD";
}
}
bool PhysicalSocketServer::WaitEpoll(int cmsWait) {
RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
int64_t tvWait = -1;
int64_t tvStop = -1;
if (cmsWait != kForever) {
tvWait = cmsWait;
tvStop = TimeAfter(cmsWait);
}
if (epoll_events_.empty()) {
// The initial space to receive events is created only if epoll is used.
epoll_events_.resize(kInitialEpollEvents);
}
fWait_ = true;
while (fWait_) {
// Wait then call handlers as appropriate
// < 0 means error
// 0 means timeout
// > 0 means count of descriptors ready
int n = epoll_wait(epoll_fd_, &epoll_events_[0],
static_cast<int>(epoll_events_.size()),
static_cast<int>(tvWait));
if (n < 0) {
if (errno != EINTR) {
RTC_LOG_E(LS_ERROR, EN, errno) << "epoll";
return false;
}
// Else ignore the error and keep going. If this EINTR was for one of the
// signals managed by this PhysicalSocketServer, the
// PosixSignalDeliveryDispatcher will be in the signaled state in the next
// iteration.
} else if (n == 0) {
// If timeout, return success
return true;
} else {
// We have signaled descriptors
CritScope cr(&crit_);
for (int i = 0; i < n; ++i) {
const epoll_event& event = epoll_events_[i];
Dispatcher* pdispatcher = static_cast<Dispatcher*>(event.data.ptr);
if (dispatchers_.find(pdispatcher) == dispatchers_.end()) {
// The dispatcher for this socket no longer exists.
continue;
}
bool readable = (event.events & (EPOLLIN | EPOLLPRI));
bool writable = (event.events & EPOLLOUT);
bool check_error = (event.events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP));
ProcessEvents(pdispatcher, readable, writable, check_error);
}
}
if (static_cast<size_t>(n) == epoll_events_.size() &&
epoll_events_.size() < kMaxEpollEvents) {
// We used the complete space to receive events, increase size for future
// iterations.
epoll_events_.resize(std::max(epoll_events_.size() * 2, kMaxEpollEvents));
}
if (cmsWait != kForever) {
tvWait = TimeDiff(tvStop, TimeMillis());
if (tvWait < 0) {
// Return success on timeout.
return true;
}
}
}
return true;
}
bool PhysicalSocketServer::WaitPoll(int cmsWait, Dispatcher* dispatcher) {
RTC_DCHECK(dispatcher);
int64_t tvWait = -1;
int64_t tvStop = -1;
if (cmsWait != kForever) {
tvWait = cmsWait;
tvStop = TimeAfter(cmsWait);
}
fWait_ = true;
struct pollfd fds = {0};
int fd = dispatcher->GetDescriptor();
fds.fd = fd;
while (fWait_) {
uint32_t ff = dispatcher->GetRequestedEvents();
fds.events = 0;
if (ff & (DE_READ | DE_ACCEPT)) {
fds.events |= POLLIN;
}
if (ff & (DE_WRITE | DE_CONNECT)) {
fds.events |= POLLOUT;
}
fds.revents = 0;
// Wait then call handlers as appropriate
// < 0 means error
// 0 means timeout
// > 0 means count of descriptors ready
int n = poll(&fds, 1, static_cast<int>(tvWait));
if (n < 0) {
if (errno != EINTR) {
RTC_LOG_E(LS_ERROR, EN, errno) << "poll";
return false;
}
// Else ignore the error and keep going. If this EINTR was for one of the
// signals managed by this PhysicalSocketServer, the
// PosixSignalDeliveryDispatcher will be in the signaled state in the next
// iteration.
} else if (n == 0) {
// If timeout, return success
return true;
} else {
// We have signaled descriptors (should only be the passed dispatcher).
RTC_DCHECK_EQ(n, 1);
RTC_DCHECK_EQ(fds.fd, fd);
bool readable = (fds.revents & (POLLIN | POLLPRI));
bool writable = (fds.revents & POLLOUT);
bool check_error = (fds.revents & (POLLRDHUP | POLLERR | POLLHUP));
ProcessEvents(dispatcher, readable, writable, check_error);
}
if (cmsWait != kForever) {
tvWait = TimeDiff(tvStop, TimeMillis());
if (tvWait < 0) {
// Return success on timeout.
return true;
}
}
}
return true;
}
#endif // WEBRTC_USE_EPOLL
static void GlobalSignalHandler(int signum) {
PosixSignalHandler::Instance()->OnPosixSignalReceived(signum);
}
bool PhysicalSocketServer::SetPosixSignalHandler(int signum,
void (*handler)(int)) {
// If handler is SIG_IGN or SIG_DFL then clear our user-level handler,
// otherwise set one.
if (handler == SIG_IGN || handler == SIG_DFL) {
if (!InstallSignal(signum, handler)) {
return false;
}
if (signal_dispatcher_) {
signal_dispatcher_->ClearHandler(signum);
if (!signal_dispatcher_->HasHandlers()) {
signal_dispatcher_.reset();
}
}
} else {
if (!signal_dispatcher_) {
signal_dispatcher_.reset(new PosixSignalDispatcher(this));
}
signal_dispatcher_->SetHandler(signum, handler);
if (!InstallSignal(signum, &GlobalSignalHandler)) {
return false;
}
}
return true;
}
Dispatcher* PhysicalSocketServer::signal_dispatcher() {
return signal_dispatcher_.get();
}
bool PhysicalSocketServer::InstallSignal(int signum, void (*handler)(int)) {
struct sigaction act;
// It doesn't really matter what we set this mask to.
if (sigemptyset(&act.sa_mask) != 0) {
RTC_LOG_ERR(LS_ERROR) << "Couldn't set mask";
return false;
}
act.sa_handler = handler;
#if !defined(__native_client__)
// Use SA_RESTART so that our syscalls don't get EINTR, since we don't need it
// and it's a nuisance. Though some syscalls still return EINTR and there's no
// real standard for which ones. :(
act.sa_flags = SA_RESTART;
#else
act.sa_flags = 0;
#endif
if (sigaction(signum, &act, nullptr) != 0) {
RTC_LOG_ERR(LS_ERROR) << "Couldn't set sigaction";
return false;
}
return true;
}
#endif // WEBRTC_POSIX
#if defined(WEBRTC_WIN)
bool PhysicalSocketServer::Wait(int cmsWait, bool process_io) {
int64_t cmsTotal = cmsWait;
int64_t cmsElapsed = 0;
int64_t msStart = Time();
fWait_ = true;
while (fWait_) {
std::vector<WSAEVENT> events;
std::vector<Dispatcher *> event_owners;
events.push_back(socket_ev_);
{
CritScope cr(&crit_);
// TODO(jbauch): Support re-entrant waiting.
RTC_DCHECK(!processing_dispatchers_);
// Calling "CheckSignalClose" might remove a closed dispatcher from the
// set. This must be deferred to prevent invalidating the iterator.
processing_dispatchers_ = true;
for (Dispatcher* disp : dispatchers_) {
if (!process_io && (disp != signal_wakeup_))
continue;
SOCKET s = disp->GetSocket();
if (disp->CheckSignalClose()) {
// We just signalled close, don't poll this socket
} else if (s != INVALID_SOCKET) {
WSAEventSelect(s,
events[0],
FlagsToEvents(disp->GetRequestedEvents()));
} else {
events.push_back(disp->GetWSAEvent());
event_owners.push_back(disp);
}
}
processing_dispatchers_ = false;
// Process deferred dispatchers that have been added/removed while the
// events were handled above.
AddRemovePendingDispatchers();
}
// Which is shorter, the delay wait or the asked wait?
int64_t cmsNext;
if (cmsWait == kForever) {
cmsNext = cmsWait;
} else {
cmsNext = std::max<int64_t>(0, cmsTotal - cmsElapsed);
}
// Wait for one of the events to signal
DWORD dw = WSAWaitForMultipleEvents(static_cast<DWORD>(events.size()),
&events[0],
false,
static_cast<DWORD>(cmsNext),
false);
if (dw == WSA_WAIT_FAILED) {
// Failed?
// TODO(pthatcher): need a better strategy than this!
WSAGetLastError();
RTC_NOTREACHED();
return false;
} else if (dw == WSA_WAIT_TIMEOUT) {
// Timeout?
return true;
} else {
// Figure out which one it is and call it
CritScope cr(&crit_);
int index = dw - WSA_WAIT_EVENT_0;
if (index > 0) {
--index; // The first event is the socket event
Dispatcher* disp = event_owners[index];
// The dispatcher could have been removed while waiting for events.
if (dispatchers_.find(disp) != dispatchers_.end()) {
disp->OnPreEvent(0);
disp->OnEvent(0, 0);
}
} else if (process_io) {
processing_dispatchers_ = true;
for (Dispatcher* disp : dispatchers_) {
SOCKET s = disp->GetSocket();
if (s == INVALID_SOCKET)
continue;
WSANETWORKEVENTS wsaEvents;
int err = WSAEnumNetworkEvents(s, events[0], &wsaEvents);
if (err == 0) {
{
if ((wsaEvents.lNetworkEvents & FD_READ) &&
wsaEvents.iErrorCode[FD_READ_BIT] != 0) {
RTC_LOG(WARNING)
<< "PhysicalSocketServer got FD_READ_BIT error "
<< wsaEvents.iErrorCode[FD_READ_BIT];
}
if ((wsaEvents.lNetworkEvents & FD_WRITE) &&
wsaEvents.iErrorCode[FD_WRITE_BIT] != 0) {
RTC_LOG(WARNING)
<< "PhysicalSocketServer got FD_WRITE_BIT error "
<< wsaEvents.iErrorCode[FD_WRITE_BIT];
}
if ((wsaEvents.lNetworkEvents & FD_CONNECT) &&
wsaEvents.iErrorCode[FD_CONNECT_BIT] != 0) {
RTC_LOG(WARNING)
<< "PhysicalSocketServer got FD_CONNECT_BIT error "
<< wsaEvents.iErrorCode[FD_CONNECT_BIT];
}
if ((wsaEvents.lNetworkEvents & FD_ACCEPT) &&
wsaEvents.iErrorCode[FD_ACCEPT_BIT] != 0) {
RTC_LOG(WARNING)
<< "PhysicalSocketServer got FD_ACCEPT_BIT error "
<< wsaEvents.iErrorCode[FD_ACCEPT_BIT];
}
if ((wsaEvents.lNetworkEvents & FD_CLOSE) &&
wsaEvents.iErrorCode[FD_CLOSE_BIT] != 0) {
RTC_LOG(WARNING)
<< "PhysicalSocketServer got FD_CLOSE_BIT error "
<< wsaEvents.iErrorCode[FD_CLOSE_BIT];
}
}
uint32_t ff = 0;
int errcode = 0;
if (wsaEvents.lNetworkEvents & FD_READ)
ff |= DE_READ;
if (wsaEvents.lNetworkEvents & FD_WRITE)
ff |= DE_WRITE;
if (wsaEvents.lNetworkEvents & FD_CONNECT) {
if (wsaEvents.iErrorCode[FD_CONNECT_BIT] == 0) {
ff |= DE_CONNECT;
} else {
ff |= DE_CLOSE;
errcode = wsaEvents.iErrorCode[FD_CONNECT_BIT];
}
}
if (wsaEvents.lNetworkEvents & FD_ACCEPT)
ff |= DE_ACCEPT;
if (wsaEvents.lNetworkEvents & FD_CLOSE) {
ff |= DE_CLOSE;
errcode = wsaEvents.iErrorCode[FD_CLOSE_BIT];
}
if (ff != 0) {
disp->OnPreEvent(ff);
disp->OnEvent(ff, errcode);
}
}
}
processing_dispatchers_ = false;
// Process deferred dispatchers that have been added/removed while the
// events were handled above.
AddRemovePendingDispatchers();
}
// Reset the network event until new activity occurs
WSAResetEvent(socket_ev_);
}
// Break?
if (!fWait_)
break;
cmsElapsed = TimeSince(msStart);
if ((cmsWait != kForever) && (cmsElapsed >= cmsWait)) {
break;
}
}
// Done
return true;
}
#endif // WEBRTC_WIN
} // namespace rtc
|