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.

TCPSocketConnection.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "TCPSocketConnection.h"
  19. #include <cstring>
  20. using std::memset;
  21. using std::memcpy;
  22. TCPSocketConnection::TCPSocketConnection() :
  23. _is_connected(false) {
  24. }
  25. int TCPSocketConnection::connect(const char* host, const int port) {
  26. if (init_socket(SOCK_STREAM) < 0)
  27. return -1;
  28. if (set_address(host, port) != 0)
  29. return -1;
  30. if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
  31. close();
  32. return -1;
  33. }
  34. _is_connected = true;
  35. return 0;
  36. }
  37. bool TCPSocketConnection::is_connected(void) {
  38. return _is_connected;
  39. }
  40. int TCPSocketConnection::send(char* data, int length) {
  41. if ((_sock_fd < 0) || !_is_connected)
  42. return -1;
  43. if (!_blocking) {
  44. TimeInterval timeout(_timeout);
  45. if (wait_writable(timeout) != 0)
  46. return -1;
  47. }
  48. int n = lwip_send(_sock_fd, data, length, 0);
  49. _is_connected = (n != 0);
  50. return n;
  51. }
  52. // -1 if unsuccessful, else number of bytes written
  53. int TCPSocketConnection::send_all(char* data, int length) {
  54. if ((_sock_fd < 0) || !_is_connected)
  55. return -1;
  56. int writtenLen = 0;
  57. TimeInterval timeout(_timeout);
  58. while (writtenLen < length) {
  59. if (!_blocking) {
  60. // Wait for socket to be writeable
  61. if (wait_writable(timeout) != 0)
  62. return writtenLen;
  63. }
  64. int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
  65. if (ret > 0) {
  66. writtenLen += ret;
  67. continue;
  68. } else if (ret == 0) {
  69. _is_connected = false;
  70. return writtenLen;
  71. } else {
  72. return -1; //Connnection error
  73. }
  74. }
  75. return writtenLen;
  76. }
  77. int TCPSocketConnection::receive(char* data, int length) {
  78. if ((_sock_fd < 0) || !_is_connected)
  79. return -1;
  80. if (!_blocking) {
  81. TimeInterval timeout(_timeout);
  82. if (wait_readable(timeout) != 0)
  83. return -1;
  84. }
  85. int n = lwip_recv(_sock_fd, data, length, 0);
  86. _is_connected = (n != 0);
  87. return n;
  88. }
  89. // -1 if unsuccessful, else number of bytes received
  90. int TCPSocketConnection::receive_all(char* data, int length) {
  91. if ((_sock_fd < 0) || !_is_connected)
  92. return -1;
  93. int readLen = 0;
  94. TimeInterval timeout(_timeout);
  95. while (readLen < length) {
  96. if (!_blocking) {
  97. //Wait for socket to be readable
  98. if (wait_readable(timeout) != 0)
  99. return readLen;
  100. }
  101. int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
  102. if (ret > 0) {
  103. readLen += ret;
  104. } else if (ret == 0) {
  105. _is_connected = false;
  106. return readLen;
  107. } else {
  108. return -1; //Connnection error
  109. }
  110. }
  111. return readLen;
  112. }