Hack for non-guaranteed send and recv functions

read-write-limit v0.5_hack
marcelb 1 year ago
parent 9217ce91c4
commit 031853d8ee
  1. 4
      .vscode/tasks.json
  2. 50
      src/tcp_socket.cpp
  3. 2
      test/client.cpp
  4. BIN
      test/client.o
  5. 21
      test/server.cpp
  6. BIN
      test/server.o

@ -6,11 +6,11 @@
"command": "/usr/bin/g++", "command": "/usr/bin/g++",
"args": [ "args": [
"-g", "-g",
"${fileDirname}/client.cpp", "${fileDirname}/server.cpp",
"${fileDirname}/../src/*.cpp", "${fileDirname}/../src/*.cpp",
// "${fileDirname}../include/*/src/*.cpp", // "${fileDirname}../include/*/src/*.cpp",
"-o", "-o",
"${fileDirname}/client.o", "${fileDirname}/server.o",
"-lssl", "-lssl",
"-lcrypto" "-lcrypto"
], ],

@ -324,15 +324,28 @@ client::~client () {
*/ */
bool client::push (const string msg) { bool client::push(const string msg) {
size_t sended = 0; size_t total_sent = 0;
size_t msg_length = msg.length();
while (total_sent < msg_length) {
size_t sent = 0;
if (ssl) { if (ssl) {
sended = SSL_write(ssl, msg.c_str(), msg.length()); sent = SSL_write(ssl, msg.c_str() + total_sent, msg_length - total_sent);
} else {
sent = send(conn, msg.c_str() + total_sent, msg_length - total_sent, 0);
} }
else {
sended = send(conn, msg.c_str(), msg.length(), 0); if (sent == -1) {
// Greška pri slanju poruke
return false;
} }
return sended == msg.length();
total_sent += sent;
}
return true;
} }
/** /**
@ -341,18 +354,29 @@ bool client::push (const string msg) {
* Vraća string primljene poruke * Vraća string primljene poruke
*/ */
string client::pull (size_t byte_limit) { string client::pull(size_t byte_limit) {
char res[byte_limit] = {0}; char res[byte_limit] = {0};
size_t total_received = 0;
while (total_received < byte_limit) {
ssize_t received = 0;
if (ssl) { if (ssl) {
SSL_read(ssl, res, byte_limit); received = SSL_read(ssl, res + total_received, byte_limit - total_received);
} else {
received = recv(conn, res + total_received, byte_limit - total_received, 0);
} }
else {
recv(conn , res, byte_limit, 0); if (received == -1) {
// Greška pri primanju poruke
break;
} else if (received == 0) {
// Veza je prekinuta
break;
}
total_received += received;
} }
return string(res); return string(res);
} }

@ -29,7 +29,7 @@ int main() {
// cout << "init cert " << endl; // cout << "init cert " << endl;
// client myserver("127.0.0.1", 5000, 5000, crypto.fds); // client myserver("127.0.0.1", 5000, 5000, crypto.fds);
client myserver("localhost", 8000, 5000, crypto.fds); // client myserver("localhost", 8000, 5000, crypto.fds);
// client myserver("localhost", 5000); // client myserver("localhost", 5000);
// cout << "init client " << endl; // cout << "init client " << endl;

Binary file not shown.

@ -10,10 +10,10 @@ int main() {
// secure crypto ("../example/cert.pem", "../example/privkey.pem"); // secure crypto ("../example/cert.pem", "../example/privkey.pem");
// cout << "init server " << endl; // cout << "init server " << endl;
// server myserver(5000, 100, crypto.fds); // server myserver(5000, 100, crypto.fds);
cout << "init cert " << endl; // cout << "init cert " << endl;
secure crypto ("../example/cert.pem", "../example/privkey.pem"); // secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init server " << endl; // cout << "init server " << endl;
server myserver(8000, 100, crypto.fds); // server myserver(8000, 100, crypto.fds);
// cout << "init client " << endl; // cout << "init client " << endl;
@ -29,12 +29,12 @@ int main() {
// // usleep(600*1000); // // usleep(600*1000);
// sleep(5); // sleep(5);
// myserver.cli->push(fromclient); // myserver.cli->push(fromclient);
string fromclient = myserver.cli->pull(); // string fromclient = myserver.cli->pull();
// string fromclient = myclient.pull(); // string fromclient = myclient.pull();
cout << "tell client " << fromclient << endl; // cout << "tell client " << fromclient << endl;
// usleep(600*1000); // usleep(600*1000);
//sleep(5); //sleep(5);
myserver.cli->push(fromclient); // myserver.cli->push(fromclient);
// myclient.push(fromclient); // myclient.push(fromclient);
// myclient.~comming(); // myclient.~comming();
@ -49,8 +49,9 @@ int main() {
// sleep(80); // sleep(80);
cout << "init server " << endl;
server myserver(5000, 100); server myserver(5000, 100);
cout << "init client " << endl;
myserver.async(8, [](client &cli, mutex &io) { myserver.async(8, [](client &cli, mutex &io) {
cout << "Klijent " << cli.ipv4 << endl; cout << "Klijent " << cli.ipv4 << endl;
string fromclient = cli.pull(); string fromclient = cli.pull();
@ -63,12 +64,10 @@ int main() {
// string teststr = " Idemooo"; // string teststr = " Idemooo";
// myserver.sync([](client &cli, mutex &io) { // myserver.sync([](client &cli) {
// cout << "Klijent " << cli.ipv4 << endl; // cout << "Klijent " << cli.ipv4 << endl;
// string fromclient = cli.pull(); // string fromclient = cli.pull();
// io.lock();
// cout << "S klijenta " << fromclient << endl; // cout << "S klijenta " << fromclient << endl;
// io.unlock();
// // fromclient += teststr; // // fromclient += teststr;
// cli.push(fromclient); // cli.push(fromclient);
// }); // });

Binary file not shown.
Loading…
Cancel
Save