/
server.cpp
91 lines (80 loc) · 2.58 KB
/
server.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
/*======================================================
> File Name: server.cpp
> Author: MiaoShuai
> E-mail:
> Other :
> Created Time: 2015年11月23日 星期一 17时16分13秒
=======================================================*/
#include "Codec.h"
#include <muduo/base/Logging.h>
#include <muduo/base/Mutex.h>
#include <muduo/net/EventLoop.h>
#include <muduo/net/TcpServer.h>
#include <functional>
#include <set>
#include <stdio.h>
using namespace muduo;
using namespace muduo::net;
class ChatServer : boost::noncopyable
{
public:
ChatServer(EventLoop *loop,const InetAddress &listenAddr)
:server_(loop,listenAddr,"ChatServer"),
codec_(std::bind(&ChatServer::onStringMessage,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3))
{
server_.setConnectionCallback(std::bind(&ChatServer::onConnection,this,std::placeholders::_1));
server_.setMessageCallback(std::bind(&LengthHeaderCodec::onMessage,&codec_,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));
}
void start()
{
server_.start();
}
private:
//新连接或断开时的回调
void onConnection(const TcpConnectionPtr &conn)
{
LOG_INFO <<conn->localAddress().toIpPort() << "->"
<<conn->peerAddress().toIpPort() << "is"
<<(conn->connected() ? "UP" : "DOWN");
if(conn->connected())
{
//插入到用户集合中
connections_.insert(conn);
}
else
{
//从集合中删除用户
connections_.erase(conn);
}
}
void onStringMessage(const TcpConnectionPtr &,const string &message,Timestamp)
{
//给每个用户发消息
for(ConnectionList::iterator it = connections_.begin();
it != connections_.end(); ++it)
{
codec_.send(get_pointer(*it),message);
}
}
typedef std::set<TcpConnectionPtr> ConnectionList;
TcpServer server_;
LengthHeaderCodec codec_;
ConnectionList connections_;
};
int main(int argc,char **argv)
{
LOG_INFO << "pid = " << getpid();
if(argc > 2)
{
EventLoop loop;
InetAddress serverAddr(argv[1],static_cast<uint16_t>(atoi(argv[2])));
ChatServer server(&loop,serverAddr);
server.start();
loop.loop();
}
else
{
printf("您输入的参数有误\n");
}
return 0;
}