Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Wed Jul 18 20:28:43 2012 +0000
Revision:
1:9d93f4dc2f46
Parent:
0:8b387bed54c2
updated to the new mbed library (version 41) and the new LwIP stack. Code is still untested.

Who changed what in which revision?

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