Advertisement
nefton

Untitled

Oct 22nd, 2020
1,015
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Client server protocol V1 by nefton. (feel free to use in any way)
  2. //First 8 bytes of a message (consist from a packets) = magic * size_of_message
  3. //magic - any number (good number will be 77546)
  4.  
  5. //NetworkClient.h
  6. #pragma once
  7.  
  8. #include <string>
  9. #include <stdint.h>
  10.  
  11.  
  12. class NetworkClient {
  13. public:
  14.     NetworkClient();
  15.     static void WSAStart(std::string* err_str);
  16.     static void WSAStop(std::string* err_str);
  17.     void CreateClientSocket(std::string* err_str);
  18.     void Connect(std::string server_ip, int port, std::string* err_str);
  19.     void SendMessageToServer(std::string message, int magic, std::string* err_str);
  20.     void ReceiveMessageBack(int max_message_size, std::string* message, int magic, std::string* err_str);
  21.     void CloseConnection();
  22.  
  23. private:
  24.     int64_t client_socket;
  25.     int32_t GetMessageSize(int magic, char* Buff);
  26.     void AddMessageSize(int magic, std::string* message);
  27. };
  28.  
  29. //NetworkClient_windows.cpp
  30. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  31.  
  32. #include <winsock2.h>
  33. #include <Windows.h>
  34. #pragma comment(lib, "Ws2_32.lib")
  35. #include <iostream>;
  36.  
  37. #include "NetworkClient.h"
  38.  
  39.  
  40. NetworkClient::NetworkClient() {
  41.     client_socket = 0;
  42.     return;
  43. }
  44.  
  45. void NetworkClient::WSAStart(std::string* err_str) {
  46.     if (err_str != nullptr) *err_str = "";
  47.     WSADATA ws;
  48.     if (WSAStartup(MAKEWORD(2, 2), &ws) != 0) {
  49.         if (err_str != nullptr) *err_str = "WSAStartup FAILED";
  50.     }
  51.     return;
  52. }
  53. void NetworkClient::WSAStop(std::string* err_str) {
  54.     if (err_str != nullptr) *err_str = "";
  55.     if (WSACleanup() < 0) {
  56.         if (err_str != nullptr) *err_str = "WSACleanup FAILED";
  57.     }
  58.     return;
  59. }
  60.  
  61.  
  62. void NetworkClient::CreateClientSocket(std::string* err_str) {
  63.     *err_str = "";
  64.     client_socket = (int64_t)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  65.     if (client_socket == INVALID_SOCKET) {
  66.         *err_str = "client_socket = INVALID_SOCKET";
  67.         return;
  68.     }
  69.     return;
  70. }
  71.  
  72. void NetworkClient::Connect(std::string server_ip, int port, std::string* err_str) {
  73.  
  74.     *err_str = "";
  75.     sockaddr_in dest_addr;
  76.     ZeroMemory(&dest_addr, sizeof(dest_addr));
  77.     dest_addr.sin_family = AF_INET;
  78.     dest_addr.sin_addr.S_un.S_addr = inet_addr(server_ip.c_str());
  79.     dest_addr.sin_port = htons(port);
  80.  
  81.     if (connect((SOCKET)client_socket, (sockaddr *)&dest_addr, sizeof(dest_addr)) == SOCKET_ERROR) {
  82.         *err_str = "connect = SOCKET_ERROR";
  83.     }
  84.     return;
  85. }
  86.  
  87.  
  88. void NetworkClient::SendMessageToServer(std::string message, int magic, std::string* err_str) {
  89.     *err_str = "";
  90.     //std::cout << message.size() << "\n";
  91.     AddMessageSize(magic, &message);
  92.     //std::cout << (int)message[0] << "|" << (int)message[1] << "|" << (int)message[2] << "|" << (int)message[3] << "\n";
  93.     //std::cout << (int)message[4] << "|" << (int)message[5] << "|" << (int)message[6] << "|" << (int)message[7] << "\n";
  94.     //Send message
  95.     int sent_bytes = send((SOCKET)client_socket, message.data(), message.size(), 0);
  96.     if (sent_bytes != message.size()) {
  97.         *err_str = "sent_bytes != message.size()";
  98.     }
  99.     return;
  100. }
  101.  
  102. void NetworkClient::ReceiveMessageBack(int max_message_size, std::string* message, int magic, std::string* err_str) {
  103.     *err_str = "";
  104.     *message = "";
  105.     char* Buff = new char[max_message_size];
  106.     char* buff_pointer = Buff;
  107.     int recive_size = 0;
  108.     int32_t expected_size = -1;
  109.     bool fail_flag = false;
  110.     while (true) {
  111.         int packet_size = recv((SOCKET)client_socket, buff_pointer, max_message_size - recive_size, 0);
  112.         if (packet_size == SOCKET_ERROR) {
  113.             *err_str = "recv() = SOCKET_ERROR.";
  114.             fail_flag = true;
  115.             break;
  116.         }
  117.         if (packet_size == 0) {
  118.             break;
  119.         }
  120.         expected_size = GetMessageSize(magic, Buff);
  121.         if (expected_size == -1) {
  122.             fail_flag = true;
  123.             *err_str = "wrong expected message size.";
  124.             break;
  125.         }
  126.         buff_pointer += packet_size;
  127.         recive_size += packet_size;
  128.         if (recive_size >= expected_size) break;
  129.     }
  130.  
  131.     if ((fail_flag == true) || (expected_size == -1) || (recive_size != expected_size)) {
  132.         if (recive_size != expected_size) *err_str += "recive_size != expected_size";
  133.         delete[] Buff;
  134.         return;
  135.     }
  136.  
  137.     if (recive_size >= 8) *message = std::string(Buff + 8, recive_size - 8);
  138.     delete[] Buff;
  139.     return;
  140. }
  141.  
  142.  
  143. void NetworkClient::AddMessageSize(int magic, std::string* message) {
  144.     uint64_t size = (message->size() + 8) * magic;
  145.     std::string prefix = "01234567";
  146.     char* p = reinterpret_cast<char*>(&size);
  147.     for (int i = 0; i < 8; i++) prefix[i] = p[i];
  148.     *message = prefix + *message;
  149.     return;
  150. }
  151.  
  152. int32_t NetworkClient::GetMessageSize(int magic, char* buff) {
  153.     uint64_t* size = reinterpret_cast<uint64_t*>(buff);
  154.     if (*size % magic != 0) return -1;
  155.     return *size / magic;
  156. }
  157.  
  158. void NetworkClient::CloseConnection() {
  159.     closesocket(client_socket);
  160.     client_socket = 0;
  161.     return;
  162. }
  163.  
  164. //NetworkServer.h
  165. #pragma once
  166.  
  167. #include <string>
  168. #include <stdint.h>
  169.  
  170.  
  171. class NetworkServer {
  172. public:
  173.     NetworkServer();
  174.     static void WSAStart(std::string* err_str);
  175.     static void WSAStop(std::string* err_str);
  176.     void CreateServerSocket(std::string* err_str);
  177.     void Bind(int port, std::string interface_str, std::string* err_str);
  178.     void StartListen(int max_queue, std::string* err_str);
  179.     void Accept(std::string* client_ip, std::string* err_str);
  180.     void ReceiveMessage(int max_message_size, std::string* message, int magic, std::string* err_str);
  181.     void SendMessageBack(std::string message, int magic, std::string* err_str);
  182.     void CloseConnection();
  183.     void CloseServerSocket();
  184.    
  185. //private:
  186.     int64_t server_socket;
  187.     int64_t new_connection_socket;
  188.     int32_t GetMessageSize(int magic, char* Buff);
  189.     void AddMessageSize(int magic, std::string* message);
  190.  
  191.  
  192. };
  193. //NetworkServer_linux.cpp
  194. #include <stdio.h>
  195. #include <stdlib.h>
  196. #include <unistd.h>
  197. #include <string.h>
  198. #include <sys/types.h>
  199. #include <sys/socket.h>
  200. #include <netinet/in.h>
  201. #include <arpa/inet.h>
  202.  
  203. #include <iostream>
  204.  
  205. #include "NetworkServer.h"
  206.  
  207.  
  208. NetworkServer::NetworkServer() {
  209.     server_socket = 0;
  210.     new_connection_socket = 0;
  211.     return;
  212. }
  213. void NetworkServer::CloseServerSocket() {
  214.     close(server_socket);
  215.     server_socket = 0;
  216.     return;
  217. }
  218.  
  219. void NetworkServer::CreateServerSocket(std::string* err_str) {
  220.     *err_str = "";
  221.     server_socket = (int64_t)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  222.     if (server_socket < 0) {
  223.         *err_str = "server_socket = INVALID_SOCKET";
  224.         return;
  225.     }
  226.     return;
  227. }
  228.  
  229. void NetworkServer::Bind(int port, std::string interface_str, std::string* err_str) {
  230.  
  231.     *err_str = "";
  232.     struct sockaddr_in serv_addr;
  233.     bzero((char *)&serv_addr, sizeof(serv_addr));
  234.    
  235.     serv_addr.sin_family = AF_INET;
  236.     if (interface_str == "any") {
  237.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  238.     }
  239.     else {
  240.         serv_addr.sin_addr.s_addr = inet_addr(interface_str.c_str());
  241.     }
  242.     serv_addr.sin_port = htons(port);
  243.  
  244.     if (bind((int)server_socket, (sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
  245.         *err_str = "bind = SOCKET_ERROR";
  246.     }
  247.     return;
  248. }
  249.  
  250. void NetworkServer::StartListen(int max_queue, std::string* err_str) {
  251.     *err_str = "";
  252.     if (listen((int)server_socket, max_queue) < 0) {
  253.         *err_str = "listem = ERROR";
  254.     }
  255.     return;
  256. }
  257.  
  258. void NetworkServer::Accept(std::string* client_ip, std::string* err_str) {
  259.     *err_str = "";
  260.     new_connection_socket = 0;
  261.  
  262.     sockaddr_in remote_addr;
  263.     socklen_t remote_addr_len = sizeof(sockaddr_in);
  264.     bzero((char *)&remote_addr, sizeof(remote_addr));
  265.  
  266.     new_connection_socket = (uint64_t)accept((int)server_socket, (sockaddr*)&remote_addr, &remote_addr_len);
  267.  
  268.     if (new_connection_socket < 0) {
  269.         *err_str = "new_connection_socket = INVALID_SOCKET";
  270.         new_connection_socket = 0;
  271.     }
  272.  
  273.     *client_ip = std::string(inet_ntoa(remote_addr.sin_addr));
  274.  
  275.     return;
  276. }
  277.  
  278. void NetworkServer::ReceiveMessage(int max_message_size, std::string* message, int magic, std::string* err_str) {
  279.     *err_str = "";
  280.     *message = "";
  281.     char* Buff = new char[max_message_size];
  282.     char* buff_pointer = Buff;
  283.     int recive_size = 0;
  284.     int32_t expected_size = -1;
  285.     bool fail_flag = false;
  286.     while (true) {
  287.         int packet_size = recv((int)new_connection_socket, buff_pointer, max_message_size - recive_size, 0);
  288.         if (packet_size < 0) {
  289.             *err_str = "recv() = SOCKET_ERROR.";
  290.             fail_flag = true;
  291.             break;
  292.         }
  293.         if (packet_size == 0) {
  294.             *err_str = "recv() = 0 (client close connection).";
  295.             fail_flag = true;
  296.             break;
  297.         }
  298.         expected_size = GetMessageSize(magic, Buff);
  299.         if (expected_size == -1) {
  300.             *err_str = "wrong expected message size. packet_size = " + std::to_string(packet_size);
  301.             *message = std::string(Buff, packet_size); //interesting what stuff they send for me
  302.             fail_flag = true;
  303.             break;
  304.         }
  305.         buff_pointer += packet_size;
  306.         recive_size += packet_size;
  307.         if (recive_size >= expected_size) break;
  308.     }
  309.  
  310.     if (fail_flag == true) {
  311.         delete[] Buff;
  312.         return;
  313.     }
  314.  
  315.     if (recive_size >= 8) *message = std::string(Buff + 8, recive_size - 8);
  316.     delete[] Buff;
  317.     return;
  318. }
  319.  
  320.  
  321. void NetworkServer::SendMessageBack(std::string message, int magic, std::string* err_str) {
  322.     *err_str = "";
  323.     AddMessageSize(magic, &message);
  324.  
  325.     //Send answer
  326.     int sent_bytes = send((int)new_connection_socket, message.c_str(), message.size(), 0);
  327.     if (sent_bytes != message.size()) {
  328.         *err_str = "sent_bytes != message.size()";
  329.     }
  330.     return;
  331. }
  332.  
  333.  
  334. void NetworkServer::AddMessageSize(int magic, std::string* message) {
  335.     uint64_t size = (message->size() + 8) * magic;
  336.     std::string prefix = "01234567";
  337.     char* p = reinterpret_cast<char*>(&size);
  338.     for (int i = 0; i < 8; i++) prefix[i] = p[i];
  339.     *message = prefix + *message;
  340.     return;
  341. }
  342.  
  343. int32_t NetworkServer::GetMessageSize(int magic, char* buff) {
  344.     uint64_t* size = reinterpret_cast<uint64_t*>(buff);
  345.     if (*size % magic != 0) return -1;
  346.     return *size / magic;
  347. }
  348.  
  349. void NetworkServer::CloseConnection() {
  350.     close(new_connection_socket);
  351.     new_connection_socket = 0;
  352.     return;
  353. }
  354.  
  355. //NetworkServer_windows.cpp
  356. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  357.  
  358.  
  359. #include <winsock2.h>
  360. #include <Windows.h>
  361. #pragma comment(lib, "Ws2_32.lib")
  362. #include <iostream>;
  363.  
  364.  
  365. #include "NetworkServer.h"
  366.  
  367.  
  368. NetworkServer::NetworkServer() {
  369.     server_socket = 0;
  370.     new_connection_socket = 0;
  371.     return;
  372. }
  373.  
  374. void NetworkServer::WSAStart(std::string* err_str) {
  375.     if (err_str != nullptr) *err_str = "";
  376.     WSADATA ws;
  377.     if (WSAStartup(MAKEWORD(2, 2), &ws) != 0) {
  378.         if (err_str != nullptr) *err_str = "WSAStartup FAILED";
  379.     }
  380.     return;
  381. }
  382. void NetworkServer::WSAStop(std::string* err_str) {
  383.     if (err_str != nullptr) *err_str = "";
  384.     if (WSACleanup() < 0) {
  385.         if (err_str != nullptr) *err_str = "WSACleanup FAILED";
  386.     }
  387.     return;
  388. }
  389. void NetworkServer::CloseServerSocket() {
  390.     closesocket((SOCKET)server_socket);
  391.     server_socket = 0;
  392.     return;
  393. }
  394.  
  395. void NetworkServer::CreateServerSocket(std::string* err_str) {
  396.     *err_str = "";
  397.     server_socket = (int64_t)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  398.     if (server_socket == INVALID_SOCKET) {
  399.         *err_str = "server_socket = INVALID_SOCKET";
  400.         return;
  401.     }
  402.     return;
  403. }
  404.  
  405. void NetworkServer::Bind(int port, std::string interface_str, std::string* err_str) {
  406.  
  407.     *err_str = "";
  408.     sockaddr_in addr;
  409.     ZeroMemory(&addr, sizeof(addr));
  410.     addr.sin_family = AF_INET;
  411.     if (interface_str == "any") {
  412.         addr.sin_addr.S_un.S_addr = INADDR_ANY;
  413.     }
  414.     else {
  415.         addr.sin_addr.S_un.S_addr = inet_addr(interface_str.c_str());
  416.     }
  417.     addr.sin_port = htons(port);
  418.  
  419.     if (bind((SOCKET)server_socket, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {
  420.         *err_str = "bind = SOCKET_ERROR";
  421.     }
  422.     return;
  423. }
  424.  
  425. void NetworkServer::StartListen(int max_queue, std::string* err_str) {
  426.     *err_str = "";
  427.     if (listen((SOCKET)server_socket, max_queue) != 0) {
  428.         *err_str = "listem = ERROR";
  429.     }
  430.     return;
  431. }
  432.  
  433. void NetworkServer::Accept(std::string* client_ip, std::string* err_str) {
  434.     *err_str = "";
  435.     new_connection_socket = 0;
  436.  
  437.     sockaddr_in remote_addr;
  438.     int remote_addr_len = sizeof(sockaddr_in);
  439.     ZeroMemory(&remote_addr, sizeof(sockaddr_in));
  440.  
  441.     new_connection_socket = (uint64_t)accept((SOCKET)server_socket, (sockaddr*)&remote_addr, &remote_addr_len);
  442.  
  443.     if (new_connection_socket == INVALID_SOCKET) {
  444.         *err_str = "new_connection_socket = INVALID_SOCKET";
  445.         new_connection_socket = 0;
  446.     }
  447.  
  448.     *client_ip = std::string(inet_ntoa(remote_addr.sin_addr));
  449.  
  450.     return;
  451. }
  452.  
  453. void NetworkServer::ReceiveMessage(int max_message_size, std::string* message, int magic, std::string* err_str) {
  454.     *err_str = "";
  455.     *message = "";
  456.     char* Buff = new char[max_message_size];
  457.     char* buff_pointer = Buff;
  458.     int recive_size = 0;
  459.     int32_t expected_size = -1;
  460.     int packet_size = 0;
  461.     bool fail_flag = false;
  462.     while (true) {
  463.         packet_size = recv((SOCKET)new_connection_socket, buff_pointer, max_message_size - recive_size, 0);
  464.         if (packet_size == SOCKET_ERROR) {
  465.             *err_str = "recv() = SOCKET_ERROR.";
  466.             fail_flag = true;
  467.             break;
  468.         }
  469.         if (packet_size == 0) {
  470.             *err_str = "recv() = 0 (client close connection).";
  471.             fail_flag = true;
  472.             break;
  473.         }
  474.         expected_size = GetMessageSize(magic, Buff);
  475.         if (expected_size == -1) {
  476.             *err_str = "wrong expected message size. packet_size = " + std::to_string(packet_size);
  477.             *message = std::string(Buff, packet_size); //interesting what stuff they send for me
  478.             fail_flag = true;
  479.             break;
  480.         }
  481.         buff_pointer += packet_size;
  482.         recive_size += packet_size;
  483.         if (recive_size >= expected_size) break;
  484.     }
  485.  
  486.     if (fail_flag == true) {
  487.         delete[] Buff;
  488.         return;
  489.     }
  490.  
  491.     if (recive_size >= 8) *message = std::string(Buff + 8, recive_size - 8);
  492.     delete[] Buff;
  493.     return;
  494. }
  495.  
  496.  
  497. void NetworkServer::SendMessageBack(std::string message, int magic, std::string* err_str) {
  498.     *err_str = "";
  499.     AddMessageSize(magic, &message);
  500.    
  501.     //Send answer
  502.     int sent_bytes = send((SOCKET)new_connection_socket, message.c_str(), message.size(), 0);
  503.     if (sent_bytes != message.size()) {
  504.         *err_str = "sent_bytes != message.size()";
  505.     }
  506.     return;
  507. }
  508.  
  509.  
  510. void NetworkServer::AddMessageSize(int magic, std::string* message) {
  511.     uint64_t size = (message->size() + 8) * magic;
  512.     std::string prefix = "01234567";
  513.     char* p = reinterpret_cast<char*>(&size);
  514.     for (int i = 0; i < 8; i++) prefix[i] = p[i];
  515.     *message = prefix + *message;
  516.     return;
  517. }
  518.  
  519. int32_t NetworkServer::GetMessageSize(int magic, char* buff) {
  520.     uint64_t* size = reinterpret_cast<uint64_t*>(buff);
  521.     if (*size % magic != 0) return -1;
  522.     return *size / magic;
  523. }
  524.  
  525. void NetworkServer::CloseConnection() {
  526.     closesocket(new_connection_socket);
  527.     new_connection_socket = 0;
  528.     return;
  529. }
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
Advertisement
RAW Paste Data Copied
Advertisement