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 /** \file
hattori_atsushi 0:f77369cabd75 25 TCP Socket header file
hattori_atsushi 0:f77369cabd75 26 */
hattori_atsushi 0:f77369cabd75 27
hattori_atsushi 0:f77369cabd75 28 #ifndef TCPSOCKET_H
hattori_atsushi 0:f77369cabd75 29 #define TCPSOCKET_H
hattori_atsushi 0:f77369cabd75 30
hattori_atsushi 0:f77369cabd75 31 #include "core/net.h"
hattori_atsushi 0:f77369cabd75 32 #include "core/host.h"
hattori_atsushi 0:f77369cabd75 33 //Essentially it is a safe interface to NetTcpSocket
hattori_atsushi 0:f77369cabd75 34
hattori_atsushi 0:f77369cabd75 35 ///TCP Socket error codes
hattori_atsushi 0:f77369cabd75 36 enum TCPSocketErr
hattori_atsushi 0:f77369cabd75 37 {
hattori_atsushi 0:f77369cabd75 38 __TCPSOCKET_MIN = -0xFFFF,
hattori_atsushi 0:f77369cabd75 39 TCPSOCKET_SETUP, ///<TCPSocket not properly configured
hattori_atsushi 0:f77369cabd75 40 TCPSOCKET_TIMEOUT, ///<Connection timed out
hattori_atsushi 0:f77369cabd75 41 TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
hattori_atsushi 0:f77369cabd75 42 TCPSOCKET_MEM, ///<Not enough mem
hattori_atsushi 0:f77369cabd75 43 TCPSOCKET_INUSE, ///<Interface / Port is in use
hattori_atsushi 0:f77369cabd75 44 TCPSOCKET_EMPTY, ///<Connections queue is empty
hattori_atsushi 0:f77369cabd75 45 TCPSOCKET_RST, ///<Connection was reset by remote host
hattori_atsushi 0:f77369cabd75 46 //...
hattori_atsushi 0:f77369cabd75 47 TCPSOCKET_OK = 0 ///<Success
hattori_atsushi 0:f77369cabd75 48 };
hattori_atsushi 0:f77369cabd75 49
hattori_atsushi 0:f77369cabd75 50 ///TCP Socket Events
hattori_atsushi 0:f77369cabd75 51 enum TCPSocketEvent
hattori_atsushi 0:f77369cabd75 52 {
hattori_atsushi 0:f77369cabd75 53 TCPSOCKET_CONNECTED, ///<Connected to host
hattori_atsushi 0:f77369cabd75 54 TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket
hattori_atsushi 0:f77369cabd75 55 TCPSOCKET_READABLE, ///<Data in buf
hattori_atsushi 0:f77369cabd75 56 TCPSOCKET_WRITEABLE, ///<Can write data to buf
hattori_atsushi 0:f77369cabd75 57 TCPSOCKET_CONTIMEOUT, ///<Connection timed out
hattori_atsushi 0:f77369cabd75 58 TCPSOCKET_CONRST, ///<Connection was reset by remote host
hattori_atsushi 0:f77369cabd75 59 TCPSOCKET_CONABRT, ///<Connection was aborted
hattori_atsushi 0:f77369cabd75 60 TCPSOCKET_ERROR, ///<Unknown error
hattori_atsushi 0:f77369cabd75 61 TCPSOCKET_DISCONNECTED ///<Disconnected
hattori_atsushi 0:f77369cabd75 62 };
hattori_atsushi 0:f77369cabd75 63
hattori_atsushi 0:f77369cabd75 64 class NetTcpSocket;
hattori_atsushi 0:f77369cabd75 65 enum NetTcpSocketEvent;
hattori_atsushi 0:f77369cabd75 66
hattori_atsushi 0:f77369cabd75 67 ///This is a simple TCP Socket class
hattori_atsushi 0:f77369cabd75 68 /**
hattori_atsushi 0:f77369cabd75 69 This class exposes an API to deal with TCP Sockets
hattori_atsushi 0:f77369cabd75 70 */
hattori_atsushi 0:f77369cabd75 71 class TCPSocket
hattori_atsushi 0:f77369cabd75 72 {
hattori_atsushi 0:f77369cabd75 73 public:
hattori_atsushi 0:f77369cabd75 74 ///Creates a new socket
hattori_atsushi 0:f77369cabd75 75 TCPSocket();
hattori_atsushi 0:f77369cabd75 76 protected:
hattori_atsushi 0:f77369cabd75 77 TCPSocket(NetTcpSocket* pNetTcpSocket);
hattori_atsushi 0:f77369cabd75 78 public:
hattori_atsushi 0:f77369cabd75 79 ///Closes if needed and destroys the socket
hattori_atsushi 0:f77369cabd75 80 ~TCPSocket(); //close()
hattori_atsushi 0:f77369cabd75 81
hattori_atsushi 0:f77369cabd75 82 ///Binds the socket to (local) host
hattori_atsushi 0:f77369cabd75 83 TCPSocketErr bind(const Host& me);
hattori_atsushi 0:f77369cabd75 84
hattori_atsushi 0:f77369cabd75 85 ///Starts listening
hattori_atsushi 0:f77369cabd75 86 TCPSocketErr listen();
hattori_atsushi 0:f77369cabd75 87
hattori_atsushi 0:f77369cabd75 88 ///Connects socket to host
hattori_atsushi 0:f77369cabd75 89 TCPSocketErr connect(const Host& host);
hattori_atsushi 0:f77369cabd75 90
hattori_atsushi 0:f77369cabd75 91 ///Accepts connection from client and gets connected socket
hattori_atsushi 0:f77369cabd75 92 TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
hattori_atsushi 0:f77369cabd75 93
hattori_atsushi 0:f77369cabd75 94 ///Sends data
hattori_atsushi 0:f77369cabd75 95 /*
hattori_atsushi 0:f77369cabd75 96 @return a negative error code or the number of bytes transmitted
hattori_atsushi 0:f77369cabd75 97 */
hattori_atsushi 0:f77369cabd75 98 int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
hattori_atsushi 0:f77369cabd75 99
hattori_atsushi 0:f77369cabd75 100 ///Receives data
hattori_atsushi 0:f77369cabd75 101 /*
hattori_atsushi 0:f77369cabd75 102 @return a negative error code or the number of bytes received
hattori_atsushi 0:f77369cabd75 103 */
hattori_atsushi 0:f77369cabd75 104 int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
hattori_atsushi 0:f77369cabd75 105
hattori_atsushi 0:f77369cabd75 106 /* TODO NTH : printf / scanf helpers that call send/recv */
hattori_atsushi 0:f77369cabd75 107
hattori_atsushi 0:f77369cabd75 108 ///Closes socket
hattori_atsushi 0:f77369cabd75 109 TCPSocketErr close();
hattori_atsushi 0:f77369cabd75 110
hattori_atsushi 0:f77369cabd75 111 //Callbacks
hattori_atsushi 0:f77369cabd75 112 ///Setups callback
hattori_atsushi 0:f77369cabd75 113 /**
hattori_atsushi 0:f77369cabd75 114 @param pMethod : callback function
hattori_atsushi 0:f77369cabd75 115 */
hattori_atsushi 0:f77369cabd75 116 void setOnEvent( void (*pMethod)(TCPSocketEvent) );
hattori_atsushi 0:f77369cabd75 117
hattori_atsushi 0:f77369cabd75 118 class CDummy;
hattori_atsushi 0:f77369cabd75 119 ///Setups callback
hattori_atsushi 0:f77369cabd75 120 /**
hattori_atsushi 0:f77369cabd75 121 @param pItem : instance of class on which to execute the callback method
hattori_atsushi 0:f77369cabd75 122 @param pMethod : callback method
hattori_atsushi 0:f77369cabd75 123 */
hattori_atsushi 0:f77369cabd75 124 template<class T>
hattori_atsushi 0:f77369cabd75 125 void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
hattori_atsushi 0:f77369cabd75 126 {
hattori_atsushi 0:f77369cabd75 127 m_pCbItem = (CDummy*) pItem;
hattori_atsushi 0:f77369cabd75 128 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
hattori_atsushi 0:f77369cabd75 129 }
hattori_atsushi 0:f77369cabd75 130
hattori_atsushi 0:f77369cabd75 131 ///Disables callback
hattori_atsushi 0:f77369cabd75 132 void resetOnEvent();
hattori_atsushi 0:f77369cabd75 133
hattori_atsushi 0:f77369cabd75 134 protected:
hattori_atsushi 0:f77369cabd75 135 void onNetTcpSocketEvent(NetTcpSocketEvent e);
hattori_atsushi 0:f77369cabd75 136 TCPSocketErr checkInst();
hattori_atsushi 0:f77369cabd75 137
hattori_atsushi 0:f77369cabd75 138 private:
hattori_atsushi 0:f77369cabd75 139 NetTcpSocket* m_pNetTcpSocket;
hattori_atsushi 0:f77369cabd75 140
hattori_atsushi 0:f77369cabd75 141 CDummy* m_pCbItem;
hattori_atsushi 0:f77369cabd75 142 void (CDummy::*m_pCbMeth)(TCPSocketEvent);
hattori_atsushi 0:f77369cabd75 143
hattori_atsushi 0:f77369cabd75 144 void (*m_pCb)(TCPSocketEvent);
hattori_atsushi 0:f77369cabd75 145
hattori_atsushi 0:f77369cabd75 146 };
hattori_atsushi 0:f77369cabd75 147
hattori_atsushi 0:f77369cabd75 148 #endif