|
libsocket 1.5
|
This class represent a local connection (client and server) More...
#include <localsocket.hh>
Public Member Functions | |||||
| LocalSocket () | |||||
| LocalSocket (PROTO_KIND pkind) | |||||
| virtual | ~LocalSocket () | ||||
| void | writeto (const std::string &str, const std::string &filename) | ||||
| function used to send a msg to a specific named socket | |||||
| std::string | read () | ||||
| function used by >> operator (read a string on current socket) | |||||
| std::string | read (int timeout) | ||||
| read a string with a timeout | |||||
| std::string | read (std::string &filename) | ||||
| read a string and put the client named socket name in filename | |||||
| std::string | read (std::string &filename, int timeout) | ||||
| read a string and put the client named socket name in filename with a timeout | |||||
| std::string | readn (unsigned int size) | ||||
| read a string from socket | |||||
| std::string | readn (int timeout, unsigned int size) | ||||
| read a string with a timeout | |||||
| std::string | readn (std::string &filename, unsigned int size) | ||||
| read a string and put the client named socket name in filename | |||||
| std::string | readn (std::string &filename, int timeout, unsigned int size) | ||||
| read a string and put the client named socket name in filename with a timeout | |||||
| void | init (const std::string &filename) | ||||
| Here is an example of named server server using libsocket : | |||||
| void | close () | ||||
| connect to a local socket (client) | |||||
Protected Member Functions | |||||
| int | _bind (const std::string &filename) | ||||
Initialize a local socket connection (server in UDP) create a named socket with name filename
| |||||
| std::string | _read_line (int socket) | ||||
Get a line from socket (when used with textual protocol)
| |||||
| std::string | _read_line (int socket, std::string &filename) | ||||
Get a line from socket and give client socket filename (for named socket) (when used with textual protocol)
| |||||
| std::string | _read_line_bin (int socket, unsigned int size) | ||||
Get a line from socket (when used with binary protocol)
| |||||
| std::string | _read_line_bin (int socket, std::string &filename, unsigned int pkg_size) | ||||
Get a line from socket and give client socket filename (for named socket) (when used with binary protocol)
| |||||
| 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)
| |||||
| 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)
| |||||
Protected Attributes | |||||
| std::string | _filename | ||||
This class represent a local connection (client and server)
Definition at line 34 of file localsocket.hh.
| Network::LocalSocket::LocalSocket | ( | ) | [inline] |
Definition at line 37 of file localsocket.hh.
| Network::LocalSocket::LocalSocket | ( | PROTO_KIND | pkind | ) | [inline] |
Definition at line 40 of file localsocket.hh.
| virtual Network::LocalSocket::~LocalSocket | ( | ) | [inline, virtual] |
| int Network::LocalSocket::_bind | ( | const std::string & | filename | ) | [protected] |
Initialize a local socket connection (server in UDP) create a named socket with name filename
| NoConnection | when there is no open socket |
| BindError | when bind libc function return a negative value. |
Definition at line 49 of file localsocket.cc.
References HERE.
Referenced by init().
{
int s;
struct sockaddr_un name;
size_t size;
s = socket(PF_UNIX, SOCK_DGRAM, 0);
if (s < 0)
throw NoConnection("Socket error", HERE);
name.sun_family = AF_UNIX;
strncpy(name.sun_path, filename.c_str(), sizeof (name.sun_path));
name.sun_path[sizeof (name.sun_path) - 1] = '\0';
size = (offsetof (struct sockaddr_un, sun_path)
+ strlen (name.sun_path) + 1);
if (bind (s, (struct sockaddr *) &name, size) < 0)
throw BindError("Bind error", HERE);
return s;
}
| std::string Network::LocalSocket::_read_line | ( | int | socket | ) | [protected, virtual] |
Get a line from socket (when used with textual protocol)
| NoConnection | when there is no open socket |
| ConnectionClosed | when there is no more connection. |
Implements Network::Socket.
Definition at line 212 of file localsocket.cc.
References Network::Socket::_buffer, Network::Socket::_state_timeout, Network::Socket::_update_buffer(), and HERE.
Referenced by read().
{
char chr[MAXPKTSIZE];
std::string str = "";
int res = 1, i;
std::pair<int, int> delim;
bool end = false;
if (socket < 0)
throw NoConnection("No Socket", HERE);
if (!_update_buffer(delim, i, str))
while (!end)
{
memset(chr, 0, MAXPKTSIZE);
#ifdef __CYGWIN__
res = recv(socket, chr, MAXPKTSIZE, 0);
#else
res = recv(socket, chr, MAXPKTSIZE, MSG_TRUNC);
#endif
if (res <= 0)
throw ConnectionClosed("Connection Closed", HERE);
_buffer += std::string(chr, res);
if (_update_buffer(delim, i, str))
end = true;
}
_state_timeout = 0;
return str;
}
| std::string Network::LocalSocket::_read_line | ( | int | socket, |
| std::string & | filename | ||
| ) | [protected] |
Get a line from socket and give client socket filename (for named socket) (when used with textual protocol)
| NoConnection | when there is no open socket |
| ConnectionClosed | when there is no more connection. |
Definition at line 120 of file localsocket.cc.
References Network::Socket::_buffer, Network::Socket::_state_timeout, Network::Socket::_update_buffer(), and HERE.
{
char chr[MAXPKTSIZE];
std::string str = "";
int res = 1, i;
std::pair<int, int> delim;
struct sockaddr_un addr;
size_t size;
bool end = false;
size = sizeof(addr);
if (socket < 0)
throw NoConnection("No Socket", HERE);
if (!_update_buffer(delim, i, str))
while (!end)
{
#ifdef __CYGWIN__
res = recvfrom(socket, chr, MAXPKTSIZE, 0,
(struct sockaddr*)&addr,
(socklen_t*)&size);
#else
res = recvfrom(socket, chr, MAXPKTSIZE, MSG_TRUNC,
(struct sockaddr*)&addr,
(socklen_t*)&size);
#endif
if (res <= 0)
throw ConnectionClosed("Connection Closed", HERE);
_buffer += std::string(chr, res);
if (_update_buffer(delim, i, str))
end = true;
}
filename = std::string(addr.sun_path);
_state_timeout = 0;
return str;
}
| std::string Network::LocalSocket::_read_line_bin | ( | int | socket, |
| std::string & | filename, | ||
| unsigned int | pkg_size | ||
| ) | [protected] |
Get a line from socket and give client socket filename (for named socket) (when used with binary protocol)
| NoConnection | when there is no open socket |
| ConnectionClosed | when there is no more connection. |
Definition at line 241 of file localsocket.cc.
References Network::Socket::_buffer, and HERE.
{
char chr[MAXPKTSIZE];
std::string str = "";
int res = 1;
struct sockaddr_un addr;
size_t size;
bool end = false;
size = sizeof(addr);
if (socket < 0)
throw NoConnection("No Socket", HERE);
if (_buffer.size() >= 2 && !pkg_size)
{
pkg_size = (unsigned char)_buffer[0] * 256 + (unsigned char)_buffer[1];
_buffer = _buffer.substr(2, _buffer.size() - 2);
}
if (pkg_size && _buffer.size() >= pkg_size)
{
str = _buffer.substr(0, pkg_size);
_buffer = _buffer.substr(pkg_size, _buffer.size() - pkg_size);
}
else
while (!end)
{
#ifdef __CYGWIN__
res = recvfrom(socket, chr, MAXPKTSIZE, 0,
(struct sockaddr*)&addr,
(socklen_t*)&size);
#else
res = recvfrom(socket, chr, MAXPKTSIZE, MSG_TRUNC,
(struct sockaddr*)&addr,
(socklen_t*)&size);
#endif
if (res <= 0)
throw ConnectionClosed("Connection Closed", HERE);
// _buffer += all octets received
_buffer += std::string(chr, res).substr(0, res);
if (!pkg_size)
{
// extract size from _buffer and reduce it
pkg_size = (unsigned char)_buffer[0] * 256 +
(unsigned char)_buffer[1];
_buffer = _buffer.substr(2, _buffer.size() - 2);
}
if (_buffer.size() > pkg_size - str.size())
{
str += _buffer.substr(0, pkg_size - str.size());
_buffer = _buffer.substr(pkg_size - str.size(),
_buffer.size() - pkg_size - str.size());
}
else
{
str += _buffer;
_buffer = "";
}
if (str.size() >= pkg_size)
end = true;
}
filename = std::string(addr.sun_path);
return str;
}
| std::string Network::LocalSocket::_read_line_bin | ( | int | socket, |
| unsigned int | size | ||
| ) | [protected, virtual] |
Get a line from socket (when used with binary protocol)
| NoConnection | when there is no open socket |
| ConnectionClosed | when there is no more connection. |
Implements Network::Socket.
Definition at line 157 of file localsocket.cc.
References Network::Socket::_buffer, and HERE.
Referenced by read(), and readn().
{
char chr[MAXPKTSIZE];
std::string str = "";
int res = 1;
bool end = false;
if (socket < 0)
throw NoConnection("No Socket", HERE);
if (_buffer.size() >= 2 && !size)
{
size = (unsigned char)_buffer[0] * 256 + (unsigned char)_buffer[1];
_buffer = _buffer.substr(2, _buffer.size() - 2);
}
if (size && _buffer.size() >= size)
{
str = _buffer.substr(0, size);
_buffer = _buffer.substr(size, _buffer.size() - size);
}
else
while (!end)
{
memset(chr, 0, MAXPKTSIZE);
#ifdef __CYGWIN__
res = recv(socket, chr, MAXPKTSIZE, 0);
#else
res = recv(socket, chr, MAXPKTSIZE, MSG_TRUNC);
#endif
if (res <= 0)
throw ConnectionClosed("Connection Closed", HERE);
// _buffer += all octets received
_buffer += std::string(chr, res);
if (!size)
{
// extract size from _buffer and reduce it
size = (unsigned char)_buffer[0] * 256 + (unsigned char)_buffer[1];
_buffer = _buffer.substr(2, _buffer.size() - 2);
}
if (_buffer.size() > size - str.size())
{
str += _buffer.substr(0, size - str.size());
_buffer = _buffer.substr(size - str.size(),
_buffer.size() - size - str.size());
}
else
{
str += _buffer;
_buffer = "";
}
if (str.size() >= size)
end = true;
}
return str;
}
| void Network::LocalSocket::_write_str | ( | int | socket, |
| const std::string & | str, | ||
| const std::string & | filename | ||
| ) | const [protected] |
Write a string to a socket to a particular named socket (when used with textual protocol)
| NoConnection | when there is no open socket |
| ConnectionClosed | when there is no more connection. |
Definition at line 68 of file localsocket.cc.
References HERE, and SENDTO_FLAGS.
Referenced by writeto().
{
int res = 1;
const char *buf = str.c_str();
unsigned int count = 0;
struct sockaddr_un name;
name.sun_family = AF_UNIX;
strncpy(name.sun_path, filename.c_str(), sizeof (name.sun_path));
name.sun_path[sizeof (name.sun_path) - 1] = '\0';
if (socket < 0)
throw NoConnection("No Socket", HERE);
while (res && count < str.size())
{
res = sendto(socket, buf + count,
str.size() - count, SENDTO_FLAGS,
(const struct sockaddr*)&name, sizeof(name));
if (res <= 0)
throw ConnectionClosed("Connection Closed", HERE);
count += res;
}
}
| void Network::LocalSocket::_write_str_bin | ( | int | socket, |
| const std::string & | str, | ||
| const std::string & | filename | ||
| ) | const [protected] |
Write a string to a socket to a particular named socket (when used with binary protocol)
| NoConnection | when there is no open socket |
| ConnectionClosed | when there is no more connection. |
Definition at line 93 of file localsocket.cc.
References HERE, and SENDTO_FLAGS.
Referenced by writeto().
{
int res = 1;
unsigned int count = 0;
struct sockaddr_un name;
char buf[str.size() + 2];
buf[0] = str.size() / 256;
buf[1] = str.size() % 256;
memcpy(buf + 2, str.c_str(), str.size());
name.sun_family = AF_UNIX;
strncpy(name.sun_path, filename.c_str(), sizeof (name.sun_path));
name.sun_path[sizeof (name.sun_path) - 1] = DEFAULT_DELIM;
if (socket < 0)
throw NoConnection("No Socket", HERE);
while (res && count < str.size() + 2)
{
res = sendto(socket, buf + count, str.size() + 2 - count, SENDTO_FLAGS,
(const struct sockaddr*)&name, sizeof(name));
if (res <= 0)
throw ConnectionClosed("Connection Closed", HERE);
count += res;
}
}
| void Network::LocalSocket::close | ( | ) |
connect to a local socket (client)
Definition at line 40 of file localsocket.cc.
References Network::Socket::_close(), _filename, and Network::Socket::_socket.
Referenced by ~LocalSocket().
| void Network::LocalSocket::init | ( | const std::string & | filename | ) |
Here is an example of named server server using libsocket :
#include <stdlib.h> #include <iostream> #include <string> #include "socket/localsocket.hh"
int main(int argc, char **argv)
{
Network::LocalSocket server;
std::string filename, client, str(""); if (argc < 2)
{
std::cout << "Use: " << argv[0] << " filename" << std::endl;
exit(0);
}
try
{
filename = std::string(argv[1]);
server.init(filename);
while (str != "quit")
{
//server.read(filename 30); //read with a timeout of 30 seconds
//server >> str; //read without geting the named name
//(cannot sent data)
str = server.read(client);
std::string msg = "ok, I received [" + str + "]";
server.writeto(msg, client);
std::cout << "[" << str << "] from : " << client << std::endl;
}
server.close();
exit (0);
}
catch (Network::Timeout e)
{
std::cerr << e;
std::cerr << "No connection during last 30s, closing connection"
<< std::endl;
exit (1);
}
catch (Network::Exception e)
{
std::cerr << e;
exit(1);
}
}Here is an example of named server server using libsocket :
#include <stdlib.h>
#include <iostream>
#include <string>
#include "socket/localsocket.hh" int main(int argc, char **argv)
{
Network::LocalSocket client;
std::string client_filename, server_filename, str(""), msg; if (argc < 3)
{
std::cout << "Use: " << argv[0] << " <client_filename> "
<< "<server_filename>" << std::endl;
exit(0);
}
try
{
client_filename = std::string(argv[1]);
server_filename = std::string(argv[2]); client.init(client_filename);
while (str != "quit")
{
std::cout << "Msg: ";
std::cin >> str;
client.writeto(str, server_filename);
client >> msg;
std::cout << "[received] " << msg << std::endl;
}
client.close();
exit (0);
}
catch (Network::Timeout e)
{
std::cerr << e;
std::cerr << "No connection during last 30s, closing connection"
<< std::endl;
exit (1);
}
catch (Network::Exception e)
{
std::cerr << e;
exit(1);
}
}Initialize a local socket (server)
Definition at line 34 of file localsocket.cc.
References _bind(), _filename, and Network::Socket::_socket.
| std::string Network::LocalSocket::read | ( | std::string & | filename, |
| int | timeout | ||
| ) |
read a string and put the client named socket name in filename with a timeout
Definition at line 382 of file localsocket.cc.
References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), Network::Socket::_set_timeout(), Network::Socket::_socket, Network::Socket::_state_timeout, and Network::binary.
{
if (_proto_kind == binary)
{
_set_timeout(true, _socket, timeout);
return _read_line_bin(_socket, filename, 0);
}
else
{
_state_timeout = timeout;
return _read_line(_socket, filename);
}
}
| std::string Network::LocalSocket::read | ( | std::string & | filename | ) |
read a string and put the client named socket name in filename
Definition at line 374 of file localsocket.cc.
References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), and Network::Socket::_socket.
{
if (_proto_kind)
return _read_line_bin(_socket, filename, 0);
else
return _read_line(_socket, filename);
}
| std::string Network::LocalSocket::read | ( | ) | [virtual] |
function used by >> operator (read a string on current socket)
Implements Network::Socket.
Definition at line 352 of file localsocket.cc.
References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), Network::Socket::_socket, and Network::binary.
{
if (_proto_kind == binary)
return _read_line_bin(_socket, 0);
else
return _read_line(_socket);
}
| std::string Network::LocalSocket::read | ( | int | timeout | ) | [virtual] |
read a string with a timeout
Implements Network::Socket.
Definition at line 360 of file localsocket.cc.
References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), Network::Socket::_set_timeout(), Network::Socket::_socket, Network::Socket::_state_timeout, and Network::binary.
{
if (_proto_kind == binary)
{
_set_timeout(true, _socket, timeout);
return _read_line_bin(_socket, 0);
}
else
{
_state_timeout = timeout;
return _read_line(_socket);
}
}
| std::string Network::LocalSocket::readn | ( | unsigned int | size | ) | [virtual] |
read a string from socket
| size | represente the number of byte to read |
Implements Network::Socket.
Definition at line 315 of file localsocket.cc.
References _read_line_bin(), and Network::Socket::_socket.
{
// _read_line_bin is bufferised with the same buffer as textual
// protocols, so this function can be used for binary and text
// protocols.
return _read_line_bin(_socket, size);
}
| std::string Network::LocalSocket::readn | ( | int | timeout, |
| unsigned int | size | ||
| ) | [virtual] |
read a string with a timeout
| size | represente the number of byte to read |
Implements Network::Socket.
Definition at line 323 of file localsocket.cc.
References Network::Socket::_buffer, _read_line_bin(), Network::Socket::_set_timeout(), and Network::Socket::_socket.
{
if (!size || size > _buffer.size())
_set_timeout(true, _socket, timeout);
// _read_line_bin is bufferised with the same buffer as textual
// protocols, so this function can be used for binary and text
// protocols.
return _read_line_bin(_socket, size);
}
| std::string Network::LocalSocket::readn | ( | std::string & | filename, |
| int | timeout, | ||
| unsigned int | size | ||
| ) |
read a string and put the client named socket name in filename with a timeout
| size | represente the number of byte to read |
Definition at line 341 of file localsocket.cc.
References Network::Socket::_buffer, _read_line_bin(), Network::Socket::_set_timeout(), and Network::Socket::_socket.
{
if (!size || size > _buffer.size())
_set_timeout(true, _socket, timeout);
// _read_line_bin is bufferised with the same buffer as textual
// protocols, so this function can be used for binary and text
// protocols.
return _read_line_bin(_socket, filename, size);
}
| std::string Network::LocalSocket::readn | ( | std::string & | filename, |
| unsigned int | size | ||
| ) |
read a string and put the client named socket name in filename
| size | represente the number of byte to read |
Definition at line 333 of file localsocket.cc.
References _read_line_bin(), and Network::Socket::_socket.
{
// _read_line_bin is bufferised with the same buffer as textual
// protocols, so this function can be used for binary and text
// protocols.
return _read_line_bin(_socket, filename, size);
}
| void Network::LocalSocket::writeto | ( | const std::string & | str, |
| const std::string & | filename | ||
| ) |
function used to send a msg to a specific named socket
Definition at line 306 of file localsocket.cc.
References Network::Socket::_proto_kind, Network::Socket::_socket, _write_str(), _write_str_bin(), and Network::binary.
{
if (_proto_kind == binary)
_write_str_bin(_socket, str, filename);
else
_write_str(_socket, str, filename);
}
std::string Network::LocalSocket::_filename [protected] |
Definition at line 224 of file localsocket.hh.