Linux muduo C++ (giantchen@gmail.com) 2012-09-30 C++ TCP C++ x86-64 Linux TCP one loop per thread Linux native muduo C++ IT 5 C++ muduo 2 C++ C++ Primer 4
W. Richard Stevens UNIX Sockets API echo Sockets TCP OOB IO C struct C/C++ UNIX fork() select(2)/poll(2)/epoll(4) IO IO CPU 100% libevent UNIX CPU IO UNIX 2 IPC 7 24 C++ STL C++ Linux muduo C++
C++ x86-64 Linux TCP 5 IO one loop per thread Linux native muduo muduo IO C++ one loop per thread IO muduo Linux A C++ C++11 2005 TR1 C++ C++ C++ 1 449 C++ UNIX UNIX C++ Primer C++11 2012 Linux g++ 4.4 C++11 x86-64 CPU GB 5 IO 1 C++ C++ 2 muduo muduo 3 C++ 4 C++ UDP iii
iv Observer Reactor Singleton class heap event loop STL algorithm C++ instance resolution instantiation override dereference class 6 syscall (s) fd file descriptor CPU core kb MB GB 10 3 10 6 10 9 KiB MiB GiB 2 10 2 20 2 30 11.5 11.5 L42 42 [JCP] [CC2e] mutex socket C++ class this mutable class fork(2) muduo::eventloop fork(2) fork() manpage 2 man 2 fork - EventLoop- ThreadPool URL muduo/base/date.h http://chenshuo.com GitHub http://github.com/ chenshuo/recipes/ http://github.com/chenshuo/muduo/ recipes/thread GitHub URL muduo/base/types.h 1 muduo::string typedef 15 namespace muduo 16 { 17 18 #ifdef MUDUO_STD_STRING 19 using std::string; 20 #else //!MUDUO_STD_STRING 21 typedef gnu_cxx:: sso_string string; 22 #endif muduo/base/types.h muduo/base/types.h 1 6 7 muduo muduo/examples/xxx examples/xxx 8 recipes/reactor/xxx reactor/xxx
v diff -u giantchen@gmail.com http://chenshuo.com/book URL http://weibo.com/giantchen http://blog.csdn.net/solstice http://github.com/chenshuo
1 C++ 1 1...................... 3 2.............................. 31 3.............. 59 4 C++........................ 83 5............................ 107 2 muduo 123 6 muduo........................... 125 7 muduo............................. 177 8 muduo....................... 277 3 337 9.......................... 339 10 C++......................... 391 11 C++...................... 429 12 C++................................ 501 4 559 A........................ 561 B C++ Primer 4 C++............. 579 C Boost............................ 591 D TCP............... 593....................................... 599 vi
1 C++ 1 1 3 1.1............................ 3 1.1.1............................ 4 1.1.2 MutexLock MutexLockGuard.................... 4 1.1.3 Counter.................... 4 1.2............................... 5 1.3.................................... 7 1.3.1 mutex............................ 7 1.3.2 mutex............... 8 1.4 Observer......................... 8 1.5............................... 11 1.6 shared_ptr/weak_ptr.......................... 13 1.7....................... 14 1.8 Observer.............................. 16 1.9 shared_ptr......................... 17 1.10 shared_ptr............................ 19 1.11..................................... 21 1.11.1 enable_shared_from_this...................... 23 1.11.2................................. 24 1.12.................................... 26 1.13................................... 26 1.14 Observer................................. 28 2 31 2.1 mutex............................... 32 vii
viii 2.1.1 mutex........................ 33 2.1.2.................................. 35 2.2 condition variable....................... 40 2.3............................ 43 2.4 MutexLock MutexLockGuard Condition.............. 44 2.5 Singleton.......................... 48 2.6 sleep(3)............................ 50 2.7................................... 51 2.8 shared_ptr copy-on-write...................... 52 3 59 3.1................................... 59 3.2........................ 61 3.3........................ 62 3.3.1 one loop per thread.......................... 62 3.3.2................................. 63 3.3.3................................ 64 3.4 TCP............................. 65 3.5........................... 67 3.5.1......................... 69 3.5.2......................... 70 3.5.3........................ 71 3.6.................. 74 4 C++ 83 4.1............................. 84 4.2 C/C++......................... 85 4.3 Linux.............................. 89 4.4........................... 91 4.4.1 pthread_cancel C++....................... 94 4.4.2 exit(3) C++................. 94 4.5 thread............................. 96 4.6 IO.................................. 98
ix 4.7 RAII........................... 99 4.8 RAII fork()................................. 101 4.9 fork()............................... 102 4.10 signal................................ 103 4.11 Linux.......................... 105 5 107 5.1.................................... 109 5.2.................................... 112 5.3................................ 114 5.4.................................... 120 2 muduo 123 6 muduo 125 6.1....................................... 125 6.2....................................... 127 6.3.................................... 129 6.3.1................................ 131 6.3.2.................................. 134 6.3.3................................ 135 6.4.................................... 136 6.4.1 TCP......................... 136 6.4.2 echo........................... 138 6.4.3 finger......................... 140 6.5.................................... 144 6.5.1 muduo Boost.Asio libevent2.......... 145 6.5.2 muduo libevent2...... 148 6.5.3 muduo Nginx................... 153 6.5.4 muduo ZeroMQ................... 156 6.6 muduo........................... 157 6.6.1............................ 157 6.6.2................. 160
x 7 muduo 177 7.1 TCP.............................. 178 7.2.................................... 185 7.3 Boost.Asio........................... 194 7.3.1 TCP................................ 194 7.3.2................................ 195 7.3.3 LengthHeaderCodec.................... 197 7.3.4............................. 198 7.3.5............................. 200 7.4 muduo Buffer....................... 204 7.4.1 muduo IO.......................... 204 7.4.2 non-blocking buffer.... 205 7.4.3 Buffer.......................... 207 7.4.4 Buffer.......................... 209 7.4.5 Buffer............................. 211 7.4.6............................. 217 7.4.7............................ 218 7.5 Google Protobuf........ 220 7.5.1 Protobuf............. 220 7.5.2 type name Message............ 221 7.5.3 Protobuf.......................... 226 7.6 muduo Protobuf............ 228 7.6.1 codec...................... 229 7.6.2 ProtobufCodec......................... 232 7.6.3 dispatcher................ 232 7.6.4 ProtobufCodec ProtobufDispatcher....... 233 7.6.5 ProtobufDispatcher.................. 234 7.6.6 ProtobufCodec ProtobufDispatcher......... 236 7.7........................ 237 7.7.1...................... 237 7.7.2 muduo.................... 238
xi 7.8..................................... 240 7.8.1............................. 240 7.8.2 Linux............................ 241 7.8.3 muduo......................... 242 7.8.4 Boost.Asio Timer........................ 243 7.8.5 Java Netty............................ 245 7.9..................... 248 7.10 timing wheel........................ 250 7.10.1 timing wheel.......................... 251 7.10.2............................ 254 7.11............................. 257 7.12.................. 260 7.13 socks4a.............................. 264 7.13.1 TCP.............................. 264 7.13.2 socks4a.......................... 267 7.13.3 N : 1 1 : N........................ 267 7.14.................................... 267 7.15................................. 268 7.15.1 UDNS................................. 270 7.15.2 c-ares DNS............................... 272 7.15.3 curl................................... 273 7.15.4.................................. 275 8 muduo 277 8.0 EventLoop........................... 277 8.1 Reactor.............................. 280 8.1.1 Channel class............................. 280 8.1.2 Poller class.............................. 283 8.1.3 EventLoop........................... 287 8.2 TimerQueue.............................. 290 8.2.1 TimerQueue class........................... 290 8.2.2 EventLoop........................... 292
xii 8.3 EventLoop::runInLoop()........................ 293 8.3.1 TimerQueue................... 296 8.3.2 EventLoopThread class........................ 297 8.4 TCP................................ 299 8.5 TcpServer............................ 303 8.5.1 TcpServer class............................ 304 8.5.2 TcpConnection class......................... 305 8.6 TcpConnection........................... 308 8.7 Buffer................................ 313 8.7.1 TcpConnection Buffer............. 314 8.7.2 Buffer::readFd().......................... 315 8.8 TcpConnection........................... 316 8.9 TcpConnection.............................. 320 8.9.1 SIGPIPE................................ 321 8.9.2 TCP No Delay TCP keepalive.................. 321 8.9.3 WriteCompleteCallback HighWaterMarkCallback....... 322 8.10 TcpServer............................... 324 8.11 Connector................................... 327 8.12 TcpClient................................... 332 8.13 epoll...................................... 333 8.14................................. 336 3 337 9 339 9.1........................... 341 9.1.1........................ 343 9.1.2..................... 344 9.2........................... 349 9.2.1 7 24................ 352 9.2.2............... 354 9.3........................ 356
xiii 9.4........................... 360 9.4.1................................ 361 9.4.2................................ 362 9.4.3 TCP............................ 363 9.5......................... 364 9.6............................... 367 9.6.1.......................... 368 9.6.2 ICE................... 369 9.7........................ 370 9.7.1......................... 370 9.7.2........................ 373 9.7.3........................ 374 9.7.4..................... 375 9.7.5................................ 379 9.8............... 380 9.8.1 1......................... 382 9.8.2 2........... 383 9.8.3 3............. 386 9.8.4 4 naming service............. 389 10 C++ 391 10.1 C......................... 394 10.1.1 C...................... 395 10.1.2 C........................... 398 10.2 C++................................ 399 10.2.1................................ 399 10.2.2................................ 402 10.3 C++ linking............................. 404 10.3.1................................ 406 10.3.2 inline............................... 407 10.3.3.................................. 409 10.3.4................................. 414
xiv 10.4........................ 415 10.4.1............................. 416 10.4.2.......................... 417 10.5........................ 418 10.5.1............................ 423 10.5.2........................ 424 10.5.3............................ 428 11 C++ 429 11.1 C++................................ 429 11.2............................ 431 11.2.1......................... 432 11.2.2 ABI..................... 433 11.2.3........................ 435 11.2.4 COM........................... 435 11.2.5................................ 436 11.3........................ 436 11.3.1 C++.................... 437 11.3.2.................. 438 11.3.3........................ 439 11.3.4 Linux COM............ 442 11.3.5 Java.......................... 443 11.4............................ 443 11.5 boost::function boost::bind............. 447 11.5.1................................ 450 11.5.2............................ 451 11.5.3..................... 453 11.6 iostream............................ 457 11.6.1 stdio..................... 457 11.6.2 iostream......................... 461 11.6.3 iostream................. 463
xv 11.6.4 iostream..................... 464 11.6.5 iostream..................... 468 11.6.6 300 memory buffer output stream........... 476 11.6.7 C++ IO................... 480 11.7............................... 482 11.7.1............................. 482 11.7.2............................ 483 11.7.3............................ 488 11.7.4 C++.......................... 488 11.7.5............................ 490 11.7.6...................... 493 11.7.7............................ 495 12 C++ 501 12.1......................... 501 12.1.1..................... 503 12.1.2...................... 505 12.2 ::operator new()...................... 507 12.2.1......................... 507 12.2.2 ::operator new()................... 508 12.2.3 ::operator new()................. 508 12.2.4............................ 509 12.2.5 ::operator new()................... 510 12.2.6 malloc()...................... 512 12.2.7 class ::operator new()......... 513 12.2.8.................... 513 12.3........................... 514 12.3.1............................ 515 12.3.2 C/C++......................... 516 12.3.3............................ 516 12.3.4......................... 517 12.3.5................................ 521
xvi 12.4 mock........................ 522 12.4.1......................... 522 12.4.2 link seam...................... 524 12.5 namespace............................. 526 12.5.1 C static................. 526 12.5.2 C++ static................ 526 12.5.3 namespace..................... 527 12.5.4................................ 529 12.6....................... 529 12.6.1 diff........................ 530 12.6.2 grep....................... 537 12.6.3............................. 538 12.7 std::string............................... 539 12.7.1 eager copy....................... 540 12.7.2 copy-on-write..................... 542 12.7.3 SSO........................ 543 12.8 STL algorithm................ 546 12.8.1 next_permutation().............. 546 12.8.2 unique()................... 548 12.8.3 {make,push,pop}_heap()............ 549 12.8.4 partition().... 553 12.8.5 lower_bound() IP............. 554 4 559 A 561 B C++ Primer 4 C++ 579 C Boost 591 D TCP 593 599