@@ -232,29 +232,29 @@ void HTTPServer::process() {
232
232
*/
233
233
void HTTPServer::acceptConnection () {
234
234
// Setup new client with prelim address info
235
- sockaddr_in clientAddr;
236
- int clientAddrLen = sizeof (clientAddr);
237
- int clfd = INVALID_SOCKET;
238
-
239
- // Accept the pending connection and retrive the client descriptor
240
- clfd = accept (listenSocket, (sockaddr*)&clientAddr, (socklen_t *)&clientAddrLen);
241
- if (clfd == INVALID_SOCKET)
242
- return ;
235
+ sockaddr_in clientAddr;
236
+ int clientAddrLen = sizeof (clientAddr);
237
+ int clfd = INVALID_SOCKET;
238
+
239
+ // Accept the pending connection and retrive the client descriptor
240
+ clfd = accept (listenSocket, (sockaddr*)&clientAddr, (socklen_t *)&clientAddrLen);
241
+ if (clfd == INVALID_SOCKET)
242
+ return ;
243
243
244
244
// Set socket as non blocking
245
245
fcntl (clfd, F_SETFL, O_NONBLOCK);
246
-
247
- // Instance Client object
248
- Client *cl = new Client (clfd, clientAddr);
249
-
246
+
247
+ // Instance Client object
248
+ Client *cl = new Client (clfd, clientAddr);
249
+
250
250
// Add kqueue event to track the new client socket for READ and WRITE events
251
251
updateEvent (clfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0 , 0 , NULL );
252
252
updateEvent (clfd, EVFILT_WRITE, EV_ADD | EV_DISABLE, 0 , 0 , NULL ); // Disabled initially
253
-
254
- // Add the client object to the client map
255
- clientMap.insert (std::pair<int , Client*>(clfd, cl));
256
-
257
- // Print the client's IP on connect
253
+
254
+ // Add the client object to the client map
255
+ clientMap.insert (std::pair<int , Client*>(clfd, cl));
256
+
257
+ // Print the client's IP on connect
258
258
std::cout << " [" << cl->getClientIP () << " ] connected" << std::endl;
259
259
}
260
260
@@ -314,37 +314,37 @@ void HTTPServer::disconnectClient(Client *cl, bool mapErase) {
314
314
* @param data_len Number of bytes waiting to be read
315
315
*/
316
316
void HTTPServer::readClient (Client *cl, int data_len) {
317
- if (cl == NULL )
318
- return ;
317
+ if (cl == NULL )
318
+ return ;
319
+
320
+ // If the read filter triggered with 0 bytes of data, client may want to disconnect
321
+ // Set data_len to the Ethernet max MTU by default
322
+ if (data_len <= 0 )
323
+ data_len = 1400 ;
319
324
320
- // If the read filter triggered with 0 bytes of data, client may want to disconnect
321
- // Set data_len to the Ethernet max MTU by default
322
- if (data_len <= 0 )
323
- data_len = 1400 ;
324
-
325
325
HTTPRequest* req;
326
- char * pData = new char [data_len];
327
-
328
- // Receive data on the wire into pData
329
- /* TODO: Figure out what flags need to be set */
330
- int flags = 0 ;
331
- ssize_t lenRecv = recv (cl->getSocket (), pData, data_len, flags);
332
-
333
- // Determine state of the client socket and act on it
334
- if (lenRecv == 0 ) {
335
- // Client closed the connection
326
+ char * pData = new char [data_len];
327
+
328
+ // Receive data on the wire into pData
329
+ /* TODO: Figure out what flags need to be set */
330
+ int flags = 0 ;
331
+ ssize_t lenRecv = recv (cl->getSocket (), pData, data_len, flags);
332
+
333
+ // Determine state of the client socket and act on it
334
+ if (lenRecv == 0 ) {
335
+ // Client closed the connection
336
336
std::cout << " [" << cl->getClientIP () << " ] has opted to close the connection" << std::endl;
337
- disconnectClient (cl);
338
- } else if (lenRecv < 0 ) {
339
- // Something went wrong with the connection
340
- // TODO: check perror() for the specific error message
341
- disconnectClient (cl);
342
- } else {
343
- // Data received: Place the data in an HTTPRequest and pass it to handleRequest for processing
337
+ disconnectClient (cl);
338
+ } else if (lenRecv < 0 ) {
339
+ // Something went wrong with the connection
340
+ // TODO: check perror() for the specific error message
341
+ disconnectClient (cl);
342
+ } else {
343
+ // Data received: Place the data in an HTTPRequest and pass it to handleRequest for processing
344
344
req = new HTTPRequest ((byte*)pData, lenRecv);
345
- handleRequest (cl, req);
345
+ handleRequest (cl, req);
346
346
delete req;
347
- }
347
+ }
348
348
349
349
delete [] pData;
350
350
}
@@ -357,8 +357,8 @@ void HTTPServer::readClient(Client *cl, int data_len) {
357
357
* @param avail_bytes Number of bytes available for writing in the send buffer
358
358
*/
359
359
bool HTTPServer::writeClient (Client* cl, int avail_bytes) {
360
- if (cl == NULL )
361
- return false ;
360
+ if (cl == NULL )
361
+ return false ;
362
362
363
363
int actual_sent = 0 ; // Actual number of bytes sent as returned by send()
364
364
int attempt_sent = 0 ; // Bytes that we're attempting to send now
@@ -398,7 +398,7 @@ bool HTTPServer::writeClient(Client* cl, int avail_bytes) {
398
398
item->setOffset (item->getOffset () + actual_sent);
399
399
else
400
400
disconnect = true ;
401
-
401
+
402
402
// std::cout << "[" << cl->getClientIP() << "] was sent " << actual_sent << " bytes " << std::endl;
403
403
404
404
// SendQueueItem isnt needed anymore. Dequeue and delete
0 commit comments