|
@@ -1,5 +1,7 @@
|
|
|
#include "socketio.hpp"
|
|
|
#include <stdio.h>
|
|
|
+#include <cstdint>
|
|
|
+#include <cinttypes>
|
|
|
#include <winsock2.h>
|
|
|
#include <ws2tcpip.h>
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
@@ -8,121 +10,133 @@
|
|
|
#include <vector>
|
|
|
#include <iostream>
|
|
|
|
|
|
+#ifdef _MSC_VER
|
|
|
+#undef min
|
|
|
+using ssize_t = int64_t;
|
|
|
+template<typename T>
|
|
|
+const T& min(const T& a,const T& b){
|
|
|
+ return (b < a) ? b : a;
|
|
|
+}
|
|
|
+#else
|
|
|
+using std::min;
|
|
|
+#endif
|
|
|
+
|
|
|
using std::size_t;
|
|
|
-struct socket_exception : public std::exception{
|
|
|
+
|
|
|
+struct socket_exception : public std::exception {
|
|
|
std::string msg;
|
|
|
- socket_exception(std::string&& _msg){
|
|
|
+ socket_exception(std::string&& _msg) {
|
|
|
msg = std::move(_msg);
|
|
|
}
|
|
|
- socket_exception(const std::string& _msg){
|
|
|
+ socket_exception(const std::string& _msg) {
|
|
|
msg = _msg;
|
|
|
}
|
|
|
- socket_exception(const char* _msg){
|
|
|
+ socket_exception(const char* _msg) {
|
|
|
msg = std::string(_msg);
|
|
|
}
|
|
|
- const char* what() const throw (){
|
|
|
+ const char* what() const throw () {
|
|
|
//return "asdasdsa";
|
|
|
- return msg.length() == 0 ? "Connection creation failure" : msg.c_str();
|
|
|
- }
|
|
|
+ return msg.length() == 0 ? "Connection creation failure" : msg.c_str();
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
-std::string GetLastErrorAsString(){
|
|
|
- //Get the error message, if any.
|
|
|
- DWORD errorMessageID = ::GetLastError();
|
|
|
- if(errorMessageID == 0)
|
|
|
- return std::string();
|
|
|
+std::string GetLastErrorAsString() {
|
|
|
+ //Get the error message, if any.
|
|
|
+ DWORD errorMessageID = ::GetLastError();
|
|
|
+ if (errorMessageID == 0)
|
|
|
+ return std::string();
|
|
|
|
|
|
- LPSTR messageBuffer = nullptr;
|
|
|
- size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
- NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
|
|
|
+ LPSTR messageBuffer = nullptr;
|
|
|
+ size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
+ NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
|
|
|
|
|
|
- std::string message(messageBuffer, size);
|
|
|
+ std::string message(messageBuffer, size);
|
|
|
|
|
|
- //Free the buffer.
|
|
|
- LocalFree(messageBuffer);
|
|
|
+ //Free the buffer.
|
|
|
+ LocalFree(messageBuffer);
|
|
|
|
|
|
- return message;
|
|
|
+ return message;
|
|
|
}
|
|
|
|
|
|
-cppsocket::cppsocket(){
|
|
|
+cppsocket::cppsocket() {
|
|
|
buffer = std::vector<char>(buffersize + 1, 0);
|
|
|
}
|
|
|
-cppsocket::cppsocket(cppsocket&& o){
|
|
|
+cppsocket::cppsocket(cppsocket&& o) {
|
|
|
s = o.s;
|
|
|
o.s = INVALID_SOCKET;
|
|
|
buffer = std::move(o.buffer);
|
|
|
}
|
|
|
-cppsocket::cppsocket(WSADATA d, SOCKET _s){
|
|
|
+cppsocket::cppsocket(WSADATA d, SOCKET _s) {
|
|
|
s = _s;
|
|
|
wsa = d;
|
|
|
buffer = std::vector<char>(buffersize + 1, 0);
|
|
|
}
|
|
|
-cppsocket::cppsocket(const std::string& addr, unsigned int PORT){
|
|
|
+cppsocket::cppsocket(const std::string& addr, unsigned int PORT) {
|
|
|
struct sockaddr_in server;
|
|
|
buffer = std::vector<char>(buffersize + 1, 0);
|
|
|
int recv_size;
|
|
|
-
|
|
|
- if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
|
|
|
+
|
|
|
+ if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
|
|
|
throw socket_exception(std::string("Socket creation error: ") + GetLastErrorAsString());
|
|
|
}
|
|
|
-
|
|
|
- if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET){
|
|
|
+
|
|
|
+ if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
|
|
|
throw socket_exception(std::string("Socket creation error: ") + GetLastErrorAsString());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
server.sin_addr.s_addr = inet_addr(addr.c_str());
|
|
|
server.sin_family = AF_INET;
|
|
|
server.sin_port = htons(PORT);
|
|
|
|
|
|
- if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0){
|
|
|
+ if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
|
|
throw socket_exception(std::string("Could not connect to host: ") + GetLastErrorAsString());
|
|
|
}
|
|
|
}
|
|
|
-void cppsocket::write(const std::string& message){
|
|
|
+void cppsocket::write(const std::string& message) {
|
|
|
std::vector<char> msg(message.c_str(), message.c_str() + message.size());
|
|
|
write(msg);
|
|
|
}
|
|
|
-void cppsocket::write(const std::vector<char>& message){
|
|
|
- for(size_t i = 0;i < message.size();i += buffersize){
|
|
|
- char cs[buffersize + 1] = {0};
|
|
|
- std::memcpy(cs, message.data() + i,buffersize);
|
|
|
- if((i + buffersize) < message.size()){
|
|
|
+void cppsocket::write(const std::vector<char>& message) {
|
|
|
+ for (size_t i = 0; i < message.size(); i += buffersize) {
|
|
|
+ char cs[buffersize + 1] = { 0 };
|
|
|
+ std::memcpy(cs, message.data() + i, buffersize);
|
|
|
+ if ((i + buffersize) < message.size()) {
|
|
|
cs[buffersize] = 'c';
|
|
|
- if(send(s, cs, buffersize + 1, 0) < 0){
|
|
|
+ if (send(s, cs, buffersize + 1, 0) < 0) {
|
|
|
throw socket_exception(std::string("Couldn't write to peer: ") + GetLastErrorAsString());
|
|
|
}
|
|
|
}
|
|
|
- else{
|
|
|
+ else {
|
|
|
cs[message.size() - i] = (char)0;
|
|
|
- if(send(s, cs, message.size() - i, 0) < 0){
|
|
|
+ if (send(s, cs, message.size() - i, 0) < 0) {
|
|
|
throw socket_exception(std::string("Couldn't write to peer: ") + GetLastErrorAsString());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-std::vector<char> cppsocket::receive(){
|
|
|
+std::vector<char> cppsocket::receive() {
|
|
|
std::vector<char> stor;
|
|
|
- while(true){
|
|
|
+ while (true) {
|
|
|
std::fill(buffer.begin(), buffer.end(), (char)0);
|
|
|
- ssize_t val = recv(s, buffer.data(), buffersize + 1,0);
|
|
|
- if(val == 0)throw socket_exception("Connection closed by peer");
|
|
|
- if(val < 0){
|
|
|
+ ssize_t val = recv(s, buffer.data(), buffersize + 1, 0);
|
|
|
+ if (val == 0)throw socket_exception("Connection closed by peer");
|
|
|
+ if (val < 0) {
|
|
|
throw socket_exception(GetLastErrorAsString());
|
|
|
}
|
|
|
- stor.insert(stor.end(), buffer.begin(), buffer.begin() + std::min(val, (ssize_t)buffersize));
|
|
|
- if(buffer.data()[buffersize] == (char)0){break;}
|
|
|
+ stor.insert(stor.end(), buffer.begin(), buffer.begin() + min(val, (ssize_t)buffersize));
|
|
|
+ if (buffer.data()[buffersize] == (char)0) { break; }
|
|
|
}
|
|
|
std::cout << std::endl;
|
|
|
return stor;
|
|
|
}
|
|
|
-void cppsocket::close(){
|
|
|
+void cppsocket::close() {
|
|
|
closesocket(s);
|
|
|
}
|
|
|
|
|
|
-server_socket::server_socket(int port){
|
|
|
+server_socket::server_socket(int port) {
|
|
|
struct addrinfo *result = NULL;
|
|
|
struct addrinfo hints;
|
|
|
- iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
|
|
|
+ iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
|
|
if (iResult != 0) {
|
|
|
printf("WSAStartup failed with error: %d\n", iResult);
|
|
|
}
|
|
@@ -134,7 +148,7 @@ server_socket::server_socket(int port){
|
|
|
|
|
|
// Resolve the server address and port
|
|
|
iResult = getaddrinfo(NULL, std::to_string(port).c_str(), &hints, &result);
|
|
|
- if ( iResult != 0 ) {
|
|
|
+ if (iResult != 0) {
|
|
|
printf("getaddrinfo failed with error: %d\n", iResult);
|
|
|
WSACleanup();
|
|
|
}
|
|
@@ -148,7 +162,7 @@ server_socket::server_socket(int port){
|
|
|
}
|
|
|
|
|
|
// Setup the TCP listening socket
|
|
|
- iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
|
|
|
+ iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
|
|
|
if (iResult == SOCKET_ERROR) {
|
|
|
printf("bind failed with error: %d\n", WSAGetLastError());
|
|
|
freeaddrinfo(result);
|
|
@@ -163,7 +177,7 @@ server_socket::server_socket(int port){
|
|
|
WSACleanup();
|
|
|
}
|
|
|
}
|
|
|
-cppsocket server_socket::accept_connection(){
|
|
|
+cppsocket server_socket::accept_connection() {
|
|
|
SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
|
|
|
if (ClientSocket == INVALID_SOCKET) {
|
|
|
printf("accept failed with error: %d\n", WSAGetLastError());
|
|
@@ -172,10 +186,10 @@ cppsocket server_socket::accept_connection(){
|
|
|
}
|
|
|
return cppsocket(wsaData, ClientSocket);
|
|
|
}
|
|
|
-void server_socket::close(){
|
|
|
+void server_socket::close() {
|
|
|
closesocket(ListenSocket);
|
|
|
}
|
|
|
-cppsocket& cppsocket::operator=(cppsocket&& o){
|
|
|
+cppsocket& cppsocket::operator=(cppsocket&& o) {
|
|
|
s = o.s;
|
|
|
o.s = INVALID_SOCKET;
|
|
|
buffer = std::move(o.buffer);
|