1 |
#include <unistd.h> |
2 |
#include <stdint.h> |
3 |
#include <cstdlib> |
4 |
#include <cstring> |
5 |
|
6 |
#include "rxvtdaemon.h" |
7 |
|
8 |
const char *rxvt_connection::unix_sockname () |
9 |
{ |
10 |
return "/tmp/rxvtd~"; |
11 |
} |
12 |
|
13 |
void rxvt_connection::send (const char *data, int len) |
14 |
{ |
15 |
uint8_t s[2]; |
16 |
|
17 |
s[0] = len >> 8; s[1] = len; |
18 |
|
19 |
write (fd, s, 2); |
20 |
write (fd, data, len); |
21 |
} |
22 |
|
23 |
void rxvt_connection::send (const char *data) |
24 |
{ |
25 |
send (data, strlen (data)); |
26 |
} |
27 |
|
28 |
bool rxvt_connection::recv (char *&data, int *len) |
29 |
{ |
30 |
uint8_t s[2]; |
31 |
int l; |
32 |
|
33 |
if (read (fd, s, 2) != 2) |
34 |
return false; |
35 |
|
36 |
l = (s[0] << 8) + s[1]; |
37 |
if (l > 4096) |
38 |
return false; |
39 |
|
40 |
if (len) |
41 |
*len = l; |
42 |
|
43 |
data = new char[l + 1]; |
44 |
|
45 |
if (!data) |
46 |
return false; |
47 |
|
48 |
if (read (fd, data, l) != l) |
49 |
return false; |
50 |
|
51 |
data[l] = 0; |
52 |
|
53 |
return true; |
54 |
} |
55 |
|
56 |
bool rxvt_connection::recv (token &data) |
57 |
{ |
58 |
char *d; |
59 |
int l; |
60 |
|
61 |
if (!recv (d, &l)) |
62 |
return false; |
63 |
|
64 |
if (l < sizeof (token) - 1) |
65 |
strcpy (data, d); |
66 |
|
67 |
delete [] d; |
68 |
|
69 |
return l < sizeof (token) - 1; |
70 |
} |
71 |
|
72 |
void rxvt_connection::send (int data) |
73 |
{ |
74 |
uint8_t s[4]; |
75 |
|
76 |
s[0] = data >> 24; s[1] = data >> 16; s[0] = data >> 8; s[1] = data; |
77 |
|
78 |
write (fd, s, 4); |
79 |
} |
80 |
|
81 |
bool rxvt_connection::recv (int &data) |
82 |
{ |
83 |
uint8_t s[4]; |
84 |
|
85 |
if (read (fd, s, 4) != 4) |
86 |
return false; |
87 |
|
88 |
data = (((((s[0] << 8) | s[1]) << 8) | s[2]) << 8) | s[3]; |
89 |
|
90 |
return true; |
91 |
} |
92 |
|
93 |
|
94 |
|