Gnash  0.8.11dev
Public Member Functions | List of all members
gnash::Socket Class Reference

A simple IOChannel subclass for reading and writing sockets. More...

#include <Socket.h>

Inheritance diagram for gnash::Socket:
gnash::IOChannel

Public Member Functions

 Socket ()
 Create a non-connected socket. More...
 
virtual ~Socket ()
 
bool connect (const std::string &hostname, std::uint16_t port)
 Initiate a connection. More...
 
void close ()
 Close the Socket. More...
 
bool connected () const
 Whether a connection attempt is complete. More...
 
virtual bool bad () const
 True if the Socket is in an error condition. More...
 
virtual std::streamsize read (void *dst, std::streamsize num)
 Read exactly the given number of bytes from the Socket or none at all. More...
 
virtual std::streamsize readNonBlocking (void *dst, std::streamsize num)
 Read up to the given number of bytes from the Socket. More...
 
virtual std::streamsize write (const void *src, std::streamsize num)
 Write the given number of bytes to the stream. More...
 
virtual std::streampos tell () const
 Return current stream position. More...
 
virtual bool seek (std::streampos p)
 Seek to the specified position. More...
 
virtual void go_to_end ()
 Seek to the end of the stream. More...
 
virtual bool eof () const
 Return true if the end of the stream has been reached. More...
 
- Public Member Functions inherited from gnash::IOChannel
virtual ~IOChannel ()
 
std::uint32_t read_le32 ()
 Read a 32-bit word from a little-endian stream. returning it as a native-endian word. More...
 
std::uint16_t read_le16 ()
 Read a 16-bit word from a little-endian stream. More...
 
std::uint8_t read_byte ()
 Read a single byte from the stream. More...
 
int read_string (char *dst, int max_length)
 Read up to max_length characters, returns the number of characters read, or -1 if the string length is longer than max_length. More...
 
virtual size_t size () const
 Get the size of the stream (unreliably). More...
 

Detailed Description

A simple IOChannel subclass for reading and writing sockets.

The Socket class will give you years of satisfaction provided you observe the following points:

  1. A socket is active only when both connected() and not bad().
  2. The only accurate way of determining an error is to check bad().
  3. read() and write() should not be called until connected() is true.

Constructor & Destructor Documentation

§ Socket()

gnash::Socket::Socket ( )

Create a non-connected socket.

§ ~Socket()

virtual gnash::Socket::~Socket ( )
inlinevirtual

Member Function Documentation

§ bad()

virtual bool gnash::Socket::bad ( ) const
inlinevirtual

True if the Socket is in an error condition.

An error condition is fatal and can only be reset when the Socket is closed. Any read or write failure other than EAGAIN or EWOULDBLOCK causes a fatal error.

Implements gnash::IOChannel.

Referenced by eof(), readNonBlocking(), gnash::rtmp::RTMP::readSocket(), and write().

§ close()

void gnash::Socket::close ( )

Close the Socket.

A closed Socket is in a state where another connection attempt can be made. Any errors are reset.

Referenced by gnash::rtmp::RTMP::close().

§ connect()

bool gnash::Socket::connect ( const std::string &  hostname,
std::uint16_t  port 
)

Initiate a connection.

A return of true does not mean the connection has succeeded, only that it did not fail. The connection attempt is only complete when connected() returns true.

Returns
false if the connection fails. In this case, the Socket is still in a closed state and is ready for a new connection attempt. Otherwise true.

References _, gnash::arraySize(), gnash::LogFile::getDefaultInstance(), gnash::log_debug(), and gnash::log_error().

Referenced by gnash::rtmp::RTMP::connect().

§ connected()

bool gnash::Socket::connected ( ) const

Whether a connection attempt is complete.

This is true as soon as the socket is ready for reading and writing. But beware! This function may still return true if the Socket is in error condition. Always check bad() if you care about this.

References gnash::log_debug().

Referenced by gnash::rtmp::RTMP::close(), and gnash::rtmp::RTMP::readSocket().

§ eof()

bool gnash::Socket::eof ( ) const
virtual

Return true if the end of the stream has been reached.

EOF is when remote end closed the connection and we have no more cached data to read

Implements gnash::IOChannel.

References bad().

Referenced by gnash::rtmp::RTMP::readSocket().

§ go_to_end()

void gnash::Socket::go_to_end ( )
virtual

Seek to the end of the stream.

Not Socket.

Implements gnash::IOChannel.

References _, and gnash::log_error().

§ read()

std::streamsize gnash::Socket::read ( void *  dst,
std::streamsize  num 
)
virtual

Read exactly the given number of bytes from the Socket or none at all.

Implements gnash::IOChannel.

Referenced by gnash::rtmp::RTMP::close(), and gnash::rtmp::RTMP::readSocket().

§ readNonBlocking()

std::streamsize gnash::Socket::readNonBlocking ( void *  dst,
std::streamsize  num 
)
virtual

Read up to the given number of bytes from the Socket.

Reimplemented from gnash::IOChannel.

References bad().

§ seek()

bool gnash::Socket::seek ( std::streampos  p)
virtual

Seek to the specified position.

Meaningless for a Socket.

Implements gnash::IOChannel.

References _, and gnash::log_error().

§ tell()

std::streampos gnash::Socket::tell ( ) const
virtual

Return current stream position.

Meaningless for a Socket.

Implements gnash::IOChannel.

References _, and gnash::log_error().

§ write()

std::streamsize gnash::Socket::write ( const void *  src,
std::streamsize  num 
)
virtual

Write the given number of bytes to the stream.

If you call write() before connected() is true, it may put the Socket into an error condition. Calling write() when the Socket is bad has no effect.

Reimplemented from gnash::IOChannel.

References _, bad(), and gnash::log_error().

Referenced by gnash::rtmp::RTMP::close(), and gnash::rtmp::RTMP::sendPacket().


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