http://mbed.org/users/okini3939/notebook/logger/

Dependencies:   mbed

Committer:
okini3939
Date:
Sat Apr 16 15:15:48 2011 +0000
Revision:
0:a3c892f39cc1

        

Who changed what in which revision?

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