![](/media/cache/profiles/68a15b5278e4f7c4c056df9d5f1d3b1f.jpg.50x50_q85.jpg)
save loops
EthernetNetIf/LPC2368/api/TCPSocket.h@1:3be7b7d050f4, 2014-12-02 (annotated)
- Committer:
- mbedalvaro
- Date:
- Tue Dec 02 08:29:59 2014 +0000
- Revision:
- 1:3be7b7d050f4
- Parent:
- 0:df6fdd9b99f0
updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 0:df6fdd9b99f0 | 1 | |
mbedalvaro | 0:df6fdd9b99f0 | 2 | /* |
mbedalvaro | 0:df6fdd9b99f0 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
mbedalvaro | 0:df6fdd9b99f0 | 4 | |
mbedalvaro | 0:df6fdd9b99f0 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
mbedalvaro | 0:df6fdd9b99f0 | 6 | of this software and associated documentation files (the "Software"), to deal |
mbedalvaro | 0:df6fdd9b99f0 | 7 | in the Software without restriction, including without limitation the rights |
mbedalvaro | 0:df6fdd9b99f0 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbedalvaro | 0:df6fdd9b99f0 | 9 | copies of the Software, and to permit persons to whom the Software is |
mbedalvaro | 0:df6fdd9b99f0 | 10 | furnished to do so, subject to the following conditions: |
mbedalvaro | 0:df6fdd9b99f0 | 11 | |
mbedalvaro | 0:df6fdd9b99f0 | 12 | The above copyright notice and this permission notice shall be included in |
mbedalvaro | 0:df6fdd9b99f0 | 13 | all copies or substantial portions of the Software. |
mbedalvaro | 0:df6fdd9b99f0 | 14 | |
mbedalvaro | 0:df6fdd9b99f0 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbedalvaro | 0:df6fdd9b99f0 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbedalvaro | 0:df6fdd9b99f0 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbedalvaro | 0:df6fdd9b99f0 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbedalvaro | 0:df6fdd9b99f0 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbedalvaro | 0:df6fdd9b99f0 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mbedalvaro | 0:df6fdd9b99f0 | 21 | THE SOFTWARE. |
mbedalvaro | 0:df6fdd9b99f0 | 22 | */ |
mbedalvaro | 0:df6fdd9b99f0 | 23 | |
mbedalvaro | 0:df6fdd9b99f0 | 24 | /** \file |
mbedalvaro | 0:df6fdd9b99f0 | 25 | TCP Socket header file |
mbedalvaro | 0:df6fdd9b99f0 | 26 | */ |
mbedalvaro | 0:df6fdd9b99f0 | 27 | |
mbedalvaro | 0:df6fdd9b99f0 | 28 | #ifndef TCPSOCKET_H |
mbedalvaro | 0:df6fdd9b99f0 | 29 | #define TCPSOCKET_H |
mbedalvaro | 0:df6fdd9b99f0 | 30 | |
mbedalvaro | 0:df6fdd9b99f0 | 31 | #include "core/net.h" |
mbedalvaro | 0:df6fdd9b99f0 | 32 | #include "core/host.h" |
mbedalvaro | 0:df6fdd9b99f0 | 33 | //Essentially it is a safe interface to NetTcpSocket |
mbedalvaro | 0:df6fdd9b99f0 | 34 | |
mbedalvaro | 0:df6fdd9b99f0 | 35 | ///TCP Socket error codes |
mbedalvaro | 0:df6fdd9b99f0 | 36 | enum TCPSocketErr |
mbedalvaro | 0:df6fdd9b99f0 | 37 | { |
mbedalvaro | 0:df6fdd9b99f0 | 38 | __TCPSOCKET_MIN = -0xFFFF, |
mbedalvaro | 0:df6fdd9b99f0 | 39 | TCPSOCKET_SETUP, ///<TCPSocket not properly configured |
mbedalvaro | 0:df6fdd9b99f0 | 40 | TCPSOCKET_TIMEOUT, ///<Connection timed out |
mbedalvaro | 0:df6fdd9b99f0 | 41 | TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized |
mbedalvaro | 0:df6fdd9b99f0 | 42 | TCPSOCKET_MEM, ///<Not enough mem |
mbedalvaro | 0:df6fdd9b99f0 | 43 | TCPSOCKET_INUSE, ///<Interface / Port is in use |
mbedalvaro | 0:df6fdd9b99f0 | 44 | TCPSOCKET_EMPTY, ///<Connections queue is empty |
mbedalvaro | 0:df6fdd9b99f0 | 45 | TCPSOCKET_RST, ///<Connection was reset by remote host |
mbedalvaro | 0:df6fdd9b99f0 | 46 | //... |
mbedalvaro | 0:df6fdd9b99f0 | 47 | TCPSOCKET_OK = 0 ///<Success |
mbedalvaro | 0:df6fdd9b99f0 | 48 | }; |
mbedalvaro | 0:df6fdd9b99f0 | 49 | |
mbedalvaro | 0:df6fdd9b99f0 | 50 | ///TCP Socket Events |
mbedalvaro | 0:df6fdd9b99f0 | 51 | enum TCPSocketEvent |
mbedalvaro | 0:df6fdd9b99f0 | 52 | { |
mbedalvaro | 0:df6fdd9b99f0 | 53 | TCPSOCKET_CONNECTED, ///<Connected to host |
mbedalvaro | 0:df6fdd9b99f0 | 54 | TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket |
mbedalvaro | 0:df6fdd9b99f0 | 55 | TCPSOCKET_READABLE, ///<Data in buf |
mbedalvaro | 0:df6fdd9b99f0 | 56 | TCPSOCKET_WRITEABLE, ///<Can write data to buf |
mbedalvaro | 0:df6fdd9b99f0 | 57 | TCPSOCKET_CONTIMEOUT, ///<Connection timed out |
mbedalvaro | 0:df6fdd9b99f0 | 58 | TCPSOCKET_CONRST, ///<Connection was reset by remote host |
mbedalvaro | 0:df6fdd9b99f0 | 59 | TCPSOCKET_CONABRT, ///<Connection was aborted |
mbedalvaro | 0:df6fdd9b99f0 | 60 | TCPSOCKET_ERROR, ///<Unknown error |
mbedalvaro | 0:df6fdd9b99f0 | 61 | TCPSOCKET_DISCONNECTED ///<Disconnected |
mbedalvaro | 0:df6fdd9b99f0 | 62 | }; |
mbedalvaro | 0:df6fdd9b99f0 | 63 | |
mbedalvaro | 0:df6fdd9b99f0 | 64 | class NetTcpSocket; |
mbedalvaro | 0:df6fdd9b99f0 | 65 | enum NetTcpSocketEvent; |
mbedalvaro | 0:df6fdd9b99f0 | 66 | |
mbedalvaro | 0:df6fdd9b99f0 | 67 | ///This is a simple TCP Socket class |
mbedalvaro | 0:df6fdd9b99f0 | 68 | /** |
mbedalvaro | 0:df6fdd9b99f0 | 69 | This class exposes an API to deal with TCP Sockets |
mbedalvaro | 0:df6fdd9b99f0 | 70 | */ |
mbedalvaro | 0:df6fdd9b99f0 | 71 | class TCPSocket |
mbedalvaro | 0:df6fdd9b99f0 | 72 | { |
mbedalvaro | 0:df6fdd9b99f0 | 73 | public: |
mbedalvaro | 0:df6fdd9b99f0 | 74 | ///Creates a new socket |
mbedalvaro | 0:df6fdd9b99f0 | 75 | TCPSocket(); |
mbedalvaro | 0:df6fdd9b99f0 | 76 | protected: |
mbedalvaro | 0:df6fdd9b99f0 | 77 | TCPSocket(NetTcpSocket* pNetTcpSocket); |
mbedalvaro | 0:df6fdd9b99f0 | 78 | public: |
mbedalvaro | 0:df6fdd9b99f0 | 79 | ///Closes if needed and destroys the socket |
mbedalvaro | 0:df6fdd9b99f0 | 80 | ~TCPSocket(); //close() |
mbedalvaro | 0:df6fdd9b99f0 | 81 | |
mbedalvaro | 0:df6fdd9b99f0 | 82 | ///Binds the socket to (local) host |
mbedalvaro | 0:df6fdd9b99f0 | 83 | TCPSocketErr bind(const Host& me); |
mbedalvaro | 0:df6fdd9b99f0 | 84 | |
mbedalvaro | 0:df6fdd9b99f0 | 85 | ///Starts listening |
mbedalvaro | 0:df6fdd9b99f0 | 86 | TCPSocketErr listen(); |
mbedalvaro | 0:df6fdd9b99f0 | 87 | |
mbedalvaro | 0:df6fdd9b99f0 | 88 | ///Connects socket to host |
mbedalvaro | 0:df6fdd9b99f0 | 89 | TCPSocketErr connect(const Host& host); |
mbedalvaro | 0:df6fdd9b99f0 | 90 | |
mbedalvaro | 0:df6fdd9b99f0 | 91 | ///Accepts connection from client and gets connected socket |
mbedalvaro | 0:df6fdd9b99f0 | 92 | TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket); |
mbedalvaro | 0:df6fdd9b99f0 | 93 | |
mbedalvaro | 0:df6fdd9b99f0 | 94 | ///Sends data |
mbedalvaro | 0:df6fdd9b99f0 | 95 | /* |
mbedalvaro | 0:df6fdd9b99f0 | 96 | @return a negative error code or the number of bytes transmitted |
mbedalvaro | 0:df6fdd9b99f0 | 97 | */ |
mbedalvaro | 0:df6fdd9b99f0 | 98 | int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len); |
mbedalvaro | 0:df6fdd9b99f0 | 99 | |
mbedalvaro | 0:df6fdd9b99f0 | 100 | ///Receives data |
mbedalvaro | 0:df6fdd9b99f0 | 101 | /* |
mbedalvaro | 0:df6fdd9b99f0 | 102 | @return a negative error code or the number of bytes received |
mbedalvaro | 0:df6fdd9b99f0 | 103 | */ |
mbedalvaro | 0:df6fdd9b99f0 | 104 | int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len); |
mbedalvaro | 0:df6fdd9b99f0 | 105 | |
mbedalvaro | 0:df6fdd9b99f0 | 106 | /* TODO NTH : printf / scanf helpers that call send/recv */ |
mbedalvaro | 0:df6fdd9b99f0 | 107 | |
mbedalvaro | 0:df6fdd9b99f0 | 108 | ///Closes socket |
mbedalvaro | 0:df6fdd9b99f0 | 109 | TCPSocketErr close(); |
mbedalvaro | 0:df6fdd9b99f0 | 110 | |
mbedalvaro | 0:df6fdd9b99f0 | 111 | //Callbacks |
mbedalvaro | 0:df6fdd9b99f0 | 112 | ///Setups callback |
mbedalvaro | 0:df6fdd9b99f0 | 113 | /** |
mbedalvaro | 0:df6fdd9b99f0 | 114 | @param pMethod : callback function |
mbedalvaro | 0:df6fdd9b99f0 | 115 | */ |
mbedalvaro | 0:df6fdd9b99f0 | 116 | void setOnEvent( void (*pMethod)(TCPSocketEvent) ); |
mbedalvaro | 0:df6fdd9b99f0 | 117 | |
mbedalvaro | 0:df6fdd9b99f0 | 118 | class CDummy; |
mbedalvaro | 0:df6fdd9b99f0 | 119 | ///Setups callback |
mbedalvaro | 0:df6fdd9b99f0 | 120 | /** |
mbedalvaro | 0:df6fdd9b99f0 | 121 | @param pItem : instance of class on which to execute the callback method |
mbedalvaro | 0:df6fdd9b99f0 | 122 | @param pMethod : callback method |
mbedalvaro | 0:df6fdd9b99f0 | 123 | */ |
mbedalvaro | 0:df6fdd9b99f0 | 124 | template<class T> |
mbedalvaro | 0:df6fdd9b99f0 | 125 | void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) ) |
mbedalvaro | 0:df6fdd9b99f0 | 126 | { |
mbedalvaro | 0:df6fdd9b99f0 | 127 | m_pCbItem = (CDummy*) pItem; |
mbedalvaro | 0:df6fdd9b99f0 | 128 | m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod; |
mbedalvaro | 0:df6fdd9b99f0 | 129 | } |
mbedalvaro | 0:df6fdd9b99f0 | 130 | |
mbedalvaro | 0:df6fdd9b99f0 | 131 | ///Disables callback |
mbedalvaro | 0:df6fdd9b99f0 | 132 | void resetOnEvent(); |
mbedalvaro | 0:df6fdd9b99f0 | 133 | |
mbedalvaro | 0:df6fdd9b99f0 | 134 | protected: |
mbedalvaro | 0:df6fdd9b99f0 | 135 | void onNetTcpSocketEvent(NetTcpSocketEvent e); |
mbedalvaro | 0:df6fdd9b99f0 | 136 | TCPSocketErr checkInst(); |
mbedalvaro | 0:df6fdd9b99f0 | 137 | |
mbedalvaro | 0:df6fdd9b99f0 | 138 | private: |
mbedalvaro | 0:df6fdd9b99f0 | 139 | NetTcpSocket* m_pNetTcpSocket; |
mbedalvaro | 0:df6fdd9b99f0 | 140 | |
mbedalvaro | 0:df6fdd9b99f0 | 141 | CDummy* m_pCbItem; |
mbedalvaro | 0:df6fdd9b99f0 | 142 | void (CDummy::*m_pCbMeth)(TCPSocketEvent); |
mbedalvaro | 0:df6fdd9b99f0 | 143 | |
mbedalvaro | 0:df6fdd9b99f0 | 144 | void (*m_pCb)(TCPSocketEvent); |
mbedalvaro | 0:df6fdd9b99f0 | 145 | |
mbedalvaro | 0:df6fdd9b99f0 | 146 | }; |
mbedalvaro | 0:df6fdd9b99f0 | 147 | |
mbedalvaro | 0:df6fdd9b99f0 | 148 | #endif |