123
TcpClient.h@11:647d53d146f1, 2019-08-30 (annotated)
- Committer:
- hudakz
- Date:
- Fri Aug 30 08:11:40 2019 +0000
- Revision:
- 11:647d53d146f1
- Parent:
- 10:e4ddab81e6a8
- Child:
- 13:95c00132cd98
"set_network" method of Mbed style added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 9:a156d3de5647 | 1 | /* |
hudakz | 9:a156d3de5647 | 2 | UIPClient.h - Arduino implementation of a UIP wrapper class. |
hudakz | 9:a156d3de5647 | 3 | Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> |
hudakz | 9:a156d3de5647 | 4 | All rights reserved. |
hudakz | 9:a156d3de5647 | 5 | |
hudakz | 9:a156d3de5647 | 6 | This program is free software: you can redistribute it and/or modify |
hudakz | 9:a156d3de5647 | 7 | it under the terms of the GNU General Public License as published by |
hudakz | 9:a156d3de5647 | 8 | the Free Software Foundation, either version 3 of the License, or |
hudakz | 9:a156d3de5647 | 9 | (at your option) any later version. |
hudakz | 9:a156d3de5647 | 10 | |
hudakz | 9:a156d3de5647 | 11 | This program is distributed in the hope that it will be useful, |
hudakz | 9:a156d3de5647 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 9:a156d3de5647 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 9:a156d3de5647 | 14 | GNU General Public License for more details. |
hudakz | 9:a156d3de5647 | 15 | |
hudakz | 9:a156d3de5647 | 16 | You should have received a copy of the GNU General Public License |
hudakz | 9:a156d3de5647 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hudakz | 9:a156d3de5647 | 18 | */ |
hudakz | 11:647d53d146f1 | 19 | #ifndef TCPCLIENT_h |
hudakz | 11:647d53d146f1 | 20 | #define TCPCLIENT_h |
hudakz | 9:a156d3de5647 | 21 | |
hudakz | 9:a156d3de5647 | 22 | #include "mbed.h" |
hudakz | 10:e4ddab81e6a8 | 23 | #include "IpAddress.h" |
hudakz | 9:a156d3de5647 | 24 | #include "utility/MemPool.h" |
hudakz | 9:a156d3de5647 | 25 | |
hudakz | 9:a156d3de5647 | 26 | extern "C" |
hudakz | 9:a156d3de5647 | 27 | { |
hudakz | 9:a156d3de5647 | 28 | #include "utility/uip.h" |
hudakz | 9:a156d3de5647 | 29 | } |
hudakz | 9:a156d3de5647 | 30 | #define UIP_SOCKET_DATALEN UIP_TCP_MSS |
hudakz | 9:a156d3de5647 | 31 | //#define UIP_SOCKET_NUMPACKETS UIP_RECEIVE_WINDOW/UIP_TCP_MSS+1 |
hudakz | 9:a156d3de5647 | 32 | |
hudakz | 9:a156d3de5647 | 33 | #ifndef UIP_SOCKET_NUMPACKETS |
hudakz | 9:a156d3de5647 | 34 | #define UIP_SOCKET_NUMPACKETS 5 |
hudakz | 9:a156d3de5647 | 35 | #endif |
hudakz | 9:a156d3de5647 | 36 | #define UIP_CLIENT_CONNECTED 0x10 |
hudakz | 9:a156d3de5647 | 37 | #define UIP_CLIENT_CLOSE 0x20 |
hudakz | 9:a156d3de5647 | 38 | #define UIP_CLIENT_REMOTECLOSED 0x40 |
hudakz | 9:a156d3de5647 | 39 | #define UIP_CLIENT_RESTART 0x80 |
hudakz | 9:a156d3de5647 | 40 | #define UIP_CLIENT_STATEFLAGS (UIP_CLIENT_CONNECTED | UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED | UIP_CLIENT_RESTART) |
hudakz | 9:a156d3de5647 | 41 | #define UIP_CLIENT_SOCKETS ~UIP_CLIENT_STATEFLAGS |
hudakz | 9:a156d3de5647 | 42 | |
hudakz | 9:a156d3de5647 | 43 | typedef uint8_t uip_socket_ptr; |
hudakz | 9:a156d3de5647 | 44 | |
hudakz | 9:a156d3de5647 | 45 | typedef struct |
hudakz | 9:a156d3de5647 | 46 | { |
hudakz | 10:e4ddab81e6a8 | 47 | uint8_t state; |
hudakz | 10:e4ddab81e6a8 | 48 | memhandle packets_in[UIP_SOCKET_NUMPACKETS]; |
hudakz | 10:e4ddab81e6a8 | 49 | uint16_t lport; /**< The local TCP port, in network byte order. */ |
hudakz | 9:a156d3de5647 | 50 | } uip_userdata_closed_t; |
hudakz | 9:a156d3de5647 | 51 | |
hudakz | 9:a156d3de5647 | 52 | typedef struct |
hudakz | 9:a156d3de5647 | 53 | { |
hudakz | 10:e4ddab81e6a8 | 54 | Timer pollTimer; |
hudakz | 10:e4ddab81e6a8 | 55 | uint8_t state; |
hudakz | 10:e4ddab81e6a8 | 56 | uip_ipaddr_t ripaddr; /**< The IP address of the remote host. */ |
hudakz | 10:e4ddab81e6a8 | 57 | memhandle packets_in[UIP_SOCKET_NUMPACKETS]; |
hudakz | 10:e4ddab81e6a8 | 58 | memhandle packets_out[UIP_SOCKET_NUMPACKETS]; |
hudakz | 10:e4ddab81e6a8 | 59 | memaddress out_pos; |
hudakz | 9:a156d3de5647 | 60 | } uip_userdata_t; |
hudakz | 9:a156d3de5647 | 61 | |
hudakz | 10:e4ddab81e6a8 | 62 | class TcpClient |
hudakz | 9:a156d3de5647 | 63 | { |
hudakz | 9:a156d3de5647 | 64 | public: |
hudakz | 9:a156d3de5647 | 65 | TcpClient(); |
hudakz | 9:a156d3de5647 | 66 | TcpClient(uip_userdata_t* conn_data); |
hudakz | 9:a156d3de5647 | 67 | virtual ~TcpClient() {} |
hudakz | 10:e4ddab81e6a8 | 68 | |
hudakz | 9:a156d3de5647 | 69 | int connect(IpAddress ip, uint16_t port); |
hudakz | 9:a156d3de5647 | 70 | int connect(const char* host, uint16_t port); |
hudakz | 10:e4ddab81e6a8 | 71 | int recv(uint8_t* buf, size_t size); |
hudakz | 9:a156d3de5647 | 72 | void stop(); |
hudakz | 9:a156d3de5647 | 73 | uint8_t connected(); |
hudakz | 9:a156d3de5647 | 74 | operator bool(); |
hudakz | 9:a156d3de5647 | 75 | virtual bool operator ==(const TcpClient& ); |
hudakz | 9:a156d3de5647 | 76 | virtual bool operator !=(const TcpClient& rhs) { return !this->operator ==(rhs); } |
hudakz | 11:647d53d146f1 | 77 | int send(uint8_t); |
hudakz | 11:647d53d146f1 | 78 | int send(const uint8_t* buf, size_t size); |
hudakz | 10:e4ddab81e6a8 | 79 | size_t available(); |
hudakz | 11:647d53d146f1 | 80 | int recv(); |
hudakz | 9:a156d3de5647 | 81 | int peek(); |
hudakz | 9:a156d3de5647 | 82 | void flush(); |
hudakz | 10:e4ddab81e6a8 | 83 | const char* getpeername(); |
hudakz | 10:e4ddab81e6a8 | 84 | IpAddress getRemoteIp(); |
hudakz | 9:a156d3de5647 | 85 | void close(); |
hudakz | 9:a156d3de5647 | 86 | |
hudakz | 9:a156d3de5647 | 87 | static uip_userdata_t all_data[UIP_CONNS]; |
hudakz | 11:647d53d146f1 | 88 | static int _write(uip_userdata_t* , const uint8_t* buf, size_t size); |
hudakz | 9:a156d3de5647 | 89 | |
hudakz | 10:e4ddab81e6a8 | 90 | protected: |
hudakz | 10:e4ddab81e6a8 | 91 | uint8_t* rawIPAddress(IpAddress& addr) { return addr.rawAddress(); } |
hudakz | 10:e4ddab81e6a8 | 92 | |
hudakz | 9:a156d3de5647 | 93 | private: |
hudakz | 9:a156d3de5647 | 94 | uip_userdata_t* data; |
hudakz | 9:a156d3de5647 | 95 | static uip_userdata_t* _allocateData(); |
hudakz | 10:e4ddab81e6a8 | 96 | static size_t _available(uip_userdata_t* ); |
hudakz | 9:a156d3de5647 | 97 | static uint8_t _currentBlock(memhandle* blocks); |
hudakz | 9:a156d3de5647 | 98 | static void _eatBlock(memhandle* blocks); |
hudakz | 9:a156d3de5647 | 99 | static void _flushBlocks(memhandle* blocks); |
hudakz | 9:a156d3de5647 | 100 | |
hudakz | 9:a156d3de5647 | 101 | #ifdef UIPETHERNET_DEBUG_CLIENT |
hudakz | 9:a156d3de5647 | 102 | static void _dumpAllData(); |
hudakz | 9:a156d3de5647 | 103 | #endif |
hudakz | 9:a156d3de5647 | 104 | friend class UipEthernet; |
hudakz | 9:a156d3de5647 | 105 | friend class TcpServer; |
hudakz | 9:a156d3de5647 | 106 | friend void uipclient_appcall(); |
hudakz | 9:a156d3de5647 | 107 | }; |
hudakz | 9:a156d3de5647 | 108 | #endif |