SimGear - Simulator Construction Tools
SGSocketUDP Class Reference

#include <sg_socket_udp.hxx>

Inheritance diagram for SGSocketUDP:
SGIOChannel

Public Member Functions

 SGSocketUDP (const std::string &host, const std::string &port)
 
 ~SGSocketUDP ()
 
bool close ()
 
virtual bool eof () const
 
SGProtocolDir get_dir () const
 
std::string get_hostname () const
 
std::string get_port_str () const
 
SGChannelType get_type () const
 
bool isvalid () const
 
bool open (const SGProtocolDir d)
 
int read (char *buf, int length)
 
int readline (char *buf, int length)
 
void set_dir (const SGProtocolDir d)
 
void set_type (SGChannelType t)
 
void set_valid (const bool v)
 
bool setBlocking (bool value)
 
int write (const char *buf, const int length)
 
int writestring (const char *str)
 

Private Attributes

std::string hostname
 
short unsigned int port
 
std::string port_str
 
char save_buf [2 *SG_IO_MAX_MSG_SIZE]
 
int save_len
 
simgear::Socket sock
 

Detailed Description

A UDP socket I/O class based on SGIOChannel and plib/net.

Definition at line 42 of file sg_socket_udp.hxx.

Constructor & Destructor Documentation

SGSocketUDP::SGSocketUDP ( const std::string &  host,
const std::string &  port 
)

Create an instance of SGSocketUDP.

When calling the constructor you need to provide a host name, and a port number. The convention used by the SGSocketUDP class is that the server side listens and the client side sends. For a server socket, the host name should be empty. For a server, the port number is optional, if you do not specify a port, the system will assign one. For a client socket, you need to specify both a destination host and destination port.

UDP sockets are a lower level protocol than TCP sockets and are "connectionless" in the sense that either client or server can exist, or not exist, startup, quit, etc. in any order and whenever both ends are alive, the communication succeeds. With UDP sockets, the server end just sits and listens for incoming packets from anywhere. The client end sends it's message and forgets about it. It doesn't care if there isn't even a server out there listening and all the packets are getting lost. Although systems/networks usually do a pretty good job (statistically) of getting your UDP packets to their destination, there is no guarantee that any particular packet will make it. But, because of this low level implementation and lack of error checking, UDP packets are much faster and efficient. UDP packets are good for sending positional information to synchronize two applications. In this case, you want the information to arrive as quickly as possible, and if you lose a packet, you'd rather get new updated information rather than have the system waste time resending a packet that is becoming older and older with every retry.

Parameters
hostname of host if direction is SG_IO_OUT or SG_IO_BI
portport number if we care to choose one.
stylespecify "udp" or "tcp"

Definition at line 39 of file sg_socket_udp.cxx.

SGSocketUDP::~SGSocketUDP ( )

Destructor

Definition at line 48 of file sg_socket_udp.cxx.

Member Function Documentation

bool SGSocketUDP::close ( void  )
virtual

The close() method is modeled after the close() Unix system call and will close an open device. You should call this method when you are done using your IO class, before it is destructed.

Returns
result of close

Reimplemented from SGIOChannel.

Definition at line 200 of file sg_socket_udp.cxx.

bool SGIOChannel::eof ( ) const
virtualinherited

The eof() method returns true if end of file has been reached in a context where that makes sense. Otherwise it returns false.

Returns
result of eof check

Reimplemented in SGFile.

Definition at line 76 of file iochannel.cxx.

SGProtocolDir SGIOChannel::get_dir ( ) const
inlineinherited

Definition at line 158 of file iochannel.hxx.

Referenced by SGFile::open(), and open().

std::string SGSocketUDP::get_hostname ( ) const
inline
Returns
the remote host name

Definition at line 124 of file sg_socket_udp.hxx.

std::string SGSocketUDP::get_port_str ( ) const
inline
Returns
the port number (in string form)

Definition at line 127 of file sg_socket_udp.hxx.

SGChannelType SGIOChannel::get_type ( ) const
inlineinherited

Definition at line 155 of file iochannel.hxx.

bool SGIOChannel::isvalid ( ) const
inlineinherited

Definition at line 159 of file iochannel.hxx.

Referenced by close(), read(), readline(), write(), and writestring().

bool SGSocketUDP::open ( const SGProtocolDir  d)
virtual

Open a channel.

Parameters
dchannel communication "direction" Direction can be one of:
  • SG_IO_IN - data will be flowing into this object to the application.
  • SG_IO_OUT - data will be flowing out of this object from the application.
  • SG_IO_BI - data will be flowing in both directions.
  • SG_IO_NONE - data will not be flowing in either direction. This is here for the sake of completeness.
Returns
result of open

Reimplemented from SGIOChannel.

Definition at line 55 of file sg_socket_udp.cxx.

int SGSocketUDP::read ( char *  buf,
int  length 
)
virtual

The read() method is modeled after the read() Unix system call. You must provide a pointer to a character buffer that has enough allocated space for your potential read. You can also specify the maximum number of bytes allowed for this particular read. The actual number of bytes read is returned. You are responsible to ensure that the size of buf is large enough to accomodate your input message

Parameters
bufa char pointer to your input buffer
lengthmax number of bytes to read
Returns
number of bytes read

Reimplemented from SGIOChannel.

Definition at line 102 of file sg_socket_udp.cxx.

int SGSocketUDP::readline ( char *  buf,
int  length 
)
virtual

The readline() method is similar to read() except that it will stop at the first end of line encountered in the input buffer.

Parameters
bufa char pointer to your input buffer
lengthmax number of bytes to read
Returns
number of bytes read

Reimplemented from SGIOChannel.

Definition at line 124 of file sg_socket_udp.cxx.

void SGIOChannel::set_dir ( const SGProtocolDir  d)
inlineinherited

Definition at line 157 of file iochannel.hxx.

Referenced by SGFile::open(), SGSerial::open(), open(), and SGSocket::open().

void SGIOChannel::set_type ( SGChannelType  t)
inlineinherited

Definition at line 154 of file iochannel.hxx.

Referenced by SGFile::SGFile(), SGSerial::SGSerial(), and SGSocket::SGSocket().

void SGIOChannel::set_valid ( const bool  v)
inlineinherited

Definition at line 160 of file iochannel.hxx.

Referenced by open(), and SGSocketUDP().

bool SGSocketUDP::setBlocking ( bool  value)

Set blocking true or false

Returns
success/failure

Definition at line 212 of file sg_socket_udp.cxx.

int SGSocketUDP::write ( const char *  buf,
const int  length 
)
virtual

The write() method is modeled after the write() Unix system call and is analogous to the read() method. You provide a pointer to a buffer of data, and then length of that data to be written out. The number of bytes written is returned.

Parameters
bufa char pointer to your output buffer
lengthnumber of bytes to write
Returns
number of bytes written

Reimplemented from SGIOChannel.

Definition at line 174 of file sg_socket_udp.cxx.

Referenced by writestring().

int SGSocketUDP::writestring ( const char *  str)
virtual

The writestring() method is a simple wrapper that will calculate the length of a null terminated character array and write it to the output channel.

Parameters
bufa char pointer to your output buffer
Returns
number of bytes written

Reimplemented from SGIOChannel.

Definition at line 189 of file sg_socket_udp.cxx.

Member Data Documentation

std::string SGSocketUDP::hostname
private

Definition at line 48 of file sg_socket_udp.hxx.

Referenced by get_hostname(), and open().

short unsigned int SGSocketUDP::port
private

Definition at line 54 of file sg_socket_udp.hxx.

Referenced by open(), and write().

std::string SGSocketUDP::port_str
private

Definition at line 49 of file sg_socket_udp.hxx.

Referenced by get_port_str(), and open().

char SGSocketUDP::save_buf[2 *SG_IO_MAX_MSG_SIZE]
private

Definition at line 51 of file sg_socket_udp.hxx.

Referenced by readline().

int SGSocketUDP::save_len
private

Definition at line 52 of file sg_socket_udp.hxx.

Referenced by readline().

simgear::Socket SGSocketUDP::sock
private

Definition at line 46 of file sg_socket_udp.hxx.

Referenced by close(), open(), read(), readline(), setBlocking(), and write().


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