cc3000 hostdriver with the mbed socket interface
Fork of cc3000_hostdriver_mbedsocket by
Socket/TCPSocketServer.cpp@5:245ac5b73132, 2013-09-26 (annotated)
- Committer:
- Kojto
- Date:
- Thu Sep 26 19:50:37 2013 +0000
- Revision:
- 5:245ac5b73132
- Parent:
- 0:615c697c33b0
- Child:
- 29:c40918cd9b6d
tcp implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 5:245ac5b73132 | 1 | /* Copyright (C) 2013 mbed.org, MIT License |
Kojto | 0:615c697c33b0 | 2 | * |
Kojto | 0:615c697c33b0 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Kojto | 0:615c697c33b0 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Kojto | 0:615c697c33b0 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Kojto | 0:615c697c33b0 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Kojto | 0:615c697c33b0 | 7 | * furnished to do so, subject to the following conditions: |
Kojto | 0:615c697c33b0 | 8 | * |
Kojto | 0:615c697c33b0 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Kojto | 0:615c697c33b0 | 10 | * substantial portions of the Software. |
Kojto | 0:615c697c33b0 | 11 | * |
Kojto | 0:615c697c33b0 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Kojto | 0:615c697c33b0 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Kojto | 0:615c697c33b0 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Kojto | 0:615c697c33b0 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Kojto | 0:615c697c33b0 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Kojto | 0:615c697c33b0 | 17 | */ |
Kojto | 0:615c697c33b0 | 18 | |
Kojto | 0:615c697c33b0 | 19 | #include "TCPSocketServer.h" |
Kojto | 0:615c697c33b0 | 20 | #include <string> |
Kojto | 0:615c697c33b0 | 21 | |
Kojto | 5:245ac5b73132 | 22 | TCPSocketServer::TCPSocketServer() { |
Kojto | 0:615c697c33b0 | 23 | |
Kojto | 0:615c697c33b0 | 24 | } |
Kojto | 0:615c697c33b0 | 25 | |
Kojto | 5:245ac5b73132 | 26 | int TCPSocketServer::bind(int port) { |
Kojto | 5:245ac5b73132 | 27 | if (init_socket(SOCK_STREAM, IPPROTO_TCP) < 0) { |
Kojto | 5:245ac5b73132 | 28 | return -1; |
Kojto | 5:245ac5b73132 | 29 | } |
Kojto | 5:245ac5b73132 | 30 | |
Kojto | 5:245ac5b73132 | 31 | sockaddr_in localHost; |
Kojto | 5:245ac5b73132 | 32 | memset(&localHost, 0, sizeof(localHost)); |
Kojto | 5:245ac5b73132 | 33 | |
Kojto | 5:245ac5b73132 | 34 | localHost.sin_family = AF_INET; |
Kojto | 5:245ac5b73132 | 35 | localHost.sin_port = htons(port); |
Kojto | 5:245ac5b73132 | 36 | localHost.sin_addr.s_addr = 0; |
Kojto | 0:615c697c33b0 | 37 | |
Kojto | 5:245ac5b73132 | 38 | if (_cc3000_module->_socket.bind(_sock_fd, (const sockaddr *)&localHost, sizeof(localHost)) < 0) { |
Kojto | 5:245ac5b73132 | 39 | close(); |
Kojto | 5:245ac5b73132 | 40 | return -1; |
Kojto | 5:245ac5b73132 | 41 | } |
Kojto | 5:245ac5b73132 | 42 | |
Kojto | 5:245ac5b73132 | 43 | return 0; |
Kojto | 5:245ac5b73132 | 44 | } |
Kojto | 5:245ac5b73132 | 45 | |
Kojto | 5:245ac5b73132 | 46 | int TCPSocketServer::listen(int max) { |
Kojto | 5:245ac5b73132 | 47 | if (_sock_fd < 0) { |
Kojto | 5:245ac5b73132 | 48 | return -1; |
Kojto | 5:245ac5b73132 | 49 | } |
Kojto | 5:245ac5b73132 | 50 | |
Kojto | 5:245ac5b73132 | 51 | if (_cc3000_module->_socket.listen(_sock_fd, max) < 0) { |
Kojto | 5:245ac5b73132 | 52 | close(); |
Kojto | 5:245ac5b73132 | 53 | return -1; |
Kojto | 5:245ac5b73132 | 54 | } |
Kojto | 5:245ac5b73132 | 55 | |
Kojto | 5:245ac5b73132 | 56 | return 0; |
Kojto | 0:615c697c33b0 | 57 | } |
Kojto | 0:615c697c33b0 | 58 | |
Kojto | 0:615c697c33b0 | 59 | |
Kojto | 0:615c697c33b0 | 60 | int TCPSocketServer::accept(TCPSocketConnection& connection) { |
Kojto | 5:245ac5b73132 | 61 | if (_sock_fd < 0) { |
Kojto | 5:245ac5b73132 | 62 | return -1; |
Kojto | 5:245ac5b73132 | 63 | } |
Kojto | 0:615c697c33b0 | 64 | |
Kojto | 5:245ac5b73132 | 65 | if (!_blocking) { |
Kojto | 5:245ac5b73132 | 66 | TimeInterval timeout(_timeout); |
Kojto | 5:245ac5b73132 | 67 | if (wait_readable(timeout) != 0) { |
Kojto | 5:245ac5b73132 | 68 | return -1; |
Kojto | 5:245ac5b73132 | 69 | } |
Kojto | 5:245ac5b73132 | 70 | } |
Kojto | 5:245ac5b73132 | 71 | |
Kojto | 5:245ac5b73132 | 72 | connection.reset_address(); |
Kojto | 5:245ac5b73132 | 73 | socklen_t newSockRemoteHostLen = sizeof(connection._remote_host); |
Kojto | 5:245ac5b73132 | 74 | int fd = _cc3000_module->_socket.accept(_sock_fd, (sockaddr *) &connection._remote_host, &newSockRemoteHostLen); |
Kojto | 5:245ac5b73132 | 75 | if (fd < 0) { |
Kojto | 5:245ac5b73132 | 76 | return -1; |
Kojto | 5:245ac5b73132 | 77 | } |
Kojto | 5:245ac5b73132 | 78 | connection._sock_fd = fd; |
Kojto | 5:245ac5b73132 | 79 | connection._is_connected = true; |
Kojto | 5:245ac5b73132 | 80 | |
Kojto | 5:245ac5b73132 | 81 | return 0; |
Kojto | 0:615c697c33b0 | 82 | } |