/
Connection.cpp
88 lines (85 loc) · 1.72 KB
/
Connection.cpp
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
#include "Connection.hpp"
#include "DataWriter.hpp"
#include <sys/types.h>
#include <fcntl.h>
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock.h>
#endif
#include <unistd.h>
#include <iostream>
#include <cassert>
using namespace std;
#ifdef WIN32
int get(int fd, void* buf, int n)
{
int r = recv(fd, (char*)buf, n, 0);
if (r<0) {
int rr = WSAGetLastError();
if (rr==WSAEWOULDBLOCK) errno=EAGAIN;
else cout<<"rect error: "<<rr<<endl;
} //else cout<<"got data "<<r<<'\n';
return r;
}
#else
int get(int fd, void* buf, int n)
{
return ::read(fd, buf, n);
}
#endif
bool Connection::read()
{
assert(cur<1<<20);
if (cur<4) {
int n = get(fd, buf+cur, 4-cur);
if (n<0) return 0;
// cout<<"got data: "<<n<<'\n';
cur += n;
if (cur<4) return 0;
need = *(int*)buf;
// cout<<"need: "<<need<<'\n';
// assert(need < 1<<10);
}
// cout<<"asd "<<cur<<' '<<need-(cur-4)<<'\n';
int n =get(fd, buf+cur, need-(cur-4));
if (n<0) return 0;
// cout<<"real data: "<<n<<'\n';
// for(int i=0; i<n; ++i) cout<<hex<<(int)((char*)buf)[cur+i]<<' ';cout<<'\n';
cur += n;
if (cur<4+need) return 0;
cur=0;
return 1;
}
void Connection::write(const void* s, int n)
{
#if 1
char* np = (char*)&n;
obuf.insert(obuf.end(), np, np+4);
char* ss = (char*)s;
obuf.insert(obuf.end(), ss, ss+n);
#else
::write(fd, &n, 4);
::write(fd, s, n);
#endif
}
void Connection::write(DataWriter& w)
{
write(w.data(), w.len());
}
void Connection::flush()
{
if (obuf.size()) {
// cout<<"sending "<<obuf.size()<<" bytes\n";
#ifdef WIN32
int r = send(fd, &obuf[0], obuf.size(), 0);
if (r != obuf.size()) {
cout<<"failed send "<<WSAGetLastError()<<endl;
}
#else
::write(fd, &obuf[0], obuf.size());
#endif
obuf.clear();
}
}