27 #include <sys/types.h> 52 struct sockaddr_un name;
55 s = socket(PF_UNIX, SOCK_DGRAM, 0);
57 throw NoConnection(
"Socket error",
HERE);
58 name.sun_family = AF_UNIX;
59 strncpy(name.sun_path, filename.c_str(),
sizeof (name.sun_path));
60 name.sun_path[
sizeof (name.sun_path) - 1] =
'\0';
61 size = (offsetof (
struct sockaddr_un, sun_path)
62 + strlen (name.sun_path) + 1);
63 if (bind (s, (
struct sockaddr *) &name, size) < 0)
64 throw BindError(
"Bind error",
HERE);
69 const std::string& filename)
const 72 const char *buf = str.c_str();
73 unsigned int count = 0;
74 struct sockaddr_un name;
76 name.sun_family = AF_UNIX;
77 strncpy(name.sun_path, filename.c_str(),
sizeof (name.sun_path));
78 name.sun_path[
sizeof (name.sun_path) - 1] =
'\0';
81 throw NoConnection(
"No Socket",
HERE);
82 while (res && count < str.size())
84 res = sendto(socket, buf + count,
86 (
const struct sockaddr*)&name,
sizeof(name));
88 throw ConnectionClosed(
"Connection Closed",
HERE);
94 const std::string& filename)
const 97 unsigned int count = 0;
98 struct sockaddr_un name;
99 char buf[str.size() + 2];
101 buf[0] = str.size() / 256;
102 buf[1] = str.size() % 256;
103 memcpy(buf + 2, str.c_str(), str.size());
104 name.sun_family = AF_UNIX;
105 strncpy(name.sun_path, filename.c_str(),
sizeof (name.sun_path));
106 name.sun_path[
sizeof (name.sun_path) - 1] = DEFAULT_DELIM;
109 throw NoConnection(
"No Socket",
HERE);
110 while (res && count < str.size() + 2)
112 res = sendto(socket, buf + count, str.size() + 2 - count,
SENDTO_FLAGS,
113 (
const struct sockaddr*)&name,
sizeof(name));
115 throw ConnectionClosed(
"Connection Closed",
HERE);
121 std::string& filename)
123 char chr[MAXPKTSIZE];
124 std::string str =
"";
126 std::pair<int, int> delim;
127 struct sockaddr_un addr;
133 throw NoConnection(
"No Socket",
HERE);
138 res = recvfrom(socket, chr, MAXPKTSIZE, 0,
139 (
struct sockaddr*)&addr,
142 res = recvfrom(socket, chr, MAXPKTSIZE, MSG_TRUNC,
143 (
struct sockaddr*)&addr,
147 throw ConnectionClosed(
"Connection Closed",
HERE);
148 _buffer += std::string(chr, res);
152 filename = std::string(addr.sun_path);
159 char chr[MAXPKTSIZE];
160 std::string str =
"";
165 throw NoConnection(
"No Socket",
HERE);
166 if (
_buffer.size() >= 2 && !size)
168 size = (
unsigned char)
_buffer[0] * 256 + (
unsigned char)
_buffer[1];
171 if (size &&
_buffer.size() >= size)
179 memset(chr, 0, MAXPKTSIZE);
181 res = recv(socket, chr, MAXPKTSIZE, 0);
183 res = recv(socket, chr, MAXPKTSIZE, MSG_TRUNC);
186 throw ConnectionClosed(
"Connection Closed",
HERE);
188 _buffer += std::string(chr, res);
192 size = (
unsigned char)
_buffer[0] * 256 + (
unsigned char)
_buffer[1];
195 if (
_buffer.size() > size - str.size())
197 str +=
_buffer.substr(0, size - str.size());
199 _buffer.size() - size - str.size());
206 if (str.size() >= size)
214 char chr[MAXPKTSIZE];
215 std::string str =
"";
217 std::pair<int, int> delim;
221 throw NoConnection(
"No Socket",
HERE);
225 memset(chr, 0, MAXPKTSIZE);
227 res = recv(socket, chr, MAXPKTSIZE, 0);
229 res = recv(socket, chr, MAXPKTSIZE, MSG_TRUNC);
232 throw ConnectionClosed(
"Connection Closed",
HERE);
233 _buffer += std::string(chr, res);
242 std::string& filename,
243 unsigned int pkg_size)
245 char chr[MAXPKTSIZE];
246 std::string str =
"";
248 struct sockaddr_un addr;
254 throw NoConnection(
"No Socket",
HERE);
255 if (
_buffer.size() >= 2 && !pkg_size)
257 pkg_size = (
unsigned char)
_buffer[0] * 256 + (
unsigned char)
_buffer[1];
260 if (pkg_size &&
_buffer.size() >= pkg_size)
262 str =
_buffer.substr(0, pkg_size);
269 res = recvfrom(socket, chr, MAXPKTSIZE, 0,
270 (
struct sockaddr*)&addr,
273 res = recvfrom(socket, chr, MAXPKTSIZE, MSG_TRUNC,
274 (
struct sockaddr*)&addr,
278 throw ConnectionClosed(
"Connection Closed",
HERE);
280 _buffer += std::string(chr, res).substr(0, res);
284 pkg_size = (
unsigned char)
_buffer[0] * 256 +
288 if (
_buffer.size() > pkg_size - str.size())
290 str +=
_buffer.substr(0, pkg_size - str.size());
292 _buffer.size() - pkg_size - str.size());
299 if (str.size() >= pkg_size)
302 filename = std::string(addr.sun_path);
307 const std::string& filename)
325 if (!size || size >
_buffer.size())
342 int timeout,
unsigned int size)
344 if (!size || size >
_buffer.size())
Network namespace represent all networks connection.
void _close(int socket) const
Close a connnection.
void init(const std::string &filename)
Here is an example of named server server using libsocket :
void writeto(const std::string &str, const std::string &filename)
function used to send a msg to a specific named socket
std::string _read_line_bin(int socket, unsigned int size)
Get a line from socket (when used with binary protocol)
void _set_timeout(bool enable, int socket, int timeout)
set a timeout on a socket
void _write_str(int socket, const std::string &str, const std::string &filename) const
Write a string to a socket to a particular named socket (when used with textual protocol) ...
bool _update_buffer(std::pair< int, int > &delim, int &i, std::string &str)
look delimiter and remove delimiter at begining of buffer if needed
void _write_str_bin(int socket, const std::string &str, const std::string &filename) const
Write a string to a socket to a particular named socket (when used with binary protocol) ...
std::string _read_line(int socket)
Get a line from socket (when used with textual protocol)
std::string read()
function used by >> operator (read a string on current socket)
int _bind(const std::string &filename)
Initialize a local socket connection (server in UDP) create a named socket with name filename...
std::string readn(unsigned int size)
read a string from socket
void close()
connect to a local socket (client)