Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Wed Jul 21 19:25:56 2010 +0000
Revision:
0:355018f44c9f

        

Who changed what in which revision?

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