About halfway there in refactoring to support pluggable SocketManager.
This commit is contained in:
parent
6bc9a938cf
commit
128a131070
9 changed files with 466 additions and 630 deletions
|
@ -25,8 +25,8 @@
|
|||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#ifndef ZT_SOCKETMANAGER_HPP
|
||||
#define ZT_SOCKETMANAGER_HPP
|
||||
#ifndef ZT_NATIVESOCKETMANAGER_HPP
|
||||
#define ZT_NATIVESOCKETMANAGER_HPP
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -34,14 +34,11 @@
|
|||
#include <map>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Constants.hpp"
|
||||
|
||||
#include "SharedPtr.hpp"
|
||||
#include "InetAddress.hpp"
|
||||
#include "Socket.hpp"
|
||||
#include "Mutex.hpp"
|
||||
#include "NonCopyable.hpp"
|
||||
#include "Buffer.hpp"
|
||||
#include "../node/Constants.hpp"
|
||||
#include "../node/SharedPtr.hpp"
|
||||
#include "../node/Mutex.hpp"
|
||||
#include "../node/SocketManager.hpp"
|
||||
#include "../node/Socket.hpp"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#include <WinSock2.h>
|
||||
|
@ -56,16 +53,19 @@
|
|||
|
||||
namespace ZeroTier {
|
||||
|
||||
class NativeSocket;
|
||||
class NativeUdpSocket;
|
||||
class NativeTcpSocket;
|
||||
|
||||
/**
|
||||
* Socket I/O multiplexer
|
||||
*
|
||||
* This wraps select(), epoll(), etc. and handles creation of Sockets.
|
||||
*/
|
||||
class SocketManager : NonCopyable
|
||||
class NativeSocketManager : public SocketManager
|
||||
{
|
||||
friend class Socket;
|
||||
friend class UdpSocket;
|
||||
friend class TcpSocket;
|
||||
friend class NativeUdpSocket;
|
||||
friend class NativeTcpSocket;
|
||||
|
||||
public:
|
||||
/**
|
||||
|
@ -75,52 +75,18 @@ public:
|
|||
* @param arg Second argument to packetHandler()
|
||||
* @throws std::runtime_error Could not bind local port(s) or open socket(s)
|
||||
*/
|
||||
SocketManager(
|
||||
NativeSocketManager(
|
||||
int localUdpPort,
|
||||
int localTcpPort,
|
||||
void (*packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),
|
||||
void *arg);
|
||||
|
||||
~SocketManager();
|
||||
virtual ~NativeSocketManager();
|
||||
|
||||
/**
|
||||
* Send a message to a remote peer
|
||||
*
|
||||
* @param to Destination address
|
||||
* @param tcp Use TCP?
|
||||
* @param autoConnectTcp If true, automatically initiate TCP connection if there is none
|
||||
* @param msg Message to send
|
||||
* @param msglen Length of message
|
||||
*/
|
||||
bool send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen);
|
||||
|
||||
/**
|
||||
* Send a message to a remote peer via UDP (shortcut for setting both TCP params to false in send)
|
||||
*
|
||||
* @param to Destination address
|
||||
* @param msg Message to send
|
||||
* @param msglen Length of message
|
||||
*/
|
||||
inline bool sendUdp(const InetAddress &to,const void *msg,unsigned int msglen) { return send(to,false,false,msg,msglen); }
|
||||
|
||||
/**
|
||||
* Perform I/O polling operation (e.g. select())
|
||||
*
|
||||
* If called concurrently, one will block until the other completes.
|
||||
*
|
||||
* @param timeout Timeout in milliseconds, may return sooner if whack() is called
|
||||
*/
|
||||
void poll(unsigned long timeout);
|
||||
|
||||
/**
|
||||
* Cause current or next blocking poll() operation to timeout immediately
|
||||
*/
|
||||
void whack();
|
||||
|
||||
/**
|
||||
* Close TCP sockets
|
||||
*/
|
||||
void closeTcpSockets();
|
||||
virtual bool send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen);
|
||||
virtual void poll(unsigned long timeout);
|
||||
virtual void whack();
|
||||
virtual void closeTcpSockets();
|
||||
|
||||
private:
|
||||
// Called by socket implementations when a packet is received
|
||||
|
@ -133,24 +99,11 @@ private:
|
|||
}
|
||||
|
||||
// Used by TcpSocket to register/unregister for write availability notification
|
||||
inline void startNotifyWrite(const Socket *sock)
|
||||
throw()
|
||||
{
|
||||
_fdSetLock.lock();
|
||||
FD_SET(sock->_sock,&_writefds);
|
||||
_fdSetLock.unlock();
|
||||
}
|
||||
inline void stopNotifyWrite(const Socket *sock)
|
||||
throw()
|
||||
{
|
||||
_fdSetLock.lock();
|
||||
FD_CLR(sock->_sock,&_writefds);
|
||||
_fdSetLock.unlock();
|
||||
}
|
||||
void _startNotifyWrite(const NativeSocket *sock);
|
||||
void _stopNotifyWrite(const NativeSocket *sock);
|
||||
|
||||
// Called in SocketManager destructor or in constructor cleanup before exception throwing
|
||||
void _closeSockets()
|
||||
throw();
|
||||
void _closeSockets();
|
||||
|
||||
// Called in SocketManager to recompute _nfds for select() based implementation
|
||||
void _updateNfds();
|
||||
|
@ -179,9 +132,6 @@ private:
|
|||
std::map< InetAddress,SharedPtr<Socket> > _tcpSockets;
|
||||
Mutex _tcpSockets_m;
|
||||
|
||||
void (*_packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &);
|
||||
void *_arg;
|
||||
|
||||
Mutex _pollLock;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue