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. 60
      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++",
"args": [
"-g",
"${fileDirname}/client.cpp",
"${fileDirname}/server.cpp",
"${fileDirname}/../src/*.cpp",
// "${fileDirname}../include/*/src/*.cpp",
"-o",
"${fileDirname}/client.o",
"${fileDirname}/server.o",
"-lssl",
"-lcrypto"
],

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

@ -29,7 +29,7 @@ int main() {
// cout << "init cert " << endl;
// 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);
// cout << "init client " << endl;

Binary file not shown.

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

Binary file not shown.
Loading…
Cancel
Save