void EchoServer::run() { _ready.set(); Poco::Timespan span(250000); char* pBuffer = new char[_bufferSize]; while (!_stop) { if (_socket.poll(span, Socket::SELECT_READ)) { StreamSocket ss = _socket.acceptConnection(); try { int n = ss.receiveBytes(pBuffer, _bufferSize); while (n > 0 && !_stop) { ss.sendBytes(pBuffer, n); n = ss.receiveBytes(pBuffer, _bufferSize); } } catch (Poco::Exception& exc) { std::cerr << "EchoServer: " << exc.displayText() << std::endl; } } } delete pBuffer; }
/** Handling requests */ bool KeyValueStore::handleRequest(StreamSocket& client, Config::ThreadControl& control, char*& buf) { char req; unsigned contentLength = 0; client.receiveBytes(&req, sizeof(req)); if(req == Protocol::EXIT) { return true; } // Always expect that content-length will be the next sent. client.receiveBytes(&contentLength, sizeof(contentLength)); if(contentLength < 0) throw Exception("Invalid content-length detected. Dropping client"); buf = new char[contentLength+1]; buf[contentLength] = '\0'; client.receiveBytes(buf, contentLength); KeyValueStore::handleQuery(client, control, req, buf); delete [] buf; buf = NULL; return false; }
pair<char, string> KeyValueStore::rcvResponse(StreamSocket& sock, char*& buf) { unsigned contentLength = 0; char resp; pair<char, string> ret; char* msg = NULL; sock.receiveBytes(&resp, sizeof(resp)); if(resp == Protocol::LOOKUP_SUC) { sock.receiveBytes(&contentLength, sizeof(contentLength)); unsigned rem = contentLength; unsigned totalRead = 0; buf = new char[contentLength+1]; msg = buf; buf[contentLength] = '\0'; while((totalRead += sock.receiveBytes(msg, rem)) != contentLength) { msg = buf + totalRead; rem = contentLength - totalRead; } ret = pair<char, string>(resp, buf); delete [] buf; buf = NULL; } else { ret = pair<char, string>(resp, ""); } return ret; }
void HTTPTestServer::run() { _ready.set(); Poco::Timespan span(250000); while (!_stop) { if (_socket.poll(span, Socket::SELECT_READ)) { StreamSocket ss = _socket.acceptConnection(); try { _lastRequest.clear(); char buffer[256]; int n = ss.receiveBytes(buffer, sizeof(buffer)); while (n > 0 && !_stop) { _lastRequest.append(buffer, n); if (!requestComplete()) n = ss.receiveBytes(buffer, sizeof(buffer)); else n = 0; } std::string response = handleRequest(); ss.sendBytes(response.data(), (int) response.size()); Poco::Thread::sleep(1000); } catch (Poco::Exception& exc) { std::cerr << "HTTPTestServer: " << exc.displayText() << std::endl; } } } }
void EchoServer::run() { _ready.set(); pi::Timespan span(250000); while (!_stop) { if (_socket.poll(span, Socket::SELECT_READ)) { StreamSocket ss = _socket.acceptConnection(); try { char buffer[256]; int n = ss.receiveBytes(buffer, sizeof(buffer)); while (n > 0 && !_stop) { ss.sendBytes(buffer, n); n = ss.receiveBytes(buffer, sizeof(buffer)); } } catch (pi::Exception& exc) { std::cerr << "EchoServer: " << exc.displayText() << std::endl; } } } }
void SocketTest::testSelect() { Timespan timeout(250000); EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); Socket::SocketList readList; Socket::SocketList writeList; Socket::SocketList exceptList; readList.push_back(ss); assert (Socket::select(readList, writeList, exceptList, timeout) == 0); assert (readList.empty()); assert (writeList.empty()); assert (exceptList.empty()); ss.sendBytes("hello", 5); ss.poll(timeout, Socket::SELECT_READ); readList.push_back(ss); writeList.push_back(ss); assert (Socket::select(readList, writeList, exceptList, timeout) == 2); assert (!readList.empty()); assert (!writeList.empty()); assert (exceptList.empty()); char buffer[256]; int n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void SocketTest::testFIFOBuffer() { Buffer<char> b(5); b[0] = 'h'; b[1] = 'e'; b[2] = 'l'; b[3] = 'l'; b[4] = 'o'; FIFOBuffer f(5, true); f.readable += delegate(this, &SocketTest::onReadable); f.writable += delegate(this, &SocketTest::onWritable); assert(0 == _notToReadable); assert(0 == _readableToNot); assert(0 == _notToWritable); assert(0 == _writableToNot); f.write(b); assert(1 == _notToReadable); assert(0 == _readableToNot); assert(0 == _notToWritable); assert(1 == _writableToNot); EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); int n = ss.sendBytes(f); assert (n == 5); assert(1 == _notToReadable); assert(1 == _readableToNot); assert(1 == _notToWritable); assert(1 == _writableToNot); assert (f.isEmpty()); n = ss.receiveBytes(f); assert (n == 5); assert(2 == _notToReadable); assert(1 == _readableToNot); assert(1 == _notToWritable); assert(2 == _writableToNot); assert (f[0] == 'h'); assert (f[1] == 'e'); assert (f[2] == 'l'); assert (f[3] == 'l'); assert (f[4] == 'o'); f.readable -= delegate(this, &SocketTest::onReadable); f.writable -= delegate(this, &SocketTest::onWritable); ss.close(); }
void SocketTest::testEcho() { EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); int n = ss.sendBytes("hello", 5); assert (n == 5); char buffer[256]; n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void SocketTest::testAvailable() { EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); Timespan timeout(1000000); ss.sendBytes("hello", 5); char buffer[256]; assert (ss.poll(timeout, Socket::SELECT_READ)); int av = ss.available(); assert (av > 0 && av <= 5); int n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
int main() { StreamSocket sock; SocketAddress addr("70.79.74.121:80"); sock.connect(addr); const char* data="GET / HTTP/1.1\r\nHost: xa.us.to\r\n\r\n"; sock.sendBytes(data,strlen(data)); const int bs=4096; char* buf=new char[bs]; int br; while((br=sock.receiveBytes(buf,bs))>0) write(1,buf,br); }
void CSimpleSocket::test(){ using namespace Poco; using namespace Poco::Net; StreamSocket sk; bool connected = false; try{ sk.connect(SocketAddress("s1.goyou.cn", 80), Timespan(6, 0)); connected = true; }catch(Poco::Exception& exp){ std::cout << exp.displayText() << std::endl; } if (connected) { try{ UInt8 buffer[2048] = "GET / HTTP/1.0\r\nUser-Agent: Mozilla/5.0\r\n\r\n"; int nSendLen = strlen((char*)buffer); int nHasSendLen = 0; while (nHasSendLen != nSendLen) { int nLen = sk.sendBytes(buffer+nHasSendLen, nSendLen-nHasSendLen); nHasSendLen += nLen; } int nRecvLen = 0; do { Socket::SocketList readList, writeList, expList; readList.push_back(sk); Socket::select(readList, writeList, expList, Timespan(3, 0)); nRecvLen = 0; if (readList.size()) { nRecvLen = sk.receiveBytes(buffer, 1024); std::cout << nSendLen << " -- " << nRecvLen << std::endl; buffer[nRecvLen] = 0; std::cout << buffer << std::endl; } } while (nRecvLen>0); sk.close(); }catch(Poco::Exception& exp){ std::cout <<exp.displayText() << std::endl; } } }
void regTest3() { tracef("reg test 3 begin: reg send and receive data, register fail"); StreamSocket ss; SocketAddress sa("127.0.0.1", 13333); ss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.register"; DynamicStruct param; param["token"] = "1234567890"; param["uuid"] = "SC000000001"; ds["param"] = param; ss.sendBytes(ds.toString().c_str(), ds.toString().length()); tracef("reg data send: %s.", ds.toString().c_str()); char buf[1024] = {0, }; ss.receiveBytes(buf, 1024); tracef("receive bytes: %s.", buf); tracef("socket closed."); tracef("register test 3 finished.\n"); }
void SocketTest::testPoll() { EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); Stopwatch sw; sw.start(); Timespan timeout(1000000); assert (!ss.poll(timeout, Socket::SELECT_READ)); assert (sw.elapsed() >= 900000); sw.restart(); assert (ss.poll(timeout, Socket::SELECT_WRITE)); assert (sw.elapsed() < 100000); ss.sendBytes("hello", 5); char buffer[256]; sw.restart(); assert (ss.poll(timeout, Socket::SELECT_READ)); assert (sw.elapsed() < 100000); int n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void SocketTest::testTimeout() { EchoServer echoServer; StreamSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); Timespan timeout0 = ss.getReceiveTimeout(); Timespan timeout(250000); ss.setReceiveTimeout(timeout); Timespan timeout1 = ss.getReceiveTimeout(); std::cout << "original receive timeout: " << timeout0.totalMicroseconds() << std::endl; std::cout << "requested receive timeout: " << timeout.totalMicroseconds() << std::endl; std::cout << "actual receive timeout: " << timeout1.totalMicroseconds() << std::endl; // some socket implementations adjust the timeout value // assert (ss.getReceiveTimeout() == timeout); Stopwatch sw; try { char buffer[256]; sw.start(); ss.receiveBytes(buffer, sizeof(buffer)); fail("nothing to receive - must timeout"); } catch (TimeoutException&) { } assert (sw.elapsed() < 1000000); timeout0 = ss.getSendTimeout(); ss.setSendTimeout(timeout); timeout1 = ss.getSendTimeout(); std::cout << "original send timeout: " << timeout0.totalMicroseconds() << std::endl; std::cout << "requested send timeout: " << timeout.totalMicroseconds() << std::endl; std::cout << "actual send timeout: " << timeout1.totalMicroseconds() << std::endl; // assert (ss.getSendTimeout() == timeout); }
void LocalSocketTest::testSocketsPerformance() { Timestamp::TimeDiff local = 0, net = 0; std::size_t initBufSize = 1; std::size_t initReps = 1; bool noDelay[2] = { true, false }; std::cout << std::endl << "OS Name: " << Environment::osName() << std::endl; std::cout << "OS Version: " << Environment::osVersion() << std::endl; std::cout << "OS Architecture: " << Environment::osArchitecture() << std::endl; for (int d = 0; d < 2; ++d) { double locData = 0.0, locTime = 0.0, netData = 0.0, netTime = 0.0; std::ostringstream os; os << Environment::osName() << '-' << Environment::osVersion() << '-' << Environment::osArchitecture() << "-TCP"; if (noDelay[d]) os << "-nodelay"; os << ".csv"; File f(os.str()); if (f.exists()) f.remove(); FileOutputStream fos(os.str()); for (std::size_t repetitions = initReps; repetitions <= 100000; repetitions *= 10) { for (std::size_t bufSize = initBufSize; bufSize < 20000; bufSize *= 2) { char* pBuf = new char[bufSize]; { SocketAddress sas("/tmp/poco.server.tcp.sock"); EchoServer echoServer(sas, bufSize); SocketAddress sac("/tmp/poco.client.tcp.sock"); StreamSocket ss(sas, &sac); int recv = 0, sent = 0; Stopwatch sw; int i = 0; for (; i < repetitions; ++i) { sent = 0; recv = 0; local = 0; do { int s; sw.restart(); s = ss.sendBytes(pBuf + sent, bufSize - sent); sw.stop(); local += sw.elapsed(); sent += s; } while (sent < bufSize); do { int r; sw.restart(); r = ss.receiveBytes(pBuf + recv, bufSize - recv); sw.stop(); local += sw.elapsed(); recv += r; } while (recv < bufSize); locData += sent; locData += recv; locTime += local; poco_assert (sent == bufSize && recv == bufSize); } std::cout << "Local TCP socket, " << i << " repetitions, " << bufSize << " bytes, " << local << " [us]." << std::endl; ss.close(); } { SocketAddress sa("localhost", 12345); EchoServer echoServer(sa, bufSize); StreamSocket ss; ss.connect(SocketAddress(sa.host(), echoServer.port())); if (noDelay[d]) ss.setNoDelay(true); int recv = 0, sent = 0; Stopwatch sw; int i = 0; for (; i < repetitions; ++i) { sent = 0; recv = 0; net = 0; do { int s; sw.restart(); s = ss.sendBytes(pBuf + sent, bufSize - sent); sw.stop(); net += sw.elapsed(); sent += s; } while (sent < bufSize); do { int r; sw.restart(); r = ss.receiveBytes(pBuf + recv, bufSize - recv); sw.stop(); net += sw.elapsed(); recv += r; } while (recv < bufSize); netData += sent; netData += recv; netTime += net; poco_assert (sent == bufSize && recv == bufSize); } std::cout << "Network TCP socket, " << i << " repetitions, " << bufSize << " bytes, " << net << " [us]." << std::endl; fos << i << ',' << bufSize << ','; ss.close(); } delete pBuf; double ratio = ((double) net) / ((double) local); double diff = ((double) net) - ((double) local); std::cout << "Ratio: " << ratio << "(" << diff / 1000.0 << "[us/msg]" << std::endl; fos << ratio << std::endl; } } poco_assert (locData == netData); double locDTR = ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000; double netDTR = ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000; std::cout << (d ? "NO DELAY" : "DELAY") << std::endl << "=================================" << std::endl << "Local DTR: " << ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl << "Network DTR: " << ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl << "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl << "=================================" << std::endl; fos << "=================================" << std::endl << "Local DTR: " << ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl << "Network DTR: " << ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl << "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl << "=================================" << std::endl; fos.close(); } }
int testOption(char option, const string userId = "", const string passwd = "", const string filename = "") { Buffer buffer; int code = option; int len = 0; int totalLen = 0; buffer.append((const char*)&code, 1); totalLen = totalLen + 1; len = userId.length(); buffer.append((const char*)&len, 4); buffer.append(userId); totalLen = totalLen + 4 + len; if (code == addUser || code == updateUser) { len = passwd.length(); buffer.append((const char*)&len, 4); buffer.append(passwd); totalLen = totalLen + 4 + len; std::ifstream ifs(filename.c_str(), std::ifstream::binary | std::ifstream::in); if (ifs.is_open()) { if (ifs.good()) { ifs.seekg(0, ifs.end); len = ifs.tellg(); ifs.seekg(0, ifs.beg); unsigned char bin[FINGER_LEN] = { 0 }; buffer.append((const char*)&len, 4); ifs.read((char*)bin, len); buffer.append((const char*)bin, len); totalLen = totalLen + 4 + len; } } else { cout << "finger file " << filename << " is not OK!" << endl; return -2; } } Buffer sendBuf; sendBuf.append((const char*)&totalLen, 4); sendBuf.append(buffer.retrieveAllAsString()); int sendLen = sendBuf.readableBytes(); ss.sendBytes(sendBuf.retrieveAllAsString().c_str(), sendLen); char buf[2048] = { 0 }; int n = ss.receiveBytes(buf, sizeof(buf)); std::copy((char*)buf, (char*)(buf + 4), (char*)&totalLen); std::copy((char*)(buf + 4), (char*)(buf + 5), &code); cout << "totalLen: " << totalLen << ", code: " << (int)code << endl; if (option == queryUserInfo) { int useridlen = 0; int lenPlus = 5; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + 4), (char*) &useridlen); string userid; userid.resize(useridlen); lenPlus = 9; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + useridlen), &(*userid.begin())); int passwdlen = 0; lenPlus = 5 + 4 + useridlen; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + 4), (char*) &passwdlen); string passwd; lenPlus = 5 + 4 + useridlen + 4; passwd.resize(passwdlen); std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + passwdlen), &(*passwd.begin())); int fingerLen = 0; lenPlus = 5 + 4 + useridlen + 4 + passwdlen; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + 4), (char*) &fingerLen); char finger[2048] = {0}; lenPlus = 5 + 4 + useridlen + 4 + passwdlen + 4; std::copy((char*) (buf + lenPlus), (char*) (buf + lenPlus + fingerLen), (char*) finger); cout << "useridlen: " << useridlen << ", userid: " << userid << ", passwdlen: " << passwdlen << ", passwd: " << passwd << ", fingerLen" << fingerLen << endl; unsigned char bin[FINGER_LEN] = { 0 }; std::ifstream ifs(filename.c_str(), std::ifstream::binary | std::ifstream::in); if (ifs.is_open()) { if (ifs.good()) { ifs.seekg(0, ifs.end); len = ifs.tellg(); ifs.seekg(0, ifs.beg); ifs.read((char*) bin, len); } } if (memcmp(bin, finger, fingerLen ) == 0) cout << "finger data ok!" << endl; } else if (option == queryAllUserInfo) { int userLen1 = 0; std::copy((char*) (buf + 5), (char*) (buf + 9), (char*)&userLen1); char user1[32] = {0}; std::copy((char*) (buf + 9), (char*) (buf + 9 + userLen1), user1); int userLen2 = 0; std::copy((char*) (buf + 9 + userLen1), (char*) (buf + 13 + userLen1), (char*) &userLen2); char user2[32] = {0}; std::copy((char*) (buf + 13 + userLen1), (char*) (buf + 13 + userLen1 + userLen2), user2); cout << "user1: " << user1 << ", user2" << user2 << endl; } return 0; }
void regTest5() { tracef("reg test 5 begin:reg twice"); Context::Ptr pContext = new Context(Context::TLSV1_CLIENT_USE, "", Context::VERIFY_NONE); SecureStreamSocket sss(pContext); SocketAddress sa("127.0.0.1", 12222); sss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.token"; Timestamp t; UInt64 tms = t.epochMicroseconds(); char tms_str[32]; snprintf(tms_str, 31, "%llu", tms); std::string key = "alpha2015"; key += tms_str; MD5Engine md5; md5.update(key); const DigestEngine::Digest& digest = md5.digest(); std::string md5key = DigestEngine::digestToHex(digest); DynamicStruct param; param["key"] = md5key; param["timestamp"] = tms_str; param["dev_name"] = "lock3"; param["dev_type"] = "sc-01"; param["uuid"] = "SC00000003"; ds["param"] = param; tracef("data send: %s.", ds.toString().c_str()); sss.sendBytes(ds.toString().c_str(), ds.toString().length()); char buf[1024] = {0, }; sss.receiveBytes(buf, 1024); JSON::Parser parser; Dynamic::Var var = parser.parse(buf); JSON::Object::Ptr pObj = var.extract<JSON::Object::Ptr>(); DynamicStruct ds_recv = *pObj; std::string token = ds_recv["param"]["token"]; DynamicStruct ds_reg; ds_reg["type"] = "request"; ds_reg["action"] = "server.register"; DynamicStruct param_reg; param_reg["token"] = token; param_reg["uuid"] = "SC00000003"; ds_reg["param"] = param_reg; StreamSocket ss; SocketAddress sa2("127.0.0.1", 13333); ss.connect(sa2, Timespan(3, 0)); ss.sendBytes(ds_reg.toString().c_str(), ds_reg.toString().length()); tracef("reg data send: %s.", ds_reg.toString().c_str()); memset(buf, 0, 1024); ss.receiveBytes(buf, 1024); tracef("receive bytes: %s.", buf); ss.close(); Thread::sleep(2000); StreamSocket ss2; ss2.connect(sa2, Timespan(3, 0)); tracef("reg data send again: %s.", ds_reg.toString().c_str()); ss2.sendBytes(ds_reg.toString().c_str(), ds_reg.toString().length()); memset(buf, 0, 1024); ss2.receiveBytes(buf, 1024); tracef("recv data: %s.", buf); ss2.close(); tracef("reg test 5 finished."); }
int main(int argc, char** argv) { if (argc > 1) serverAddr = string(argv[1]); if (argc > 2) port = atoi(argv[2]); if (argc > 3) filename = argv[3]; Buffer buffer; int len = 0; int totalLen = 0; char code = validateUser; buffer.append((const char*)&code, 1); totalLen = totalLen + 1; std::ifstream ifs(filename.c_str(), std::ifstream::binary | std::ifstream::in); if (ifs.is_open()) { if (ifs.good()) { ifs.seekg(0, ifs.end); len = ifs.tellg(); ifs.seekg(0, ifs.beg); buffer.append((const char*)&len, 4); unsigned char bin[FINGER_LEN] = { 0 }; ifs.read((char*)bin, len); buffer.append((const char*)bin, len); totalLen = totalLen + 4 + len; } } else { cout << "finger file " << filename << " is not OK!" << endl; return -2; } Buffer sendBuf; sendBuf.append((const char*)&totalLen, 4); sendBuf.append(buffer.retrieveAllAsString()); int sendLen = sendBuf.readableBytes(); StreamSocket ss; try { ss.connect(SocketAddress(serverAddr, port)); } catch (Poco::Exception& exc) { cerr << "Connect server failed, err: " << exc.displayText() << endl; return -1; } ss.sendBytes(sendBuf.retrieveAllAsString().c_str(), sendLen); char buf[FINGER_LEN] = { 0 }; int n = ss.receiveBytes(buf, sizeof(buf)); std::copy(buf, buf + 4, (char*)&totalLen); std::copy(buf + 4, buf + 5, &code); if (code == 0) { char *p = buf; p += 5; char tmp[32] = { 0 }; int nameIdLen = 0; std::copy(p, (char*)(p + 4), (char*)&nameIdLen); p += 4; string nameId; nameId.resize(nameIdLen); std::copy(p, (char*)(p + nameIdLen), nameId.begin()); p += nameIdLen; memset(tmp, 0, 32); int passwdLen = 0; std::copy(p, p + 4, (char*)&passwdLen); p += 4; string passwd; passwd.resize(passwdLen); std::copy(p, (char*)(p + passwdLen), passwd.begin()); cout << "nameId: " << nameId << ", passwd: " << passwd << endl; } cout << "totalLen: " << totalLen << ", code: " << (int)code << endl; //char c(' '); //while (c != 'q' && c != 'Q') //{ // cout << "Press q then enter to quit: \n"; // cin >> c; //} return 0; }