mirror of
https://github.com/thestk/stk
synced 2026-01-11 20:11:52 +00:00
Version 4.2.1
This commit is contained in:
committed by
Stephen Sinclair
parent
a6381b9d38
commit
2cbce2d8bd
101
include/Socket.h
101
include/Socket.h
@@ -1,20 +1,13 @@
|
||||
/***************************************************/
|
||||
/*! \class Socket
|
||||
\brief STK TCP socket client/server class.
|
||||
\brief STK internet socket abstract base class.
|
||||
|
||||
This class provides a uniform cross-platform
|
||||
TCP socket client or socket server interface.
|
||||
Methods are provided for reading or writing
|
||||
data buffers to/from connections. This class
|
||||
also provides a number of static functions for
|
||||
use with external socket descriptors.
|
||||
This class provides common functionality for TCP and UDP internet
|
||||
socket server and client subclasses. This class also provides a
|
||||
number of static functions for use with external socket
|
||||
descriptors.
|
||||
|
||||
The user is responsible for checking the values
|
||||
returned by the read/write methods. Values
|
||||
less than or equal to zero indicate a closed
|
||||
or lost connection or the occurence of an error.
|
||||
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2004.
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
|
||||
*/
|
||||
/***************************************************/
|
||||
|
||||
@@ -23,71 +16,62 @@
|
||||
|
||||
#include "Stk.h"
|
||||
|
||||
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
|
||||
#elif defined(__OS_WINDOWS__)
|
||||
|
||||
#include <winsock.h>
|
||||
|
||||
#endif
|
||||
|
||||
class Socket : public Stk
|
||||
{
|
||||
public:
|
||||
//! Default constructor which creates a local socket server on port 2006 (or the specified port number).
|
||||
/*!
|
||||
An StkError will be thrown if a socket error occurs during instantiation.
|
||||
*/
|
||||
Socket( int port = 2006 );
|
||||
|
||||
//! Class constructor which creates a socket client connection to the specified host and port.
|
||||
/*!
|
||||
An StkError will be thrown if a socket error occurs during instantiation.
|
||||
*/
|
||||
Socket( int port, const char *hostname );
|
||||
enum ProtocolType {
|
||||
PROTO_TCP,
|
||||
PROTO_UDP
|
||||
};
|
||||
|
||||
//! The class destructor closes the socket instance, breaking any existing connections.
|
||||
~Socket();
|
||||
//! Class constructor
|
||||
Socket();
|
||||
|
||||
//! Connect a socket client to the specified host and port and returns the resulting socket descriptor.
|
||||
/*!
|
||||
This method is valid for socket clients only. If it is called for
|
||||
a socket server, -1 is returned. If the socket client is already
|
||||
connected, that connection is terminated and a new connection is
|
||||
attempted. Server connections are made using the accept() method.
|
||||
An StkError will be thrown if a socket error occurs during
|
||||
instantiation. \sa accept
|
||||
*/
|
||||
int connect( int port, const char *hostname = "localhost" );
|
||||
//! Class destructor.
|
||||
virtual ~Socket();
|
||||
|
||||
//! Close this socket.
|
||||
void close( void );
|
||||
//! Close the socket.
|
||||
static void close( int socket );
|
||||
|
||||
//! Return the server/client socket descriptor.
|
||||
//! Return the socket descriptor.
|
||||
int id( void ) const;
|
||||
|
||||
//! Return the server/client port number.
|
||||
//! Return the socket port number.
|
||||
int port( void ) const;
|
||||
|
||||
//! If this is a socket server, extract the first pending connection request from the queue and create a new connection, returning the descriptor for the accepted socket.
|
||||
/*!
|
||||
If no connection requests are pending and the socket has not
|
||||
been set non-blocking, this function will block until a connection
|
||||
is present. If an error occurs or this is a socket client, -1 is
|
||||
returned.
|
||||
*/
|
||||
int accept( void );
|
||||
|
||||
//! If enable = false, the socket is set to non-blocking mode. When first created, sockets are by default in blocking mode.
|
||||
static void setBlocking( int socket, bool enable );
|
||||
|
||||
//! Close the socket with the given descriptor.
|
||||
static void close( int socket );
|
||||
|
||||
//! Returns true if the socket descriptor is valid.
|
||||
static bool isValid( int socket );
|
||||
|
||||
//! If enable = false, the socket is set to non-blocking mode. When first created, sockets are by default in blocking mode.
|
||||
static void setBlocking( int socket, bool enable );
|
||||
|
||||
//! Write a buffer over the socket connection. Returns the number of bytes written or -1 if an error occurs.
|
||||
int writeBuffer(const void *buffer, long bufferSize, int flags = 0);
|
||||
virtual int writeBuffer(const void *buffer, long bufferSize, int flags = 0) = 0;
|
||||
|
||||
//! Read an input buffer, up to length \e bufferSize. Returns the number of bytes read or -1 if an error occurs.
|
||||
virtual int readBuffer(void *buffer, long bufferSize, int flags = 0) = 0;
|
||||
|
||||
//! Write a buffer via the specified socket. Returns the number of bytes written or -1 if an error occurs.
|
||||
static int writeBuffer(int socket, const void *buffer, long bufferSize, int flags );
|
||||
|
||||
//! Read a buffer from the socket connection, up to length \e bufferSize. Returns the number of bytes read or -1 if an error occurs.
|
||||
int readBuffer(void *buffer, long bufferSize, int flags = 0);
|
||||
|
||||
//! Read a buffer via the specified socket. Returns the number of bytes read or -1 if an error occurs.
|
||||
static int readBuffer(int socket, void *buffer, long bufferSize, int flags );
|
||||
|
||||
@@ -95,7 +79,6 @@ class Socket : public Stk
|
||||
|
||||
int soket_;
|
||||
int port_;
|
||||
bool server_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user