Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Socket.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 2012 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without restriction,
  5. * including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef SOCKET_H_
  19. #define SOCKET_H_
  20. #include "lwip/sockets.h"
  21. #include "lwip/netdb.h"
  22. //DNS
  23. inline struct hostent *gethostbyname(const char *name) {
  24. return lwip_gethostbyname(name);
  25. }
  26. inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) {
  27. return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  28. }
  29. class TimeInterval;
  30. /** Socket file descriptor and select wrapper
  31. */
  32. class Socket {
  33. public:
  34. /** Socket
  35. */
  36. Socket();
  37. /** Set blocking or non-blocking mode of the socket and a timeout on
  38. blocking socket operations
  39. \param blocking true for blocking mode, false for non-blocking mode.
  40. \param timeout timeout in ms [Default: (1500)ms].
  41. */
  42. void set_blocking(bool blocking, unsigned int timeout=1500);
  43. /** Set socket options
  44. \param level stack level (see: lwip/sockets.h)
  45. \param optname option ID
  46. \param optval option value
  47. \param socklen_t length of the option value
  48. \return 0 on success, -1 on failure
  49. */
  50. int set_option(int level, int optname, const void *optval, socklen_t optlen);
  51. /** Get socket options
  52. \param level stack level (see: lwip/sockets.h)
  53. \param optname option ID
  54. \param optval buffer pointer where to write the option value
  55. \param socklen_t length of the option value
  56. \return 0 on success, -1 on failure
  57. */
  58. int get_option(int level, int optname, void *optval, socklen_t *optlen);
  59. /** Close the socket
  60. \param shutdown free the left-over data in message queues
  61. */
  62. int close(bool shutdown=true);
  63. ~Socket();
  64. protected:
  65. int _sock_fd;
  66. int init_socket(int type);
  67. int wait_readable(TimeInterval& timeout);
  68. int wait_writable(TimeInterval& timeout);
  69. bool _blocking;
  70. unsigned int _timeout;
  71. private:
  72. int select(struct timeval *timeout, bool read, bool write);
  73. };
  74. /** Time interval class used to specify timeouts
  75. */
  76. class TimeInterval {
  77. friend class Socket;
  78. public:
  79. /** Time Interval
  80. \param ms time interval expressed in milliseconds
  81. */
  82. TimeInterval(unsigned int ms);
  83. private:
  84. struct timeval _time;
  85. };
  86. #endif /* SOCKET_H_ */