-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpServer.h
More file actions
52 lines (44 loc) · 1.61 KB
/
TcpServer.h
File metadata and controls
52 lines (44 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once
#include <functional>
#include <memory>
#include <vector>
#include <map>
#include "EventLoop.h"
#include "Acceptor.h"
#include "Socket.h"
#include "ThreadPool.h"
#include "Connection.h"
class TcpServer {
private:
std::unique_ptr<EventLoop> m_mainloop;
std::vector<std::unique_ptr<EventLoop>> m_subloops;
Acceptor m_acceptor;
std::map<int, spConnection> m_conns;
ThreadPool m_threadpool;
int m_threadnum;
std::mutex m_mmutex;
std::function<void(spConnection)> m_newconnectioncb;
std::function<void(spConnection)> m_closeconnectioncb;
std::function<void(spConnection)> m_errorconnectioncb;
std::function<void(spConnection, std::string& message)> m_onmessagecb;
std::function<void(spConnection)> m_sendcompletecb;
std::function<void(EventLoop*)> m_timeoutcb;
public:
TcpServer(const std::string& ip, const uint16_t port, int threadnum=3);
~TcpServer();
void start();
void stop();
void newconnection(std::unique_ptr<Socket> clientsock);
void closeconnection(spConnection conn);
void errorconnection(spConnection conn);
void onmessage(spConnection conn, std::string& message);
void sendcomplete(spConnection conn);
void epolltimeout(EventLoop* loop);
void setnewconnectioncb(std::function<void(spConnection)> fn);
void setcloseconnectioncb(std::function<void(spConnection)> fn);
void seterrorconnectioncb(std::function<void(spConnection)> fn);
void setonmessagecb(std::function<void(spConnection, std::string& message)> fn);
void setsendcompletecb(std::function<void(spConnection)> fn);
void settimeoutcb(std::function<void(EventLoop*)> fn);
void removeconn(int fd);
};