SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.networking.sockets.h>
  2. #include <string>
  3. #include <iostream>
  4. #include <regex>
  5.  
  6. #pragma comment(lib, "Ws2_32.lib")
  7.  
  8. enum SocketType { CLIENT, SERVER };
  9. enum ConnectionType { TCP, UDP };
  10. const int DEFAULT_SIZE = 128;
  11.  
  12. class MySocket {
  13.     char *Buffer;
  14.     SOCKET WelcomeSocket;
  15.     SOCKET ConnectionSocket;
  16.     sockaddr_in SvrAddr;
  17.     sockaddr_in RespAddr;
  18.     SocketType mySocket;
  19.     std::string IPAddr;
  20.     int Port;
  21.     ConnectionType connectionType;
  22.     bool bConnect;
  23.     int MaxSize;
  24. public:
  25.     // Sets socket to a safe empty state if its construction fails for any reason
  26.     // Maybe a potential alternative to killing execution entirely? (Probably not)
  27.     //void setSafeEmptyState() {
  28.     //  this->Buffer = nullptr;
  29.     //  this->SetIPAddr("");
  30.     //  this->SetPortNum(-1);
  31.     //  this->bConnect = false;
  32.     //  this->MaxSize = -1;
  33.     //}
  34.    
  35.     MySocket(SocketType socketType, std::string ip, unsigned int port, ConnectionType connectionType, unsigned int size) {
  36.         this->Buffer = new char[
  37.             // NOTE: If an invalid size is provided the DEFAULT_SIZE should be used
  38.             this->MaxSize = (size > 0 ? size : DEFAULT_SIZE)
  39.         ];
  40.  
  41.         bool failedToConstruct = (!(
  42.             this->SetType(socketType) &&
  43.             this->SetIPAddr(ip) &&
  44.             this->SetPortNum(port) &&
  45.             (((this->connectionType = connectionType) == TCP) ? ConnectTCP() : SetupUDP()) // ?
  46.         ));
  47.  
  48.         if (failedToConstruct)
  49.             exit('c'); // setSafeEmptyState();
  50.     }
  51.  
  52.     ~MySocket() {
  53.         if (this->connectionType == TCP ? this->DisconnectTCP() : this->TerminateUDP())
  54.             exit('~'); // failed to disconnect/terminate
  55.     };
  56.  
  57.     bool StartWSA() { // are any MySocket member functions responsible for calling this?
  58.         WSADATA wsaData;
  59.         return WSAStartup(MAKEWORD(2, 2), &wsaData) == 0;
  60.     };
  61.  
  62.     bool ConnectTCP() {
  63.         if (this->mySocket == CLIENT) {
  64.             // TODO
  65.         }
  66.         else {
  67.             // TODO
  68.         }
  69.     }
  70.  
  71.     bool DisconnectTCP() {
  72.         if (this->mySocket == CLIENT) {
  73.             // TODO
  74.         }
  75.         else {
  76.             // TODO
  77.         }
  78.     }
  79.  
  80.     bool SetupUDP() {
  81.         if (this->connectionType == UDP) {
  82.             this->ConnectionSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  83.             if (this->ConnectionSocket == INVALID_SOCKET) {
  84.                 WSACleanup();
  85.                 return false;
  86.             }
  87.             else {
  88.                 if (this->mySocket == CLIENT) {
  89.                     sockaddr_in SvrAddr;
  90.                     SvrAddr.sin_family = AF_INET;
  91.                     SvrAddr.sin_port = htons(this->GetPort);
  92.                     SvrAddr.sin_addr.s_addr = inet_addr(this->GetIPAddr);
  93.                     return true;
  94.                 }
  95.                 else {
  96.                     sockaddr_in SvrAddr;
  97.                     SvrAddr.sin_family = AF_INET;
  98.                     SvrAddr.sin_addr.s_addr = INADDR_ANY;
  99.                     SvrAddr.sin_port = htons(27000);
  100.                     //bind(this->ConnectionSocket, (struct sockaddr *)&SvrAddr, sizeof(SvrAddr)
  101.                     if (bind(this->ConnectionSocket, (struct sockaddr *)&SvrAddr, sizeof(SvrAddr)) == SOCKET_ERROR)
  102.                     {
  103.                         closesocket(this->ConnectionSocket);
  104.                         WSACleanup();
  105.                         return -1;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         else if (this->connectionType == TCP) {
  111.             // TODO
  112.         }
  113.     }
  114.  
  115.     bool TerminateUDP() {
  116.         //Subject to change
  117.         closesocket(this->ConnectionSocket);
  118.         WSACleanup();
  119.         return true;
  120.     }
  121.  
  122.     int SendData(const char*, int) {
  123.         if (this->connectionType == TCP) {
  124.             // TODO
  125.         }
  126.         else {
  127.             // TODO
  128.         }
  129.     }
  130.  
  131.     int GetData(char*) {
  132.         if (this->connectionType == TCP) {
  133.             // TODO
  134.         }
  135.         else {
  136.             // TODO
  137.         }
  138.     }
  139.  
  140.     std::string GetIPAddr() { return IPAddr; }
  141.  
  142.     bool SetIPAddr(std::string ip) {       
  143.         bool ipIsValid = false;
  144.  
  145.         // https://www.regular-expressions.info/stdregex.html
  146.         // https://www.tutorialspoint.com/cpp_standard_library/cpp_regex_match.htm
  147.         /* REGEX NOT YET TESTED */
  148.         try {
  149.             std::regex re("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]? [0-9][0-9]?)$");
  150.             ipIsValid = std::regex_match(ip, re);
  151.         }
  152.         catch (std::regex_error& e) {
  153.             exit('r'); // Syntax error in the regular expression
  154.         }
  155.  
  156.         return !(this->bConnect || this->Port) ? ipIsValid && (this->IPAddr = ip) == ip : false;
  157.     }
  158.  
  159.     bool SetPortNum(int port) {
  160.         return !(this->bConnect || this->Port) ? port > 0 && (this->Port = port) == port : false;
  161.     }
  162.  
  163.     int GetPort() { return this->Port; }
  164.  
  165.     SocketType GetType() { return this->mySocket; }
  166.  
  167.     bool SetType(SocketType socketType) {
  168.         return !(this->bConnect || this->Port) ? (this->mySocket = socketType) == socketType : false;
  169.     }
  170. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top