Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Created May 5, 2013 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratstail91/5521429 to your computer and use it in GitHub Desktop.
Save Ratstail91/5521429 to your computer and use it in GitHub Desktop.
This is my idea for a minimalist networking interface. This is what I /wish/ existed. I might try and make this work, but I'm inexperienced with networking and making APIs.
#ifndef NETWORK_HPP_
#define NETWORK_HPP_
/* Unless noted otherwise, functions return 0 on success, or -1 on error.
* There are NO blocking functions.
*
* This is my idea for a minimalist networking interface. This is what I
* /wish/ existed. I might try and make this work, but I'm inexperienced
* with networking and making APIs.
*/
#include <cstdint>
struct IPAddress {
uint32_t host;
uint16_t port;
};
class TCPSocket {
public:
TCPSocket();
TCPSocket(const char* ip, int port);
TCPSocket(IPAddress);
~TCPSocket();
int Open(const char* ip, int port);
int Open(IPAddress);
void Close();
int Send(const void* data, int len);
int Recv(const void* data, int maxlen);
private:
IPAddress address;
//socket stuff...
};
class TCPServerSocket {
public:
TCPServerSocket();
TCPServerSocket(int port);
~TCPServerSocket();
int Open(int port);
void Close();
/* param 1: pointer to a socket to be created.
* return:
* 1: No new connection, socket is not altered
* 0: New connection, socket can be used normally
* -1: Unknown error
*/
int Accept(TCPSocket*);
private:
uint16_t port;
//socket stuff...
};
class UDPPacket {
public:
UDPPacket(int size);
~UDPPakcet();
int Alloc(int size);
int Resize(int size);
int Free();
int GetSize();
void* GetDataPtr();
int SetIPAddress(const char* host, int port);
int SetIPAddress(IPAddress);
IPAddress GetIPAddress();
private:
IPAddress address;
int size;
void* data;
};
class UDPSocket {
public:
UDPSocket();
UDPSocket(int port);
~UDPSocket();
int Send(UDPPacket*);
int Recv(UDPPacket*);
int GetPort();
private:
uint16_t port;
//socket stuff...
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment