iocpframework /Example/Network/ProtoBuf/ProtoBuf/ProtoBuf.cpp

Language C++ Lines 329
MD5 Hash 8195f507c4d3856dcdd79f1d6cedb9c9 Estimated Cost $5,642 (why?)
Repository http://iocpframework.googlecode.com/svn/trunk/ View Raw File
  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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// ProtoBuf.cpp : ??????????????
//

#include "stdafx.h"

#include "Codec.hpp"
#include "Dispatcher.hpp"
#include "Server.hpp"

#include <iostream>

using std::cout;
using std::endl;

#include "test.pb.h"

#pragma comment(lib, "libprotobuf")


void print(const std::string& buf)
{
	printf("encoded to %zd bytes\n", buf.c_str());
	for (size_t i = 0; i < buf.length(); ++i)
	{
		unsigned char ch = static_cast<unsigned char>(buf[i]);

		printf("%2zd:  0x%02x  %c\n", i, ch, isgraph(ch) ? ch : ' ');
	}
}

using namespace async;

void TestQuery()
{
	typedef ProtobufCodec<network::Tcp::Socket> DefaultProtoBuf;

	muduo::Query query;
	query.set_id(1);
	query.set_questioner("Chen Shuo");
	query.add_question("Running?");

	std::string buf;
	DefaultProtoBuf::FillBuffer(buf, query);
	print(buf);

	size_t len = 0;
	memcpy(&len, buf.data(), sizeof(len));

	DefaultProtoBuf::ErrorCode errorCode = DefaultProtoBuf::kNoError;
	MessagePtr message = DefaultProtoBuf::Parse(buf.data() + sizeof(size_t), len, &errorCode);
	assert(errorCode == DefaultProtoBuf::kNoError);
	assert(message != NULL);
	message->PrintDebugString();
	assert(message->DebugString() == query.DebugString());

	std::tr1::shared_ptr<muduo::Query> newQuery = std::tr1::static_pointer_cast<muduo::Query>(message);
	assert(newQuery != NULL);
}


void testAnswer()
{
	typedef ProtobufCodec<network::Tcp::Socket> DefaultProtoBuf;

	muduo::Answer answer;
	answer.set_id(1);
	answer.set_questioner("Chen Shuo");
	answer.set_answerer("blog.csdn.net/Solstice");
	answer.add_solution("Jump!");
	answer.add_solution("Win!");

	std::string buf;
	DefaultProtoBuf::FillBuffer(buf, answer);
	print(buf);

	size_t len = 0;
	memcpy(&len, buf.data(), sizeof(len));

	DefaultProtoBuf::ErrorCode errorCode = DefaultProtoBuf::kNoError;
	MessagePtr message = DefaultProtoBuf::Parse(buf.data() + sizeof(size_t), len, &errorCode);
	assert(errorCode == DefaultProtoBuf::kNoError);
	assert(message != NULL);
	message->PrintDebugString();
	assert(message->DebugString() == answer.DebugString());

	std::tr1::shared_ptr<muduo::Answer> newAnswer = std::tr1::static_pointer_cast<muduo::Answer>(message);
	assert(newAnswer != NULL);
}


void testEmpty()
{
	muduo::Empty empty;

	typedef ProtobufCodec<network::Tcp::Socket> DefaultProtoBuf;

	std::string buf;
	DefaultProtoBuf::FillBuffer(buf, empty);
	print(buf);

	size_t len = 0;
	memcpy(&len, buf.data(), sizeof(len));

	DefaultProtoBuf::ErrorCode errorCode = DefaultProtoBuf::kNoError;
	MessagePtr message = DefaultProtoBuf::Parse(buf.data() + sizeof(size_t), len, &errorCode);
	assert(message != NULL);
	message->PrintDebugString();
	assert(message->DebugString() == empty.DebugString());
}


void redoCheckSum(std::string& data, int len)
{
	int checkSum = 0;/*sockets::hostToNetwork32(static_cast<int32_t>(
		::adler32(1,
		reinterpret_cast<const Bytef*>(data.c_str()),
		static_cast<int>(len - 4))));*/
	data[len-4] = reinterpret_cast<const char*>(&checkSum)[0];
	data[len-3] = reinterpret_cast<const char*>(&checkSum)[1];
	data[len-2] = reinterpret_cast<const char*>(&checkSum)[2];
	data[len-1] = reinterpret_cast<const char*>(&checkSum)[3];
}


