Dependents:   SimpleLCDClock readCard2Twitter_http AnalogClock_StepperMotor_NTP ServoCamV1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwipNetTcpSocket.h Source File

lwipNetTcpSocket.h

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef LWIPNETTCPSOCKET_H
00025 #define LWIPNETTCPSOCKET_H
00026 
00027 #define NET_LWIP_STACK 1
00028 #include "lwip/ip_addr.h"
00029 #include "if/net/net.h"
00030 #include "LwipNetIf.h"
00031 
00032 //Implements NetTcpSockets over lwIP raw API
00033 
00034 struct tcp_pcb; //Represents a Tcp Connection, "Protocol Control Block", see rawapi.txt & tcp.h
00035 struct pbuf; //Lwip Buffer Container
00036 
00037 typedef signed char err_t;
00038 
00039 class LwipNetTcpSocket: public NetTcpSocket
00040 {
00041 public:
00042   LwipNetTcpSocket(tcp_pcb* pPcb = NULL); //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
00043   virtual ~LwipNetTcpSocket();
00044   
00045   virtual NetTcpSocketErr bind(const Host& me);
00046   virtual NetTcpSocketErr listen();
00047   virtual NetTcpSocketErr connect(const Host& host);
00048   virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket);
00049   
00050   virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len);
00051   virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len);
00052 
00053   virtual NetTcpSocketErr close();
00054 
00055   virtual NetTcpSocketErr poll();
00056   
00057 protected:
00058   volatile tcp_pcb* m_pPcb;
00059   
00060   //Events callbacks from lwIp
00061   err_t acceptCb(tcp_pcb* newpcb, err_t err);
00062   err_t connectedCb(tcp_pcb* tpcb, err_t err);
00063   
00064   void errCb(err_t err);
00065   
00066   err_t sentCb(tcp_pcb* tpcb, u16_t len);
00067   err_t recvCb(tcp_pcb* tpcb, pbuf *p, err_t err);
00068   
00069 private:
00070   void cleanUp(); //Flush input buffer
00071 
00072 //  queue<tcp_pcb*> m_lpInPcb; //Incoming connections that have not been accepted yet
00073   queue<LwipNetTcpSocket*> m_lpInNetTcpSocket; //Incoming connections that have not been accepted yet
00074   
00075   volatile pbuf* m_pReadPbuf; //Ptr to read buffer
00076   
00077   //Static callbacks : Transforms into a C++ callback
00078   static err_t sAcceptCb(void *arg, struct tcp_pcb *newpcb, err_t err);
00079   static err_t sConnectedCb(void *arg, struct tcp_pcb *tpcb, err_t err);
00080   
00081   static void sErrCb(void *arg, err_t err);
00082   
00083   static err_t sSentCb(void *arg, struct tcp_pcb *tpcb, u16_t len);
00084   static err_t sRecvCb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
00085   
00086 };
00087 
00088 #endif