Flair
Framework Libre Air
flair::core::Socket Class Reference

Class encapsulating a UDP socket. It assumes packets are coming from only one distant host on a given port. More...

#include <Socket.h>

+ Inheritance diagram for flair::core::Socket:

Public Member Functions

 Socket (const Object *parent, std::string name, std::string address, bool broadcast=false)
 Constructor. More...
 
 Socket (const Object *parent, std::string name, uint16_t port)
 Constructor. More...
 
 ~Socket ()
 Destructor. More...
 
void SendMessage (std::string message)
 Send a message. More...
 
void SendMessage (const char *message, size_t message_len)
 Send a message. More...
 
ssize_t RecvMessage (char *buf, size_t buf_len, Time timeout, char *src=NULL, size_t *src_len=NULL)
 Receive a message. More...
 
void NetworkToHost (char *data, size_t dataSize)
 
void HostToNetwork (char *data, size_t dataSize)
 
- Public Member Functions inherited from flair::core::Object
 Object (const Object *parent=NULL, std::string name="", std::string type="")
 Constructor. More...
 
virtual ~Object ()
 Destructor. More...
 
std::string ObjectName (void) const
 Name. More...
 
std::string ObjectType (void) const
 Type. More...
 
const ObjectParent (void) const
 Parent. More...
 
std::vector< const Object * > * TypeChilds (void) const
 Childs of the same type. More...
 
std::vector< const Object * > * Childs (void) const
 Childs. More...
 
void Information (const char *function, int line, const char *format,...) const
 Formatted information. More...
 
void Warning (const char *function, const char *format,...) const
 Formatted warning. More...
 
void Error (const char *function, const char *format,...) const
 Formatted error. More...
 
bool ErrorOccured (bool recursive=true) const
 Has an errror occured? More...
 

Additional Inherited Members

- Public Types inherited from flair::core::Object
enum  color_t { Auto = 0, Red = 31, Green = 32, Orange = 33 }
 

Detailed Description

Class encapsulating a UDP socket. It assumes packets are coming from only one distant host on a given port.

Constructor & Destructor Documentation

flair::core::Socket::Socket ( const Object parent,
std::string  name,
std::string  address,
bool  broadcast = false 
)

Constructor.

Construct the client side of the socket

Parameters
parentparent
namename
addressserver address (ex 192.168.1.1:9000)
broadcasttrue if address is a broadcast address
flair::core::Socket::Socket ( const Object parent,
std::string  name,
uint16_t  port 
)

Constructor.

Construct the server side of the socket

Parameters
parentparent
namename
portlistening port
flair::core::Socket::~Socket ( )

Destructor.

Member Function Documentation

void flair::core::Socket::SendMessage ( std::string  message)

Send a message.

In case of a broadcast Socket, Parent()->ObjectName() is used as source of the message, this name should be unique.

Parameters
messagemessage
void flair::core::Socket::SendMessage ( const char *  message,
size_t  message_len 
)

Send a message.

Parameters
messagemessage
message_lenmessage length
ssize_t flair::core::Socket::RecvMessage ( char *  buf,
size_t  buf_len,
Time  timeout,
char *  src = NULL,
size_t *  src_len = NULL 
)

Receive a message.

Receive a message and wait up to timeout.
If src and src_len are specified, the source of the message will be copied in the src buffer.
Note that in case of a broadcast socket, own messages are filtered and are not received.

Parameters
bufbuffer to put the message
buf_lenbuffer length
timeouttimeout
srcbuffer to put source name
src_lenbuffer length
Returns
size of the received message