@@ -232,29 +232,29 @@ void HTTPServer::process() {
232232 */
233233void HTTPServer::acceptConnection () {
234234 // 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 ;
243243
244244 // Set socket as non blocking
245245 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+
250250 // Add kqueue event to track the new client socket for READ and WRITE events
251251 updateEvent (clfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0 , 0 , NULL );
252252 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
258258 std::cout << " [" << cl->getClientIP () << " ] connected" << std::endl;
259259}
260260
@@ -314,37 +314,37 @@ void HTTPServer::disconnectClient(Client *cl, bool mapErase) {
314314 * @param data_len Number of bytes waiting to be read
315315 */
316316void 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 ;
319324
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-
325325 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
336336 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
344344 req = new HTTPRequest ((byte*)pData, lenRecv);
345- handleRequest (cl, req);
345+ handleRequest (cl, req);
346346 delete req;
347- }
347+ }
348348
349349 delete [] pData;
350350}
@@ -357,8 +357,8 @@ void HTTPServer::readClient(Client *cl, int data_len) {
357357 * @param avail_bytes Number of bytes available for writing in the send buffer
358358 */
359359bool HTTPServer::writeClient (Client* cl, int avail_bytes) {
360- if (cl == NULL )
361- return false ;
360+ if (cl == NULL )
361+ return false ;
362362
363363 int actual_sent = 0 ; // Actual number of bytes sent as returned by send()
364364 int attempt_sent = 0 ; // Bytes that we're attempting to send now
@@ -398,7 +398,7 @@ bool HTTPServer::writeClient(Client* cl, int avail_bytes) {
398398 item->setOffset (item->getOffset () + actual_sent);
399399 else
400400 disconnect = true ;
401-
401+
402402 // std::cout << "[" << cl->getClientIP() << "] was sent " << actual_sent << " bytes " << std::endl;
403403
404404 // SendQueueItem isnt needed anymore. Dequeue and delete
0 commit comments