tobicore  7.0.0
 All Classes Functions Variables Typedefs Enumerator Friends Groups Pages
TPSocket Class Reference

TCP/UDP socket. More...

#include <tobiplatform/TPSocket.hpp>

+ Collaboration diagram for TPSocket:

Public Types

enum  { TCP = 0, UDP = 1 }

Public Member Functions

 TPSocket (int type=TPSocket::TCP, size_t bsize=4096)
 Constructor.
virtual ~TPSocket (void)
 Destructor.
bool Open (bool asserver)
 Initializes whatever needs to be initialized.
bool Close (void)
 Closes the socket.
bool Async (bool block)
 Configures the socket for asynchronous operation.
bool Bind (const std::string &ip, const std::string &port)
 Bind the socket to a specified IP:port.
bool Listen (void)
 Starts listening.
int Accept (TPSocket *endpoint)
 Accepts an endpoint.
bool Connect (const std::string &ip, const std::string &port)
 Connects the socket to a specified IP:port.
ssize_t Send (const std::string &message)
 Sends a string to the remote endpoint.
ssize_t Recv (std::string *message)
 Receives a string from the remote endpoint.
bool IsConnected (void)
 Checks if socket is connected.

Public Attributes

TPHost local
 Host informations associated to the current socket.
TPHost remote
 Host informations associated to the remote endpoint.

Protected Member Functions

void Init (void)
 Allocates internal memory and initializes structures.
void Free (void)
 Releases internal memory.
bool GetLocal (void)
 Updates local TPHost.
bool GetRemote (void)
 Updates remote TPHost.
void GetMaxBSize (void)
 Updates maximum buffer size.

Protected Attributes

int _fd
 File descriptor.
struct sockaddr_in _address
 Socket address.
struct sockaddr_storage _endpoint
 Endpoint address.
void * _buffer
 Buffer.
size_t _bsize
 Buffer size.
unsigned int _mc
 Maximum number of connections.
struct addrinfo _results
 Socket info.
struct addrinfo * _info
 Socket info.
size_t _bsizemax
 Maximum size of message.

Detailed Description

TCP/UDP socket.

Provides basic sync/async connectivity.

Examples:
tptcpclient.cpp, and tptcpserver.cpp.

Definition at line 61 of file TPSocket.hpp.

Member Enumeration Documentation

anonymous enum
Enumerator:
TCP 

Configure as TCP.

UDP 

Configure as UDP.

Definition at line 159 of file TPSocket.hpp.

Constructor & Destructor Documentation

TPSocket::TPSocket ( int  type = TPSocket::TCP,
size_t  bsize = 4096 
)

Constructor.

  • type Socket type, TCP or UDP
  • bsize Internal buffer size

Definition at line 42 of file TPSocket.cpp.

Member Function Documentation

int TPSocket::Accept ( TPSocket endpoint)

Accepts an endpoint.

Returns
The file descriptor of successful, <= 0 otherwise

Definition at line 216 of file TPSocket.cpp.

+ Here is the call graph for this function:

bool TPSocket::Async ( bool  block)

Configures the socket for asynchronous operation.

  • block True for asynchronous operation, false for synchronous
Examples:
tptcpclient.cpp.

Definition at line 53 of file TPSocket.cpp.

bool TPSocket::Bind ( const std::string &  ip,
const std::string &  port 
)

Bind the socket to a specified IP:port.

If TPSocket is configured AsClient this method will always fail.

  • ip IP address (i.e. "127.0.0.1", "1.2.3.4", "0.0.0.0")
  • port Port to bind (i.e. "8080")
    Returns
    True if successful, false otherwise

Definition at line 173 of file TPSocket.cpp.

+ Here is the call graph for this function:

bool TPSocket::Connect ( const std::string &  ip,
const std::string &  port 
)

Connects the socket to a specified IP:port.

If TPSocket is configured AsServer this method will always fail.

  • ip IP address (i.e. "127.0.0.1", "1.2.3.4", "0.0.0.0")
  • port Port to bind (i.e. "8080")
    Returns
    True if successful, false otherwise
Examples:
tptcpclient.cpp.

Definition at line 235 of file TPSocket.cpp.

+ Here is the call graph for this function:

bool TPSocket::IsConnected ( void  )

Checks if socket is connected.

Returns
True if connected, false otherwise

Definition at line 324 of file TPSocket.cpp.

+ Here is the call graph for this function:

bool TPSocket::Listen ( void  )

Starts listening.

Returns
True if successful, false otherwise

Definition at line 212 of file TPSocket.cpp.

bool TPSocket::Open ( bool  asserver)

Initializes whatever needs to be initialized.

  • asserver Perform initialization for a server socket if true, for a client socket otherwise.
Examples:
tptcpclient.cpp.

Definition at line 150 of file TPSocket.cpp.

+ Here is the call graph for this function:

ssize_t TPSocket::Recv ( std::string *  message)

Receives a string from the remote endpoint.

  • message Buffer for receiving
    Returns
    Received bytes
Examples:
tptcpclient.cpp, and tptcpserver.cpp.

Definition at line 285 of file TPSocket.cpp.

+ Here is the caller graph for this function:

ssize_t TPSocket::Send ( const std::string &  message)

Sends a string to the remote endpoint.

  • message Message to send
    Returns
    Sent bytes
Examples:
tptcpclient.cpp, and tptcpserver.cpp.

Definition at line 266 of file TPSocket.cpp.

+ Here is the caller graph for this function:


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