2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2004-01-01 22:25:59 +01:00
|
|
|
#ifndef TORRENT_SOCKET_HPP_INCLUDED
|
|
|
|
#define TORRENT_SOCKET_HPP_INCLUDED
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/noncopyable.hpp>
|
2004-02-26 01:27:06 +01:00
|
|
|
#include <boost/cstdint.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <exception>
|
|
|
|
#include <string>
|
|
|
|
|
2004-01-31 11:20:19 +01:00
|
|
|
// TODO: support ToS
|
2004-01-26 18:39:44 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
|
|
|
class network_error : public std::exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
network_error(int error_code): m_error_code(error_code) {}
|
|
|
|
virtual const char* what() const throw();
|
2004-02-26 01:27:06 +01:00
|
|
|
int error_code() const
|
|
|
|
{ return m_error_code; }
|
2003-10-23 01:00:57 +02:00
|
|
|
private:
|
|
|
|
int m_error_code;
|
|
|
|
};
|
|
|
|
|
|
|
|
class socket;
|
|
|
|
|
|
|
|
class address
|
|
|
|
{
|
|
|
|
friend class socket;
|
|
|
|
public:
|
|
|
|
address();
|
2004-01-01 22:25:59 +01:00
|
|
|
address(
|
|
|
|
unsigned char a
|
|
|
|
, unsigned char b
|
|
|
|
, unsigned char c
|
|
|
|
, unsigned char d
|
|
|
|
, unsigned short port);
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
address(unsigned int addr, unsigned short port);
|
2004-01-01 22:25:59 +01:00
|
|
|
|
2004-02-26 01:27:06 +01:00
|
|
|
address(const char* addr, unsigned short port);
|
2003-10-23 01:00:57 +02:00
|
|
|
address(const address& a);
|
|
|
|
~address();
|
|
|
|
|
2003-11-23 04:00:45 +01:00
|
|
|
std::string as_string() const;
|
2004-01-01 22:25:59 +01:00
|
|
|
unsigned int ip() const { return m_ip; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-01 22:25:59 +01:00
|
|
|
bool operator<(const address& a) const
|
2004-02-26 01:27:06 +01:00
|
|
|
{ if (ip() == a.ip()) return port < a.port; else return ip() < a.ip(); }
|
2004-01-01 22:25:59 +01:00
|
|
|
bool operator!=(const address& a) const
|
2004-02-26 01:27:06 +01:00
|
|
|
{ return ip() != a.ip() || port != a.port; }
|
2004-01-01 22:25:59 +01:00
|
|
|
bool operator==(const address& a) const
|
2004-02-26 01:27:06 +01:00
|
|
|
{ return ip() == a.ip() && port == a.port; }
|
|
|
|
|
|
|
|
unsigned short port;
|
|
|
|
|
2004-02-26 13:59:01 +01:00
|
|
|
BOOST_STATIC_CONSTANT(unsigned short, any_port = 0);
|
|
|
|
BOOST_STATIC_CONSTANT(unsigned int, any_addr = 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2004-01-01 22:25:59 +01:00
|
|
|
unsigned int m_ip;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class socket: public boost::noncopyable
|
|
|
|
{
|
|
|
|
friend class address;
|
|
|
|
friend class selector;
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum type
|
|
|
|
{
|
|
|
|
tcp = 0,
|
|
|
|
udp
|
|
|
|
};
|
|
|
|
|
|
|
|
socket(type t, bool blocking = true, unsigned short receive_port = 0);
|
|
|
|
virtual ~socket();
|
|
|
|
|
2004-02-26 01:27:06 +01:00
|
|
|
void connect(
|
|
|
|
const address& addr
|
|
|
|
, address const& bind_to
|
|
|
|
= address(address::any_addr, address::any_port));
|
2003-10-23 01:00:57 +02:00
|
|
|
void close();
|
|
|
|
|
|
|
|
void set_blocking(bool blocking);
|
|
|
|
bool is_blocking() { return m_blocking; }
|
|
|
|
|
|
|
|
const address& sender() const { return m_sender; }
|
|
|
|
address name() const;
|
|
|
|
|
2004-02-26 01:27:06 +01:00
|
|
|
void listen(libtorrent::address const& iface, int queue);
|
2003-10-23 01:00:57 +02:00
|
|
|
boost::shared_ptr<libtorrent::socket> accept();
|
|
|
|
|
|
|
|
template<class T> int send(const T& buffer);
|
|
|
|
template<class T> int send_to(const address& addr, const T& buffer);
|
|
|
|
template<class T> int receive(T& buf);
|
|
|
|
|
|
|
|
int send(const char* buffer, int size);
|
|
|
|
int send_to(const address& addr, const char* buffer, int size);
|
|
|
|
int receive(char* buffer, int size);
|
|
|
|
|
|
|
|
void set_receive_bufsize(int size);
|
|
|
|
void set_send_bufsize(int size);
|
|
|
|
|
|
|
|
bool is_readable() const;
|
|
|
|
bool is_writable() const;
|
2004-10-10 02:42:48 +02:00
|
|
|
bool has_error() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
enum error_code
|
|
|
|
{
|
2003-12-01 06:01:40 +01:00
|
|
|
netdown,
|
|
|
|
fault,
|
|
|
|
access,
|
|
|
|
address_in_use,
|
|
|
|
address_not_available,
|
|
|
|
in_progress,
|
|
|
|
interrupted,
|
|
|
|
invalid,
|
|
|
|
net_reset,
|
|
|
|
not_connected,
|
|
|
|
no_buffers,
|
|
|
|
operation_not_supported,
|
|
|
|
not_socket,
|
|
|
|
shutdown,
|
|
|
|
would_block,
|
|
|
|
connection_reset,
|
|
|
|
timed_out,
|
|
|
|
connection_aborted,
|
|
|
|
message_size,
|
|
|
|
not_ready,
|
|
|
|
no_support,
|
|
|
|
connection_refused,
|
|
|
|
is_connected,
|
|
|
|
net_unreachable,
|
|
|
|
not_initialized,
|
2004-10-10 02:42:48 +02:00
|
|
|
host_not_found,
|
2003-12-01 06:01:40 +01:00
|
|
|
unknown_error
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
error_code last_error() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2004-03-24 23:50:07 +01:00
|
|
|
socket(int sock, const address& sender, bool blocking);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
int m_socket;
|
|
|
|
address m_sender;
|
|
|
|
bool m_blocking;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
bool m_connected; // indicates that this socket has been connected
|
|
|
|
type m_type;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline int socket::send(const T& buf)
|
|
|
|
{
|
|
|
|
return send(reinterpret_cast<const char*>(&buf), sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline int socket::send_to(const address& addr, const T& buf)
|
|
|
|
{
|
|
|
|
return send_to(addr, reinterpret_cast<const unsigned char*>(&buf), sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline int socket::receive(T& buf)
|
|
|
|
{
|
|
|
|
return receive(reinterpret_cast<char*>(&buf), sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// timeout is given in microseconds
|
|
|
|
// modified is cleared and filled with the sockets that is ready for reading or writing
|
|
|
|
// or have had an error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class selector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2004-06-28 22:23:42 +02:00
|
|
|
void monitor_readability(boost::shared_ptr<socket> s)
|
|
|
|
{
|
|
|
|
assert(std::find(m_readable.begin(), m_readable.end(), s) == m_readable.end());
|
|
|
|
m_readable.push_back(s);
|
|
|
|
}
|
|
|
|
void monitor_writability(boost::shared_ptr<socket> s)
|
|
|
|
{
|
|
|
|
assert(std::find(m_writable.begin(), m_writable.end(), s) == m_writable.end());
|
|
|
|
m_writable.push_back(s);
|
|
|
|
}
|
|
|
|
void monitor_errors(boost::shared_ptr<socket> s)
|
|
|
|
{
|
|
|
|
assert(std::find(m_error.begin(), m_error.end(), s) == m_error.end());
|
|
|
|
m_error.push_back(s);
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
void remove(boost::shared_ptr<socket> s);
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
void remove_writable(boost::shared_ptr<socket> s)
|
|
|
|
{ m_writable.erase(std::find(m_writable.begin(), m_writable.end(), s)); }
|
|
|
|
|
2004-03-28 19:45:37 +02:00
|
|
|
void remove_readable(boost::shared_ptr<socket> s)
|
|
|
|
{ m_readable.erase(std::find(m_readable.begin(), m_readable.end(), s)); }
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
bool is_writability_monitored(boost::shared_ptr<socket> s)
|
|
|
|
{
|
|
|
|
return std::find(m_writable.begin(), m_writable.end(), s)
|
|
|
|
!= m_writable.end();
|
|
|
|
}
|
|
|
|
|
2004-03-28 19:45:37 +02:00
|
|
|
bool is_readability_monitored(boost::shared_ptr<socket> s)
|
|
|
|
{
|
|
|
|
return std::find(m_readable.begin(), m_readable.end(), s)
|
|
|
|
!= m_readable.end();
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
void wait(int timeout
|
|
|
|
, std::vector<boost::shared_ptr<socket> >& readable
|
|
|
|
, std::vector<boost::shared_ptr<socket> >& writable
|
|
|
|
, std::vector<boost::shared_ptr<socket> >& error);
|
|
|
|
|
2004-01-02 21:46:24 +01:00
|
|
|
int count_read_monitors() const { return (int)m_readable.size(); }
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
std::vector<boost::shared_ptr<socket> > m_readable;
|
|
|
|
std::vector<boost::shared_ptr<socket> > m_writable;
|
|
|
|
std::vector<boost::shared_ptr<socket> > m_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-01-16 00:57:11 +01:00
|
|
|
#endif // TORRENT_SOCKET_HPP_INCLUDED
|
2004-01-01 22:25:59 +01:00
|
|
|
|