Dependents:   RTnoV3 RTnoV3_LED RTnoV3_Template RTnoV3_ADC ... more

Committer:
sherckuith
Date:
Sat Dec 31 11:25:27 2011 +0000
Revision:
0:479ce5546098
Ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sherckuith 0:479ce5546098 1
sherckuith 0:479ce5546098 2 /*
sherckuith 0:479ce5546098 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
sherckuith 0:479ce5546098 4
sherckuith 0:479ce5546098 5 Permission is hereby granted, free of charge, to any person obtaining a copy
sherckuith 0:479ce5546098 6 of this software and associated documentation files (the "Software"), to deal
sherckuith 0:479ce5546098 7 in the Software without restriction, including without limitation the rights
sherckuith 0:479ce5546098 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
sherckuith 0:479ce5546098 9 copies of the Software, and to permit persons to whom the Software is
sherckuith 0:479ce5546098 10 furnished to do so, subject to the following conditions:
sherckuith 0:479ce5546098 11
sherckuith 0:479ce5546098 12 The above copyright notice and this permission notice shall be included in
sherckuith 0:479ce5546098 13 all copies or substantial portions of the Software.
sherckuith 0:479ce5546098 14
sherckuith 0:479ce5546098 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
sherckuith 0:479ce5546098 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
sherckuith 0:479ce5546098 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
sherckuith 0:479ce5546098 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
sherckuith 0:479ce5546098 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sherckuith 0:479ce5546098 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
sherckuith 0:479ce5546098 21 THE SOFTWARE.
sherckuith 0:479ce5546098 22 */
sherckuith 0:479ce5546098 23
sherckuith 0:479ce5546098 24 /** \file
sherckuith 0:479ce5546098 25 TCP Socket header file
sherckuith 0:479ce5546098 26 */
sherckuith 0:479ce5546098 27
sherckuith 0:479ce5546098 28 #ifndef TCPSOCKET_H
sherckuith 0:479ce5546098 29 #define TCPSOCKET_H
sherckuith 0:479ce5546098 30
sherckuith 0:479ce5546098 31 #include "core/net.h"
sherckuith 0:479ce5546098 32 #include "core/host.h"
sherckuith 0:479ce5546098 33 //Essentially it is a safe interface to NetTcpSocket
sherckuith 0:479ce5546098 34
sherckuith 0:479ce5546098 35 ///TCP Socket error codes
sherckuith 0:479ce5546098 36 enum TCPSocketErr
sherckuith 0:479ce5546098 37 {
sherckuith 0:479ce5546098 38 __TCPSOCKET_MIN = -0xFFFF,
sherckuith 0:479ce5546098 39 TCPSOCKET_SETUP, ///<TCPSocket not properly configured
sherckuith 0:479ce5546098 40 TCPSOCKET_TIMEOUT, ///<Connection timed out
sherckuith 0:479ce5546098 41 TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
sherckuith 0:479ce5546098 42 TCPSOCKET_MEM, ///<Not enough mem
sherckuith 0:479ce5546098 43 TCPSOCKET_INUSE, ///<Interface / Port is in use
sherckuith 0:479ce5546098 44 TCPSOCKET_EMPTY, ///<Connections queue is empty
sherckuith 0:479ce5546098 45 TCPSOCKET_RST, ///<Connection was reset by remote host
sherckuith 0:479ce5546098 46 //...
sherckuith 0:479ce5546098 47 TCPSOCKET_OK = 0 ///<Success
sherckuith 0:479ce5546098 48 };
sherckuith 0:479ce5546098 49
sherckuith 0:479ce5546098 50 ///TCP Socket Events
sherckuith 0:479ce5546098 51 enum TCPSocketEvent
sherckuith 0:479ce5546098 52 {
sherckuith 0:479ce5546098 53 TCPSOCKET_CONNECTED, ///<Connected to host
sherckuith 0:479ce5546098 54 TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket
sherckuith 0:479ce5546098 55 TCPSOCKET_READABLE, ///<Data in buf
sherckuith 0:479ce5546098 56 TCPSOCKET_WRITEABLE, ///<Can write data to buf
sherckuith 0:479ce5546098 57 TCPSOCKET_CONTIMEOUT, ///<Connection timed out
sherckuith 0:479ce5546098 58 TCPSOCKET_CONRST, ///<Connection was reset by remote host
sherckuith 0:479ce5546098 59 TCPSOCKET_CONABRT, ///<Connection was aborted
sherckuith 0:479ce5546098 60 TCPSOCKET_ERROR, ///<Unknown error
sherckuith 0:479ce5546098 61 TCPSOCKET_DISCONNECTED ///<Disconnected
sherckuith 0:479ce5546098 62 };
sherckuith 0:479ce5546098 63
sherckuith 0:479ce5546098 64 class NetTcpSocket;
sherckuith 0:479ce5546098 65 enum NetTcpSocketEvent;
sherckuith 0:479ce5546098 66
sherckuith 0:479ce5546098 67 ///This is a simple TCP Socket class
sherckuith 0:479ce5546098 68 /**
sherckuith 0:479ce5546098 69 This class exposes an API to deal with TCP Sockets
sherckuith 0:479ce5546098 70 */
sherckuith 0:479ce5546098 71 class TCPSocket
sherckuith 0:479ce5546098 72 {
sherckuith 0:479ce5546098 73 public:
sherckuith 0:479ce5546098 74 ///Creates a new socket
sherckuith 0:479ce5546098 75 TCPSocket();
sherckuith 0:479ce5546098 76 protected:
sherckuith 0:479ce5546098 77 TCPSocket(NetTcpSocket* pNetTcpSocket);
sherckuith 0:479ce5546098 78 public:
sherckuith 0:479ce5546098 79 ///Closes if needed and destroys the socket
sherckuith 0:479ce5546098 80 ~TCPSocket(); //close()
sherckuith 0:479ce5546098 81
sherckuith 0:479ce5546098 82 ///Binds the socket to (local) host
sherckuith 0:479ce5546098 83 TCPSocketErr bind(const Host& me);
sherckuith 0:479ce5546098 84
sherckuith 0:479ce5546098 85 ///Starts listening
sherckuith 0:479ce5546098 86 TCPSocketErr listen();
sherckuith 0:479ce5546098 87
sherckuith 0:479ce5546098 88 ///Connects socket to host
sherckuith 0:479ce5546098 89 TCPSocketErr connect(const Host& host);
sherckuith 0:479ce5546098 90
sherckuith 0:479ce5546098 91 ///Accepts connection from client and gets connected socket
sherckuith 0:479ce5546098 92 TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
sherckuith 0:479ce5546098 93
sherckuith 0:479ce5546098 94 ///Sends data
sherckuith 0:479ce5546098 95 /*
sherckuith 0:479ce5546098 96 @return a negative error code or the number of bytes transmitted
sherckuith 0:479ce5546098 97 */
sherckuith 0:479ce5546098 98 int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
sherckuith 0:479ce5546098 99
sherckuith 0:479ce5546098 100 ///Receives data
sherckuith 0:479ce5546098 101 /*
sherckuith 0:479ce5546098 102 @return a negative error code or the number of bytes received
sherckuith 0:479ce5546098 103 */
sherckuith 0:479ce5546098 104 int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
sherckuith 0:479ce5546098 105
sherckuith 0:479ce5546098 106 /* TODO NTH : printf / scanf helpers that call send/recv */
sherckuith 0:479ce5546098 107
sherckuith 0:479ce5546098 108 ///Closes socket
sherckuith 0:479ce5546098 109 TCPSocketErr close();
sherckuith 0:479ce5546098 110
sherckuith 0:479ce5546098 111 //Callbacks
sherckuith 0:479ce5546098 112 ///Setups callback
sherckuith 0:479ce5546098 113 /**
sherckuith 0:479ce5546098 114 @param pMethod : callback function
sherckuith 0:479ce5546098 115 */
sherckuith 0:479ce5546098 116 void setOnEvent( void (*pMethod)(TCPSocketEvent) );
sherckuith 0:479ce5546098 117
sherckuith 0:479ce5546098 118 class CDummy;
sherckuith 0:479ce5546098 119 ///Setups callback
sherckuith 0:479ce5546098 120 /**
sherckuith 0:479ce5546098 121 @param pItem : instance of class on which to execute the callback method
sherckuith 0:479ce5546098 122 @param pMethod : callback method
sherckuith 0:479ce5546098 123 */
sherckuith 0:479ce5546098 124 template<class T>
sherckuith 0:479ce5546098 125 void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
sherckuith 0:479ce5546098 126 {
sherckuith 0:479ce5546098 127 m_pCbItem = (CDummy*) pItem;
sherckuith 0:479ce5546098 128 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
sherckuith 0:479ce5546098 129 }
sherckuith 0:479ce5546098 130
sherckuith 0:479ce5546098 131 ///Disables callback
sherckuith 0:479ce5546098 132 void resetOnEvent();
sherckuith 0:479ce5546098 133
sherckuith 0:479ce5546098 134 protected:
sherckuith 0:479ce5546098 135 void onNetTcpSocketEvent(NetTcpSocketEvent e);
sherckuith 0:479ce5546098 136 TCPSocketErr checkInst();
sherckuith 0:479ce5546098 137
sherckuith 0:479ce5546098 138 private:
sherckuith 0:479ce5546098 139 NetTcpSocket* m_pNetTcpSocket;
sherckuith 0:479ce5546098 140
sherckuith 0:479ce5546098 141 CDummy* m_pCbItem;
sherckuith 0:479ce5546098 142 void (CDummy::*m_pCbMeth)(TCPSocketEvent);
sherckuith 0:479ce5546098 143
sherckuith 0:479ce5546098 144 void (*m_pCb)(TCPSocketEvent);
sherckuith 0:479ce5546098 145
sherckuith 0:479ce5546098 146 };
sherckuith 0:479ce5546098 147
sherckuith 0:479ce5546098 148 #endif