simple tester example >>> osc destination & receive

Dependencies:   mbed

Fork of myOSC_test by Alvaro Cassinelli

Committer:
sfjmt
Date:
Tue Aug 06 10:12:56 2013 +0000
Revision:
2:44f1e5803762
Parent:
0:f76708a93fb3
revision_01

Who changed what in which revision?

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