Dependencies:   mbed

Committer:
robertcook
Date:
Wed Jun 13 18:39:46 2012 +0000
Revision:
0:32a0996dff0f

        

Who changed what in which revision?

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