SUMO - Simulation of Urban MObility
tcpip::Socket Class Reference

#include <socket.h>

Collaboration diagram for tcpip::Socket:
Collaboration graph

Public Member Functions

void accept () throw ( SocketException )
 Wait for a incoming connection to port_. More...
 
void close ()
 
void connect () throw ( SocketException )
 Connects to host_:port_. More...
 
bool has_client_connection () const
 
bool is_blocking () throw ()
 
int port ()
 
std::vector< unsigned char > receive (int bufSize=2048) throw ( SocketException )
 Receive up to bufSize available bytes from Socket::socket_. More...
 
bool receiveExact (Storage &) throw ( SocketException )
 Receive a complete TraCI message from Socket::socket_. More...
 
void send (const std::vector< unsigned char > &buffer) throw ( SocketException )
 
void sendExact (const Storage &) throw ( SocketException )
 
void set_blocking (bool) throw ( SocketException )
 
void set_verbose (bool newVerbose)
 
 Socket (std::string host, int port)
 Constructor that prepare to connect to host:port. More...
 
 Socket (int port)
 Constructor that prepare for accepting a connection on given port. More...
 
bool verbose ()
 
 ~Socket ()
 Destructor. More...
 

Protected Member Functions

void printBufferOnVerbose (const std::vector< unsigned char > buffer, const std::string &label) const
 Print label and buffer to stderr if Socket::verbose_ is set. More...
 
void receiveComplete (unsigned char *const buffer, std::size_t len) const
 Receive len bytes from Socket::socket_. More...
 
size_t recvAndCheck (unsigned char *const buffer, std::size_t len) const
 Receive up to len available bytes from Socket::socket_. More...
 

Static Protected Attributes

static const int lengthLen = 4
 Length of the message length part of a TraCI message. More...
 

Private Member Functions

bool atoaddr (std::string, struct sockaddr_in &addr)
 
void BailOnSocketError (std::string) const throw ( SocketException )
 
bool datawaiting (int sock) const throw ()
 
void init ()
 

Private Attributes

bool blocking_
 
std::string host_
 
int port_
 
int server_socket_
 
int socket_
 
bool verbose_
 

Friends

class Response
 

Detailed Description

Definition at line 78 of file socket.h.

Constructor & Destructor Documentation

§ Socket() [1/2]

tcpip::Socket::Socket ( std::string  host,
int  port 
)

Constructor that prepare to connect to host:port.

Definition at line 73 of file socket.cpp.

References init().

§ Socket() [2/2]

tcpip::Socket::Socket ( int  port)

Constructor that prepare for accepting a connection on given port.

Definition at line 86 of file socket.cpp.

References init().

§ ~Socket()

tcpip::Socket::~Socket ( )

Destructor.

Definition at line 117 of file socket.cpp.

References BailOnSocketError(), close(), server_socket_, and socket_.

Referenced by init().

Member Function Documentation

§ accept()

void tcpip::Socket::accept ( )
throw (SocketException
)

Wait for a incoming connection to port_.

Definition at line 231 of file socket.cpp.

References BailOnSocketError(), blocking_, port_, server_socket_, set_blocking(), and socket_.

Referenced by atoaddr(), and TraCIServer::TraCIServer().

§ atoaddr()

bool tcpip::Socket::atoaddr ( std::string  address,
struct sockaddr_in &  addr 
)
private

Definition at line 196 of file socket.cpp.

References accept(), and port_.

Referenced by connect(), and datawaiting().

§ BailOnSocketError()

void tcpip::Socket::BailOnSocketError ( std::string  context) const
throw (SocketException
)
private

Definition at line 147 of file socket.cpp.

References port().

Referenced by accept(), connect(), datawaiting(), init(), recvAndCheck(), send(), set_blocking(), and ~Socket().

§ close()

void tcpip::Socket::close ( )

§ connect()

void tcpip::Socket::connect ( )
throw (SocketException
)

Connects to host_:port_.

Definition at line 324 of file socket.cpp.

References atoaddr(), BailOnSocketError(), close(), host_, and socket_.

Referenced by TraCIAPI::connect(), OutputDevice_Network::OutputDevice_Network(), receive(), and set_blocking().

§ datawaiting()

bool tcpip::Socket::datawaiting ( int  sock) const
throw (
)
private

Definition at line 171 of file socket.cpp.

