htgghg
Dependents: WizFi310_Station WizFi310_Station1 WizFi310_AP dweetIo_CloudService_WizFi310
Fork of WizFi310Interface_Legacy by
Socket/TCPSocketConnection.cpp@2:a4890cc8be69, 2017-09-18 (annotated)
- Committer:
- ajeet3004
- Date:
- Mon Sep 18 02:50:44 2017 +0000
- Revision:
- 2:a4890cc8be69
- Parent:
- 0:774ff1e8b26b
Changed the Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:774ff1e8b26b | 1 | /* |
kaizen | 0:774ff1e8b26b | 2 | * Copyright (C) 2013 gsfan, MIT License |
kaizen | 0:774ff1e8b26b | 3 | * |
kaizen | 0:774ff1e8b26b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kaizen | 0:774ff1e8b26b | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kaizen | 0:774ff1e8b26b | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kaizen | 0:774ff1e8b26b | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kaizen | 0:774ff1e8b26b | 8 | * furnished to do so, subject to the following conditions: |
kaizen | 0:774ff1e8b26b | 9 | * |
kaizen | 0:774ff1e8b26b | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
kaizen | 0:774ff1e8b26b | 11 | * substantial portions of the Software. |
kaizen | 0:774ff1e8b26b | 12 | * |
kaizen | 0:774ff1e8b26b | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kaizen | 0:774ff1e8b26b | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kaizen | 0:774ff1e8b26b | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kaizen | 0:774ff1e8b26b | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kaizen | 0:774ff1e8b26b | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kaizen | 0:774ff1e8b26b | 18 | */ |
kaizen | 0:774ff1e8b26b | 19 | /* Copyright (C) 2014 Wiznet, MIT License |
kaizen | 0:774ff1e8b26b | 20 | * port to the Wiznet Module WizFi250 |
kaizen | 0:774ff1e8b26b | 21 | */ |
kaizen | 0:774ff1e8b26b | 22 | /* Copyright (C) 2017 Wiznet, MIT License |
kaizen | 0:774ff1e8b26b | 23 | * port to the Wiznet Module WizFi310 |
kaizen | 0:774ff1e8b26b | 24 | */ |
kaizen | 0:774ff1e8b26b | 25 | |
kaizen | 0:774ff1e8b26b | 26 | #include "TCPSocketConnection.h" |
kaizen | 0:774ff1e8b26b | 27 | #include <algorithm> |
kaizen | 0:774ff1e8b26b | 28 | |
kaizen | 0:774ff1e8b26b | 29 | TCPSocketConnection::TCPSocketConnection() {} |
kaizen | 0:774ff1e8b26b | 30 | |
kaizen | 0:774ff1e8b26b | 31 | int TCPSocketConnection::connect(const char* host, const int port) |
kaizen | 0:774ff1e8b26b | 32 | { |
kaizen | 0:774ff1e8b26b | 33 | if (set_address(host, port) != 0) return -1; |
kaizen | 0:774ff1e8b26b | 34 | |
kaizen | 0:774ff1e8b26b | 35 | _server = false; |
kaizen | 0:774ff1e8b26b | 36 | _cid = _wizfi310->open(WizFi310::PROTO_TCP, get_address(), get_port()); |
kaizen | 0:774ff1e8b26b | 37 | if (_cid < 0 ) return -1; |
kaizen | 0:774ff1e8b26b | 38 | |
kaizen | 0:774ff1e8b26b | 39 | return 0; |
kaizen | 0:774ff1e8b26b | 40 | } |
kaizen | 0:774ff1e8b26b | 41 | |
kaizen | 0:774ff1e8b26b | 42 | bool TCPSocketConnection::is_connected(void) |
kaizen | 0:774ff1e8b26b | 43 | { |
kaizen | 0:774ff1e8b26b | 44 | bool _is_connected = _wizfi310->isConnected(_cid); |
kaizen | 0:774ff1e8b26b | 45 | if(!_is_connected) _cid = -1; |
kaizen | 0:774ff1e8b26b | 46 | |
kaizen | 0:774ff1e8b26b | 47 | return _is_connected; |
kaizen | 0:774ff1e8b26b | 48 | } |
kaizen | 0:774ff1e8b26b | 49 | |
kaizen | 0:774ff1e8b26b | 50 | int TCPSocketConnection::send(char *data, int length) |
kaizen | 0:774ff1e8b26b | 51 | { |
kaizen | 0:774ff1e8b26b | 52 | if (_cid < 0 || !is_connected()) return -1; |
kaizen | 0:774ff1e8b26b | 53 | |
kaizen | 0:774ff1e8b26b | 54 | return _wizfi310->send(_cid, data, length); |
kaizen | 0:774ff1e8b26b | 55 | } |
kaizen | 0:774ff1e8b26b | 56 | |
kaizen | 0:774ff1e8b26b | 57 | int TCPSocketConnection::send_all(char *data, int length) |
kaizen | 0:774ff1e8b26b | 58 | { |
kaizen | 0:774ff1e8b26b | 59 | Timer tmr; |
kaizen | 0:774ff1e8b26b | 60 | int idx = 0; |
kaizen | 0:774ff1e8b26b | 61 | |
kaizen | 0:774ff1e8b26b | 62 | if(_cid <0 || !is_connected()) return -1; |
kaizen | 0:774ff1e8b26b | 63 | |
kaizen | 0:774ff1e8b26b | 64 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 65 | while((tmr.read_ms() < _timeout) || _blocking) |
kaizen | 0:774ff1e8b26b | 66 | { |
kaizen | 0:774ff1e8b26b | 67 | idx += _wizfi310->send(_cid, &data[idx], length - idx); |
kaizen | 0:774ff1e8b26b | 68 | if(idx < 0) return -1; |
kaizen | 0:774ff1e8b26b | 69 | |
kaizen | 0:774ff1e8b26b | 70 | if(idx == length) |
kaizen | 0:774ff1e8b26b | 71 | return idx; |
kaizen | 0:774ff1e8b26b | 72 | } |
kaizen | 0:774ff1e8b26b | 73 | return (idx == 0) ? -1 : idx; |
kaizen | 0:774ff1e8b26b | 74 | } |
kaizen | 0:774ff1e8b26b | 75 | |
kaizen | 0:774ff1e8b26b | 76 | int TCPSocketConnection::receive(char* data, int length) |
kaizen | 0:774ff1e8b26b | 77 | { |
kaizen | 0:774ff1e8b26b | 78 | Timer tmr; |
kaizen | 0:774ff1e8b26b | 79 | int time = -1; |
kaizen | 0:774ff1e8b26b | 80 | int len = 0; |
kaizen | 0:774ff1e8b26b | 81 | |
kaizen | 0:774ff1e8b26b | 82 | if(_cid < 0 || !is_connected()) return -1; |
kaizen | 0:774ff1e8b26b | 83 | |
kaizen | 0:774ff1e8b26b | 84 | if(_blocking) |
kaizen | 0:774ff1e8b26b | 85 | { |
kaizen | 0:774ff1e8b26b | 86 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 87 | while(time < _timeout) |
kaizen | 0:774ff1e8b26b | 88 | { |
kaizen | 0:774ff1e8b26b | 89 | len = _wizfi310->readable(_cid); |
kaizen | 0:774ff1e8b26b | 90 | if(len == -1) |
kaizen | 0:774ff1e8b26b | 91 | return len; |
kaizen | 0:774ff1e8b26b | 92 | |
kaizen | 0:774ff1e8b26b | 93 | if(len > 0) |
kaizen | 0:774ff1e8b26b | 94 | { |
kaizen | 0:774ff1e8b26b | 95 | WIZ_DBG("receive readable : %d\r\n",len); |
kaizen | 0:774ff1e8b26b | 96 | break; |
kaizen | 0:774ff1e8b26b | 97 | } |
kaizen | 0:774ff1e8b26b | 98 | time = tmr.read_ms(); |
kaizen | 0:774ff1e8b26b | 99 | } |
kaizen | 0:774ff1e8b26b | 100 | if(time >= _timeout) |
kaizen | 0:774ff1e8b26b | 101 | { |
kaizen | 0:774ff1e8b26b | 102 | //WIZ_DBG("receive timeout"); |
kaizen | 0:774ff1e8b26b | 103 | return 0; |
kaizen | 0:774ff1e8b26b | 104 | } |
kaizen | 0:774ff1e8b26b | 105 | } |
kaizen | 0:774ff1e8b26b | 106 | |
kaizen | 0:774ff1e8b26b | 107 | return _wizfi310->recv(_cid, data, length); |
kaizen | 0:774ff1e8b26b | 108 | } |
kaizen | 0:774ff1e8b26b | 109 | |
kaizen | 0:774ff1e8b26b | 110 | int TCPSocketConnection::receive_all(char* data, int length) |
kaizen | 0:774ff1e8b26b | 111 | { |
kaizen | 0:774ff1e8b26b | 112 | Timer tmr; |
kaizen | 0:774ff1e8b26b | 113 | int idx = 0; |
kaizen | 0:774ff1e8b26b | 114 | int time = -1; |
kaizen | 0:774ff1e8b26b | 115 | |
kaizen | 0:774ff1e8b26b | 116 | if(_cid < 0 || !is_connected()) return -1; |
kaizen | 0:774ff1e8b26b | 117 | |
kaizen | 0:774ff1e8b26b | 118 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 119 | |
kaizen | 0:774ff1e8b26b | 120 | while(time < _timeout || _blocking) |
kaizen | 0:774ff1e8b26b | 121 | { |
kaizen | 0:774ff1e8b26b | 122 | idx += _wizfi310->recv(_cid, &data[idx], length - idx); |
kaizen | 0:774ff1e8b26b | 123 | if (idx < 0) return -1; |
kaizen | 0:774ff1e8b26b | 124 | |
kaizen | 0:774ff1e8b26b | 125 | if(idx == length) |
kaizen | 0:774ff1e8b26b | 126 | break; |
kaizen | 0:774ff1e8b26b | 127 | |
kaizen | 0:774ff1e8b26b | 128 | time = tmr.read_ms(); |
kaizen | 0:774ff1e8b26b | 129 | } |
kaizen | 0:774ff1e8b26b | 130 | |
kaizen | 0:774ff1e8b26b | 131 | return (idx == 0) ? -1 : idx; |
kaizen | 0:774ff1e8b26b | 132 | } |
kaizen | 0:774ff1e8b26b | 133 | |
kaizen | 0:774ff1e8b26b | 134 | void TCPSocketConnection::acceptCID (int cid) |
kaizen | 0:774ff1e8b26b | 135 | { |
kaizen | 0:774ff1e8b26b | 136 | char *ip; |
kaizen | 0:774ff1e8b26b | 137 | int port; |
kaizen | 0:774ff1e8b26b | 138 | _server = true; |
kaizen | 0:774ff1e8b26b | 139 | _cid = cid; |
kaizen | 0:774ff1e8b26b | 140 | |
kaizen | 0:774ff1e8b26b | 141 | |
kaizen | 0:774ff1e8b26b | 142 | if( _wizfi310->cmdSMGMT(cid) ) return; |
kaizen | 0:774ff1e8b26b | 143 | if( !_wizfi310->getRemote(_cid, &ip, &port)) |
kaizen | 0:774ff1e8b26b | 144 | { |
kaizen | 0:774ff1e8b26b | 145 | set_address(ip, port); |
kaizen | 0:774ff1e8b26b | 146 | } |
kaizen | 0:774ff1e8b26b | 147 | } |