-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.h
More file actions
54 lines (43 loc) · 1.43 KB
/
Connection.h
File metadata and controls
54 lines (43 loc) · 1.43 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
53
54
#pragma once
#include <functional>
#include <memory>
#include "Channel.h"
#include "EventLoop.h"
#include "Socket.h"
#include "Buffer.h"
#include "Timestamp.h"
class Connection;
class EventLoop;
class Channel;
using spConnection = std::shared_ptr<Connection>;
class Connection : public std::enable_shared_from_this<Connection> {
private:
EventLoop* m_loop;
std::unique_ptr<Socket> m_clientsock;
std::unique_ptr<Channel> m_clientchannel;
Buffer m_inputbuffer;
Buffer m_outputbuffer;
std::atomic_bool m_disconnect;
std::function<void(spConnection)> m_closecallback;
std::function<void(spConnection)> m_errorcallback;
std::function<void(spConnection, std::string&)> m_onmessagecallback;
std::function<void(spConnection)> m_sendcompletecallback;
Timestamp m_lasttime;
public:
Connection(EventLoop* loop, std::unique_ptr<Socket> clientsock);
~Connection();
int fd() const;
std::string ip() const;
uint16_t port() const;
void onmessage();
void closecallback();
void errorcallback();
void writecallback();
void setclosecallback(std::function<void(spConnection)> fn);
void seterrorcallback(std::function<void(spConnection)> fn);
void setonmessagecallback(std::function<void(spConnection, std::string&)> fn);
void setsendcompletecallback(std::function<void(spConnection)> fn);
void send(const char* data, size_t size);
void sendinloop(const char* data, size_t size);
bool timeout(time_t now, int val);
};