Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HighSpeedNetIO to take in two ports instead of one #119

Merged
merged 3 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 33 additions & 40 deletions emp-tool/io/highspeed_net_io_channel.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#ifndef EMP_HIGHSPEED_NETWORK_IO_CHANNEL_H__
#define EMP_HIGHSPEED_NETWORK_IO_CHANNEL_H__

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <string>

#include "emp-tool/io/io_channel.h"
using std::string;

#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

namespace emp {

class SubChannel {
public:
class SubChannel { public:
int sock;
FILE *stream = nullptr;
char *buf = nullptr;
Expand All @@ -42,9 +43,7 @@ class SubChannel {
};

class SenderSubChannel : public SubChannel { public:
SenderSubChannel(int sock): SubChannel(sock) {
ptr = 0;
}
SenderSubChannel(int sock) : SubChannel(sock) { ptr = 0; }

void flush() {
flushes++;
Expand Down Expand Up @@ -80,9 +79,7 @@ class SenderSubChannel: public SubChannel {public:
};

class RecverSubChannel : public SubChannel { public:
RecverSubChannel(int sock): SubChannel(sock) {
ptr = NETWORK_BUFFER_SIZE2;
}
RecverSubChannel(int sock) : SubChannel(sock) { ptr = NETWORK_BUFFER_SIZE2; }
void flush() {
flushes++;
ptr = NETWORK_BUFFER_SIZE2;
Expand Down Expand Up @@ -123,15 +120,33 @@ class RecverSubChannel: public SubChannel {public:
}
}
};

class HighSpeedNetIO : public IOChannel<HighSpeedNetIO> { public:
bool is_server, quiet;
int send_sock = 0;
int recv_sock = 0;
int FSM = 0;
SenderSubChannel *schannel;
RecverSubChannel *rchannel;
string addr;
int port;

HighSpeedNetIO(const char *address, int send_port, int recv_port, bool quiet = true) : quiet(quiet) {
is_server = (address == nullptr);
if (is_server) {
recv_sock = server_listen(send_port);
usleep(2000);
send_sock = server_listen(recv_port & 0xFFFF);
} else {
send_sock = client_connect(address, send_port);
recv_sock = client_connect(address, recv_port & 0xFFFF);
}
FSM = 0;
set_delay_opt(send_sock, true);
set_delay_opt(recv_sock, true);
schannel = new SenderSubChannel(send_sock);
rchannel = new RecverSubChannel(recv_sock);
if (not quiet) std::cout << "connected\n";
}

int server_listen(int port) {
int mysocket;
struct sockaddr_in dest;
Expand Down Expand Up @@ -166,34 +181,12 @@ class HighSpeedNetIO: public IOChannel<HighSpeedNetIO> { public:

while (1) {
sock = socket(AF_INET, SOCK_STREAM, 0);
if (connect(sock, (struct sockaddr *)&dest, sizeof(struct sockaddr)) == 0)
break;
if (connect(sock, (struct sockaddr *)&dest, sizeof(struct sockaddr)) == 0) break;

close(sock);
usleep(1000);
}
return sock;

}
HighSpeedNetIO(const char * address, int port, bool quiet = true): quiet(quiet), port(port & 0xFFFF) {
is_server = (address == nullptr);
if (is_server) {
recv_sock = server_listen(port);
usleep(2000);
send_sock = server_listen((port+1)&0xFFFF);
}
else {
addr = string(address);
send_sock = client_connect(address, port);
recv_sock = client_connect(address, (port+1)&0xFFFF);
}
FSM = 0;
set_delay_opt(send_sock, true);
set_delay_opt(recv_sock, true);
schannel = new SenderSubChannel(send_sock);
rchannel = new RecverSubChannel(recv_sock);
if(not quiet)
std::cout << "connected\n";
}

~HighSpeedNetIO() {
Expand All @@ -209,8 +202,7 @@ class HighSpeedNetIO: public IOChannel<HighSpeedNetIO> { public:
close(recv_sock);
}

void sync() {
}
void sync() {}

void set_delay_opt(int sock, bool enable_nodelay) {
if (enable_nodelay) {
Expand Down Expand Up @@ -240,6 +232,7 @@ class HighSpeedNetIO: public IOChannel<HighSpeedNetIO> { public:
schannel->send_data(data, len);
FSM = 2;
}

void recv_data_internal(void *data, int len) {
if (FSM == 2) {
schannel->flush();
Expand All @@ -249,5 +242,5 @@ class HighSpeedNetIO: public IOChannel<HighSpeedNetIO> { public:
}
};

}
} // namespace emp
#endif
6 changes: 4 additions & 2 deletions test/garble.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "emp-tool/emp-tool.h"
#include <iostream>

#include "emp-tool/emp-tool.h"
using namespace std;
using namespace emp;

Expand Down Expand Up @@ -65,6 +66,7 @@ void test(T * netio) {
delete[] b;
delete[] c;
}

int main(int argc, char** argv) {
parse_party_and_port(argv, &party, &port);
cout << "Using NetIO\n";
Expand All @@ -73,7 +75,7 @@ int main(int argc, char** argv) {
delete netio;

cout << "Using HighSpeedNetIO\n";
HighSpeedNetIO* hsnetio = new HighSpeedNetIO(party == ALICE?nullptr:"127.0.0.1", port);
HighSpeedNetIO* hsnetio = new HighSpeedNetIO(party == ALICE ? nullptr : "127.0.0.1", port, port+1);
test<HighSpeedNetIO>(hsnetio);
delete hsnetio;
}
3 changes: 1 addition & 2 deletions test/netio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ int main(int argc, char** argv) {
delete io;

cout <<"HighSpeed NetIO\n";
HighSpeedNetIO * hsio = new HighSpeedNetIO(party == ALICE ? nullptr:"127.0.0.1", port);
HighSpeedNetIO * hsio = new HighSpeedNetIO(party == ALICE ? nullptr:"127.0.0.1", port, port+1);
test<HighSpeedNetIO>(hsio);
delete hsio;
}