Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.
Dependents: christmasLights device_server pop3demo device_server_udp ... more
if/net/nettcpsocket.h@0:8b387bed54c2, 2011-01-10 (annotated)
- Committer:
- hlipka
- Date:
- Mon Jan 10 21:03:11 2011 +0000
- Revision:
- 0:8b387bed54c2
initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:8b387bed54c2 | 1 | |
hlipka | 0:8b387bed54c2 | 2 | /* |
hlipka | 0:8b387bed54c2 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
hlipka | 0:8b387bed54c2 | 4 | |
hlipka | 0:8b387bed54c2 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
hlipka | 0:8b387bed54c2 | 6 | of this software and associated documentation files (the "Software"), to deal |
hlipka | 0:8b387bed54c2 | 7 | in the Software without restriction, including without limitation the rights |
hlipka | 0:8b387bed54c2 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hlipka | 0:8b387bed54c2 | 9 | copies of the Software, and to permit persons to whom the Software is |
hlipka | 0:8b387bed54c2 | 10 | furnished to do so, subject to the following conditions: |
hlipka | 0:8b387bed54c2 | 11 | |
hlipka | 0:8b387bed54c2 | 12 | The above copyright notice and this permission notice shall be included in |
hlipka | 0:8b387bed54c2 | 13 | all copies or substantial portions of the Software. |
hlipka | 0:8b387bed54c2 | 14 | |
hlipka | 0:8b387bed54c2 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hlipka | 0:8b387bed54c2 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hlipka | 0:8b387bed54c2 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hlipka | 0:8b387bed54c2 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hlipka | 0:8b387bed54c2 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hlipka | 0:8b387bed54c2 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hlipka | 0:8b387bed54c2 | 21 | THE SOFTWARE. |
hlipka | 0:8b387bed54c2 | 22 | */ |
hlipka | 0:8b387bed54c2 | 23 | |
hlipka | 0:8b387bed54c2 | 24 | #ifndef NETTCPSOCKET_H |
hlipka | 0:8b387bed54c2 | 25 | #define NETTCPSOCKET_H |
hlipka | 0:8b387bed54c2 | 26 | |
hlipka | 0:8b387bed54c2 | 27 | #include "net.h" |
hlipka | 0:8b387bed54c2 | 28 | #include "host.h" |
hlipka | 0:8b387bed54c2 | 29 | |
hlipka | 0:8b387bed54c2 | 30 | #include <queue> |
hlipka | 0:8b387bed54c2 | 31 | using std::queue; |
hlipka | 0:8b387bed54c2 | 32 | |
hlipka | 0:8b387bed54c2 | 33 | //Implements a Berkeley-like socket if |
hlipka | 0:8b387bed54c2 | 34 | //Can be interfaced either to lwip or a Telit module |
hlipka | 0:8b387bed54c2 | 35 | |
hlipka | 0:8b387bed54c2 | 36 | enum NetTcpSocketErr |
hlipka | 0:8b387bed54c2 | 37 | { |
hlipka | 0:8b387bed54c2 | 38 | __NETTCPSOCKET_MIN = -0xFFFF, |
hlipka | 0:8b387bed54c2 | 39 | NETTCPSOCKET_SETUP, //NetTcpSocket not properly configured |
hlipka | 0:8b387bed54c2 | 40 | NETTCPSOCKET_TIMEOUT, |
hlipka | 0:8b387bed54c2 | 41 | NETTCPSOCKET_IF, //If has problems |
hlipka | 0:8b387bed54c2 | 42 | NETTCPSOCKET_MEM, //Not enough mem |
hlipka | 0:8b387bed54c2 | 43 | NETTCPSOCKET_INUSE, //If/Port is in use |
hlipka | 0:8b387bed54c2 | 44 | NETTCPSOCKET_EMPTY, //Connections queue is empty |
hlipka | 0:8b387bed54c2 | 45 | NETTCPSOCKET_RST, // Connection was reset by remote host |
hlipka | 0:8b387bed54c2 | 46 | //... |
hlipka | 0:8b387bed54c2 | 47 | NETTCPSOCKET_OK = 0 |
hlipka | 0:8b387bed54c2 | 48 | }; |
hlipka | 0:8b387bed54c2 | 49 | |
hlipka | 0:8b387bed54c2 | 50 | enum NetTcpSocketEvent |
hlipka | 0:8b387bed54c2 | 51 | { |
hlipka | 0:8b387bed54c2 | 52 | NETTCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening |
hlipka | 0:8b387bed54c2 | 53 | NETTCPSOCKET_ACCEPT, //Connected to client |
hlipka | 0:8b387bed54c2 | 54 | NETTCPSOCKET_READABLE, //Data in buf |
hlipka | 0:8b387bed54c2 | 55 | NETTCPSOCKET_WRITEABLE, //Can write data to buf |
hlipka | 0:8b387bed54c2 | 56 | NETTCPSOCKET_CONTIMEOUT, |
hlipka | 0:8b387bed54c2 | 57 | NETTCPSOCKET_CONRST, |
hlipka | 0:8b387bed54c2 | 58 | NETTCPSOCKET_CONABRT, |
hlipka | 0:8b387bed54c2 | 59 | NETTCPSOCKET_ERROR, |
hlipka | 0:8b387bed54c2 | 60 | NETTCPSOCKET_DISCONNECTED |
hlipka | 0:8b387bed54c2 | 61 | }; |
hlipka | 0:8b387bed54c2 | 62 | |
hlipka | 0:8b387bed54c2 | 63 | |
hlipka | 0:8b387bed54c2 | 64 | class NetTcpSocket |
hlipka | 0:8b387bed54c2 | 65 | { |
hlipka | 0:8b387bed54c2 | 66 | public: |
hlipka | 0:8b387bed54c2 | 67 | NetTcpSocket(); |
hlipka | 0:8b387bed54c2 | 68 | virtual ~NetTcpSocket(); //close() |
hlipka | 0:8b387bed54c2 | 69 | |
hlipka | 0:8b387bed54c2 | 70 | virtual NetTcpSocketErr bind(const Host& me) = 0; |
hlipka | 0:8b387bed54c2 | 71 | virtual NetTcpSocketErr listen() = 0; |
hlipka | 0:8b387bed54c2 | 72 | virtual NetTcpSocketErr connect(const Host& host) = 0; |
hlipka | 0:8b387bed54c2 | 73 | virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) = 0; |
hlipka | 0:8b387bed54c2 | 74 | |
hlipka | 0:8b387bed54c2 | 75 | virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len) = 0; |
hlipka | 0:8b387bed54c2 | 76 | virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len) = 0; |
hlipka | 0:8b387bed54c2 | 77 | |
hlipka | 0:8b387bed54c2 | 78 | virtual NetTcpSocketErr close() = 0; |
hlipka | 0:8b387bed54c2 | 79 | |
hlipka | 0:8b387bed54c2 | 80 | virtual NetTcpSocketErr poll() = 0; |
hlipka | 0:8b387bed54c2 | 81 | |
hlipka | 0:8b387bed54c2 | 82 | class CDummy; |
hlipka | 0:8b387bed54c2 | 83 | //Callbacks |
hlipka | 0:8b387bed54c2 | 84 | template<class T> |
hlipka | 0:8b387bed54c2 | 85 | //Linker bug : Must be defined here :( |
hlipka | 0:8b387bed54c2 | 86 | void setOnEvent( T* pItem, void (T::*pMethod)(NetTcpSocketEvent) ) |
hlipka | 0:8b387bed54c2 | 87 | { |
hlipka | 0:8b387bed54c2 | 88 | m_pCbItem = (CDummy*) pItem; |
hlipka | 0:8b387bed54c2 | 89 | m_pCbMeth = (void (CDummy::*)(NetTcpSocketEvent)) pMethod; |
hlipka | 0:8b387bed54c2 | 90 | } |
hlipka | 0:8b387bed54c2 | 91 | |
hlipka | 0:8b387bed54c2 | 92 | void resetOnEvent(); //Disable callback |
hlipka | 0:8b387bed54c2 | 93 | |
hlipka | 0:8b387bed54c2 | 94 | protected: |
hlipka | 0:8b387bed54c2 | 95 | void queueEvent(NetTcpSocketEvent e); |
hlipka | 0:8b387bed54c2 | 96 | void discardEvents(); |
hlipka | 0:8b387bed54c2 | 97 | void flushEvents(); //to be called during polling |
hlipka | 0:8b387bed54c2 | 98 | |
hlipka | 0:8b387bed54c2 | 99 | Host m_host; |
hlipka | 0:8b387bed54c2 | 100 | Host m_client; |
hlipka | 0:8b387bed54c2 | 101 | |
hlipka | 0:8b387bed54c2 | 102 | friend class Net; |
hlipka | 0:8b387bed54c2 | 103 | int m_refs; |
hlipka | 0:8b387bed54c2 | 104 | |
hlipka | 0:8b387bed54c2 | 105 | bool m_closed; |
hlipka | 0:8b387bed54c2 | 106 | bool m_removed; |
hlipka | 0:8b387bed54c2 | 107 | |
hlipka | 0:8b387bed54c2 | 108 | private: |
hlipka | 0:8b387bed54c2 | 109 | //We do not want to execute user code in interrupt routines, so we queue events until the server is polled |
hlipka | 0:8b387bed54c2 | 110 | //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) ) |
hlipka | 0:8b387bed54c2 | 111 | void onEvent(NetTcpSocketEvent e); //To be called on poll |
hlipka | 0:8b387bed54c2 | 112 | CDummy* m_pCbItem; |
hlipka | 0:8b387bed54c2 | 113 | void (CDummy::*m_pCbMeth)(NetTcpSocketEvent); |
hlipka | 0:8b387bed54c2 | 114 | queue<NetTcpSocketEvent> m_events; |
hlipka | 0:8b387bed54c2 | 115 | |
hlipka | 0:8b387bed54c2 | 116 | }; |
hlipka | 0:8b387bed54c2 | 117 | |
hlipka | 0:8b387bed54c2 | 118 | #endif |