-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocketBuffer.h
More file actions
112 lines (93 loc) · 2.31 KB
/
Copy pathsocketBuffer.h
File metadata and controls
112 lines (93 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef SOCKET_BUFFER_H
#define SOCKET_BUFFER_H
#include <stdio.h>
#include <stdlib.h>
typedef struct SocketNode {
char c;
struct SocketNode *next;
} SocketNode;
typedef struct SocketBuffer {
int size;
SocketNode *head;
SocketNode *tail;
} SocketBuffer;
static void addCharToBuffer(SocketBuffer *socketBuffer, char c) {
SocketNode *node = malloc(sizeof(SocketNode));
node->c = c;
node->next = NULL;
if(socketBuffer->tail == NULL) {
socketBuffer->tail = node;
socketBuffer->head = node;
} else {
socketBuffer->tail->next = node;
socketBuffer->tail = node;
}
socketBuffer->size += 1;
}
static SocketBuffer *createBuffer() {
SocketBuffer *socketBuffer = malloc(sizeof(SocketBuffer));
socketBuffer->head = NULL;
socketBuffer->tail = NULL;
socketBuffer->size = 0;
return socketBuffer;
}
// this function returns a string, which user should deallocate himself.
static char* readAllBuffer(SocketBuffer *socketBuffer) {
char *result = malloc(sizeof(char) * (socketBuffer->size + 1));
SocketNode *node = socketBuffer->head;
int i = 0;
while(node != NULL) {
result[i++] = node->c;
SocketNode *d = node;
node = node->next;
free(d);
}
result[i] = '\0'; // Add null terminator at last
socketBuffer->head = NULL;
socketBuffer->tail = NULL;
socketBuffer->size = 0;
return result;
}
static void readNBytes(SocketBuffer *socketBuffer, int sockfd, long int numBytes) {
char c;
long int i = 0;
while(i++ < numBytes) {
read(sockfd, &c, 1);
if(c == '\0') {
break; // Client disconnected.
}
addCharToBuffer(socketBuffer, c);
}
}
static void readTillDelimiter(SocketBuffer *socketBuffer, int sockfd, char delimiter) {
char c;
while(1) {
read(sockfd, &c, 1);
// for files,if EOF is reached, we get \0
if(c == '\0') {
break; // Client disconnected.
}
if(c == delimiter) {
break;
}
// Do not read the delimiter to buffer.
addCharToBuffer(socketBuffer, c);
}
}
static void clearSocketBuffer(SocketBuffer *socketBuffer) {
SocketNode *node = socketBuffer->head;
while(node != NULL) {
SocketNode *d = node;
node = node->next;
free(d);
}
socketBuffer->head = NULL;
socketBuffer->tail = NULL;
socketBuffer->size = 0;
// Do not free the buffer object
}
static void freeSocketBuffer(SocketBuffer *socketBuffer) {
clearSocketBuffer(socketBuffer);
free(socketBuffer);
}
#endif