-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathframe.h
More file actions
98 lines (84 loc) · 2.78 KB
/
frame.h
File metadata and controls
98 lines (84 loc) · 2.78 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef FRAME_H_
#define FRAME_H_
#include <inttypes.h>
#include <unordered_map>
#include <list>
#include <set>
#include <vector>
#include <memory>
#include <unistd.h>
#include "thread.h"
#include "work.h"
#include "mutex.h"
#include "util/defer.h"
#include "util/time.h"
#include "util/log.h"
#include "listener.h"
struct event_base;
namespace tinyco {
class IsCompleteBase {
public:
IsCompleteBase() {}
virtual ~IsCompleteBase() {}
virtual int CheckPkg(const char *buffer, uint32_t buffer_len) { return 0; }
};
class Frame {
public:
static bool Init();
static bool Fini();
public:
static int UdpSendAndRecv(const std::string &sendbuf,
struct sockaddr_in &dest_addr,
std::string *recvbuf);
static int TcpSendAndRecv(const void *sendbuf, size_t sendlen, void *recvbuf,
size_t *recvlen, IsCompleteBase *is_complete);
static int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
static int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
static int send(int sockfd, const void *buf, size_t len, int flags);
static int recv(int sockfd, void *buf, size_t len, int flags);
static int sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
static ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
static ThreadWrapper CreateThread(Work *w);
static void Sleep(uint32_t ms);
static void RecycleRunningThread();
// it's very useful before mainloop of your program
static Thread *InitHereAsNewThread() {
auto t = new Thread();
t->Init();
t->Schedule();
running_thread_ = t;
}
private:
static int Schedule();
static Thread *AllocNewThread();
static void SocketReadOrWrite(int fd, short events, void *arg);
static int MainThreadLoop(void *arg);
static void PendThread(Thread *t);
static Thread *PopPendingTop();
static void WakeupPendingThread();
static timeval GetEventLoopTimeout();
static int EventLoop(const timeval &tv);
static void UpdateLoopTimestamp();
static uint64_t GetLastLoopTimestamp() { return last_loop_ts_; }
static event_base *GetEventBase() {
if (base_) return base_;
return (base_ = event_base_new());
}
static std::unordered_map<int, Thread *> io_wait_map_;
static std::list<Thread *> thread_runnable_;
static std::list<Thread *> thread_free_;
static std::vector<Thread *> thread_pending_;
static Thread *main_thread_;
static Thread *running_thread_;
static struct event_base *base_;
static int HandleProcess(void *arg);
private:
Frame() {}
virtual ~Frame() {}
static uint64_t last_loop_ts_;
};
}
#endif