hattori&ide

Dependencies:   mbed

Committer:
hattori_atsushi
Date:
Sun Dec 18 08:16:01 2022 +0000
Revision:
0:f77369cabd75
hattori

Who changed what in which revision?

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