Skip to content

Commit 503ce80

Browse files
committed
Added proper packet send queue. Fixed MIME bug.
1 parent b6d54e4 commit 503ce80

File tree

9 files changed

+284
-114
lines changed

9 files changed

+284
-114
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ BUILDDIR := build
88
TARGET := httpserver
99

1010
# Debug Flags
11-
DEBUGFLAGS := -g -O0 -Wall
11+
DEBUGFLAGS := -g3 -O0 -Wall
12+
RTCHECKS := -fmudflap -fstack-check -gnato
1213
# Production Flags
13-
PRODFLAGS := -Wall -O3
14+
PRODFLAGS := -Wall -O2
1415
# Active Flags
1516
CFLAGS := -std=c++11 -stdlib=libc++ -Iinclude/ $(DEBUGFLAGS)
1617
LINK := -stdlib=libc++ $(DEBUGFLAGS)
@@ -27,6 +28,6 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
2728
@echo " CC $<"; $(CC) $(CFLAGS) -c -o $@ $<
2829

2930
clean:
30-
@echo " Cleaning..."; $(RM) -r $(BUILDDIR) $(BINDIR)/$(TARGET)
31+
@echo " Cleaning..."; rm -r $(BUILDDIR) $(BINDIR)/$(TARGET)*
3132

3233
.PHONY: clean

src/ByteBuffer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
limitations under the License.
1717
*/
1818

19-
#ifndef _BYTEBUFFER_H
20-
#define _BYTEBUFFER_H
19+
#ifndef _BYTEBUFFER_H_
20+
#define _BYTEBUFFER_H_
2121

2222
// Default number of bytes to allocate in the backing buffer if no size is provided
2323
#define DEFAULT_SIZE 4096
@@ -63,7 +63,7 @@ class ByteBuffer {
6363
if (size() < (wpos + s))
6464
buf.resize(wpos + s);
6565
memcpy(&buf[wpos], (byte*)&data, s);
66-
//printf("writing %c to %u\n", ((uns, wpos);
66+
//printf("writing %c to %i\n", (byte)data, wpos);
6767

6868
wpos += s;
6969
}
@@ -79,7 +79,7 @@ class ByteBuffer {
7979
public:
8080
ByteBuffer(unsigned int size = DEFAULT_SIZE);
8181
ByteBuffer(byte* arr, unsigned int size);
82-
~ByteBuffer();
82+
virtual ~ByteBuffer();
8383

8484
unsigned int bytesRemaining(); // Number of bytes from the current read position till the end of the buffer
8585
void clear(); // Clear our the vector and reset read and write positions

src/Client.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,39 @@ Client::Client(SOCKET fd, sockaddr_in addr) {
2525
}
2626

2727
Client::~Client() {
28-
28+
clearSendQueue();
29+
}
30+
31+
void Client::addToSendQueue(SendQueueItem* item) {
32+
sendQueue.push(item);
33+
}
34+
35+
unsigned int Client::sendQueueSize() {
36+
return sendQueue.size();
37+
}
38+
39+
SendQueueItem* Client::nextFromSendQueue(unsigned int bytesToSend) {
40+
if(sendQueue.empty())
41+
return NULL;
42+
43+
SendQueueItem* item = sendQueue.front();
44+
unsigned int remaining = item->getSize() - item->getOffset();
45+
46+
47+
if(bytesToSend > remaining) { // Rest or entire resource data can be sent, Dequeue
48+
sendQueue.pop(); // Dequeue
49+
} else { // Only a portion of the resource can be sent, keep in queue
50+
// Add to the back of the queue
51+
sendQueue.pop();
52+
sendQueue.push(item);
53+
}
54+
55+
return item;
2956
}
57+
58+
void Client::clearSendQueue() {
59+
while(!sendQueue.empty()) {
60+
delete sendQueue.front();
61+
sendQueue.pop();
62+
}
63+
}

src/Client.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@
2121

2222
#include <netinet/in.h>
2323
#include <arpa/inet.h>
24+
#include <queue>
25+
26+
#include "SendQueueItem.h"
2427

2528
#define SOCKET int
2629

30+
typedef unsigned char byte;
31+
2732
class Client {
2833
SOCKET socketDesc; // Socket Descriptor
2934
sockaddr_in clientAddr;
35+
36+
std::queue<SendQueueItem*> sendQueue;
3037

3138
public:
3239
Client(SOCKET fd, sockaddr_in addr);
@@ -40,9 +47,14 @@ class Client {
4047
return socketDesc;
4148
}
4249

43-
char *getClientIP() {
50+
char* getClientIP() {
4451
return inet_ntoa(clientAddr.sin_addr);
4552
}
53+
54+
void addToSendQueue(SendQueueItem* item);
55+
unsigned int sendQueueSize();
56+
SendQueueItem* nextFromSendQueue(unsigned int bytesToSend);
57+
void clearSendQueue();
4658
};
4759

4860
#endif

src/HTTPResponse.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void HTTPResponse::determineReasonStr() {
9292
/**
9393
* Create
9494
* Create and return a byte array of an HTTP response, built from the variables of this HTTPResponse
95+
* Caller will be responsible for cleaning up returned byte array
9596
*
9697
* @return Byte array of this HTTPResponse to be sent over the wire
9798
*/

0 commit comments

Comments
 (0)