-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathcppnet.h
59 lines (44 loc) · 1.39 KB
/
cppnet.h
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
53
54
55
56
57
58
59
// Use of this source code is governed by a BSD 3-Clause License
// that can be found in the LICENSE file.
// Author: caozhiyi ([email protected])
#ifndef INCLUDE_CPPNET
#define INCLUDE_CPPNET
#include <memory>
#include <string>
#include "cppnet_buffer.h"
#include "cppnet_socket.h"
#include "cppnet_type.h"
namespace cppnet {
class CppNetBase;
// cppnet instance
class CppNet {
public:
CppNet() = default;
~CppNet();
// common
// init cppnet library.
// thread_num : the number of running threads.
void Init(int32_t thread_num = 0);
void Destory();
// thread join
void Join();
// must set callback before listen
void SetReadCallback(read_call_back&& cb);
void SetWriteCallback(write_call_back&& cb);
void SetDisconnectionCallback(connect_call_back&& cb);
// if use socket timer, set it
void SetTimerCallback(timer_call_back&& cb);
// return timer id
uint64_t AddTimer(int32_t interval, user_timer_call_back&& cb, void* param = nullptr, bool always = false);
void RemoveTimer(uint64_t timer_id);
//server
void SetAcceptCallback(connect_call_back&& cb);
bool ListenAndAccept(const std::string& ip, uint16_t port);
//client
void SetConnectionCallback(connect_call_back&& cb);
bool Connection(const std::string& ip, uint16_t port);
private:
std::shared_ptr<CppNetBase> _cppnet_base;
};
} // namespace cppnet
#endif