MLIB
Loading...
Searching...
No Matches
tcpserver.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2024) Licensed under MIT License.
3 This is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7
8#pragma once
9
10#include "wsockstream.h"
11#include "inaddr.h"
12#include "thread.h"
13#include "event.h"
14#include "critsect.h"
15
16namespace mlib {
17
19typedef void (*conn_iter_func) (sock& conn, void* param);
20
21class tcpserver : public thread
22{
23public:
24 tcpserver (unsigned short port, const std::string& name = std::string (),
25 unsigned int max_conn = 0);
26 ~tcpserver ();
27
29 sock& socket ();
30
31 void foreach (conn_iter_func f, void* param);
32 thread* get_connection_thread (const sock& conn_sock);
33 void close_connection (const sock& conn_sock);
34 void terminate ();
35
37 size_t numconn () const;
38
40 unsigned int timeout () const;
41
43 void timeout (DWORD msec);
44
46 void maxconn (unsigned int new_max);
47
49 unsigned int maxconn () const;
50
51 void set_connfunc (std::function<int (const sock&)> f);
52
53protected:
54 bool init () override;
55 void run () override;
56
57 virtual bool idle_action ();
58 virtual void initconn (sock& conn_sock, thread* thread);
59 virtual void termconn (sock& conn_sock, thread* thread);
60 virtual thread* make_thread (sock& conn_sock);
61
62private:
63 sock srv_sock; // listening socket
64 inaddr addr; // listening address
65
67 struct conndata
68 {
69 sock socket;
71 bool condemned;
72 };
73 std::vector<conndata> contab;
74 criticalsection contab_lock;
75 unsigned int limit; // max number of active connections
76 auto_event evt; // main event
77 bool end_req; // true when server must exit
78 DWORD idle;
79 std::function<int (const sock& s)> connfunc;
80};
81
82/*==================== INLINE FUNCTIONS ===========================*/
83
84inline
86{
87 return srv_sock;
88}
89
90inline
91size_t tcpserver::numconn () const
92{
93 return contab.size ();
94}
95
102inline
103unsigned int tcpserver::timeout () const
104{
105 return (idle == INFINITE) ? 0 : idle;
106}
107
114inline void tcpserver::timeout (DWORD msec)
115{
116 idle = msec ? msec : INFINITE;
117 if (is_running ())
118 evt.signal ();
119}
120
121inline
122unsigned int tcpserver::maxconn () const
123{
124 return limit;
125}
126
127
135{
136 return true;
137};
138
139} // namespace mlib
Event objects that reset automatically after a successful wait.
Definition event.h:47
Lightweight inter-thread synchronization.
Definition critsect.h:25
void signal()
Set event to signaled state.
Definition event.h:19
sockaddr wrapper
Definition inaddr.h:23
Encapsulation of a Windows socket.
Definition wsockstream.h:27
multi-threaded TCP server.
Definition tcpserver.h:22
sock & socket()
Provides access to server listening socket.
Definition tcpserver.h:85
void run() override
Run loop.
Definition tcpserver.cpp:122
virtual void termconn(sock &conn_sock, thread *thread)
Finalizes a connection.
Definition tcpserver.cpp:293
~tcpserver()
Terminate any connection that still exists.
Definition tcpserver.cpp:83
unsigned int maxconn() const
Return maximum number of connections accepted.
Definition tcpserver.h:122
void set_connfunc(std::function< int(const sock &)> f)
Set function object that becomes the body of the thread serving a new connection.
Definition tcpserver.cpp:252
void close_connection(const sock &conn_sock)
Closes a connection.
Definition tcpserver.cpp:207
bool init() override
Binds the server socket to listening address and places it in listen mode.
Definition tcpserver.cpp:95
virtual void initconn(sock &conn_sock, thread *thread)
Initializes a connection.
Definition tcpserver.cpp:236
thread * get_connection_thread(const sock &conn_sock)
Return the thread servicing a connection.
Definition tcpserver.cpp:335
size_t numconn() const
Return number of active connections.
Definition tcpserver.h:91
virtual thread * make_thread(sock &conn_sock)
Return a servicing thread for each connection.
Definition tcpserver.cpp:269
void terminate()
Terminate the tcp server.
Definition tcpserver.cpp:349
unsigned int timeout() const
Return max interval to wait for an incoming connection (in milliseconds)
Definition tcpserver.h:103
virtual bool idle_action()
Called periodically from run loop.
Definition tcpserver.h:134
Wrapper for a Windows thread.
Definition thread.h:15
bool is_running() const
Return true if thread is running.
Definition thread.h:144
virtual const std::string & name() const
Return object's name.
Definition syncbase.h:60
criticalsection and lock classes
event class definition.
Definition of inaddr class.
void(* conn_iter_func)(sock &conn, void *param)
Connections iteration function.
Definition tcpserver.h:19
thread class definition.
Definition of sock and sockstream classes.