simgear  3.5.0 [next] git://gitorious.org/fg/simgear.git
SimGear - Simulator Construction Tools
SGSocket Class Reference

#include <sg_socket.hxx>

Inheritance diagram for SGSocket:
SGIOChannel

Public Member Functions

 SGSocket (const std::string &host, const std::string &port, const std::string &style)
 
 ~SGSocket ()
 
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 nonblock ()
 
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_timeout (int i)
 
void set_type (SGChannelType t)
 
void set_valid (const bool v)
 
int write (const char *buf, const int length)
 
int writestring (const char *str)
 

Private Member Functions

bool make_client_socket ()
 
bool make_server_socket ()
 
int poll ()
 

Private Attributes

simgear::Socketclient
 
bool first_read
 
std::string hostname
 
bool is_server
 
bool is_tcp
 
unsigned short port
 
std::string port_str
 
char save_buf [2 *SG_IO_MAX_MSG_SIZE]
 
int save_len
 
simgear::Socket sock
 
int timeout
 

Static Private Attributes

static bool init = false
 

Detailed Description

A socket I/O class based on SGIOChannel.

Definition at line 44 of file sg_socket.hxx.

Constructor & Destructor Documentation

SGSocket::SGSocket ( const std::string &  host,
const std::string &  port,
const std::string &  style 
)

Create an instance of SGSocket.

When calling the constructor you need to provide a host name, a port number, and a socket style. The convention used by the SGSocket 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. For both client and server sockets you must specify the socket type. Type must be either udp or tcp. Here's a quick breakdown of the major differences between UDP and TCP type sockets.

TCP sockets are the type where you establish a connection and then can read and write to the socket from both ends. If one end of TCP socket connect quits, the other end will likely segfault if it doesn't take special precautions. But, the nice thing about TCP connections is that the underlying protocol guarantees that your message will get through. This imposes a certain performance overhead though on the communication because the protocol must resend failed messages. TCP sockets are good for sending periodic command/response type messages where performance isn't a big issues, but reliability is.

UDP sockets on the other hand are a lower level protocol and don't have the same sort of connection as TCP sockets. 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 41 of file sg_socket.cxx.

SGSocket::~SGSocket ( )

Destructor

Definition at line 72 of file sg_socket.cxx.

Member Function Documentation

bool SGSocket::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 368 of file sg_socket.cxx.

Referenced by ~SGSocket().

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 SGSocketUDP::open().

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

Definition at line 156 of file sg_socket.hxx.

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

Definition at line 159 of file sg_socket.hxx.

SGChannelType SGIOChannel::get_type ( ) const
inlineinherited

Definition at line 155 of file iochannel.hxx.

bool SGIOChannel::isvalid ( ) const
inlineinherited
bool SGSocket::make_client_socket ( )
private

Definition at line 101 of file sg_socket.cxx.

Referenced by open().

bool SGSocket::make_server_socket ( )
private

Definition at line 79 of file sg_socket.cxx.

Referenced by open().

bool SGSocket::nonblock ( )

Enable non-blocking mode.

Returns
success/failure

Definition at line 380 of file sg_socket.cxx.

Referenced by open().

bool SGSocket::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 125 of file sg_socket.cxx.

int SGSocket::poll ( )
private

Definition at line 391 of file sg_socket.cxx.

Referenced by read(), and readline().

int SGSocket::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 212 of file sg_socket.cxx.

int SGSocket::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 248 of file sg_socket.cxx.

void SGIOChannel::set_dir ( const SGProtocolDir  d)
inlineinherited

Definition at line 157 of file iochannel.hxx.

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

void SGSocket::set_timeout ( int  i)
inline

Definition at line 153 of file sg_socket.hxx.

void SGIOChannel::set_type ( SGChannelType  t)
inlineinherited

Definition at line 154 of file iochannel.hxx.

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

void SGIOChannel::set_valid ( const bool  v)
inlineinherited

Definition at line 160 of file iochannel.hxx.

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

int SGSocket::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 333 of file sg_socket.cxx.

Referenced by writestring().

int SGSocket::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 359 of file sg_socket.cxx.

Member Data Documentation

simgear::Socket* SGSocket::client
private

Definition at line 54 of file sg_socket.hxx.

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

bool SGSocket::first_read
private

Definition at line 58 of file sg_socket.hxx.

Referenced by open(), and readline().

std::string SGSocket::hostname
private

Definition at line 47 of file sg_socket.hxx.

Referenced by get_hostname(), make_client_socket(), and make_server_socket().

bool SGSocket::init = false
staticprivate

Definition at line 61 of file sg_socket.hxx.

Referenced by SGSocket().

bool SGSocket::is_server
private

Definition at line 57 of file sg_socket.hxx.

Referenced by open(), poll(), read(), and readline().

bool SGSocket::is_tcp
private

Definition at line 56 of file sg_socket.hxx.

Referenced by make_client_socket(), make_server_socket(), open(), read(), readline(), and SGSocket().

unsigned short SGSocket::port
private

Definition at line 55 of file sg_socket.hxx.

Referenced by make_client_socket(), make_server_socket(), open(), and write().

std::string SGSocket::port_str
private

Definition at line 48 of file sg_socket.hxx.

Referenced by get_port_str(), and open().

char SGSocket::save_buf[2 *SG_IO_MAX_MSG_SIZE]
private

Definition at line 50 of file sg_socket.hxx.

Referenced by readline().

int SGSocket::save_len
private

Definition at line 51 of file sg_socket.hxx.

Referenced by readline().

simgear::Socket SGSocket::sock
private
int SGSocket::timeout
private

Definition at line 59 of file sg_socket.hxx.

Referenced by poll().


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