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