1 #ifndef _spin_listener_h
2 #define _spin_listener_h
4 #include "Details/prologue.h"
5 #include <boost/cstdint.hpp>
6 #include <boost/scoped_ptr.hpp>
7 #include <boost/filesystem/path.hpp>
8 #include "Connection.h"
9 #include "Details/Address.h"
10 #include <Scorpion/Context.h>
12 #ifndef DOXYGEN_GENERATING
13 namespace Scorpion
{ class BIO
; }
19 class NewConnectionHandler
;
21 /** A basic server-implementing class.
22 * The Listener class listens on any given local address and port and allows you to accept incoming
23 * connections. If given a Scorpion context during construction, it will use the context to accept
24 * SSL connections in stead of accepting unencrypted/unsecured connections. Once the context is in
25 * place, though, this is completely transparent.
27 * Connections can be accepted synchronously, using the accept method, or asynchronously by setting
28 * a handler for new connections, which is an instance of \link Handlers::NewConnectionHandler
29 * NewConnectionHandler \endlink.
31 * When using SSL, it is important to note that the security is only as good as the context you set
32 * up for it. For example: if you want your server to check the validity of its clients, you need to
33 * set the context up to do those checks before constructing the Listener as you will not be able to
34 * do it afterwards. */
35 class SPIN_API Listener
38 //! Construct a non-secured server
39 Listener(Details::Address local_address
, boost::uint16_t local_port
);
40 //! Construct a secured server
41 Listener(const Scorpion::Context
& security_context
, Details::Address local_address
, boost::uint16_t local_port
);
44 //! Synchronously accept a connection (i.e. wait for it)
45 boost::shared_ptr
< Connection
> accept();
46 /** Stop waiting for connections.
47 * You should destroy the listener immediately after using this method! */
50 //! Set a new connection handler for accepting connections asynchronously
51 void setNewConnectionHandler(Handlers::NewConnectionHandler
& handler
);
52 //! Clear the handler for asynchronous new connections
53 void clearNewConnectionHandler();
56 // Neither CopyConstructible nor Assignable
57 Listener(const Listener
&);
58 Listener
& operator=(const Listener
&);
60 std::string
constructLocalAddress_(Details::Address local_address
, boost::uint16_t local_port
);
61 Scorpion::BIO
* createBIO_(const std::string
& local_address
);
62 Scorpion::BIO
* createSSLBIO_(const std::string
& local_address
);
64 void onNewConnection_();
66 Scorpion::Context security_context_
;
67 boost::scoped_ptr
< Scorpion::BIO
> bio_
;
68 Handlers::NewConnectionHandler
* new_connection_handler_
;