text
stringlengths
0
2.2M
server.waitConnectedClient(500);
std::string messageSent = "message";
ofxTest(client.sendRawBytes(messageSent.c_str(), messageSent.size()), "send blocking from client");
std::vector<char> messageReceived(messageSent.size()+1, 0);
std::size_t received = 0;
do{
auto ret = server.receiveRawBytes(0, messageReceived.data() + received, messageSent.size());
ofxTest(ret>0, "received blocking from server");
if(ret>0){
received += ret;
}else{
break;
}
}while(received<messageSent.size());
ofxTestEq(messageSent, std::string(messageReceived.data()), "messageSent == messageReceived");
}
void testWrongConnect(){
ofLogNotice() << "";
ofLogNotice() << "---------------------------------------";
ofLogNotice() << "testWrongConnect";
ofxTCPManager client;
client.SetTimeoutConnect(5);
ofxTest(client.Create(), "socket creation");
auto then = ofGetElapsedTimeMillis();
ofxTest(!client.Connect("127.0.0.1", 200), "connect to non open port, if this fails the port might be really open:");
auto now = ofGetElapsedTimeMillis();
ofxTest(now-then<6000, "Connect waits 5s to timeout, waited: " + ofToString(now - then));
}
void testReceiveTimeout(){
ofLogNotice() << "";
ofLogNotice() << "---------------------------------------";
ofLogNotice() << "testReceiveTimeout";
int port = ofRandom(15000, 65535);
ofxTCPManager server;
ofxTest(server.Create(), "server socket creation");
ofxTest(server.Bind(port), "server socket bind");
ofxTest(server.Listen(1), "server socket listen");
std::condition_variable done;
std::mutex mtx;
std::thread serverThread([&]{
ofxTCPManager client;
ofxTest(server.Accept(client), "server socket accept");
std::unique_lock<std::mutex> lck(mtx);
done.wait(lck);
});
ofxTCPManager client;
ofxTest(client.Create(), "client socket create");
ofxTest(client.Connect("127.0.0.1", port), "client socket connect");
ofSleepMillis(100);
client.SetTimeoutReceive(5);
auto then = ofGetElapsedTimeMillis();
char buffer;
ofxTestEq(client.Receive(&buffer,1), SOCKET_TIMEOUT, "socket timeouts on no receive");
auto now = ofGetElapsedTimeMillis();
// seems timers in the test servers are not very accurate so
// we test this with a margin of 500ms
ofxTestGt(now-then, std::uint64_t(4500), "Connect waits 5s to timeout, waited: " + ofToString(now - then));
done.notify_all();
serverThread.join();
}
void testSendMaxSize(){
ofLogNotice() << "";
ofLogNotice() << "---------------------------------------";
ofLogNotice() << "testSendMaxSize tests #3478";
int port = ofRandom(15000, 65535);
ofxTCPServer server;
ofxTest(server.setup(port,true), "blocking server");
ofxTCPClient client;
ofxTest(client.setup("127.0.0.1", port, true), "blocking client");
// wait for connection to be made
server.waitConnectedClient(500);
string str;
string received;
for(int i=0;i<TCP_MAX_MSG_SIZE;i++){
str.append(ofToString((int)ofRandom(10)));
}
ofxTest(server.sendRawMsg(0, str.c_str(), str.size()), "could send max size");
do{
received += client.receiveRaw();
}while(received.size()<str.size());
ofxTestEq(received, str, "received max size message == sent message");
}
void run(){