PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libs/netcomm/socket/socket.h

https://gitlab.com/F34140r/rockin-refbox
C Header | 148 lines | 90 code | 28 blank | 30 comment | 2 complexity | 9e0ab67b8f5b178f98f6befc12de323c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /***************************************************************************
  2. * socket.h - Fawkes socket base class
  3. *
  4. * Created: Thu Nov 09 12:55:25 2006
  5. * Copyright 2006 Tim Niemueller [www.niemueller.de]
  6. *
  7. ****************************************************************************/
  8. /* This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version. A runtime exception applies to
  12. * this software (see LICENSE.GPL_WRE file mentioned below for details).
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Library General Public License for more details.
  18. *
  19. * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
  20. */
  21. #ifndef __NETCOMM_SOCKET_SOCKET_H_
  22. #define __NETCOMM_SOCKET_SOCKET_H_
  23. #include <core/exception.h>
  24. #include <core/exceptions/software.h>
  25. #include <sys/socket.h>
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. // just to be safe nobody else can do it
  29. #include <sys/signal.h>
  30. #ifdef POLL_IN
  31. # undef POLL_IN
  32. #endif
  33. #ifdef POLL_OUT
  34. # undef POLL_OUT
  35. #endif
  36. #ifdef POLL_PRI
  37. # undef POLL_PRI
  38. #endif
  39. #ifdef POLL_RDHUP
  40. # undef POLL_RDHUP
  41. #endif
  42. #ifdef POLL_ERR
  43. # undef POLL_ERR
  44. #endif
  45. #ifdef POLL_HUP
  46. # undef POLL_HUP
  47. #endif
  48. namespace fawkes {
  49. class SocketException : public Exception
  50. {
  51. public:
  52. SocketException(const char *msg, int _errno);
  53. SocketException(const char *msg);
  54. };
  55. class Socket
  56. {
  57. public:
  58. static const short POLL_IN;
  59. static const short POLL_OUT;
  60. static const short POLL_PRI;
  61. static const short POLL_RDHUP;
  62. static const short POLL_ERR;
  63. static const short POLL_HUP;
  64. static const short POLL_NVAL;
  65. Socket(int domain, int type, int protocol, float timeout = 0.f);
  66. Socket(Socket &socket);
  67. virtual ~Socket();
  68. virtual void connect(const char *hostname, const unsigned short int port);
  69. virtual void connect(struct sockaddr *addr_port, unsigned int struct_size);
  70. virtual void bind(const unsigned short int port);
  71. virtual void bind(const unsigned short int port,
  72. const char *hostname);
  73. virtual void listen(int backlog = 1);
  74. virtual Socket * accept();
  75. virtual void close();
  76. virtual bool available();
  77. virtual size_t read(void *buf, size_t count, bool read_all = true);
  78. virtual void write(const void *buf, size_t count);
  79. virtual void send(void *buf, size_t buf_len);
  80. virtual void send(void *buf, size_t buf_len,
  81. const struct sockaddr *to_addr, socklen_t addr_len);
  82. virtual size_t recv(void *buf, size_t buf_len);
  83. virtual size_t recv(void *buf, size_t buf_len,
  84. struct sockaddr *from_addr, socklen_t *addr_len);
  85. /** Clone socket.
  86. * This method has to be implemented by subclass to correctly clone the instance.
  87. * @return cloned socket
  88. */
  89. virtual Socket * clone() = 0;
  90. virtual short poll(int timeout = -1, short what = POLL_IN | POLL_HUP | POLL_PRI | POLL_RDHUP);
  91. virtual bool listening();
  92. virtual unsigned int mtu();
  93. /** Accept connection.
  94. * This method works like accept() but it ensures that the returned socket is of
  95. * the given type.
  96. * @return socket to client
  97. */
  98. template <class SocketType>
  99. SocketType * accept();
  100. protected:
  101. Socket();
  102. int sock_fd;
  103. float timeout;
  104. struct ::sockaddr_in *client_addr;
  105. unsigned int client_addr_len;
  106. };
  107. template <class SocketType>
  108. SocketType *
  109. Socket::accept()
  110. {
  111. Socket *s = accept();
  112. if (SocketType *ts = dynamic_cast<SocketType *>(s)) {
  113. return ts;
  114. } else {
  115. delete s;
  116. throw TypeMismatchException("Socket types do not match");
  117. }
  118. }
  119. } // end namespace fawkes
  120. #endif