Lesson 35 - Get Compute Auth Token Working

This commit is contained in:
Norman Lansing
2026-02-28 12:32:28 -05:00
parent 1d477ee42a
commit 4fde462bce
7743 changed files with 1397833 additions and 18 deletions

View File

@@ -0,0 +1,6 @@
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SOCKETS_KERNEl_1_
#include "sockets_kernel_2.h"
#endif

View File

@@ -0,0 +1,151 @@
// Copyright (C) 2006 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SOCKETS_EXTENSIONs_
#define DLIB_SOCKETS_EXTENSIONs_
#include <iosfwd>
#include <memory>
#include <string>
#include "../sockets.h"
#include "../smart_pointers/scoped_ptr.h"
#include "sockets_extensions_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
class invalid_network_address : public dlib::error
{
public:
invalid_network_address(const std::string& msg) : dlib::error(msg) {};
};
// ----------------------------------------------------------------------------------------
struct network_address
{
network_address() : port(0){}
network_address(
const std::string& full_address
);
network_address (
const char* full_address
)
{
*this = network_address(std::string(full_address));
}
network_address(
const std::string& host_address_,
const unsigned short port_
) : host_address(host_address_), port(port_) {}
std::string host_address;
unsigned short port;
};
// ----------------------------------------------------------------------------------------
inline bool operator < (
const network_address& a,
const network_address& b
)
{
if (a.host_address < b.host_address)
return true;
else if (a.host_address > b.host_address)
return false;
else if (a.port < b.port)
return true;
else
return false;
}
inline bool operator== (
const network_address& a,
const network_address& b
) { return a.host_address == b.host_address && a.port == b.port; }
inline bool operator != (
const network_address& a,
const network_address& b
) { return !(a == b); }
// ----------------------------------------------------------------------------------------
void serialize(
const network_address& item,
std::ostream& out
);
void deserialize(
network_address& item,
std::istream& in
);
std::ostream& operator<< (
std::ostream& out,
const network_address& item
);
std::istream& operator>> (
std::istream& in,
network_address& item
);
// ----------------------------------------------------------------------------------------
connection* connect (
const std::string& host_or_ip,
unsigned short port
);
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr
);
// ----------------------------------------------------------------------------------------
connection* connect (
const std::string& host_or_ip,
unsigned short port,
unsigned long timeout
);
// ----------------------------------------------------------------------------------------
bool is_ip_address (
std::string ip
);
// ----------------------------------------------------------------------------------------
void close_gracefully (
connection* con,
unsigned long timeout = 500
);
// ----------------------------------------------------------------------------------------
void close_gracefully (
std::unique_ptr<connection>& con,
unsigned long timeout = 500
);
// ----------------------------------------------------------------------------------------
}
#ifdef NO_MAKEFILE
#include "sockets_extensions.cpp"
#endif
#endif // DLIB_SOCKETS_EXTENSIONs_

View File

@@ -0,0 +1,300 @@
// Copyright (C) 2006 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_SOCKETS_EXTENSIONs_ABSTRACT_
#ifdef DLIB_SOCKETS_EXTENSIONs_ABSTRACT_
#include <memory>
#include <string>
#include "sockets_kernel_abstract.h"
#include "../error.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
class invalid_network_address : public dlib::error
{
/*!
WHAT THIS OBJECT REPRESENTS
This is the exception thrown by network_address's constructor if the
input is invalid.
!*/
};
// ----------------------------------------------------------------------------------------
struct network_address
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is simply a container for two things:
- A host machine address which is either an IP address or DNS name
for a machine.
- A port number.
Together, these things define a machine and port on that machine.
!*/
network_address(
);
/*!
ensures
- host_address == ""
- #port == 0
!*/
network_address(
const std::string& full_address
);
/*!
ensures
- interprets full_address as a network address of the form:
host_address:port
and assigns each part into #host_address and #port. For example,
network_address("localhost:80") would result in a network_address
object where host_address was "localhost" and port was 80.
throws
- invalid_network_address
This exception is thrown if the full_address string can't be
interpreted as a valid network address.
!*/
network_address (
const char* full_address
);
/*!
requires
- full_address == a valid pointer to a null terminated string
ensures
- Invoking this constructor is equivalent to performing
network_address(std::string(full_address))
!*/
network_address(
const std::string& host_address_,
const unsigned short port_
);
/*!
ensures
- #host_address == host_address_
- #port == port_
!*/
std::string host_address;
unsigned short port;
};
// ----------------------------------------------------------------------------------------
inline bool operator < (
const network_address& a,
const network_address& b
);
/*!
ensures
- provides a total ordering over network_address objects so you can use them in
the standard associative containers. The ordering is defined such that if
you sorted network addresses they would sort first on the host_address string
and then, for network_address objects with equal host_address, they would
sort on the port number
!*/
inline bool operator== (
const network_address& a,
const network_address& b
);
/*!
ensures
- returns true if a and b contain exactly the same address and false otherwise.
That is, the following must be true for this function to return true:
- a.host_address == b.host_address
- a.port == b.port
Note that this means that two addresses which are logically equivalent but
written differently will not compare equal. For example, suppose example.com
has the IP address 10.1.1.1. Then network_address("10.1.1.1:80") and
network_address("example.com:80") really refer to the same network resource
but will nevertheless not compare equal since.
!*/
inline bool operator != (
const network_address& a,
const network_address& b
);
/*!
ensures
- returns !(a == b)
!*/
// ----------------------------------------------------------------------------------------
void serialize(
const network_address& item,
std::ostream& out
);
/*!
ensures
- provides serialization support
!*/
void deserialize(
network_address& item,
std::istream& in
);
/*!
ensures
- provides deserialization support
!*/
std::ostream& operator<< (
std::ostream& out,
const network_address& item
);
/*!
ensures
- writes the given network_address to the output stream. The format is the
host_address, then a colon, then the port number. So for example:
cout << network_address("localhost", 80);
would print:
localhost:80
- returns #out
!*/
std::istream& operator>> (
std::istream& in,
network_address& item
);
/*!
ensures
- reads a network_address from the given input stream. The expected format is
the same as the one used to print them by the above operator<<() routine.
- returns #in
- if (there is an error reading the network_address) then
- #in.good() == false
!*/
// ----------------------------------------------------------------------------------------
connection* connect (
const std::string& host_or_ip,
unsigned short port
);
/*!
ensures
- returns a connection object that is connected to the given host at the
given port
throws
- dlib::socket_error
This exception is thrown if there is some problem that prevents us from
creating the connection
- std::bad_alloc
!*/
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr
);
/*!
ensures
- returns connect(addr.host_address, addr_port);
!*/
// ----------------------------------------------------------------------------------------
connection* connect (
const std::string& host_or_ip,
unsigned short port,
unsigned long timeout
);
/*!
ensures
- returns a connection object that is connected to the given host at the
given port.
- blocks for at most timeout milliseconds
throws
- dlib::socket_error
This exception is thrown if there is some problem that prevents us from
creating the connection or if timeout milliseconds elapses before the
connect is successful.
- std::bad_alloc
!*/
// ----------------------------------------------------------------------------------------
bool is_ip_address (
std::string ip
);
/*!
ensures
- if (ip is a valid ip address) then
- returns true
- else
- returns false
!*/
// ----------------------------------------------------------------------------------------
void close_gracefully (
connection* con,
unsigned long timeout = 500
);
/*!
requires
- con == a valid pointer to a connection object or 0
ensures
- This function does nothing if con == 0, otherwise it performs the following:
- performs a graceful close of the given connection and if it takes longer
than timeout milliseconds to complete then forces the connection closed.
- Specifically, a graceful close means that the outgoing part of con is
closed (a FIN is sent) and then we wait for the other end to to close
their end of the connection. This way any data still on its way to
the other end of the connection will be received properly.
- This function will block until the graceful close is completed or we
timeout.
- calls "delete con;". Thus con is no longer a valid pointer after this
function has finished.
throws
- std::bad_alloc or dlib::thread_error
If either of these exceptions are thrown con will still be closed via
"delete con;"
!*/
// ----------------------------------------------------------------------------------------
void close_gracefully (
std::unique_ptr<connection>& con,
unsigned long timeout = 500
);
/*!
requires
- con == a valid pointer to a connection object or con.get() == 0
ensures
- This function does nothing if con.get() == 0, otherwise it performs the
following:
- performs a graceful close of the given connection and if it takes longer
than timeout milliseconds to complete then forces the connection closed.
- Specifically, a graceful close means that the outgoing part of con is
closed (a FIN is sent) and then we wait for the other end to to close
their end of the connection. This way any data still on its way to
the other end of the connection will be received properly.
- This function will block until the graceful close is completed or we
timeout.
- #con.get() == 0. Thus con is no longer a valid pointer after this
function has finished.
throws
- std::bad_alloc or dlib::thread_error
If either of these exceptions are thrown con will still be closed and
deleted (i.e. #con.get() == 0).
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_SOCKETS_EXTENSIONs_ABSTRACT_

View File

@@ -0,0 +1,351 @@
// Copyright (C) 2003 Davis E. King (davis@dlib.net), Miguel Grinberg
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SOCKETS_KERNEl_1_
#define DLIB_SOCKETS_KERNEl_1_
#ifdef DLIB_ISO_CPP_ONLY
#error "DLIB_ISO_CPP_ONLY is defined so you can't use this OS dependent code. Turn DLIB_ISO_CPP_ONLY off if you want to use it."
#endif
#include "sockets_kernel_abstract.h"
#include <memory>
#include <string>
#include "../algs.h"
#include "../threads.h"
#include "../uintn.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
// forward declarations
class socket_factory;
class listener;
class SOCKET_container;
// ----------------------------------------------------------------------------------------
// lookup functions
int
get_local_hostname (
std::string& hostname
);
// -----------------
int
hostname_to_ip (
const std::string& hostname,
std::string& ip,
int n = 0
);
// -----------------
int
ip_to_hostname (
const std::string& ip,
std::string& hostname
);
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// connection object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class connection
{
/*!
INITIAL_VALUE
- sd == false
- sdo == false
- sdr == 0
CONVENTION
- connection_socket == the socket handle for this connection.
- connection_foreign_port == the port that foreign host is using for
this connection.
- connection_foreign_ip == a string containing the IP address of the
foreign host.
- connection_local_port == the port that the local host is using for
this connection.
- connection_local_ip == a string containing the IP address of the
local interface being used by this connection.
- sd == if shutdown() has been called then true else false.
- sdo == if shutdown_outgoing() has been called then true else false.
- sdr == the return value of shutdown() if it has been called. if it
hasn't been called then 0.
!*/
friend class listener; // make listener a friend of connection
// make create_connection a friend of connection
friend int create_connection (
connection*& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port,
const std::string& local_ip
);
public:
~connection (
);
void* user_data;
long write (
const char* buf,
long num
);
long read (
char* buf,
long num
);
long read (
char* buf,
long num,
unsigned long timeout
);
unsigned short get_local_port (
) const { return connection_local_port; }
unsigned short get_foreign_port (
) const { return connection_foreign_port; }
const std::string& get_local_ip (
) const { return connection_local_ip; }
const std::string& get_foreign_ip (
) const { return connection_foreign_ip; }
int shutdown_outgoing (
);
int shutdown (
);
// I would use SOCKET here but I don't want to include the windows
// header files since they bring in a bunch of unpleasantness. So
// I'm doing this instead which should ultimately be the same type
// as the SOCKET win the windows API.
typedef unsigned_type<void*>::type socket_descriptor_type;
int disable_nagle(
);
socket_descriptor_type get_socket_descriptor (
) const;
private:
bool readable (
unsigned long timeout
) const;
/*!
requires
- timeout < 2000000
ensures
- returns true if a read call on this connection will not block.
- returns false if a read call on this connection will block or if
there was an error.
!*/
bool sd_called (
)const
/*!
ensures
- returns true if shutdown() has been called else
returns false
!*/
{
sd_mutex.lock();
bool temp = sd;
sd_mutex.unlock();
return temp;
}
bool sdo_called (
)const
/*!
ensures
- returns true if shutdown_outgoing() or shutdown() has been called
else returns false
!*/
{
sd_mutex.lock();
bool temp = false;
if (sdo || sd)
temp = true;
sd_mutex.unlock();
return temp;
}
// data members
SOCKET_container& connection_socket;
const unsigned short connection_foreign_port;
const std::string connection_foreign_ip;
const unsigned short connection_local_port;
const std::string connection_local_ip;
bool sd; // called shutdown
bool sdo; // called shutdown_outgoing
int sdr; // return value for shutdown
mutex sd_mutex; // a lock for the three above vars
connection(
SOCKET_container sock,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port,
const std::string& local_ip
);
/*!
requires
sock is a socket handle and
sock is the handle for the connection between foreign_ip:foreign_port
and local_ip:local_port
ensures
*this is initialized correctly with the above parameters
!*/
// restricted functions
connection(connection&); // copy constructor
connection& operator=(connection&); // assignment operator
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// listener object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class listener
{
/*!
CONVENTION
if (inaddr_any == false)
{
listening_ip == a string containing the address the listener is
listening on
}
else
{
the listener is listening on all interfaces
}
listening_port == the port the listener is listening on
listening_socket == the listening socket handle for this object
!*/
// make the create_listener a friend of listener
friend int create_listener (
listener*& new_listener,
unsigned short port,
const std::string& ip
);
public:
~listener (
);
int accept (
connection*& new_connection,
unsigned long timeout = 0
);
int accept (
std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0
);
unsigned short get_listening_port (
) { return listening_port; }
const std::string& get_listening_ip (
) { return listening_ip; }
private:
// data members
SOCKET_container& listening_socket;
const unsigned short listening_port;
const std::string listening_ip;
const bool inaddr_any;
listener(
SOCKET_container sock,
unsigned short port,
const std::string& ip
);
/*!
requires
sock is a socket handle and
sock is listening on the port and ip(may be "") indicated in the
above parameters
ensures
*this is initialized correctly with the above parameters
!*/
// restricted functions
listener(listener&); // copy constructor
listener& operator=(listener&); // assignment operator
};
// ----------------------------------------------------------------------------------------
int create_listener (
listener*& new_listener,
unsigned short port,
const std::string& ip = ""
);
int create_connection (
connection*& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,
const std::string& local_ip = ""
);
int create_listener (
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip = ""
);
int create_connection (
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,
const std::string& local_ip = ""
);
// ----------------------------------------------------------------------------------------
}
#ifdef NO_MAKEFILE
#include "sockets_kernel_1.cpp"
#endif
#endif // DLIB_SOCKETS_KERNEl_1_

