-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
118 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
testserver : | ||
g++ -g -o testserver testserver.cc -lmymuduo -lpthread -std=c++11 | ||
|
||
clean : | ||
rm -f testserver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <string> | ||
|
||
#include <mymuduo/TcpServer.h> | ||
#include <mymuduo/Logger.h> | ||
|
||
class EchoServer | ||
{ | ||
public: | ||
EchoServer(EventLoop *loop, const InetAddress &addr, const std::string &name) | ||
: server_(loop, addr, name) | ||
, loop_(loop) | ||
{ | ||
// 注册回调函数 | ||
server_.setConnectionCallback( | ||
std::bind(&EchoServer::onConnection, this, std::placeholders::_1)); | ||
|
||
server_.setMessageCallback( | ||
std::bind(&EchoServer::onMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); | ||
|
||
// 设置合适的subloop线程数量 | ||
server_.setThreadNum(3); | ||
} | ||
void start() | ||
{ | ||
server_.start(); | ||
} | ||
|
||
private: | ||
// 连接建立或断开的回调函数 | ||
void onConnection(const TcpConnectionPtr &conn) | ||
{ | ||
if (conn->connected()) | ||
{ | ||
LOG_INFO("Connection UP : %s", conn->peerAddress().toIpPort().c_str()); | ||
} | ||
else | ||
{ | ||
LOG_INFO("Connection DOWN : %s", conn->peerAddress().toIpPort().c_str()); | ||
} | ||
} | ||
|
||
// 可读写事件回调 | ||
void onMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp time) | ||
{ | ||
std::string msg = buf->retrieveAllAsString(); | ||
conn->send(msg); | ||
// conn->shutdown(); // 关闭写端 底层响应EPOLLHUP => 执行closeCallback_ | ||
} | ||
|
||
EventLoop *loop_; | ||
TcpServer server_; | ||
}; | ||
|
||
int main() { | ||
EventLoop loop; | ||
InetAddress addr(8002); | ||
EchoServer server(&loop, addr, "EchoServer"); | ||
server.start(); | ||
loop.loop(); | ||
return 0; | ||
} |