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 More... | |
std::string | read () |
function used by >> operator (read a string on current socket) More... | |
std::string | read (int timeout) |
read a string with a timeout More... | |
std::string | read (std::string &filename) |
read a string and put the client named socket name in filename More... | |
std::string | read (std::string &filename, int timeout) |
read a string and put the client named socket name in filename with a timeout More... | |
std::string | readn (unsigned int size) |
read a string from socket More... | |
std::string | readn (int timeout, unsigned int size) |
read a string with a timeout More... | |
std::string | readn (std::string &filename, unsigned int size) |
read a string and put the client named socket name in filename More... | |
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 More... | |
void | init (const std::string &filename) |
Here is an example of named server server using libsocket : More... | |
void | close () |
connect to a local socket (client) More... | |
![]() | |
Socket (SOCKET_KIND kind, SOCKET_VERSION version=V4) | |
Socket (SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version=V4) | |
virtual | ~Socket () |
void | write (const std::string &str) |
function used by << operator (write a string on current socket) More... | |
bool | connected () const |
return true when socket is connected More... | |
int | get_socket () |
get socket (fd) warning: be very carefull with this method More... | |
void | add_delim (const std::string &delim) |
set the delimitor for the text mode More... | |
void | del_delim (const std::string &delim) |
delete this delimitor for the socket More... | |
void | allow_empty_lines () |
, if set, empty lines will be returned in text procols (if not, they are skipped) More... | |
void | init_tls (GnuTLSKind kind, unsigned size=1024, const std::string &certfile="", const std::string &keyfile="", const std::string &trustfile="", const std::string &crlfile="") |
void | enable_tls () |
Enable TLS on socket. More... | |
Protected Member Functions | |
int | _bind (const std::string &filename) |
Initialize a local socket connection (server in UDP) create a named socket with name filename. More... | |
std::string | _read_line (int socket) |
Get a line from socket (when used with textual protocol) More... | |
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) More... | |
std::string | _read_line_bin (int socket, unsigned int size) |
Get a line from socket (when used with binary protocol) More... | |
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) More... | |
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) More... | |
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) More... | |
![]() | |
void | _close (int socket) const |
Close a connnection. More... | |
void | _listen (int socket) const |
Listen on port. More... | |
void | _write_str (int socket, const std::string &str) const |
Write a string to a socket (when used with textual protocol) More... | |
void | _write_str_bin (int socket, const std::string &str) const |
Write a string to a socket (when used with binary protocol) More... | |
void | _set_timeout (bool enable, int socket, int timeout) |
set a timeout on a socket More... | |
std::pair< int, int > | _find_delim (const std::string &str, int start) const |
bool | _update_buffer (std::pair< int, int > &delim, int &i, std::string &str) |
look delimiter and remove delimiter at begining of buffer if needed More... | |
bool | _check_answer (int res, std::string &str) |
return the content of the buffer is there is More... | |
Protected Attributes | |
std::string | _filename |
![]() | |
SOCKET_KIND | _kind |
SOCKET_VERSION | _version |
unsigned | _state_timeout |
int | _socket |
int | _recv_flags |
struct sockaddr_in | _addr |
PROTO_KIND | _proto_kind |
std::list< std::string > | _delim |
bool | _empty_lines |
std::string | _buffer |
bool | _tls |
This class represent a local connection (client and server)
Definition at line 34 of file localsocket.hh.
|
inline |
Definition at line 37 of file localsocket.hh.
|
inline |
Definition at line 40 of file localsocket.hh.
|
inlinevirtual |
|
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().
|
protectedvirtual |
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.
Referenced by read().
|
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.
|
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.
|
protectedvirtual |
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.
|
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().
|
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.
Referenced by writeto().
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.
|
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.
|
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.
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.
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.
|
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.
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.
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.
|
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.
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.
|
protected |
Definition at line 224 of file localsocket.hh.