Network::LocalSocket Class Reference

This class represent a local connection (client and server). More...

#include <localsocket.hh>

Inheritance diagram for Network::LocalSocket:

Network::Socket List of all members.

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)
 Initialize a local socket (server).
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
Exceptions:
NoConnection when there is no open socket
BindError when bind libc function return a negative value.

std::string _read_line (int socket)
 Get a line from socket (when used with textual protocol)
Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

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)
Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

std::string _read_line_bin (int socket, unsigned int size)
 Get a line from socket (when used with binary protocol)
Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

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)
Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

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)
Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

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)
Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.


Protected Attributes

std::string _filename

Detailed Description

This class represent a local connection (client and server).

Author:
Julien Lemoine <speedblue at="" happycoders="" dot="" org>="">

Definition at line 34 of file localsocket.hh.


Constructor & Destructor Documentation

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]

Definition at line 44 of file localsocket.hh.

References close().


Member Function Documentation

int Network::LocalSocket::_bind ( const std::string &  filename  )  [protected]

Initialize a local socket connection (server in UDP) create a named socket with name filename

Exceptions:
NoConnection when there is no open socket
BindError when bind libc function return a negative value.

Definition at line 48 of file localsocket.cc.

References HERE.

Referenced by init().

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)

Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

Definition at line 126 of file localsocket.cc.

References Network::Socket::_buffer, Network::Socket::_state_timeout, Network::Socket::_update_buffer(), and HERE.

std::string Network::LocalSocket::_read_line ( int  socket  )  [protected, virtual]

Get a line from socket (when used with textual protocol)

Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

Implements Network::Socket.

Definition at line 218 of file localsocket.cc.

References Network::Socket::_buffer, Network::Socket::_state_timeout, Network::Socket::_update_buffer(), and HERE.

Referenced by read().

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)

Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

Definition at line 247 of file localsocket.cc.

References Network::Socket::_buffer, and HERE.

std::string Network::LocalSocket::_read_line_bin ( int  socket,
unsigned int  size 
) [protected, virtual]

Get a line from socket (when used with binary protocol)

Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

Implements Network::Socket.

Definition at line 163 of file localsocket.cc.

References Network::Socket::_buffer, and HERE.

Referenced by read(), and readn().

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)

Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

Definition at line 67 of file localsocket.cc.

References HERE, and SENDTO_FLAGS.

Referenced by writeto().

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)

Exceptions:
NoConnection when there is no open socket
ConnectionClosed when there is no more connection.

Definition at line 92 of file localsocket.cc.

References HERE, and SENDTO_FLAGS.

Referenced by writeto().

void Network::LocalSocket::close (  ) 

connect to a local socket (client)

Definition at line 39 of file localsocket.cc.

References Network::Socket::_close(), _filename, and Network::Socket::_socket.

Referenced by ~LocalSocket().

void Network::LocalSocket::init ( const std::string &  filename  ) 

Initialize a local socket (server).

 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);
       server.add_delim("\n");
       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 socket client 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);
       client.add_delim("\n");
       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);
     }
 }

 

Definition at line 33 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 388 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 380 of file localsocket.cc.

References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), and Network::Socket::_socket.

std::string Network::LocalSocket::read ( int  timeout  )  [virtual]

read a string with a timeout

Implements Network::Socket.

Definition at line 366 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 (  )  [virtual]

function used by >> operator (read a string on current socket)

Implements Network::Socket.

Definition at line 358 of file localsocket.cc.

References Network::Socket::_proto_kind, _read_line(), _read_line_bin(), Network::Socket::_socket, and Network::binary.

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

Parameters:
size represente the number of byte to read

Definition at line 347 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

Parameters:
size represente the number of byte to read

Definition at line 339 of file localsocket.cc.

References _read_line_bin(), and Network::Socket::_socket.

std::string Network::LocalSocket::readn ( int  timeout,
unsigned int  size 
) [virtual]

read a string with a timeout

Parameters:
size represente the number of byte to read

Implements Network::Socket.

Definition at line 329 of file localsocket.cc.

References Network::Socket::_buffer, _read_line_bin(), Network::Socket::_set_timeout(), and Network::Socket::_socket.

std::string Network::LocalSocket::readn ( unsigned int  size  )  [virtual]

read a string from socket

Parameters:
size represente the number of byte to read

Implements Network::Socket.

Definition at line 321 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 312 of file localsocket.cc.

References Network::Socket::_proto_kind, Network::Socket::_socket, _write_str(), _write_str_bin(), and Network::binary.


Member Data Documentation

std::string Network::LocalSocket::_filename [protected]

Definition at line 226 of file localsocket.hh.

Referenced by close(), and init().


The documentation for this class was generated from the following files:
Generated on Thu Dec 28 19:14:02 2006 for libsocket by  doxygen 1.4.7