forked from alibaba/PhotonLibOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.cpp
168 lines (140 loc) · 6.43 KB
/
simple.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <fcntl.h>
#include <vector>
#include <photon/thread/std-compat.h>
#include <photon/common/alog.h>
#include <photon/common/iovector.h>
#include <photon/fs/localfs.h>
#include <photon/net/socket.h>
// In this example, we will demonstrate a simple example using various functional modules,
// namely `common`, `thread`, `fs`, `io` and `net`. The program basically sets up two Photon threads,
// creates a Photon file and fs for IO, and sent buffer through Photon socket.
//
// Because every module has its own document, this example will not focus on the API details.
// Please refer to the README under the module directories.
static void run_socket_server(photon::net::ISocketServer* server, photon::fs::IFile* file, AlignedAlloc& alloc,
photon_std::condition_variable& cv, photon_std::mutex& mu, bool& got_msg);
int main() {
// Initialize Photon environment in current vcpu.
//
// Note Photon's event engine could be either epoll or io_uring. Running an io_uring program would need
// the kernel version to be greater than 5.8. If you are willing to use io_uring, please switch the
// event_engine argument from `photon::INIT_EVENT_EPOLL` to `photon::INIT_EVENT_IOURING`.
int ret = photon::init(photon::INIT_EVENT_EPOLL, photon::INIT_IO_LIBAIO);
if (ret < 0) {
LOG_ERROR_RETURN(0, -1, "failed to init photon environment");
}
// DEFER is a helper macro from `common` module. Like Go's defer, it ensures the statement be executed
// before the function returns. Its implementation is based on the concept of RAII.
DEFER(photon::fini());
// Create a local IFileSystem under current working dir.
// When enabling io_uring, please switch the io_engine_type from `photon::fs::ioengine_libaio` to
// `photon::fs::ioengine_iouring`.
auto fs = photon::fs::new_localfs_adaptor(".", photon::fs::ioengine_libaio);
if (!fs) {
LOG_ERRNO_RETURN(0, -1, "failed to create fs");
}
DEFER(delete fs);
// Open a IFile from IFileSystem. The IFile object will close itself at destruction.
auto file = fs->open("simple-test-file", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (!file) {
LOG_ERRNO_RETURN(0, -1, "failed to open file");
}
DEFER(delete file);
auto server = photon::net::new_tcp_socket_server();
if (server == nullptr) {
LOG_ERRNO_RETURN(0, -1, "failed to create tcp server");
}
DEFER(delete server);
// Photon's std is equivalent to the standard std, but specially working for coroutines
photon_std::mutex mu;
photon_std::condition_variable cv;
bool got_msg = false;
AlignedAlloc alloc(512);
// So the thread is actually a coroutine. Photon threads run on top of vcpu(native OS threads).
// We create a Photon thread to run socket server. Pass some local variables to the new thread as arguments.
auto server_thread = photon_std::thread(run_socket_server, server, file, alloc, cv, mu, got_msg);
// Create a watcher thread to wait the go_msg flag
auto watcher_thread = photon_std::thread([&] {
LOG_INFO("Start to watch message");
photon_std::unique_lock<photon_std::mutex> lock(mu);
while (!got_msg) {
cv.wait(lock);
}
LOG_INFO("Got message!");
});
// Wait server to be ready to accept
photon_std::this_thread::sleep_for(std::chrono::seconds(1));
// Create socket client and connect
auto client = photon::net::new_tcp_socket_client();
if (client == nullptr) {
LOG_ERRNO_RETURN(0, -1, "failed to create tcp client");
}
DEFER(delete client);
photon::net::EndPoint ep{photon::net::IPAddr("127.0.0.1"), 9527};
auto stream = client->connect(ep);
if (!stream) {
LOG_ERRNO_RETURN(0, -1, "failed to connect server");
}
// Write socket
void* buf = alloc.alloc(1024);
if (stream->send(buf, 1024) != 1024) {
LOG_ERRNO_RETURN(0, -1, "failed to write socket");
}
// Close connection
delete stream;
// Wait for a while and shutdown the server
photon_std::this_thread::sleep_for(std::chrono::seconds(1));
server->terminate();
// Join other threads
watcher_thread.join();
server_thread.join();
}
void run_socket_server(photon::net::ISocketServer* server, photon::fs::IFile* file, AlignedAlloc& alloc,
photon_std::condition_variable& cv, photon_std::mutex& mu, bool& got_msg) {
void* buf = alloc.alloc(1024);
DEFER(alloc.dealloc(buf));
auto handler = [&](photon::net::ISocketStream* sock) -> int {
// read is a wrapper for fully recv
ssize_t ret = sock->read(buf, 1024);
if (ret <= 0) {
LOG_ERRNO_RETURN(0, -1, "failed to read socket");
}
// IOVector is a helper class to manipulate io-vectors, taking the buf as underlying buffer
IOVector iov;
iov.push_back(buf, 512);
iov.push_back((char*) buf + 512, 512);
// This is a demo about how to use the io-vector interface. Even though some io engines
// may not have the writev method, Photon's IFile encapsulation would make it compatible.
// Note all the IOs in Photon are non-blocking.
ssize_t written = file->writev(iov.iovec(), iov.iovcnt());
if (written != (ssize_t) iov.sum()) {
LOG_ERRNO_RETURN(0, -1, "failed to write file");
}
// Got message. Notify the watcher
{
photon_std::lock_guard<photon_std::mutex> lock(mu);
got_msg = true;
cv.notify_one();
}
return 0;
};
server->set_handler(handler);
server->bind(9527, photon::net::IPAddr());
server->listen(1024);
// Photon's logging system formats the output string at compile time, and has better performance
// than other systems using snprintf. The ` is a generic placeholder.
LOG_INFO("Server is listening for port ` ...", 9527);
server->start_loop(false);
}