-
Notifications
You must be signed in to change notification settings - Fork 34
/
msgbuf.c
138 lines (129 loc) · 3.67 KB
/
msgbuf.c
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "msgbuf.h"
#ifndef MIN
#define MIN(x,y) (((x) < (y))? (x) : (y))
#endif
struct msgbuf * msgbuf_new(int bufsize)
{
struct msgbuf * mbuf;
mbuf = malloc(sizeof(*mbuf));
assert(mbuf);
mbuf->len = bufsize;
mbuf->buf = malloc(mbuf->len);
assert(mbuf->len);
mbuf->start = mbuf->end = 0;
return mbuf;
}
/**********************************************************************/
int msgbuf_read(struct msgbuf * mbuf, int sock)
{
int count = read(sock, &mbuf->buf[mbuf->end], mbuf->len - mbuf->end);
if(count>0)
mbuf->end+=count;
if( mbuf->end >= mbuf->len) // resize buffer if need be
msgbuf_grow(mbuf);
return count;
}
/**********************************************************************/
int msgbuf_read_all(struct msgbuf * mbuf, int sock, int len)
{
int count = 0;
int tmp;
while(count < len)
{
/** fprintf(stderr, "in msgbuf_read_all... count = %d len = %d tmp = %d\n",
count, len, tmp); **/
tmp =msgbuf_read(mbuf,sock);
if((tmp == 0) ||
((tmp<0) && (errno != EWOULDBLOCK ) && (errno != EINTR) && (errno != EAGAIN)))
return tmp;
if(tmp > 0)
count+=tmp;
}
return count;
}
/**********************************************************************/
int msgbuf_write(struct msgbuf * mbuf, int sock, int len)
{
int send_len = mbuf->end - mbuf->start;
if (len > 0)
{
if (send_len < len)
return -1;
if (send_len > len)
send_len = len;
}
int count = write(sock, &mbuf->buf[mbuf->start], send_len);
if(count>0)
mbuf->start+=count;
if(mbuf->start >= mbuf->end)
mbuf->start = mbuf->end = 0;
return count;
}
/**********************************************************************/
int msgbuf_write_all(struct msgbuf * mbuf, int sock, int len)
{
int tmp=0,count=0;
while(mbuf->start < mbuf->end)
{
tmp=msgbuf_write(mbuf, sock, len);
if((tmp < 0) &&
(errno != EAGAIN) &&
(errno != EWOULDBLOCK) &&
(errno != EINTR))
return tmp;
if(count > 0)
count+=tmp;
}
return count;
}
/**********************************************************************/
void msgbuf_clear(struct msgbuf * mbuf)
{
mbuf->start = mbuf->end = 0;
}
/**********************************************************************/
void msgbuf_grow(struct msgbuf * mbuf)
{
mbuf->len *=2 ;
mbuf->buf = realloc(mbuf->buf, mbuf->len);
if(mbuf->buf == NULL) {
perror("msgbuf_grow failed");
printf("buffer len: %d\n", mbuf->len);
}
assert(mbuf->buf);
}
/**********************************************************************/
void * msgbuf_peek(struct msgbuf *mbuf)
{
if(mbuf->start >= mbuf->end)
return NULL;
return (void *) &mbuf->buf[mbuf->start];
}
/**********************************************************************/
int msgbuf_pull(struct msgbuf *mbuf, char * buf, int count)
{
int min = MIN(count, mbuf->end - mbuf->start);
if( min <= 0)
return -1;
if(buf) // don't write if NULL
memcpy(buf, &mbuf->buf[mbuf->start], min);
mbuf->start+=min;
if(mbuf->start>= mbuf->end)
mbuf->start = mbuf->end = 0;
return min;
}
/**********************************************************************/
void msgbuf_push(struct msgbuf *mbuf, char * buf, int count)
{
while((mbuf->end + count) > mbuf->len)
msgbuf_grow(mbuf);
memcpy(&mbuf->buf[mbuf->end], buf, count);
mbuf->end += count;
}