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

#include <socket.h>

Collaboration diagram for tcpip::Socket:

Public Member Functions

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

Static Public Member Functions

static int getFreeSocketPort ()
 Returns an free port on the system. 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)
 
bool datawaiting (int sock) const
 
void init ()
 

Static Private Member Functions

static void BailOnSocketError (std::string context)
 

Private Attributes

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

Friends

class Response
 

Detailed Description

Definition at line 60 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 79 of file socket.cpp.

◆ Socket() [2/2]

tcpip::Socket::Socket ( int  port)

Constructor that prepare for accepting a connection on given port.

Definition at line 92 of file socket.cpp.

◆ ~Socket()

tcpip::Socket::~Socket ( )

Destructor.

Definition at line 153 of file socket.cpp.

References close(), and server_socket_.

Member Function Documentation

◆ accept()

Socket * tcpip::Socket::accept ( const bool  create = false)

Wait for a incoming connection to port_.

Definition at line 266 of file socket.cpp.

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

Referenced by TraCIServer::TraCIServer().

◆ atoaddr()

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

Definition at line 231 of file socket.cpp.

◆ BailOnSocketError()

void tcpip::Socket::BailOnSocketError ( std::string  context)
staticprivate

Definition at line 183 of file socket.cpp.

Referenced by accept(), init(), and send().

◆ close()

void tcpip::Socket::close ( )

◆ connect()

void tcpip::Socket::connect ( )

Connects to host_:port_.

Definition at line 364 of file socket.cpp.

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

◆ datawaiting()

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

Definition at line 206 of file socket.cpp.

◆ getFreeSocketPort()

int tcpip::Socket::getFreeSocketPort ( )
static

Returns an free port on the system.

Note
This is done by binding a socket with port=0, getting the assigned port, and closing the socket again

Definition at line 124 of file socket.cpp.

◆ has_client_connection()

bool tcpip::Socket::has_client_connection ( ) const

Definition at line 565 of file socket.cpp.

References blocking_.

◆ init()

void tcpip::Socket::init ( )
private

Definition at line 106 of file socket.cpp.

References BailOnSocketError().

◆ is_blocking()

bool tcpip::Socket::is_blocking ( )

Definition at line 574 of file socket.cpp.

◆ port()

int tcpip::Socket::port ( )

Definition at line 197 of file socket.cpp.

◆ 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 490 of file socket.cpp.

◆ receive()

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

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

Definition at line 508 of file socket.cpp.

◆ receiveComplete()

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

Receive len bytes from Socket::socket_.

Definition at line 475 of file socket.cpp.

◆ receiveExact()

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

Receive a complete TraCI message from Socket::socket_.

Definition at line 533 of file socket.cpp.

Referenced by TraCIAPI::check_resultState().

◆ 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 455 of file socket.cpp.

◆ send()

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

Definition at line 406 of file socket.cpp.

References BailOnSocketError(), and socket_.

Referenced by OutputDevice_Network::postWriteHook().

◆ sendExact()

◆ set_blocking()

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

Definition at line 337 of file socket.cpp.

Referenced by accept().

◆ set_verbose()

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

Definition at line 97 of file socket.h.

References verbose_.

◆ verbose()

bool tcpip::Socket::verbose ( )
inline

Definition at line 96 of file socket.h.

References verbose_.

Friends And Related Function Documentation

◆ Response

friend class Response
friend

Definition at line 62 of file socket.h.

Field Documentation

◆ blocking_

bool tcpip::Socket::blocking_
private

Definition at line 123 of file socket.h.

Referenced by accept(), and has_client_connection().

◆ host_

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

Definition at line 119 of file socket.h.

◆ lengthLen

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

Length of the message length part of a TraCI message.

Definition at line 101 of file socket.h.

◆ port_

int tcpip::Socket::port_
private

Definition at line 120 of file socket.h.

Referenced by accept().

◆ server_socket_

int tcpip::Socket::server_socket_
private

Definition at line 122 of file socket.h.

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

◆ socket_

int tcpip::Socket::socket_
private

Definition at line 121 of file socket.h.

Referenced by send().

◆ verbose_

bool tcpip::Socket::verbose_
private

Definition at line 125 of file socket.h.

Referenced by set_verbose(), and verbose().


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