| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- /*
- * TcpServer_test.cpp
- *
- * @build
- * make libhv && sudo make install
- * g++ -std=c++11 TcpServer_test.cpp -o TcpServer_test -I/usr/local/include/hv -lhv -lpthread
- *
- */
- #include "TcpServer.h"
- using namespace hv;
- int main(int argc, char* argv[]) {
- if (argc < 2) {
- printf("Usage: %s port\n", argv[0]);
- return -10;
- }
- int port = atoi(argv[1]);
- TcpServer srv;
- int listenfd = srv.createsocket(port);
- if (listenfd < 0) {
- return -20;
- }
- printf("server listen on port %d, listenfd=%d ...\n", port, listenfd);
- srv.onConnection = [](const SocketChannelPtr& channel) {
- std::string peeraddr = channel->peeraddr();
- if (channel->isConnected()) {
- printf("%s connected! connfd=%d\n", peeraddr.c_str(), channel->fd());
- } else {
- printf("%s disconnected! connfd=%d\n", peeraddr.c_str(), channel->fd());
- }
- };
- srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
- // echo
- printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
- channel->write(buf);
- };
- srv.onWriteComplete = [](const SocketChannelPtr& channel, Buffer* buf) {
- printf("> %.*s\n", (int)buf->size(), (char*)buf->data());
- };
- srv.setThreadNum(4);
- srv.start();
- while (1) sleep(1);
- return 0;
- }
|