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