This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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