-
Notifications
You must be signed in to change notification settings - Fork 63
/
EchoServer.cc
63 lines (52 loc) · 1.27 KB
/
EchoServer.cc
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
//author voidccc
#include "EchoServer.h"
#include "TcpConnection.h"
#include "EventLoop.h"
#include "CurrentThread.h"
#include "Task.h"
#include <iostream>
#define MESSAGE_LENGTH 8
EchoServer::EchoServer(EventLoop* pLoop)
:_pLoop(pLoop)
,_pServer(pLoop)
,_timer(-1)
,_index(0)
{
_pServer.setCallback(this);
}
EchoServer::~EchoServer()
{}
void EchoServer::start()
{
_pServer.start();
_threadpool.start(3);
}
void EchoServer::onConnection(TcpConnection* pCon)
{
cout << "onConnection" << endl;
}
void EchoServer::onMessage(TcpConnection* pCon, Buffer* pBuf)
{
while(pBuf->readableBytes() > MESSAGE_LENGTH)
{
string message = pBuf->retrieveAsString(MESSAGE_LENGTH);
Task task(this, message, pCon);
_threadpool.addTask(task);
}
}
void EchoServer::onWriteComplate(TcpConnection* pCon)
{
cout << "onWriteComplate" << endl;
}
//run in different therad
void EchoServer::run2(const string& str, void* tcp)
{
//IO blocking task or CPU busy task
cout << "fib(30) = " << fib(30) << " tid = " << CurrentThread::tid() << endl;
((TcpConnection*)tcp)->send(str + "\n");
}
//fib is short for Fibonacci, fib is a CPU busy method
int EchoServer::fib(int n)
{
return (n == 1 || n == 2) ? 1 : (fib(n-1) + fib(n-2));
}