#include "stdafx.h" #include "utils.h" CRITICAL_SECTION cs; //----< throw exception string >--------------------------------------- void throwString(const char *msg, const char *file, int line) { int errType = WSAGetLastError(); std::ostringstream collect; std::string duplicatesMsg = "Server is already running in another instance of the application."; duplicatesMsg += "\nSo you can connect to other machines, but other machines will"; duplicatesMsg += "\nconnect to the application which has the server running. To"; duplicatesMsg += "\nrun the server in this application quit all instances of the"; duplicatesMsg += "\napplication and restart the application."; if (errType == 10048) throw duplicatesMsg; collect << msg << " " << errType << "\nfile: " << file << "\nline: " << line << '\n' << std::ends; const char *debug = collect.str().c_str(); throw collect.str(); } //----< create socket >---------------------------------------- SOCKET createSocket() { SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(sock == INVALID_SOCKET) throwString("\n initialization error type:",__FILE__, __LINE__); InitializeCriticalSection(&cs); return sock; } //----< initialize socket run-time >--------------------------- void initializeSockets() { WORD version = MAKEWORD(1,1); WSADATA wsa; if(WSAStartup(version,&wsa) == SOCKET_ERROR) throwString("\n initialization error type:",__FILE__, __LINE__); } //----< terminate socket run-time >---------------------------- void cleanUpSockets() { WSACleanup(); DeleteCriticalSection(&cs); } //----< convert binary data to hex >------------------------ std::string toHex(unsigned short value) { std::string temp; const int mask = 0xf; while(value > 0) { temp.insert(0,HalfByteToHex(mask & value)); value = value >> 4; } while(temp.size() < 2*sizeof(unsigned short)) temp.insert(0,'0'); return temp; } //----< convert hex data to binary >------------------------ unsigned int toBin(std::string value) { unsigned int temp = 0; for (int i=0; i