Home > Back-end >  C the HTTP server to return after the big picture to the browser appeared torn and display incomplet
C the HTTP server to return after the big picture to the browser appeared torn and display incomplet

Time:05-03

Based on epoll + nonblocking socket + edge model I implemented a simple HTTP server, sending is a function call write, receives is read function,
When I was in test small picture browser display is normal, but for the big picture (such as more than 10 m) browsers sometimes appeared torn, normal, sometimes even suggest that half of...
As a comparison, I use python flask framework also implements the function of the same picture browsing page, flask is normal, so I'm sure is in c + + HTTP server sends the part of the problem, but I don't know where there is a problem, what are the members who know??

Part of the code
 
Int TcpConnection: : write (int * err) {
Size_t len=sender_. Readable ();//may send data size
Int r_len=len;
Int n=1;
While (true) {
N=: : write (connfd_ sender_. Peek () + len - r_len, r_len);

If (n & lt; 0 {
* err=errno;
return n;
}
Sender_. Retired (n);
R_len -=n;
If (r_len & lt;=0) {
N=0;
Sender_. Retired_all ();//reset the read/write pointer
break;
}
}
return n;
}


Void TcpServer: : on_write (TcpConnectionPtr conn) {
Int err;
Int n=conn - & gt; Write (& amp; Err);
If (n==0) {
If (conn - & gt; Keep_alive ()) {
If (conn - & gt; The sender () - & gt; Readable ()==0) {
Mod_fd (conn - & gt; Fd (), EPOLLIN | kConnEvent);
} else {
Mod_fd (conn - & gt; Fd (), EPOLLOUT | kConnEvent);
}
return;
}
} else if (n & lt; 0 {
If (err==EAGAIN) {
//the Continue sending messages
Mod_fd (conn - & gt; Fd (), EPOLLOUT | kConnEvent);
return;
}
}
Handle_conn_close (conn);
}

CodePudding user response:

 
Constexpr static int kConnEvent=EPOLLET | EPOLLONESHOT | EPOLLRDHUP;
  • Related