Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Wed Dec 15 18:01:30 2010 +0000
Revision:
7:4e2468d7d5cb
Parent:
0:ac1725ba162c

        

Who changed what in which revision?

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