wifly/socket interface for wifly modules
Dependents: PROJET_MATTHIEU_2019 PROJET_MATTHIEU_2019
Socket/TCPSocketConnection.cpp@14:352388da9fa5, 2019-05-20 (annotated)
- Committer:
- robin25
- Date:
- Mon May 20 09:09:10 2019 +0000
- Revision:
- 14:352388da9fa5
- Parent:
- 11:b912f91e3212
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:8f04181f9ad8 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
samux | 1:8f04181f9ad8 | 2 | * |
samux | 1:8f04181f9ad8 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:8f04181f9ad8 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
samux | 1:8f04181f9ad8 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
samux | 1:8f04181f9ad8 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
samux | 1:8f04181f9ad8 | 7 | * furnished to do so, subject to the following conditions: |
samux | 1:8f04181f9ad8 | 8 | * |
samux | 1:8f04181f9ad8 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:8f04181f9ad8 | 10 | * substantial portions of the Software. |
samux | 1:8f04181f9ad8 | 11 | * |
samux | 1:8f04181f9ad8 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:8f04181f9ad8 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:8f04181f9ad8 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:8f04181f9ad8 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:8f04181f9ad8 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:8f04181f9ad8 | 17 | */ |
samux | 1:8f04181f9ad8 | 18 | |
samux | 1:8f04181f9ad8 | 19 | #include "TCPSocketConnection.h" |
samux | 1:8f04181f9ad8 | 20 | #include <algorithm> |
samux | 1:8f04181f9ad8 | 21 | |
samux | 1:8f04181f9ad8 | 22 | TCPSocketConnection::TCPSocketConnection() {} |
samux | 1:8f04181f9ad8 | 23 | |
samux | 1:8f04181f9ad8 | 24 | int TCPSocketConnection::connect(const char* host, const int port) |
samux | 11:b912f91e3212 | 25 | { |
samux | 11:b912f91e3212 | 26 | if (!wifi->connect(host, port)) |
samux | 1:8f04181f9ad8 | 27 | return -1; |
samux | 1:8f04181f9ad8 | 28 | wifi->flush(); |
samux | 1:8f04181f9ad8 | 29 | return 0; |
samux | 1:8f04181f9ad8 | 30 | } |
samux | 1:8f04181f9ad8 | 31 | |
samux | 1:8f04181f9ad8 | 32 | bool TCPSocketConnection::is_connected(void) |
samux | 1:8f04181f9ad8 | 33 | { |
samux | 1:8f04181f9ad8 | 34 | return wifi->is_connected(); |
samux | 1:8f04181f9ad8 | 35 | } |
samux | 1:8f04181f9ad8 | 36 | |
samux | 1:8f04181f9ad8 | 37 | int TCPSocketConnection::send(char* data, int length) |
samux | 1:8f04181f9ad8 | 38 | { |
samux | 1:8f04181f9ad8 | 39 | Timer tmr; |
samux | 1:8f04181f9ad8 | 40 | |
samux | 1:8f04181f9ad8 | 41 | if (!_blocking) { |
samux | 1:8f04181f9ad8 | 42 | tmr.start(); |
samux | 1:8f04181f9ad8 | 43 | while (tmr.read_ms() < _timeout) { |
samux | 1:8f04181f9ad8 | 44 | if (wifi->writeable()) |
samux | 1:8f04181f9ad8 | 45 | break; |
samux | 1:8f04181f9ad8 | 46 | } |
samux | 1:8f04181f9ad8 | 47 | if (tmr.read_ms() >= _timeout) { |
samux | 1:8f04181f9ad8 | 48 | return -1; |
samux | 1:8f04181f9ad8 | 49 | } |
samux | 1:8f04181f9ad8 | 50 | } |
samux | 1:8f04181f9ad8 | 51 | return wifi->send(data, length); |
samux | 1:8f04181f9ad8 | 52 | } |
samux | 1:8f04181f9ad8 | 53 | |
samux | 1:8f04181f9ad8 | 54 | // -1 if unsuccessful, else number of bytes written |
samux | 1:8f04181f9ad8 | 55 | int TCPSocketConnection::send_all(char* data, int length) |
samux | 1:8f04181f9ad8 | 56 | { |
samux | 1:8f04181f9ad8 | 57 | Timer tmr; |
samux | 1:8f04181f9ad8 | 58 | int idx = 0; |
samux | 1:8f04181f9ad8 | 59 | tmr.start(); |
samux | 1:8f04181f9ad8 | 60 | |
samux | 1:8f04181f9ad8 | 61 | while ((tmr.read_ms() < _timeout) || _blocking) { |
samux | 1:8f04181f9ad8 | 62 | |
samux | 1:8f04181f9ad8 | 63 | idx += wifi->send(data, length); |
samux | 1:8f04181f9ad8 | 64 | |
samux | 1:8f04181f9ad8 | 65 | if (idx == length) |
samux | 1:8f04181f9ad8 | 66 | return idx; |
samux | 1:8f04181f9ad8 | 67 | } |
samux | 1:8f04181f9ad8 | 68 | return (idx == 0) ? -1 : idx; |
samux | 1:8f04181f9ad8 | 69 | } |
samux | 1:8f04181f9ad8 | 70 | |
samux | 1:8f04181f9ad8 | 71 | // -1 if unsuccessful, else number of bytes received |
samux | 1:8f04181f9ad8 | 72 | int TCPSocketConnection::receive(char* data, int length) |
samux | 1:8f04181f9ad8 | 73 | { |
samux | 1:8f04181f9ad8 | 74 | Timer tmr; |
samux | 6:f281180726e8 | 75 | int time = -1; |
samux | 6:f281180726e8 | 76 | |
samux | 6:f281180726e8 | 77 | |
samux | 1:8f04181f9ad8 | 78 | if (!_blocking) { |
samux | 1:8f04181f9ad8 | 79 | tmr.start(); |
samux | 6:f281180726e8 | 80 | while (time < _timeout + 20) { |
samux | 6:f281180726e8 | 81 | if (wifi->readable()) { |
samux | 1:8f04181f9ad8 | 82 | break; |
samux | 6:f281180726e8 | 83 | } |
samux | 6:f281180726e8 | 84 | time = tmr.read_ms(); |
samux | 1:8f04181f9ad8 | 85 | } |
samux | 6:f281180726e8 | 86 | if (time >= _timeout + 20) { |
samux | 1:8f04181f9ad8 | 87 | return -1; |
samux | 6:f281180726e8 | 88 | } |
samux | 1:8f04181f9ad8 | 89 | } |
samux | 1:8f04181f9ad8 | 90 | |
samux | 6:f281180726e8 | 91 | |
samux | 1:8f04181f9ad8 | 92 | while(!wifi->readable()); |
samux | 1:8f04181f9ad8 | 93 | int nb_available = wifi->readable(); |
samux | 1:8f04181f9ad8 | 94 | for (int i = 0; i < min(nb_available, length); i++) { |
samux | 1:8f04181f9ad8 | 95 | data[i] = wifi->getc(); |
samux | 1:8f04181f9ad8 | 96 | } |
samux | 6:f281180726e8 | 97 | |
samux | 1:8f04181f9ad8 | 98 | return min(nb_available, length); |
samux | 1:8f04181f9ad8 | 99 | } |
samux | 1:8f04181f9ad8 | 100 | |
samux | 1:8f04181f9ad8 | 101 | |
samux | 1:8f04181f9ad8 | 102 | // -1 if unsuccessful, else number of bytes received |
samux | 1:8f04181f9ad8 | 103 | int TCPSocketConnection::receive_all(char* data, int length) |
samux | 1:8f04181f9ad8 | 104 | { |
samux | 1:8f04181f9ad8 | 105 | Timer tmr; |
samux | 1:8f04181f9ad8 | 106 | int idx = 0; |
samux | 6:f281180726e8 | 107 | int time = -1; |
samux | 1:8f04181f9ad8 | 108 | |
samux | 1:8f04181f9ad8 | 109 | tmr.start(); |
samux | 6:f281180726e8 | 110 | |
samux | 6:f281180726e8 | 111 | while (time < _timeout || _blocking) { |
samux | 1:8f04181f9ad8 | 112 | |
samux | 1:8f04181f9ad8 | 113 | int nb_available = wifi->readable(); |
samux | 1:8f04181f9ad8 | 114 | for (int i = 0; i < min(nb_available, length); i++) { |
samux | 1:8f04181f9ad8 | 115 | data[idx++] = wifi->getc(); |
samux | 1:8f04181f9ad8 | 116 | } |
samux | 1:8f04181f9ad8 | 117 | |
samux | 1:8f04181f9ad8 | 118 | if (idx == length) |
samux | 6:f281180726e8 | 119 | break; |
samux | 6:f281180726e8 | 120 | |
samux | 6:f281180726e8 | 121 | time = tmr.read_ms(); |
samux | 1:8f04181f9ad8 | 122 | } |
samux | 1:8f04181f9ad8 | 123 | |
samux | 1:8f04181f9ad8 | 124 | return (idx == 0) ? -1 : idx; |
samux | 1:8f04181f9ad8 | 125 | } |