Committer:
mbed714
Date:
Tue Nov 09 19:32:44 2010 +0000
Revision:
3:0c324737064d
Parent:
0:98aadd37a5c5

        

Who changed what in which revision?

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