Advertisement
Ghostriax-Atrocity

AbstractSocket.h

Apr 11th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #ifndef ABSTRACTSOCKET_H
  2. #define ABSTRACTSOCKET_H
  3.  
  4. #define SOCKET_DEBUG(Direction, String) {\
  5. char ___[1024]; \
  6. if(strlen(String) <= (_countof(___) - 2)) {\
  7. wsprintfA(___, "%c%s", Direction, String); \
  8. OutputDebugStringA(___);\
  9. }\
  10. }
  11.  
  12. class AbstractSocket
  13. {
  14. public:
  15. AbstractSocket();
  16. /*
  17. \brief Sets up a connection.
  18.  
  19. \param server Can handle IPv4 and hostnames
  20. */
  21. virtual bool Connect(const char* server, unsigned short port) = 0;
  22. /*
  23. \brief Tears down the conneciton.
  24. Not automatically called in deconstructor - you have to call it yourself
  25. */
  26. void Disconnect();
  27.  
  28. virtual void Send(const char* data, size_t length) = 0;
  29. virtual void Send(std::string data) = 0;
  30.  
  31. virtual void Recv(char* Buffer, size_t& Length) = 0;
  32. virtual void RecvLine(char* Buffer, size_t& Length, size_t MaxLength) = 0;
  33.  
  34. void Recv(std::string& res);
  35.  
  36. int GetLastError()
  37. {
  38. return WSAGetLastError();
  39. }
  40.  
  41. void SetTimeout(unsigned int timeouts)
  42. {
  43. timeoutvalsec = timeouts;
  44. }
  45.  
  46. bool GetConnectionClosed()
  47. {
  48. return WSAGetLastError() == WSAECONNRESET;
  49. }
  50.  
  51. // Needed for Switching from unencrypted to encrypted but keeping the same socket and thus connection
  52. virtual SOCKET GetSocket() = 0;
  53. virtual void SetSocket(SOCKET s) = 0;
  54. protected:
  55. SOCKET s;
  56. sockaddr_in addr;
  57. unsigned int timeoutvalsec;
  58.  
  59. BOOL ConnectWithTimeout();
  60. };
  61. #endif // ABSTRACTSOCKET_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement