Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	websockets/websockets.cpp
  • Loading branch information
marcelboldt committed Mar 6, 2017
2 parents 4b9495e + c360739 commit 89a05c2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 16 deletions.
26 changes: 14 additions & 12 deletions websockets/websockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Websockets_connection::Websockets_connection(const char *ip, uint16_t port, cons
{ /* Creates socket and initialises a Websocket connection to a remote server.
Returns: A Websockets_connection object */


s = INVALID_SOCKET;
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
Expand All @@ -58,10 +59,12 @@ Websockets_connection::Websockets_connection(const char *ip, uint16_t port, cons
strcat(message, key_b64.c_str());
strcat(message, "\r\n\r\n");
#ifdef _WIN32
throw std::runtime_error("WIN32");
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Winsock startup failed. Error Code : %d\n", WSAGetLastError());
throw winsock_startup_error();
}
#endif

Expand All @@ -74,8 +77,9 @@ Websockets_connection::Websockets_connection(const char *ip, uint16_t port, cons
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
#else
printf("Error at socket(): %ld\n", errno);
printf("Error at socket(): %d\n", errno);
#endif
throw socket_create_error();
}


Expand All @@ -87,24 +91,23 @@ Websockets_connection::Websockets_connection(const char *ip, uint16_t port, cons

if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0)
{
throw "WEBSOCKETS_CONNECTION: connect error";
throw socket_connect_error();
}


// send data


if (send(s, message, (int)strlen(message), 0) == SOCKET_ERROR)
{
throw "Send failed";
throw socket_send_error();
}

std::this_thread::sleep_for(std::chrono::milliseconds(RECV_DELAY));

// Receive a reply from the server
if ((recv(s, server_reply, BUFFER_SIZE, 0)) == SOCKET_ERROR)
{
throw "recv failed";
throw socket_recv_error();
}

this->CONNECTED = true; // TODO: parse the server reply
Expand All @@ -121,8 +124,8 @@ Websockets_connection::~Websockets_connection()

int Websockets_connection::send_data(const char *data, size_t length, uint8_t oc)
{/* Sends the data given over the open Websockets connection.
Returns: an the number of frames sent if successful, otherwise -1.
Operation codes:
Returns: an the number of frames sent if successful, otherwise -1.
Operation codes:
* 0 : continuation
* 1 : text data
* 2 : binary data
Expand Down Expand Up @@ -400,8 +403,7 @@ Websockets_frame::Websockets_frame(int socket, const char *filename) {
tfile.open(fn, std::ios::app | std::ios::binary);
break;
default:
// std::cout << "ERROR: Unknown opcode:" << this->OPCODE << std::endl;
throw "UNKNOWN_OPCODE";
throw ws_unknown_opcode();
}

this->PAYLOAD = fn;
Expand All @@ -421,7 +423,7 @@ Websockets_frame::Websockets_frame(int socket, const char *filename) {
//TODO: add a timeout
std::this_thread::sleep_for(std::chrono::milliseconds(RECV_DELAY));
recv_len = recv(socket, data, BUFFER_SIZE, 0);
if (recv_len == 0) throw "Socket closed while receiving websockets frame";
if (recv_len == 0) throw socket_unexp_close();
for (auto i = 0; i < recv_len; i++) {
tfile << (*(data + i) ^ *(masking_key + ((write_len + i) % 4))); // unmask and write to file
}
Expand All @@ -436,7 +438,7 @@ Websockets_frame::Websockets_frame(int socket, const char *filename) {
while (write_len < this->PAYLOAD_LENGTH) {
std::this_thread::sleep_for(std::chrono::milliseconds(RECV_DELAY));
recv_len = recv(socket, data, BUFFER_SIZE, 0);
if (recv_len == 0) throw "Socket closed while receiving websockets frame";
if (recv_len == 0) throw socket_unexp_close();
for (auto i = 0; i < recv_len; i++) {
tfile << *(data + i); // write to file
}
Expand Down Expand Up @@ -522,7 +524,7 @@ bool Websockets_frame::payload_file() const {
Websockets_frame::~Websockets_frame() {

if (this->PAYLOAD_FILE) {
// if (remove(this->PAYLOAD) != 0) throw "tempfile couldn't be deleted";
if (remove(this->PAYLOAD) != 0) throw ws_tempfile_delete();
}
}

Expand Down
64 changes: 60 additions & 4 deletions websockets/websockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Marcel Boldt <marcel.boldt@exasol.com>
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<exception>

#ifdef _WIN32
#define _WINSOCK_DEPRECATED_NO_WARNINGS
Expand All @@ -46,15 +47,16 @@ Marcel Boldt <marcel.boldt@exasol.com>
#include <sys/socket.h>
#include<arpa/inet.h>
// see http://stackoverflow.com/questions/3022552/is-there-any-standard-htonl-like-function-for-64-bits-integers-in-c
#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))

#define INVALID_SOCKET -1
#define SOCKET_ERROR -1

#include<errno.h>

#endif

#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))

#include <thread> // std::this_thread::
#include <ctime>
#include <chrono> // std::chrono::seconds
Expand All @@ -71,11 +73,65 @@ Marcel Boldt <marcel.boldt@exasol.com>

// todo: send stream

struct socket_connect_error : public std::exception {
const char * what() const throw () {
return "Error connecting to socket";
}
};

struct socket_send_error : public std::exception {
const char * what() const throw () {
return "Error sending via socket";
}
};

struct socket_recv_error : public std::exception {
const char * what() const throw () {
return "Error receiving from socket";
}
};

struct ws_unknown_opcode : public std::exception {
const char * what() const throw () {
return "Error: websockets frame with unknown opcode received";
}
};

struct socket_unexp_close : public std::exception {
const char * what() const throw () {
return "Error: socket closed unexpectedly";
}
};

struct ws_tempfile_delete : public std::exception {
const char * what() const throw () {
return "Error: could not remove tempfile";
}
};

struct socket_create_error : public std::exception {
const char * what() const throw () {
return "Error: could not create socket";
}
};

struct winsock_startup_error : public std::exception {
const char * what() const throw () {
return "Error: winsock could not be started";
}
};

class Websockets_frame;

class Websockets_connection {
public:
static void write_msg_to_file(std::string msg, std::string name = "errorfile.txt") {
std::ofstream outfile;
outfile.open(name, std::ios::app);
outfile << "Error: " << msg << std::endl;
outfile.close();
};

Websockets_connection(const char *server, uint16_t port, const char *host);
~Websockets_connection();

Expand Down Expand Up @@ -153,4 +209,4 @@ class Websockets_frame {

};

#endif /* websockets.h */
#endif /* websockets.h */

0 comments on commit 89a05c2

Please sign in to comment.