-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadwriter.h
46 lines (44 loc) · 1.05 KB
/
readwriter.h
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
#ifndef __READWRITER_H__
#define __READWRITER_H__
#include <stdio.h>
#include "ipclog.h"
class readwriter {
public:
virtual ~readwriter() { }
virtual size_t readsome(void*p, size_t n)= 0;
virtual size_t writesome(const void*p, size_t n)= 0;
bool read(void*p, size_t n)
{
ipclog("reading %d\n", (int)n);
size_t total= 0;
while (total<n)
{
int r= readsome((char*)p+total, n-total);
if (r==-1) {
perror("read");
return false;
}
total += r;
}
ipclog("read %d\n", (int)n);
return true;
}
bool write(const void*p, size_t n)
{
ipclog("writing %d\n", (int)n);
size_t total= 0;
while (total<n)
{
int r= writesome((const char*)p+total, n-total);
if (r==-1)
{
perror("write");
return false;
}
total += r;
}
ipclog("wrote %d\n", (int)n);
return true;
}
};
#endif