Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ABSTRACTSOCKET_H
- #define ABSTRACTSOCKET_H
- #define SOCKET_DEBUG(Direction, String) {\
- char ___[1024]; \
- if(strlen(String) <= (_countof(___) - 2)) {\
- wsprintfA(___, "%c%s", Direction, String); \
- OutputDebugStringA(___);\
- }\
- }
- class AbstractSocket
- {
- public:
- AbstractSocket();
- /*
- \brief Sets up a connection.
- \param server Can handle IPv4 and hostnames
- */
- virtual bool Connect(const char* server, unsigned short port) = 0;
- /*
- \brief Tears down the conneciton.
- Not automatically called in deconstructor - you have to call it yourself
- */
- void Disconnect();
- virtual void Send(const char* data, size_t length) = 0;
- virtual void Send(std::string data) = 0;
- virtual void Recv(char* Buffer, size_t& Length) = 0;
- virtual void RecvLine(char* Buffer, size_t& Length, size_t MaxLength) = 0;
- void Recv(std::string& res);
- int GetLastError()
- {
- return WSAGetLastError();
- }
- void SetTimeout(unsigned int timeouts)
- {
- timeoutvalsec = timeouts;
- }
- bool GetConnectionClosed()
- {
- return WSAGetLastError() == WSAECONNRESET;
- }
- // Needed for Switching from unencrypted to encrypted but keeping the same socket and thus connection
- virtual SOCKET GetSocket() = 0;
- virtual void SetSocket(SOCKET s) = 0;
- protected:
- SOCKET s;
- sockaddr_in addr;
- unsigned int timeoutvalsec;
- BOOL ConnectWithTimeout();
- };
- #endif // ABSTRACTSOCKET_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement