-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.cpp
152 lines (118 loc) · 3.97 KB
/
test_main.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
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
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include "East/IO/IoConnectionHandler.h"
#include "East/IO/IoServiceManager.h"
#include "East/IO/Impl/TcpServerConnector.h"
#include "East/IO/Impl/UdpServerConnector.h"
boost::mutex mutex;
class TestServerHandler : public IoConnectionHandler
{
public:
TestServerHandler()
: mIoStream(&mStreamBuffer)
{
}
virtual ~TestServerHandler() {}
public:
boost::asio::io_service::strand* mStrand;
boost::asio::streambuf mStreamBuffer;
std::iostream mIoStream;
virtual void OnConnected(IoSocket& socket)
{
printf("[%d][%08X] OnConnected \r\n", GetTickCount(), this);
socket.AsyncRead();
}
virtual void OnDisconnected(IoSocket& socket)
{
printf("[%d][%08X] OnDisconnected \r\n", GetTickCount(), this);
socket.Close();
}
virtual void OnRecevied(IoSocket& socket, boost::asio::const_buffer& data)
{
socket.AsyncRead();
socket.AsyncWrite(data);
size_t size = boost::asio::buffer_size(data);
const uint8_t* pData = boost::asio::buffer_cast<const uint8_t*>(data);
mIoStream << std::string(pData, pData + size);
std::string line;
std::getline(mIoStream, line);
if (!line.empty())
std::cout << line << std::endl;
//printf("[%d][%08X] OnConnected : %s \r\n", GetTickCount(), this, std::string(pData, pData + size).c_str());
}
virtual void OnSent(IoSocket& socket)
{
}
virtual void OnError(IoSocket& socket, const boost::system::error_code& error)
{
printf("[%d][%08X] OnError : %s \r\n", GetTickCount(), this, error.message().c_str());
socket.Close();
}
};
class TestClientHandler : public IoConnectionHandler
{
public:
TestClientHandler()
{
}
virtual ~TestClientHandler() {}
public:
boost::asio::io_service::strand* mStrand;
virtual void OnConnected(IoSocket& socket)
{
printf("[%d][%08X] OnConnected \r\n", GetTickCount(), this);
char* test = "HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello";
socket.AsyncRead();
socket.AsyncWrite(boost::asio::buffer(test, strlen(test)));
}
virtual void OnDisconnected(IoSocket& socket)
{
printf("[%d][%08X] OnDisconnected \r\n", GetTickCount(), this);
socket.Close();
}
boost::mutex mutex;
virtual void OnRecevied(IoSocket& socket, boost::asio::const_buffer& data)
{
socket.AsyncRead();
socket.AsyncWrite(data);
size_t size = boost::asio::buffer_size(data);
const uint8_t* pData = boost::asio::buffer_cast<const uint8_t*>(data);
//printf("[%d][%08X] OnConnected : %s \r\n", GetTickCount(), this, std::string(pData, pData + size).c_str());
}
virtual void OnSent(IoSocket& socket)
{
}
virtual void OnError(IoSocket& socket, const boost::system::error_code& error)
{
printf("[%d][%08X] OnError : %s \r\n", GetTickCount(), this, error.message().c_str());
socket.Close();
}
};
#include "East/IO/Impl/TcpConnection.h"
#include "East/IO/Impl/TcpClientConnector.h"
#include "East/IO/Impl/UdpConnection.h"
#include "East/IO/Impl/UdpServerConnector.h"
#include "East/Protocol/EDTP/EdtpClient.h"
#include "East/Protocol/EDTP/Service.h"
#include <boost/iostreams/stream.hpp>
int main()
{
IoServiceManager ioServiceManger;
ioServiceManger.Enable();
TcpServerConnector serverConnector(ioServiceManger, 8888);
//TcpClientConnector clientConnector(ioServiceManger, "127.0.0.1", 8888);
//EdtpClient client("Test1", "127.0.0.1", 8888);
//client.RegisterService(boost::shared_ptr<Service>(new ServiceImp<int>));
//client.RegisterService(boost::shared_ptr<Service>(new ServiceImp<std::string>));
//client.RegisterService(boost::shared_ptr<Service>(new ServiceImp<long>));
//client.RegisterService(boost::shared_ptr<Service>(new ServiceImp<float>));
RESTART:
serverConnector.Connect(new TestServerHandler);
//clientConnector.Connect(new TestClientHandler);
//client.Enable();
getchar();
goto RESTART;
ioServiceManger.Disable();
return 0;
}