void testBadBuffer()
{
	typedef ProtobufCodec<network::Tcp::Socket> DefaultProtoBuf;

	muduo::Empty empty;
	empty.set_id(43);

	std::string buf;
	DefaultProtoBuf::FillBuffer(buf, empty);
	// print(buf);

	size_t len = 0;
	memcpy(&len, buf.data(), sizeof(len));

	{
		std::string data(buf.data(), len);
		DefaultProtoBuf::ErrorCode errorCode = DefaultProtoBuf::kNoError;
		MessagePtr message = DefaultProtoBuf::Parse(data.c_str() + sizeof(size_t), len-1, &errorCode);
		assert(message == NULL);
		assert(errorCode == DefaultProtoBuf::kCheckSumError);
	}

	{
		std::string data(buf.data(), len);
		DefaultProtoBuf::ErrorCode errorCode = DefaultProtoBuf::kNoError;
		data[len-1]++;
		MessagePtr message = DefaultProtoBuf::Parse(data.c_str() + sizeof(size_t), len, &errorCode);
		assert(message == NULL);
		assert(errorCode == DefaultProtoBuf::kCheckSumError);
	}

	{
		std::string data(buf.data(), len);
		DefaultProtoBuf::ErrorCode errorCode = DefaultProtoBuf::kNoError;
		data[0]++;
		MessagePtr message = DefaultProtoBuf::Parse(data.c_str() + sizeof(size_t), len, &errorCode);
		assert(message == NULL);
		assert(errorCode == DefaultProtoBuf::kCheckSumError);
	}

}


int g_count = 0;

void onMessage(const network::Tcp::Socket& conn, const MessagePtr& message) 
{
	g_count++;
}

void testOnMessage()
{
	typedef ProtobufCodec<network::Tcp::Socket> DefaultProtoBuf;

	muduo::Query query;
	query.set_id(1);
	query.set_questioner("Chen Shuo");
	query.add_question("Running?");

	std::string buf1;
	DefaultProtoBuf::FillBuffer(buf1, query);

	muduo::Empty empty;
	empty.set_id(43);
	empty.set_id(1982);

	std::string buf2;
	DefaultProtoBuf::FillBuffer(buf2, empty);

	size_t totalLen = buf1.size() + buf2.size();
	std::string all;
	all.append(buf1.data(), buf1.size());
	all.append(buf2.data(), buf2.size());
	assert(all.size() == totalLen);


	network::SocketPtr sock;
	network::Tcp::Socket conn(sock);
	DefaultProtoBuf codec(onMessage);
	for (size_t len = 0; len <= totalLen; ++len)
	{
		std::string input;
		input.append(all.data(), len);

		g_count = 0;
		codec.OnMessage(conn, iocp::Buffer(input));
		int expected = len < buf1.size() ? 0 : 1;
		if (len == totalLen) expected = 2;
		assert(g_count == expected);
		// printf("%2zd %d\n", len, g_count);

		input.append(all.data() + len, totalLen - len);
		codec.OnMessage(conn, iocp::Buffer(input));
		assert(g_count == 2);
	}
}




typedef std::tr1::shared_ptr<muduo::Query> QueryPtr;
typedef std::tr1::shared_ptr<muduo::Answer> AnswerPtr;

void onQuery(async::network::Tcp::Socket&, const QueryPtr& message)
{
	cout << "onQuery: " << message->GetTypeName() << endl;
}

void onAnswer(async::network::Tcp::Socket&, const AnswerPtr& message)
{
	cout << "onAnswer: " << message->GetTypeName() << endl;
}

void onUnknownMessageType(async::network::Tcp::Socket&, const MessagePtr& message)
{
	cout << "onUnknownMessageType: " << message->GetTypeName() << endl;
}


void TestDispatcher()
{

	dispatch::ProtobufDispatcher<network::Tcp::Socket> dispatcher(onUnknownMessageType);
	dispatcher.RegisterMessageCallback<muduo::Query>(onQuery);
	dispatcher.RegisterMessageCallback<muduo::Answer>(onAnswer);

	async::network::SocketPtr sock;
	async::network::Tcp::Socket conn(sock);

	QueryPtr query(new muduo::Query);
	AnswerPtr answer(new muduo::Answer);
	std::tr1::shared_ptr<muduo::Empty> empty(new muduo::Empty);

	dispatcher.OnProtobufMessage(conn, query);
	dispatcher.OnProtobufMessage(conn, answer);
	dispatcher.OnProtobufMessage(conn, empty);

	struct NotMessage
	{};

	std::tr1::shared_ptr<NotMessage> not(new NotMessage);
	//dispatcher.RegisterMessageCallback<NotMessage>(onQuery);
}



void OnQuery(Session &session, const MessagePtr& message)
{
	std::cout << "onQuery:\n" << message->GetTypeName() << message->DebugString();
	
	session.Send(*message);

	//conn->shutdown();
}

void OnAnswer(Session &session, const MessagePtr& message)
{
	std::cout << "onAnswer: " << message->GetTypeName();
	
	session.Send(*message);
}

void TestServer()
{
	async::iocp::IODispatcher io;
	Server svr(io, 5050);

	using namespace std::tr1::placeholders;
	svr.Register<muduo::Query>(std::tr1::bind(&OnQuery, _1, _2));
	svr.Register<muduo::Answer>(std::tr1::bind(&OnAnswer, _1, _2));

	svr.Start();
	

	system("pause");

	svr.Stop();
}

int _tmain(int argc, _TCHAR* argv[])
{	
	GOOGLE_PROTOBUF_VERIFY_VERSION;

	TestQuery();

	testAnswer();

	testEmpty();

	testBadBuffer();

	//testOnMessage();
	
	TestDispatcher();

	TestServer();
	

	google::protobuf::ShutdownProtobufLibrary();

	system("pause");
	return 0;
}
Back to Top