Skip to content

Commit

Permalink
Fix c++ examples
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Oct 23, 2024
1 parent 2648146 commit 8b89482
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 77 deletions.
6 changes: 4 additions & 2 deletions examples/cpp/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
all: co repeat
all: co repeat test_server

co: co.cc
g++ co.cc -I../.. -I../../include -DHAVE_CONFIG_H -L../../lib -lswoole -o co -g
repeat: repeat.cc
g++ repeat.cc -I../.. -I../../include -DHAVE_CONFIG_H -L../../lib -lswoole -o repeat -g
test_server: test_server.cc
g++ test_server.cc -I../.. -I../../include -DHAVE_CONFIG_H -L../../lib -lswoole -o test_server -g
clean:
rm -f co repeat
rm -f co repeat test_server
147 changes: 74 additions & 73 deletions examples/cpp/co.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,94 +17,95 @@ list<Socket *> slaves;
size_t qs;

int main(int argc, char **argv) {
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);

signal(SIGPIPE, SIG_IGN);

Coroutine::create([](void *arg) {
System::sleep(2.0);
cout << "CO-1, sleep 2\n";
});
Coroutine::run([](void *arg) {
Coroutine::create([](void *arg) {
System::sleep(2.0);
cout << "CO-1, sleep 2\n";
});

Coroutine::create([](void *arg) {
System::sleep(1);
cout << "CO-2, sleep 1\n";
});
Coroutine::create([](void *arg) {
System::sleep(1);
cout << "CO-2, sleep 1\n";
});

Coroutine::create([](void *arg) {
cout << "CO-3, listen tcp:0.0.0.0:9001\n";
Socket s(SW_SOCK_TCP);
s.bind("0.0.0.0", 9001);
s.listen();
Coroutine::create([](void *arg) {
cout << "CO-3, listen tcp:0.0.0.0:9001\n";
Socket s(SW_SOCK_TCP);
s.bind("0.0.0.0", 9001);
s.listen();

while (1) {
Socket *_client = s.accept();
Coroutine::create(
[](void *arg) {
Socket *client = (Socket *) arg;
while (1) {
char buf[1024];
auto retval = client->recv(buf, sizeof(buf));
if (retval == 0) {
cout << "connection close\n";
break;
} else {
if (strncasecmp("push", buf, 4) == 0) {
q.push_back(string(buf + 5, retval - 5));
qs += retval - 5;
string resp("OK\n");
client->send(resp.c_str(), resp.length());
while (1) {
Socket *_client = s.accept();
Coroutine::create(
[](void *arg) {
Socket *client = (Socket *) arg;
while (1) {
char buf[1024];
auto retval = client->recv(buf, sizeof(buf));
if (retval == 0) {
cout << "connection close\n";
break;
} else {
if (strncasecmp("push", buf, 4) == 0) {
q.push_back(string(buf + 5, retval - 5));
qs += retval - 5;
string resp("OK\n");
client->send(resp.c_str(), resp.length());

for (auto it = slaves.begin(); it != slaves.end();) {
auto sc = *it;
auto n = sc->send(buf, retval);
if (n <= 0) {
it = slaves.erase(it);
delete sc;
for (auto it = slaves.begin(); it != slaves.end();) {
auto sc = *it;
auto n = sc->send(buf, retval);
if (n <= 0) {
it = slaves.erase(it);
delete sc;
} else {
it++;
}
}
} else if (strncasecmp("pop", buf, 3) == 0) {
if (q.empty()) {
string resp("EMPTY\n");
client->send(resp.c_str(), resp.length());
} else {
it++;
auto data = q.front();
q.pop_front();
qs -= data.length();
client->send(data.c_str(), data.length());
}
}
} else if (strncasecmp("pop", buf, 3) == 0) {
if (q.empty()) {
string resp("EMPTY\n");
client->send(resp.c_str(), resp.length());
} else if (strncasecmp("stat", buf, 4) == 0) {
char stat_buf[64];
int n = snprintf(stat_buf, sizeof(stat_buf), "count=%ld,bytes=%ld\n", q.size(), qs);
client->send(stat_buf, n);
} else {
auto data = q.front();
q.pop_front();
qs -= data.length();
client->send(data.c_str(), data.length());
string resp("ERROR\n");
client->send(resp.c_str(), resp.length());
}
} else if (strncasecmp("stat", buf, 4) == 0) {
char stat_buf[64];
int n = snprintf(stat_buf, sizeof(stat_buf), "count=%ld,bytes=%ld\n", q.size(), qs);
client->send(stat_buf, n);
} else {
string resp("ERROR\n");
client->send(resp.c_str(), resp.length());
}
}
}
delete client;
},
_client);
}
});
delete client;
},
_client);
}
});

Coroutine::create([](void *arg) {
Socket s(SW_SOCK_TCP);
s.bind("0.0.0.0", 9002);
s.listen();
while (1) {
Socket *_client = s.accept();
for (auto data : q) {
_client->send(data.c_str(), data.length());
Coroutine::create([](void *arg) {
Socket s(SW_SOCK_TCP);
s.bind("0.0.0.0", 9002);
s.listen();
while (1) {
Socket *_client = s.accept();
if (_client == nullptr) {
break;
}
for (auto data : q) {
_client->send(data.c_str(), data.length());
}
slaves.push_back(_client);
}
slaves.push_back(_client);
}
});
});

swoole_event_wait();

return 0;
}
4 changes: 2 additions & 2 deletions examples/cpp/repeat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ int main(int argc, char **argv) {

serv.onPacket = [](Server *serv, RecvData *req) { return SW_OK; };

serv.onWorkerStart = [](Server *serv, int worker_id) {
swoole_notice("WorkerStart[%d]PID=%d, serv=%p,", worker_id, getpid(), serv);
serv.onWorkerStart = [](Server *serv, Worker *worker) {
swoole_notice("WorkerStart[%d]PID=%d, serv=%p,", worker->id, getpid(), serv);
swoole_timer_after(
1000,
[serv](Timer *, TimerNode *tnode) {
Expand Down

0 comments on commit 8b89482

Please sign in to comment.