1. #include <sys/socket.h>
    2. #include <netinet/in.h>
    3. #include <arpa/inet.h>
    4. #include <stdio.h>
    5. #include <unistd.h>
    6. #include <errno.h>
    7. #include <string.h>
    8. #include <fcntl.h>
    9. #include <stdlib.h>
    10. #include <sys/epoll.h>
    11. #include "locker.h"
    12. #include "threadpool.h"
    13. #include "http_conn.h"
    14. #define MAX_FD 65536 // 最大的文件描述符个数
    15. #define MAX_EVENT_NUMBER 10000 // 监听的最大的事件数量
    16. // 添加文件描述符
    17. extern void addfd( int epollfd, int fd, bool one_shot );
    18. extern void removefd( int epollfd, int fd );
    19. void addsig(int sig, void( handler )(int)){
    20. struct sigaction sa;
    21. memset( &sa, '\0', sizeof( sa ) );
    22. sa.sa_handler = handler;
    23. sigfillset( &sa.sa_mask );
    24. assert( sigaction( sig, &sa, NULL ) != -1 );
    25. }
    26. int main( int argc, char* argv[] ) {
    27. if( argc <= 1 ) {
    28. printf( "usage: %s port_number\n", basename(argv[0]));
    29. return 1;
    30. }
    31. int port = atoi( argv[1] );
    32. addsig( SIGPIPE, SIG_IGN );
    33. threadpool< http_conn >* pool = NULL;
    34. try {
    35. pool = new threadpool<http_conn>;
    36. } catch( ... ) {
    37. return 1;
    38. }
    39. http_conn* users = new http_conn[ MAX_FD ];
    40. int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
    41. int ret = 0;
    42. struct sockaddr_in address;
    43. address.sin_addr.s_addr = INADDR_ANY;
    44. address.sin_family = AF_INET;
    45. address.sin_port = htons( port );
    46. // 端口复用
    47. int reuse = 1;
    48. setsockopt( listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof( reuse ) );
    49. ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) );
    50. ret = listen( listenfd, 5 );
    51. // 创建epoll对象,和事件数组,添加
    52. epoll_event events[ MAX_EVENT_NUMBER ];
    53. int epollfd = epoll_create( 5 );
    54. // 添加到epoll对象中
    55. addfd( epollfd, listenfd, false );
    56. http_conn::m_epollfd = epollfd;
    57. while(true) {
    58. int number = epoll_wait( epollfd, events, MAX_EVENT_NUMBER, -1 );
    59. if ( ( number < 0 ) && ( errno != EINTR ) ) {
    60. printf( "epoll failure\n" );
    61. break;
    62. }
    63. for ( int i = 0; i < number; i++ ) {
    64. int sockfd = events[i].data.fd;
    65. if( sockfd == listenfd ) {
    66. struct sockaddr_in client_address;
    67. socklen_t client_addrlength = sizeof( client_address );
    68. int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
    69. if ( connfd < 0 ) {
    70. printf( "errno is: %d\n", errno );
    71. continue;
    72. }
    73. if( http_conn::m_user_count >= MAX_FD ) {
    74. close(connfd);
    75. continue;
    76. }
    77. users[connfd].init( connfd, client_address);
    78. } else if( events[i].events & ( EPOLLRDHUP | EPOLLHUP | EPOLLERR ) ) {
    79. users[sockfd].close_conn();
    80. } else if(events[i].events & EPOLLIN) {
    81. if(users[sockfd].read()) {
    82. pool->append(users + sockfd);
    83. } else {
    84. users[sockfd].close_conn();
    85. }
    86. } else if( events[i].events & EPOLLOUT ) {
    87. if( !users[sockfd].write() ) {
    88. users[sockfd].close_conn();
    89. }
    90. }
    91. }
    92. }
    93. close( epollfd );
    94. close( listenfd );
    95. delete [] users;
    96. delete pool;
    97. return 0;
    98. }