View File

@@ -0,0 +1,396 @@
// Copyright (C) 2003 Davis E. King (davis@dlib.net), Miguel Grinberg
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SOCKETS_KERNEl_2_
#define DLIB_SOCKETS_KERNEl_2_
#ifdef DLIB_ISO_CPP_ONLY
#error "DLIB_ISO_CPP_ONLY is defined so you can't use this OS dependent code. Turn DLIB_ISO_CPP_ONLY off if you want to use it."
#endif
#include "../platform.h"
#include "sockets_kernel_abstract.h"
#define _BSD_SOCKLEN_T_
#include <ctime>
#include <memory>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#ifndef HPUX
#include <sys/select.h>
#endif
#include <arpa/inet.h>
#include <signal.h>
#include <inttypes.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/param.h>
#include <netinet/in.h>
#include "../threads.h"
#include "../algs.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
// forward declarations
class socket_factory;
class listener;
// ----------------------------------------------------------------------------------------
// lookup functions
int
get_local_hostname (
std::string& hostname
);
// -----------------
int
hostname_to_ip (
const std::string& hostname,
std::string& ip,
int n = 0
);
// -----------------
int
ip_to_hostname (
const std::string& ip,
std::string& hostname
);
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// connection object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class connection
{
/*!
INITIAL_VALUE
sd == false
sdo == false
sdr == 0
CONVENTION
connection_socket == the socket handle for this connection.
connection_foreign_port == the port that foreign host is using for
this connection
connection_foreign_ip == a string containing the IP address of the
foreign host
connection_local_port == the port that the local host is using for
this connection
connection_local_ip == a string containing the IP address of the
local interface being used by this connection
sd == if shutdown() has been called then true
else false
sdo == if shutdown_outgoing() has been called then true
else false
sdr == the return value of shutdown() if it has been
called. if it hasn't been called then 0
!*/
friend class listener; // make listener a friend of connection
// make create_connection a friend of connection
friend int create_connection (
connection*& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port,
const std::string& local_ip
);
public:
~connection();
void* user_data;
long write (
const char* buf,
long num
);
long read (
char* buf,
long num
);
long read (
char* buf,
long num,
unsigned long timeout
);
int get_local_port (
) const { return connection_local_port; }
int get_foreign_port (
) const { return connection_foreign_port; }
const std::string& get_local_ip (
) const { return connection_local_ip; }
const std::string& get_foreign_ip (
) const { return connection_foreign_ip; }
int shutdown_outgoing (
)
{
sd_mutex.lock();
if (sdo || sd)
{
sd_mutex.unlock();
return sdr;
}
sdo = true;
sdr = ::shutdown(connection_socket,SHUT_WR);
int temp = sdr;
sd_mutex.unlock();
return temp;
}
int shutdown (
)
{
sd_mutex.lock();
if (sd)
{
sd_mutex.unlock();
return sdr;
}
sd = true;
sdr = ::shutdown(connection_socket,SHUT_RDWR);
int temp = sdr;
sd_mutex.unlock();
return temp;
}
int disable_nagle(
);
typedef int socket_descriptor_type;
socket_descriptor_type get_socket_descriptor (
) const { return connection_socket; }
private:
bool readable (
unsigned long timeout
) const;
/*!
requires
- timeout < 2000000
ensures
- returns true if a read call on this connection will not block.
- returns false if a read call on this connection will block or if
there was an error.
!*/
bool sd_called (
)const
/*!
ensures
- returns true if shutdown() has been called else
- returns false
!*/
{
sd_mutex.lock();
bool temp = sd;
sd_mutex.unlock();
return temp;
}
bool sdo_called (
)const
/*!
ensures
- returns true if shutdown_outgoing() or shutdown() has been called
else returns false
!*/
{
sd_mutex.lock();
bool temp = false;
if (sdo || sd)
temp = true;
sd_mutex.unlock();
return temp;
}
// data members
int connection_socket;
const int connection_foreign_port;
const std::string connection_foreign_ip;
const int connection_local_port;
const std::string connection_local_ip;
bool sd; // called shutdown
bool sdo; // called shutdown_outgoing
int sdr; // return value for shutdown
mutex sd_mutex; // a lock for the three above vars
connection(
int sock,
int foreign_port,
const std::string& foreign_ip,
int local_port,
const std::string& local_ip
);
/*!
requires
- sock is a socket handle
- sock is the handle for the connection between foreign_ip:foreign_port
and local_ip:local_port
ensures
- *this is initialized correctly with the above parameters
!*/
// restricted functions
connection();
connection(connection&); // copy constructor
connection& operator=(connection&); // assignement opertor
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// listener object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class listener
{
/*!
CONVENTION
if (inaddr_any == false)
{
listening_ip == a string containing the address the listener is
listening on
}
else
{
the listener is listening on all interfaces
}
listening_port == the port the listener is listening on
listening_socket == the listening socket handle for this object
!*/
// make the create_listener a friend of listener
friend int create_listener (
listener*& new_listener,
unsigned short port,
const std::string& ip
);
public:
~listener();
int accept (
connection*& new_connection,
unsigned long timeout = 0
);
int accept (
std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0
);
int get_listening_port (
) const { return listening_port; }
const std::string& get_listening_ip (
) const { return listening_ip; }
private:
// data members
int listening_socket;
const int listening_port;
const std::string listening_ip;
const bool inaddr_any;
listener(
int sock,
int port,
const std::string& ip
);
/*!
requires
- sock is a socket handle
- sock is listening on the port and ip(may be "") indicated in the above
parameters
ensures
- *this is initialized correctly with the above parameters
!*/
// restricted functions
listener();
listener(listener&); // copy constructor
listener& operator=(listener&); // assignement opertor
};
// ----------------------------------------------------------------------------------------
int create_listener (
listener*& new_listener,
unsigned short port,
const std::string& ip = ""
);
int create_connection (
connection*& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,
const std::string& local_ip = ""
);
int create_listener (
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip = ""
);
int create_connection (
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,
const std::string& local_ip = ""
);
// ----------------------------------------------------------------------------------------
}
#ifdef NO_MAKEFILE
#include "sockets_kernel_2.cpp"
#endif
#endif // DLIB_SOCKETS_KERNEl_2_

View File

@@ -0,0 +1,495 @@
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_SOCKETS_KERNEl_ABSTRACT_
#ifdef DLIB_SOCKETS_KERNEl_ABSTRACT_
#include <string>
#include "../threads.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
/*!
GENERAL COMMENTS:
Nothing in here will throw exceptions.
All ip address strings in this file refer to IPv4 addresses. For
example "192.168.1.1"
Timeouts:
All timeout values are measured in milliseconds but you are not
guaranteed to have that level of resolution. The actual resolution
is implementation defined.
GENERAL WARNING
Don't call any of these functions or make any of these objects
before main() has been entered.
EXCEPTIONS
Unless specified otherwise, nothing in this file throws exceptions.
!*/
// ----------------------------------------------------------------------------------------
// LOOKUP FUNCTIONS
// all lookup functions are thread-safe
int get_local_hostname (
std::string& hostname
);
/*!
ensures
- if (#get_local_hostname() == 0) then
- #hostname == a string containing the hostname of the local computer
- returns 0 upon success
- returns OTHER_ERROR upon failure and in this case #hostname's value
is undefined
!*/
// -----------------
int hostname_to_ip (
const std::string& hostname,
std::string& ip,
int n = 0
);
/*!
requires
- n >= 0
ensures
- if (#hostname_to_ip() == 0) then
- #ip == string containing the nth ip address associated with the hostname
- returns 0 upon success
- returns OTHER_ERROR upon failure
!*/
// -----------------
int ip_to_hostname (
const std::string& ip,
std::string& hostname
);
/*!
ensures
- if (#ip_to_hostname() == 0) then
- #hostname == string containing the hostname associated with ip
- returns 0 upon success
- returns OTHER_ERROR upon failure
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
//
// socket creation functions
//
// The following functions are guaranteed to be thread-safe
//
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
int create_listener (
listener*& new_listener,
unsigned short port,
const std::string& ip = ""
);
/*!
requires
- 0 <= port <= 65535
ensures
- if (#create_listener() == 0) then
- #new_listener == a pointer to a listener object that is listening on
the specified port and ip for an incoming connection
- if (ip == "") then
- the new listener will be listening on all interfaces
- if (port == 0) then
- the operating system will assign a free port to listen on
- returns 0 if create_listener was successful
- returns PORTINUSE if the specified local port was already in use
- returns OTHER_ERROR if some other error occurred
!*/
int create_listener (
std::unique_ptr<listener>& new_listener,
unsigned short port,
const std::string& ip = ""
);
/*!
This function is just an overload of the above function but it gives you a
std::unique_ptr smart pointer instead of a C pointer.
!*/
int create_connection (
connection*& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,
const std::string& local_ip = ""
);
/*!
requires
- 0 < foreign_port <= 65535
- 0 <= local_port <= 65535
ensures
- if (#create_connection() == 0) then
- #new_connection == a pointer to a connection object that is connected
to foreign_ip on port foreign_port and is using the local interface
local_ip and local port local_port
- #new_connection->user_data == 0
- if (local_ip == "") then
- the operating system will chose this for you
- if (local_port == 0) then
- the operating system will chose this for you
- returns 0 if create_connection was successful
- returns PORTINUSE if the specified local port was already in use
- returns OTHER_ERROR if some other error occurred
!*/
int create_connection (
std::unique_ptr<connection>& new_connection,
unsigned short foreign_port,
const std::string& foreign_ip,
unsigned short local_port = 0,
const std::string& local_ip = ""
);
/*!
This function is just an overload of the above function but it gives you a
std::unique_ptr smart pointer instead of a C pointer.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// connection object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class connection
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a TCP connection.
Instances of this class can only be created by using the
create_connection function or listener class defined below.
NOTE:
A connection object must ALWAYS be closed (delete the pointer to the
connection) or it will cause a resource leak.
Note also that all errors indicated by a return code of OTHER_ERROR
are fatal so if one occurs the connection should just be closed.
CLOSING A CONNECTION
Note that if ~connection() or shutdown() is called before the remote client
has received all sent data it is possible that the data will be lost. To
avoid this you should call the close_gracefully() function to close your
connections (unless you actually do want to immediately dispose of a
connection and don't care about the data).
(example: close_gracefully(con); // close con gracefully but force it closed
// if it takes more than 500 milliseconds.)
THREAD SAFETY
- It is always safe to call shutdown() or shutdown_outgoing().
- you may NOT call any function more than once at a time (except the
shutdown functions).
- do not call read() more than once at a time
- do not call write() more than once at a time
- You can safely call shutdown or shutdown_outgoing in conjunction with
the read/write functions.
This is helpful if you want to unblock another thread that is
blocking on a read/write operation. Shutting down the connection
will cause the read/write functions to return a value of SHUTDOWN.
OUT-OF-BAND DATA:
All out-of-band data will be put inline into the normal data stream.
This means that you can read any out-of-band data via calls to read().
(i.e. the SO_OOBINLINE socket option will be set)
!*/
public:
~connection (
);
/*!
requires
- no other threads are using this connection object
ensures
- closes the connection (this is an abrupt non-graceful close)
- frees the resources used by this object
!*/
void* user_data;
/*!
This pointer is provided so that the client programmer may easily associate
some data with a connection object. You can really do whatever you want
with it. Initially user_data is 0.
!*/
long write (
const char* buf,
long num
);
/*!
requires
- num > 0
- buf points to an array of at least num bytes
ensures
- will block until ONE of the following occurs:
- num bytes from buf have been written to the connection
- an error has occurred
- the outgoing channel of the connection has been shutdown locally
- returns num if write succeeded
- returns OTHER_ERROR if there was an error (this could be due to a
connection close)
- returns SHUTDOWN if the outgoing channel of the connection has been
shutdown locally
!*/
long read (
char* buf,
long num
);
/*!
requires
- num > 0
- buf points to an array of at least num bytes
ensures
- read() will not read more than num bytes of data into #buf
- read blocks until ONE of the following happens:
- there is some data available and it has been written into #buf
- the remote end of the connection is closed
- an error has occurred
- the connection has been shutdown locally
- returns the number of bytes read into #buf if there was any data.
- returns 0 if the connection has ended/terminated and there is no more data.
- returns OTHER_ERROR if there was an error.
- returns SHUTDOWN if the connection has been shutdown locally
!*/
long read (
char* buf,
long num,
unsigned long timeout
);
/*!
requires
- num > 0
- buf points to an array of at least num bytes
- timeout < 2000000
ensures
- read() will not read more than num bytes of data into #buf
- if (timeout > 0) then read() blocks until ONE of the following happens:
- there is some data available and it has been written into #buf
- the remote end of the connection is closed
- an error has occurred
- the connection has been shutdown locally
- timeout milliseconds has elapsed
- else
- read() does not block
- returns the number of bytes read into #buf if there was any data.
- returns 0 if the connection has ended/terminated and there is no more data.
- returns TIMEOUT if timeout milliseconds elapsed before we got any data.
- returns OTHER_ERROR if there was an error.
- returns SHUTDOWN if the connection has been shutdown locally
!*/
unsigned short get_local_port (
) const;
/*!
ensures
- returns the local port number for this connection
!*/
unsigned short get_foreign_port (
) const;
/*!
ensures
- returns the foreign port number for this connection
!*/
const std::string& get_local_ip (
) const;
/*!
ensures
- returns the IP of the local interface this connection is using
!*/
const std::string& get_foreign_ip (
) const;
/*!
ensures
- returns the IP of the foreign host for this connection
!*/
int shutdown (
);
/*!
ensures
- if (#shutdown() == 0 && connection was still open) then
- terminates the connection but does not free the resources for the
connection object
- any read() or write() calls on this connection will return immediately
with the code SHUTDOWN.
- returns 0 upon success
- returns OTHER_ERROR if there was an error
!*/
int shutdown_outgoing (
);
/*!
ensures
- if (#shutdown_outgoing() == 0 && outgoing channel was still open) then
- sends a FIN to indicate that no more data will be sent on this
connection but leaves the receive half of the connection open to
receive more data from the other host
- any calls to write() will return immediately with the code SHUTDOWN.
- returns 0 upon success
- returns OTHER_ERROR if there was an error
!*/
int disable_nagle(
);
/*!
ensures
- Sets the TCP_NODELAY socket option to disable Nagle's algorithm.
This can sometimes reduce transmission latency, however, in almost
all normal cases you don't want to mess with this as the default
setting is usually appropriate.
- returns 0 upon success
- returns OTHER_ERROR if there was an error
!*/
typedef platform_specific_type socket_descriptor_type;
socket_descriptor_type get_socket_descriptor (
) const;
/*!
ensures
- returns the underlying socket descriptor for this connection
object. The reason you might want access to this is to
pass it to some other library that requires a socket file
descriptor. However, if you do this then you probably shouldn't
use the dlib::connection read() and write() anymore since
whatever you are doing with the socket descriptor is probably
doing I/O with the socket.
!*/
private:
// restricted functions
connection();
connection(connection&); // copy constructor
connection& operator=(connection&); // assignment operator
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// listener object
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class listener
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a TCP socket waiting for incoming connections.
Calling accept returns a pointer to any new incoming connections on its
port.
Instances of this class can only be created by using the
create_listener function defined below.
NOTE:
A listener object must ALWAYS be closed (delete the pointer to it) or
it will cause a resource leak.
Note also that all errors indicated by a return code of OTHER_ERROR
are fatal so if one occurs the listener should be closed.
THREAD SAFETY
None of the functions in this object are guaranteed to be thread-safe.
This means that you must serialize all access to this object.
!*/
public:
~listener (
);
/*!
requires
- no other threads are using this listener object
ensures
- closes the listener
- frees the resources used by this object
!*/
int accept (
connection*& new_connection,
unsigned long timeout = 0
);
/*!
requires
- timeout < 2000000
ensures
- blocks until a new connection is ready or timeout milliseconds have
elapsed.
- #new_connection == a pointer to the new connection object
- #new_connection->user_data == 0
- if (timeout == 0) then
- the timeout argument is ignored
- returns 0 if accept() was successful
- returns TIMEOUT if timeout milliseconds have elapsed
- returns OTHER_ERROR if an error has occurred
!*/
int accept (
std::unique_ptr<connection>& new_connection,
unsigned long timeout = 0
);
/*!
This function is just an overload of the above function but it gives you a
std::unique_ptr smart pointer instead of a C pointer.
!*/
unsigned short get_listening_port (
) const;
/*!
ensures
- returns the port number that this object is listening on
!*/
const std::string& get_listening_ip (
) const;
/*!
ensures
- returns a string containing the IP (e.g. "127.0.0.1") of the
interface this object is listening on
- returns "" if it is accepting connections on all interfaces
!*/
private:
// restricted functions
listener();
listener(listener&); // copy constructor
listener& operator=(listener&); // assignment operator
};
}
#endif // DLIB_SOCKETS_KERNEl_ABSTRACT_

View File

@@ -0,0 +1,6 @@
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SOCKETS_KERNEl_2_
#include "sockets_kernel_1.h"
#endif