Skip to content

Commit 4498124

Browse files
committed
up
1 parent 7ac6a6c commit 4498124

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

.vimrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax on
2+
set nu
3+
set tabstop=4
4+
set shiftwidth=4
5+
set smartindent
6+
set cindent
7+
set hlsearch

LinuxServerCodes/9/9-6mytalk_client.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main( int argc, char* argv[] )
3939
}
4040

4141
pollfd fds[2];
42-
fds[0].fd = 0;
42+
fds[0].fd = 0; // STDIN
4343
fds[0].events = POLLIN;
4444
fds[0].revents = 0;
4545
fds[1].fd = sockfd;
@@ -67,17 +67,21 @@ int main( int argc, char* argv[] )
6767
else if( fds[1].revents & POLLIN )
6868
{
6969
memset( read_buf, '\0', BUFFER_SIZE );
70+
// sockfd --> readbuf
7071
recv( fds[1].fd, read_buf, BUFFER_SIZE-1, 0 );
7172
printf( "%s\n", read_buf );
7273
}
7374

7475
if( fds[0].revents & POLLIN )
7576
{
77+
// STDIN --> pipefd[1]
7678
ret = splice( 0, NULL, pipefd[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
79+
// pipefd[0] --> sockfd
7780
ret = splice( pipefd[0], NULL, sockfd, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
7881
}
7982
}
8083

8184
close( sockfd );
8285
return 0;
8386
}
87+

LinuxServerCodes/9/9-7mytalk_server.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int main( int argc, char* argv[] )
5858
assert( ret != -1 );
5959

6060
client_data* users = new client_data[FD_LIMIT];
61-
pollfd fds[USER_LIMIT+1];
61+
pollfd fds[USER_LIMIT+1]; // size is 6
6262
int user_counter = 0;
6363
for( int i = 1; i <= USER_LIMIT; ++i )
6464
{
@@ -78,9 +78,10 @@ int main( int argc, char* argv[] )
7878
break;
7979
}
8080

81+
//遍历fds查看哪些fd的事件触发
8182
for( int i = 0; i < user_counter+1; ++i )
8283
{
83-
if( ( fds[i].fd == listenfd ) && ( fds[i].revents & POLLIN ) )
84+
if( ( fds[i].fd == listenfd ) && ( fds[i].revents & POLLIN ) ) //listenfd可读表示有连接到来
8485
{
8586
struct sockaddr_in client_address;
8687
socklen_t client_addrlength = sizeof( client_address );
@@ -119,11 +120,15 @@ int main( int argc, char* argv[] )
119120
continue;
120121
}
121122
else if( fds[i].revents & POLLRDHUP )
122-
{
123+
{ // 最后一个user移动到当前user的位置
123124
users[fds[i].fd] = users[fds[user_counter].fd];
125+
// close当前的fd
124126
close( fds[i].fd );
127+
// fds数组中最后一个fd移动到当前的fd
125128
fds[i] = fds[user_counter];
129+
// 当前索引减一,防止异常和溢出
126130
i--;
131+
// user_counter减一
127132
user_counter--;
128133
printf( "a client left\n" );
129134
}
@@ -138,17 +143,21 @@ int main( int argc, char* argv[] )
138143
if( errno != EAGAIN )
139144
{
140145
close( connfd );
146+
// 最后一个user移动到当前user的位置
141147
users[fds[i].fd] = users[fds[user_counter].fd];
148+
// 最后一个fd移动到当前的fds
142149
fds[i] = fds[user_counter];
150+
// 当前索引减一,防止异常和溢出
143151
i--;
144-
user_counter--;
152+
// 最后把user_counter减一
153+
user_counter--;
145154
}
146155
}
147156
else if( ret == 0 )
148157
{
149158
printf( "code should not come to here\n" );
150159
}
151-
else
160+
else // 正常接收到了数据
152161
{
153162
for( int j = 1; j <= user_counter; ++j )
154163
{
@@ -157,13 +166,15 @@ int main( int argc, char* argv[] )
157166
continue;
158167
}
159168

169+
// server收到一个client的数据后,广播给连接到同server的其他的client
160170
fds[j].events |= ~POLLIN;
161171
fds[j].events |= POLLOUT;
172+
// 收到的数据赋值给write_buf
162173
users[fds[j].fd].write_buf = users[connfd].buf;
163174
}
164175
}
165176
}
166-
else if( fds[i].revents & POLLOUT )
177+
else if( fds[i].revents & POLLOUT ) // 检测到写数据事件
167178
{
168179
int connfd = fds[i].fd;
169180
if( ! users[connfd].write_buf )

0 commit comments

Comments
 (0)