Dependents:   RTnoV3 RTnoV3_LED RTnoV3_Template RTnoV3_ADC ... more

Committer:
sherckuith
Date:
Sat Dec 31 11:25:27 2011 +0000
Revision:
0:479ce5546098
Ethernet

Who changed what in which revision?

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