References atoaddr(), and BailOnSocketError().

Referenced by port(), and receive().

§ has_client_connection()

bool tcpip::Socket::has_client_connection ( ) const

Definition at line 530 of file socket.cpp.

References is_blocking(), and socket_.

Referenced by receiveExact().

§ init()

void tcpip::Socket::init ( )
private

Definition at line 100 of file socket.cpp.

References BailOnSocketError(), and ~Socket().

Referenced by Socket().

§ is_blocking()

bool tcpip::Socket::is_blocking ( )
throw (
)

Definition at line 539 of file socket.cpp.

References blocking_.

Referenced by has_client_connection().

§ port()

int tcpip::Socket::port ( )

Definition at line 162 of file socket.cpp.

References datawaiting(), and port_.

Referenced by BailOnSocketError().

§ printBufferOnVerbose()

void tcpip::Socket::printBufferOnVerbose ( const std::vector< unsigned char >  buffer,
const std::string &  label 
) const
protected

Print label and buffer to stderr if Socket::verbose_ is set.

Definition at line 453 of file socket.cpp.

References receive(), and verbose_.

Referenced by receive(), receiveComplete(), receiveExact(), and send().

§ receive()

std::vector< unsigned char > tcpip::Socket::receive ( int  bufSize = 2048)
throw (SocketException
)

Receive up to bufSize available bytes from Socket::socket_.

Definition at line 471 of file socket.cpp.

References connect(), datawaiting(), printBufferOnVerbose(), receiveExact(), recvAndCheck(), and socket_.

Referenced by printBufferOnVerbose().

§ receiveComplete()

void tcpip::Socket::receiveComplete ( unsigned char *const  buffer,
std::size_t  len 
) const
protected

Receive len bytes from Socket::socket_.

Definition at line 438 of file socket.cpp.

References printBufferOnVerbose(), and recvAndCheck().

Referenced by receiveExact(), and recvAndCheck().

§ receiveExact()

bool tcpip::Socket::receiveExact ( Storage msg)
throw (SocketException
)

§ recvAndCheck()

size_t tcpip::Socket::recvAndCheck ( unsigned char *const  buffer,
std::size_t  len 
) const
protected

Receive up to len available bytes from Socket::socket_.

Definition at line 418 of file socket.cpp.

References BailOnSocketError(), receiveComplete(), and socket_.

Referenced by receive(), receiveComplete(), and sendExact().

§ send()

void tcpip::Socket::send ( const std::vector< unsigned char > &  buffer)
throw (SocketException
)

§ sendExact()

§ set_blocking()

void tcpip::Socket::set_blocking ( bool  blocking)
throw (SocketException
)

Definition at line 296 of file socket.cpp.

References BailOnSocketError(), blocking_, connect(), and server_socket_.

Referenced by accept().

§ set_verbose()

void tcpip::Socket::set_verbose ( bool  newVerbose)
inline

Definition at line 111 of file socket.h.

§ verbose()

bool tcpip::Socket::verbose ( )
inline

Definition at line 110 of file socket.h.

Friends And Related Function Documentation

§ Response

friend class Response
friend

Definition at line 80 of file socket.h.

Field Documentation

§ blocking_

bool tcpip::Socket::blocking_
private

Definition at line 137 of file socket.h.

Referenced by accept(), is_blocking(), and set_blocking().

§ host_

std::string tcpip::Socket::host_
private

Definition at line 133 of file socket.h.

Referenced by connect().

§ lengthLen

const int tcpip::Socket::lengthLen = 4
staticprotected

Length of the message length part of a TraCI message.

Definition at line 115 of file socket.h.

Referenced by receiveExact(), and sendExact().

§ port_

int tcpip::Socket::port_
private

Definition at line 134 of file socket.h.

Referenced by accept(), atoaddr(), and port().

§ server_socket_

int tcpip::Socket::server_socket_
private

Definition at line 136 of file socket.h.

Referenced by accept(), set_blocking(), and ~Socket().

§ socket_

int tcpip::Socket::socket_
private

Definition at line 135 of file socket.h.

Referenced by accept(), close(), connect(), has_client_connection(), receive(), recvAndCheck(), send(), and ~Socket().

§ verbose_

bool tcpip::Socket::verbose_
private

Definition at line 139 of file socket.h.

Referenced by printBufferOnVerbose().


The documentation for this class was generated from the following files: