Example program with HTTPServer and sensor data streaming over TCPSockets, using Donatien Garnier's Net APIs and services code on top of LWIP. Files StreamServer.h and .cpp encapsulate streaming over TCPSockets. Broadcast is done by sendToAll(), and all incoming data is echoed back to the client. Echo code can be replaced with some remote control of the streaming interface. See main() that shows how to periodically send some data to all subscribed clients. To subscribe, a client should open a socket at <mbed_ip> port 123. I used few lines in TCL code to set up a quick sink for the data. HTTP files are served on port 80 concurrently to the streaming.
if/lwip/lwipNetTcpSocket.h@1:3ee499525aa5, 2010-06-14 (annotated)
- Committer:
- iva2k
- Date:
- Mon Jun 14 03:24:33 2010 +0000
- Revision:
- 1:3ee499525aa5
- Parent:
- 0:e614f7875b60
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iva2k | 0:e614f7875b60 | 1 | |
iva2k | 0:e614f7875b60 | 2 | /* |
iva2k | 0:e614f7875b60 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
iva2k | 0:e614f7875b60 | 4 | |
iva2k | 0:e614f7875b60 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
iva2k | 0:e614f7875b60 | 6 | of this software and associated documentation files (the "Software"), to deal |
iva2k | 0:e614f7875b60 | 7 | in the Software without restriction, including without limitation the rights |
iva2k | 0:e614f7875b60 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
iva2k | 0:e614f7875b60 | 9 | copies of the Software, and to permit persons to whom the Software is |
iva2k | 0:e614f7875b60 | 10 | furnished to do so, subject to the following conditions: |
iva2k | 0:e614f7875b60 | 11 | |
iva2k | 0:e614f7875b60 | 12 | The above copyright notice and this permission notice shall be included in |
iva2k | 0:e614f7875b60 | 13 | all copies or substantial portions of the Software. |
iva2k | 0:e614f7875b60 | 14 | |
iva2k | 0:e614f7875b60 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
iva2k | 0:e614f7875b60 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
iva2k | 0:e614f7875b60 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
iva2k | 0:e614f7875b60 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
iva2k | 0:e614f7875b60 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
iva2k | 0:e614f7875b60 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
iva2k | 0:e614f7875b60 | 21 | THE SOFTWARE. |
iva2k | 0:e614f7875b60 | 22 | */ |
iva2k | 0:e614f7875b60 | 23 | |
iva2k | 0:e614f7875b60 | 24 | #ifndef LWIPNETTCPSOCKET_H |
iva2k | 0:e614f7875b60 | 25 | #define LWIPNETTCPSOCKET_H |
iva2k | 0:e614f7875b60 | 26 | |
iva2k | 0:e614f7875b60 | 27 | #define NET_LWIP_STACK 1 |
iva2k | 0:e614f7875b60 | 28 | #include "lwip/ip_addr.h" |
iva2k | 0:e614f7875b60 | 29 | #include "if/net/net.h" |
iva2k | 0:e614f7875b60 | 30 | #include "LwipNetIf.h" |
iva2k | 0:e614f7875b60 | 31 | |
iva2k | 0:e614f7875b60 | 32 | //Implements NetTcpSockets over lwIP raw API |
iva2k | 0:e614f7875b60 | 33 | |
iva2k | 0:e614f7875b60 | 34 | struct tcp_pcb; //Represents a Tcp Connection, "Protocol Control Block", see rawapi.txt & tcp.h |
iva2k | 0:e614f7875b60 | 35 | struct pbuf; //Lwip Buffer Container |
iva2k | 0:e614f7875b60 | 36 | |
iva2k | 0:e614f7875b60 | 37 | typedef signed char err_t; |
iva2k | 0:e614f7875b60 | 38 | |
iva2k | 0:e614f7875b60 | 39 | class LwipNetTcpSocket: public NetTcpSocket |
iva2k | 0:e614f7875b60 | 40 | { |
iva2k | 0:e614f7875b60 | 41 | public: |
iva2k | 0:e614f7875b60 | 42 | LwipNetTcpSocket(tcp_pcb* pPcb = NULL); //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership |
iva2k | 0:e614f7875b60 | 43 | virtual ~LwipNetTcpSocket(); |
iva2k | 0:e614f7875b60 | 44 | |
iva2k | 0:e614f7875b60 | 45 | virtual NetTcpSocketErr bind(const Host& me); |
iva2k | 0:e614f7875b60 | 46 | virtual NetTcpSocketErr listen(); |
iva2k | 0:e614f7875b60 | 47 | virtual NetTcpSocketErr connect(const Host& host); |
iva2k | 0:e614f7875b60 | 48 | virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket); |
iva2k | 0:e614f7875b60 | 49 | |
iva2k | 0:e614f7875b60 | 50 | virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len); |
iva2k | 0:e614f7875b60 | 51 | virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len); |
iva2k | 0:e614f7875b60 | 52 | |
iva2k | 0:e614f7875b60 | 53 | virtual NetTcpSocketErr close(); |
iva2k | 0:e614f7875b60 | 54 | |
iva2k | 0:e614f7875b60 | 55 | virtual NetTcpSocketErr poll(); |
iva2k | 0:e614f7875b60 | 56 | |
iva2k | 0:e614f7875b60 | 57 | protected: |
iva2k | 0:e614f7875b60 | 58 | volatile tcp_pcb* m_pPcb; |
iva2k | 0:e614f7875b60 | 59 | |
iva2k | 0:e614f7875b60 | 60 | //Events callbacks from lwIp |
iva2k | 0:e614f7875b60 | 61 | err_t acceptCb(tcp_pcb* newpcb, err_t err); |
iva2k | 0:e614f7875b60 | 62 | err_t connectedCb(tcp_pcb* tpcb, err_t err); |
iva2k | 0:e614f7875b60 | 63 | |
iva2k | 0:e614f7875b60 | 64 | void errCb(err_t err); |
iva2k | 0:e614f7875b60 | 65 | |
iva2k | 0:e614f7875b60 | 66 | err_t sentCb(tcp_pcb* tpcb, u16_t len); |
iva2k | 0:e614f7875b60 | 67 | err_t recvCb(tcp_pcb* tpcb, pbuf *p, err_t err); |
iva2k | 0:e614f7875b60 | 68 | |
iva2k | 0:e614f7875b60 | 69 | private: |
iva2k | 0:e614f7875b60 | 70 | void cleanUp(); //Flush input buffer |
iva2k | 0:e614f7875b60 | 71 | |
iva2k | 0:e614f7875b60 | 72 | // queue<tcp_pcb*> m_lpInPcb; //Incoming connections that have not been accepted yet |
iva2k | 0:e614f7875b60 | 73 | queue<LwipNetTcpSocket*> m_lpInNetTcpSocket; //Incoming connections that have not been accepted yet |
iva2k | 0:e614f7875b60 | 74 | |
iva2k | 0:e614f7875b60 | 75 | volatile pbuf* m_pReadPbuf; //Ptr to read buffer |
iva2k | 0:e614f7875b60 | 76 | |
iva2k | 0:e614f7875b60 | 77 | //Static callbacks : Transforms into a C++ callback |
iva2k | 0:e614f7875b60 | 78 | static err_t sAcceptCb(void *arg, struct tcp_pcb *newpcb, err_t err); |
iva2k | 0:e614f7875b60 | 79 | static err_t sConnectedCb(void *arg, struct tcp_pcb *tpcb, err_t err); |
iva2k | 0:e614f7875b60 | 80 | |
iva2k | 0:e614f7875b60 | 81 | static void sErrCb(void *arg, err_t err); |
iva2k | 0:e614f7875b60 | 82 | |
iva2k | 0:e614f7875b60 | 83 | static err_t sSentCb(void *arg, struct tcp_pcb *tpcb, u16_t len); |
iva2k | 0:e614f7875b60 | 84 | static err_t sRecvCb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err); |
iva2k | 0:e614f7875b60 | 85 | |
iva2k | 0:e614f7875b60 | 86 | }; |
iva2k | 0:e614f7875b60 | 87 | |
iva2k | 0:e614f7875b60 | 88 | #endif |