Dependents:   SNMPAgent HTTPServer think_speak_a cyassl-client ... more

Committer:
mamezu
Date:
Thu Dec 09 01:33:56 2010 +0000
Revision:
0:0f6c82fcde82

        

Who changed what in which revision?

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