From: Gerd v. Egidy Date: Thu, 2 Nov 2006 10:28:36 +0000 (+0000) Subject: libt2n: (gerd) fix bug receiving fragmented data X-Git-Tag: v0.2~127 X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=commitdiff_plain;h=58b327c6c65474fdfde7d0d99d66d3986400f208 libt2n: (gerd) fix bug receiving fragmented data --- diff --git a/src/socket_handler.cpp b/src/socket_handler.cpp index ed37e4d..98944b0 100644 --- a/src/socket_handler.cpp +++ b/src/socket_handler.cpp @@ -190,7 +190,7 @@ bool socket_handler::fill_buffer(std::string& buffer) // Data read -> store it if (nbytes > 0) { - buffer.assign(socket_buffer,nbytes); + buffer.append(socket_buffer,nbytes); LOGSTREAM(debug,nbytes << " bytes read"); } diff --git a/test/comm.cpp b/test/comm.cpp index eb4de8c..169c183 100644 --- a/test/comm.cpp +++ b/test/comm.cpp @@ -32,6 +32,7 @@ class test_comm : public TestFixture CPPUNIT_TEST(UnixCommToServer); CPPUNIT_TEST(UnixCommToServerAndBack); + CPPUNIT_TEST(UnixCommToServerAndBackBig); CPPUNIT_TEST(IPCommToServer); CPPUNIT_TEST(IPCommToServerAndBack); @@ -156,6 +157,66 @@ class test_comm : public TestFixture } } + void UnixCommToServerAndBackBig() + { + pid_t pid; + + switch(pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + socket_server ss("./socket"); + ss.set_logging(&cerr,debug); + + // max 10 sec + for (int i=0; i < 10; i++) + { + ss.fill_buffer(1000000); + + string data; + unsigned int cid; + + if(ss.get_packet(data,cid)) + { + server_connection* con=ss.get_connection(cid); + + if (data=="QUIT") + break; + + con->write(string().insert(0,100*1024,'y')); + } + } + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + string data; + + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + + sc.write(string().insert(0,100*1024,'x')); + + while (!sc.get_packet(data)) + sc.fill_buffer(1000000); + + CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data); + + sc.write("QUIT"); + } + } + } + void IPCommToServer() { pid_t pid; diff --git a/test/simplecmd.cpp b/test/simplecmd.cpp index b2b5bbd..eb4343c 100644 --- a/test/simplecmd.cpp +++ b/test/simplecmd.cpp @@ -36,10 +36,13 @@ using namespace CppUnit; string testfunc(const string& str) { + string ret; if (str=="throw") throw libt2n::t2n_runtime_error("throw me around"); - string ret(str); - ret+=", testfunc() was here"; + if (str=="big") + ret.insert(0,100*1024,'x'); + else + ret=str+", testfunc() was here"; return ret; } @@ -113,6 +116,8 @@ class test_simplecmd : public TestFixture CPPUNIT_TEST(SimpleCmd); CPPUNIT_TEST(SimpleException); + CPPUNIT_TEST(BigReturn); + CPPUNIT_TEST(BigParameter); CPPUNIT_TEST_SUITE_END(); @@ -222,6 +227,92 @@ class test_simplecmd : public TestFixture } } + void BigReturn() + { + pid_t pid; + + switch(pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + socket_server ss("./socket"); + command_server cs(ss); + + // max 10 sec + for (int i=0; i < 10; i++) + cs.handle(1000000); + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + command_client cc(sc); + + result_container rc; + cc.send_command(new testfunc_cmd("big"),rc); + + string ret=dynamic_cast(rc.get_result())->get_data(); + + CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'x'),ret); + } + } + } + + void BigParameter() + { + pid_t pid; + + switch(pid=fork()) + { + case -1: + { + CPPUNIT_FAIL("fork error"); + break; + } + case 0: + // child + { + socket_server ss("./socket"); + command_server cs(ss); + + // max 10 sec + for (int i=0; i < 10; i++) + cs.handle(1000000); + + // don't call atexit and stuff + _exit(0); + } + + default: + // parent + { + // wait till server is up + sleep(1); + socket_client_connection sc("./socket"); + command_client cc(sc); + + result_container rc; + cc.send_command(new testfunc_cmd(string().insert(0,100*1024,'y')),rc); + + string ret=dynamic_cast(rc.get_result())->get_data(); + + CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret); + } + } + } + }; CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);