Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.networking.sockets.h>
- #include <string>
- #include <iostream>
- #include <regex>
- #pragma comment(lib, "Ws2_32.lib")
- enum SocketType { CLIENT, SERVER };
- enum ConnectionType { TCP, UDP };
- const int DEFAULT_SIZE = 128;
- class MySocket {
- char *Buffer;
- SOCKET WelcomeSocket;
- SOCKET ConnectionSocket;
- sockaddr_in SvrAddr;
- sockaddr_in RespAddr;
- SocketType mySocket;
- std::string IPAddr;
- int Port;
- ConnectionType connectionType;
- bool bConnect;
- int MaxSize;
- public:
- // Sets socket to a safe empty state if its construction fails for any reason
- // Maybe a potential alternative to killing execution entirely? (Probably not)
- //void setSafeEmptyState() {
- // this->Buffer = nullptr;
- // this->SetIPAddr("");
- // this->SetPortNum(-1);
- // this->bConnect = false;
- // this->MaxSize = -1;
- //}
- MySocket(SocketType socketType, std::string ip, unsigned int port, ConnectionType connectionType, unsigned int size) {
- this->Buffer = new char[
- // NOTE: If an invalid size is provided the DEFAULT_SIZE should be used
- this->MaxSize = (size > 0 ? size : DEFAULT_SIZE)
- ];
- bool failedToConstruct = (!(
- this->SetType(socketType) &&
- this->SetIPAddr(ip) &&
- this->SetPortNum(port) &&
- (((this->connectionType = connectionType) == TCP) ? ConnectTCP() : SetupUDP()) // ?
- ));
- if (failedToConstruct)
- exit('c'); // setSafeEmptyState();
- }
- ~MySocket() {
- if (this->connectionType == TCP ? this->DisconnectTCP() : this->TerminateUDP())
- exit('~'); // failed to disconnect/terminate
- };
- bool StartWSA() { // are any MySocket member functions responsible for calling this?
- WSADATA wsaData;
- return WSAStartup(MAKEWORD(2, 2), &wsaData) == 0;
- };
- bool ConnectTCP() {
- if (this->mySocket == CLIENT) {
- // TODO
- }
- else {
- // TODO
- }
- }
- bool DisconnectTCP() {
- if (this->mySocket == CLIENT) {
- // TODO
- }
- else {
- // TODO
- }
- }
- bool SetupUDP() {
- if (this->connectionType == UDP) {
- this->ConnectionSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (this->ConnectionSocket == INVALID_SOCKET) {
- WSACleanup();
- return false;
- }
- else {
- if (this->mySocket == CLIENT) {
- sockaddr_in SvrAddr;
- SvrAddr.sin_family = AF_INET;
- SvrAddr.sin_port = htons(this->GetPort);
- SvrAddr.sin_addr.s_addr = inet_addr(this->GetIPAddr);
- return true;
- }
- else {
- sockaddr_in SvrAddr;
- SvrAddr.sin_family = AF_INET;
- SvrAddr.sin_addr.s_addr = INADDR_ANY;
- SvrAddr.sin_port = htons(27000);
- //bind(this->ConnectionSocket, (struct sockaddr *)&SvrAddr, sizeof(SvrAddr)
- if (bind(this->ConnectionSocket, (struct sockaddr *)&SvrAddr, sizeof(SvrAddr)) == SOCKET_ERROR)
- {
- closesocket(this->ConnectionSocket);
- WSACleanup();
- return -1;
- }
- }
- }
- }
- else if (this->connectionType == TCP) {
- // TODO
- }
- }
- bool TerminateUDP() {
- //Subject to change
- closesocket(this->ConnectionSocket);
- WSACleanup();
- return true;
- }
- int SendData(const char*, int) {
- if (this->connectionType == TCP) {
- // TODO
- }
- else {
- // TODO
- }
- }
- int GetData(char*) {
- if (this->connectionType == TCP) {
- // TODO
- }
- else {
- // TODO
- }
- }
- std::string GetIPAddr() { return IPAddr; }
- bool SetIPAddr(std::string ip) {
- bool ipIsValid = false;
- // https://www.regular-expressions.info/stdregex.html
- // https://www.tutorialspoint.com/cpp_standard_library/cpp_regex_match.htm
- /* REGEX NOT YET TESTED */
- try {
- 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]?)$");
- ipIsValid = std::regex_match(ip, re);
- }
- catch (std::regex_error& e) {
- exit('r'); // Syntax error in the regular expression
- }
- return !(this->bConnect || this->Port) ? ipIsValid && (this->IPAddr = ip) == ip : false;
- }
- bool SetPortNum(int port) {
- return !(this->bConnect || this->Port) ? port > 0 && (this->Port = port) == port : false;
- }
- int GetPort() { return this->Port; }
- SocketType GetType() { return this->mySocket; }
- bool SetType(SocketType socketType) {
- return !(this->bConnect || this->Port) ? (this->mySocket = socketType) == socketType : false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement