-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (134 loc) · 3.66 KB
/
main.cpp
File metadata and controls
159 lines (134 loc) · 3.66 KB
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
/*************************************************************************
> File Name: main.cpp
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: Sun 08 Mar 2015 01:38:31 AM PST
************************************************************************/
#include "pollingserver.h"
#include<iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "EventLoop.h"
#include "Handler.h"
#include <sys/timerfd.h>
#include "EventLoopThread.h"
#include "Acceptor.h"
#include "InetAddress.h"
#include "SocketOps.h"
#include "TcpServer.h"
using namespace std;
#define MAX_EVENT_NUMBER 1024
EventLoop* g_loop;
void timeout()
{
printf("Timout!\n");
g_loop->quit();
}
void test1()
{
EventLoop loop;
g_loop = &loop;
int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
Handler handler(&loop, timerfd);
handler.setReadCallback(timeout);
handler.enableReading();
struct itimerspec howlong;
bzero(&howlong, sizeof(howlong));
howlong.it_value.tv_sec = 5;
::timerfd_settime(timerfd, 0, &howlong, NULL);
loop.loop();
::close(timerfd);
}
void runInThread()
{
printf("runInThread():pid=%d, tid=%d\n", getpid(), CurrentThread::tid());
}
void test_EventLoopThread()
{
printf("test2():pid=%d, tid=%d\n", getpid(), CurrentThread::tid());
EventLoopThread loopThread;
EventLoop* loop = loopThread.startLoop();
printf("main 1111111111\n");
loop->runInLoop(runInThread);
printf("main 222222222222\n");
sleep(1);
loop->quit();
printf("exit test2\n");
}
void newConnection(int sockfd, const InetAddress& peerAddr)
{
printf("newConnection:addcept a new connection from %s\n", peerAddr.toHostPort().c_str());
::write(sockfd, "how are you?\n", 13);
SocketOps::close(sockfd);
}
void test_Acceptor()
{
printf("test_Acceptor:pid = %d\n", getpid());
InetAddress listenaddr(9981);
EventLoop loop;
Acceptor acceptor(&loop, listenaddr);
acceptor.setNewConnectionCallback(newConnection);
acceptor.listen();
loop.loop();
}
std::string message;
void onConnection(const TcpConnectionPtr& conn)
{
if(conn->isConnected())
{
printf("onConnection: new connection [%s] from %s\n", conn->getName().c_str(), conn->getPeerAddress().toHostPort().c_str());
conn->send(message);
}
else
{
printf("onConnection: connection [%s] is down\n", conn->getName().c_str());
}
}
void onWriteComplete(const TcpConnectionPtr& conn)
{
conn->send(message);
}
void onMessage(const TcpConnectionPtr& conn,
Buffer* buf)
{
printf("onMessage(): received %d bytes from connection [%s]\n",
buf->readableBytes(),
conn->getName().c_str());
printf("ccccccccc:%s\n", buf->retrieveAllAsString().c_str());
buf->retrieveAll();
}
void test_TcpServer()
{
std::string servername = "test_Tcpserver";
message = "bbbbbbbbbbbbbbb\n";
InetAddress listenAddr(9981);
EventLoop loop;
TcpServer server(&loop, listenAddr, servername);
server.setConnectionCallback(onConnection);
server.setMessageCallback(onMessage);
server.setWriteCompleteCallback(onWriteComplete);
server.start();
loop.loop();
}
int main(int argc, char** argv)
{
/* if(argc < 3)
{
printf("Need ip and port\n");
}
const char* ip = argv[1];
int port = atoi(argv[2]);*/
/*const char* ip = "127.0.0.1";
int port = 8086;
PollingServer pollserv;
std::string servname = "GenerateId";
pollserv.Init(servname, ip, port);
pollserv.Run();
pollserv.Release();*/
//test_EventLoopThread();
//test_Acceptor();
test_TcpServer();
return 0;
}