@@ -35,7 +35,13 @@ class Net_API TCPServerConnectionFactory
35
35
// / it accepts.
36
36
// /
37
37
// / Subclasses must override the createConnection()
38
- // / method.
38
+ // / method, which can refuse connections by returning nullptr.
39
+ // / Some examples when an implementation may refuse new connections:
40
+ // /
41
+ // / - number of connections exceeded a limit
42
+ // / - a connection from unwanted client attempted
43
+ // / - too many connection attempts in a short timespan
44
+ // / - etc.
39
45
// /
40
46
// / The TCPServerConnectionFactoryImpl template class
41
47
// / can be used to automatically instantiate a
@@ -45,23 +51,55 @@ class Net_API TCPServerConnectionFactory
45
51
public:
46
52
using Ptr = Poco::SharedPtr<TCPServerConnectionFactory>;
47
53
54
+ TCPServerConnectionFactory (const TCPServerConnectionFactory&) = delete ;
55
+ TCPServerConnectionFactory& operator = (const TCPServerConnectionFactory&) = delete ;
56
+ TCPServerConnectionFactory (TCPServerConnectionFactory&&) = delete ;
57
+ TCPServerConnectionFactory& operator = (TCPServerConnectionFactory&&) = delete ;
58
+
48
59
virtual ~TCPServerConnectionFactory ();
49
60
// / Destroys the TCPServerConnectionFactory.
50
61
51
62
virtual TCPServerConnection* createConnection (const StreamSocket& socket) = 0;
52
63
// / Creates an instance of a subclass of TCPServerConnection,
53
64
// / using the given StreamSocket.
65
+ // / This function is allowed to return nullptr, in which case an accepted
66
+ // / socket will be destroyed by the TCPServerDispatcher.
67
+
68
+ void stop ();
69
+ // / Stops the factory.
70
+ // / Normally, this function is called by TCPServerDispatcher
71
+ // / to indicate that the server is shutting down; the expected
72
+ // / implementation behavior after this call is to return nullptr
73
+ // / on all subsequent connection creation attempts.
74
+
75
+ bool isStopped () const ;
76
+ // / Returns true if the factory was stopped, false otherwise.
54
77
55
78
protected:
56
79
TCPServerConnectionFactory ();
57
80
// / Creates the TCPServerConnectionFactory.
58
81
59
82
private:
60
- TCPServerConnectionFactory (const TCPServerConnectionFactory&);
61
- TCPServerConnectionFactory& operator = (const TCPServerConnectionFactory&);
83
+ std::atomic<bool > _stopped;
62
84
};
63
85
64
86
87
+ //
88
+ // inlines
89
+ //
90
+
91
+ inline void TCPServerConnectionFactory::stop ()
92
+ {
93
+ _stopped = true ;
94
+ }
95
+
96
+
97
+ inline bool TCPServerConnectionFactory::isStopped () const
98
+ {
99
+ return _stopped;
100
+ }
101
+
102
+
65
103
template <class S >
66
104
class TCPServerConnectionFactoryImpl : public TCPServerConnectionFactory
67
105
// / This template provides a basic implementation of
@@ -78,7 +116,10 @@ class TCPServerConnectionFactoryImpl: public TCPServerConnectionFactory
78
116
79
117
TCPServerConnection* createConnection (const StreamSocket& socket)
80
118
{
81
- return new S (socket);
119
+ if (!isStopped ())
120
+ return new S (socket);
121
+
122
+ return nullptr ;
82
123
}
83
124
};
84
125
0 commit comments