Merge win-support to testing

testing v0.4_async+win
marcelb 1 year ago
commit 9217ce91c4
  1. 13
      lib/ip.hpp
  2. 29
      lib/tcp_socket.hpp
  3. 100
      src/tcp_socket.cpp
  4. 1
      test/client.cpp
  5. BIN
      test/client.exe
  6. 1
      test/compile-client.ps1
  7. 1
      test/compile-server.ps1
  8. 10
      test/server.cpp
  9. BIN
      test/server.exe

@ -4,9 +4,16 @@
#include <iostream>
#include <string>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#if __linux__
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif _WIN32
// #include <sstream>
#include <WinSock.h>
#include <ws2tcpip.h>
#endif
using namespace std;

@ -7,12 +7,21 @@
#include <thread>
#include <mutex>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#if __linux__
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif _WIN32
#include <WinSock.h>
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#define ushort u_short
#define uint u_int
#endif
#include "ip.hpp"
using namespace std;
@ -29,7 +38,12 @@ class client;
class server {
public:
int sock;
#if __linux__
int sock;
#elif _WIN32
WSADATA wsa;
SOCKET sock;
#endif
struct sockaddr_in addr;
SSL_CTX* securefds = NULL;
vector<thread> thr;
@ -65,7 +79,12 @@ class secure {
class client {
public:
// zajedničke
int conn; // mijenja sock
#if __linux__
int conn; // mijenja sock
#elif _WIN32
WSADATA wsa;
SOCKET conn; // mijenja sock
#endif
struct sockaddr_in addr;
SSL* ssl = NULL;
// server s klijentima

@ -11,6 +11,12 @@ server::server (const ushort port, const uint queue, SSL_CTX* _securefds) {
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port);
#if _WIN32
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
throw string("[ERROR] WSA Startup. Detail: " + to_string(WSAGetLastError()));
}
#endif
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock <= 0) {
throw string("[ERROR] Unable to open TCP socket ");
@ -18,9 +24,16 @@ server::server (const ushort port, const uint queue, SSL_CTX* _securefds) {
}
int opt=1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
}
#if __linux__
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
}
#elif _WIN32
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt))) {
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
}
#endif
if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
throw string("[ERROR] Unable to bind socket ");
@ -81,10 +94,21 @@ server::~server () {
throw string("[ERROR] The socket is already closed ");
}
else if (close(sock) != 0) {
throw string("[ERROR] Unable to close socket ");
}
else {
#if __linux__
if (close(sock) != 0) {
throw string("[ERROR] Unable to close socket ");
}
#elif _WIN32
if (closesocket(sock) != 0) {
throw string("[ERROR] Unable to close socket ");
}
WSACleanup();
#endif
}
}
/**
@ -149,6 +173,12 @@ secure::~secure () {
client::client(const string address, const ushort port, const uint timeout, SSL_CTX* securefds) {
#if _WIN32
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
throw string("[ERROR] Unable to set WinSock " + to_string(WSAGetLastError()));
}
#endif
conn = socket(AF_INET, SOCK_STREAM, 0);
if (conn < 0) {
throw string("[ERROR] Unable to open TCP socket ");
@ -164,13 +194,20 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
throw string("Unable to connect to server ");
}
struct timeval tv;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
#if __linux__
struct timeval tv;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout ");
}
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout ");
}
#elif _WIN32
DWORD tv = timeout;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) {
throw string("[ERROR] Unable to set timeout ");
}
#endif
if (securefds) {
ssl = SSL_new(securefds);
@ -205,13 +242,23 @@ client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
throw string("[ERROR] Unable to accept client connection ");
}
struct timeval tv;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
#if __linux__
struct timeval tv;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout ");
}
#elif _WIN32
DWORD tv = timeout;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) {
throw string("[ERROR] Unable to set timeout ");
}
#endif
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout ");
}
if (securefds) {
@ -255,8 +302,17 @@ client::~client () {
throw string("[ERROR] The socket is already closed ");
}
else if (close(conn) != 0) {
throw string("[ERROR] Unable to close socket ");
else {
#if __linux__
if (close(conn) != 0) {
throw string("[ERROR] Unable to close socket ");
}
#elif _WIN32
if (closesocket(conn) != 0) {
throw string("[ERROR] Unable to close socket ");
}
//WSACleanup();
#endif
}
}
@ -274,7 +330,7 @@ bool client::push (const string msg) {
sended = SSL_write(ssl, msg.c_str(), msg.length());
}
else {
sended = write(conn, msg.c_str(), msg.length());
sended = send(conn, msg.c_str(), msg.length(), 0);
}
return sended == msg.length();
}
@ -292,7 +348,7 @@ string client::pull (size_t byte_limit) {
SSL_read(ssl, res, byte_limit);
}
else {
read(conn , res, byte_limit);
recv(conn , res, byte_limit, 0);
}
return string(res);

@ -29,6 +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", 5000);
// cout << "init client " << endl;

Binary file not shown.

@ -0,0 +1 @@
g++ client.cpp ../src/* -o client.exe -lssl -lcrypto -lws2_32

@ -0,0 +1 @@
g++ server.cpp ../src/* -o server.exe -lssl -lcrypto -lws2_32

@ -10,6 +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 client " << endl;
@ -25,6 +29,12 @@ int main() {
// // usleep(600*1000);
// sleep(5);
// myserver.cli->push(fromclient);
string fromclient = myserver.cli->pull();
// string fromclient = myclient.pull();
cout << "tell client " << fromclient << endl;
// usleep(600*1000);
//sleep(5);
myserver.cli->push(fromclient);
// myclient.push(fromclient);
// myclient.~comming();

Binary file not shown.
Loading…
Cancel
Save