starting to add new Socket interface

This commit is contained in:
Erik Frojdh
2019-01-23 11:47:13 +01:00
parent 3eca4c0535
commit 99a1c609f1
10 changed files with 212 additions and 21 deletions

View File

@ -0,0 +1,35 @@
#include "ClientSocket.h"
#include <arpa/inet.h>
#include <cstring>
#include <iostream>
#include <unistd.h>
namespace sls {
ClientSocket::ClientSocket(const std::string &host, uint16_t port) : DataSocket(socket(AF_INET, SOCK_STREAM, 0)) {
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
if (getaddrinfo(host.c_str(), NULL, &hints, &result) != 0) {
throw std::runtime_error("ClientSocket ERROR: cannot decode host\n");
}
//TODO! Erik, results could have multiple entries do we need to loop through them?
struct sockaddr_in serverAddr {};
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
memcpy((char *)&serverAddr.sin_addr.s_addr,
&((struct sockaddr_in *)result->ai_addr)->sin_addr, sizeof(in_addr_t));
if (connect(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0){
freeaddrinfo(result);
throw std::runtime_error("ClientSocket ERROR: cannot connect to host\n");
}
freeaddrinfo(result);
}
}; //namespace sls

View File

@ -0,0 +1,38 @@
#include "DataSocket.h"
#include <arpa/inet.h>
#include <cstring>
#include <iostream>
#include <unistd.h>
namespace sls {
DataSocket::DataSocket(int socketId) : socketId_(socketId) {}
size_t DataSocket::receiveData(char *buffer, size_t size) {
std::cout << "Sending\n";
size_t dataRead = 0;
while (dataRead < size) {
dataRead += read(getSocketId(), buffer + dataRead, size - dataRead);
}
return dataRead;
}
size_t DataSocket::sendData(char *buffer, size_t size) {
std::cout << "Receiving\n";
size_t dataSent = 0;
while (dataSent < size) {
dataSent += write(getSocketId(), buffer + dataSent, size - dataSent);
}
return dataSent;
}
void DataSocket::close() {
if (socketId_ > 0) {
::close(socketId_);
} else {
throw std::runtime_error("Socket ERROR: close called on bad socket\n");
}
}
} // namespace sls

View File

@ -0,0 +1,46 @@
#include "ServerSocket.h"
#include "DataSocket.h"
#include <arpa/inet.h>
#include <iostream>
#include <unistd.h>
#define DEFAULT_PACKET_SIZE 1286
#define SOCKET_BUFFER_SIZE (100 * 1024 * 1024) //100 MB
#define DEFAULT_BACKLOG 5
namespace sls {
ServerSocket::ServerSocket(int port) : DataSocket(socket(AF_INET, SOCK_STREAM, 0)) {
std::cout << "Server constructed\n";
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(getSocketId(), (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) {
close();
throw std::runtime_error("Server ERROR: cannot bind socket");
}
if (listen(getSocketId(), DEFAULT_BACKLOG) != 0) {
close();
throw std::runtime_error("Server ERROR: cannot listen to socket");
}
}
DataSocket ServerSocket::accept() {
struct sockaddr_in clientAddr;
socklen_t addr_size = sizeof clientAddr;
int newSocket = ::accept(getSocketId(), (struct sockaddr *)&clientAddr, &addr_size);
if (newSocket == -1) {
throw std::runtime_error("Server ERROR: socket accept failed\n");
}
inet_ntop(AF_INET, &(clientAddr.sin_addr), &thisClient_.front(), INET_ADDRSTRLEN);
std::cout << "lastClient: " << lastClient_ << " thisClient: " << thisClient_ << '\n';
//Here goes any check for locks etc
lastClient_ = thisClient_;
return DataSocket(newSocket);
}
}; //namespace sls