http://mbed.org/users/okini3939/notebook/logger/

Dependencies:   mbed

Committer:
okini3939
Date:
Sat Apr 16 15:15:48 2011 +0000
Revision:
0:a3c892f39cc1

        

Who changed what in which revision?

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