User | Revision | Line number | New 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
|
#ifndef LWIPNETUDPSOCKET_H
|
robertcook |
0:32a0996dff0f
|
25
|
#define LWIPNETUDPSOCKET_H
|
robertcook |
0:32a0996dff0f
|
26
|
|
robertcook |
0:32a0996dff0f
|
27
|
#define NET_LWIP_STACK 1
|
robertcook |
0:32a0996dff0f
|
28
|
//#include "lwip/ip_addr.h"
|
robertcook |
0:32a0996dff0f
|
29
|
#include "if/net/netudpsocket.h"
|
robertcook |
0:32a0996dff0f
|
30
|
#include "LwipNetIf.h"
|
robertcook |
0:32a0996dff0f
|
31
|
|
robertcook |
0:32a0996dff0f
|
32
|
#include "stdint.h"
|
robertcook |
0:32a0996dff0f
|
33
|
|
robertcook |
0:32a0996dff0f
|
34
|
#include <list>
|
robertcook |
0:32a0996dff0f
|
35
|
using std::list;
|
robertcook |
0:32a0996dff0f
|
36
|
|
robertcook |
0:32a0996dff0f
|
37
|
//Implements NetUdpSockets over lwIP raw API
|
robertcook |
0:32a0996dff0f
|
38
|
|
robertcook |
0:32a0996dff0f
|
39
|
struct udp_pcb; //Represents a Udp Connection, "Protocol Control Block", see rawapi.txt & udp.h
|
robertcook |
0:32a0996dff0f
|
40
|
struct pbuf; //Lwip Buffer Container
|
robertcook |
0:32a0996dff0f
|
41
|
typedef struct ip_addr ip_addr_t;
|
robertcook |
0:32a0996dff0f
|
42
|
|
robertcook |
0:32a0996dff0f
|
43
|
//typedef signed char err_t;
|
robertcook |
0:32a0996dff0f
|
44
|
typedef uint16_t u16_t;
|
robertcook |
0:32a0996dff0f
|
45
|
|
robertcook |
0:32a0996dff0f
|
46
|
class LwipNetUdpSocket: public NetUdpSocket
|
robertcook |
0:32a0996dff0f
|
47
|
{
|
robertcook |
0:32a0996dff0f
|
48
|
public:
|
robertcook |
0:32a0996dff0f
|
49
|
LwipNetUdpSocket(udp_pcb* pPcb = NULL); //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
|
robertcook |
0:32a0996dff0f
|
50
|
virtual ~LwipNetUdpSocket();
|
robertcook |
0:32a0996dff0f
|
51
|
|
robertcook |
0:32a0996dff0f
|
52
|
virtual NetUdpSocketErr bind(const Host& me);
|
robertcook |
0:32a0996dff0f
|
53
|
|
robertcook |
0:32a0996dff0f
|
54
|
virtual int /*if < 0 : NetUdpSocketErr*/ sendto(const char* buf, int len, Host* pHost);
|
robertcook |
0:32a0996dff0f
|
55
|
virtual int /*if < 0 : NetUdpSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
|
robertcook |
0:32a0996dff0f
|
56
|
|
robertcook |
0:32a0996dff0f
|
57
|
virtual NetUdpSocketErr close();
|
robertcook |
0:32a0996dff0f
|
58
|
|
robertcook |
0:32a0996dff0f
|
59
|
virtual NetUdpSocketErr poll();
|
robertcook |
0:32a0996dff0f
|
60
|
|
robertcook |
0:32a0996dff0f
|
61
|
protected:
|
robertcook |
0:32a0996dff0f
|
62
|
volatile udp_pcb* m_pPcb;
|
robertcook |
0:32a0996dff0f
|
63
|
|
robertcook |
0:32a0996dff0f
|
64
|
//Event callback from lwIp
|
robertcook |
0:32a0996dff0f
|
65
|
void recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port);
|
robertcook |
0:32a0996dff0f
|
66
|
|
robertcook |
0:32a0996dff0f
|
67
|
private:
|
robertcook |
0:32a0996dff0f
|
68
|
void cleanUp(); //Flush input buffer
|
robertcook |
0:32a0996dff0f
|
69
|
struct InPacket
|
robertcook |
0:32a0996dff0f
|
70
|
{
|
robertcook |
0:32a0996dff0f
|
71
|
volatile pbuf* pBuf;
|
robertcook |
0:32a0996dff0f
|
72
|
ip_addr_t addr;
|
robertcook |
0:32a0996dff0f
|
73
|
u16_t port;
|
robertcook |
0:32a0996dff0f
|
74
|
};
|
robertcook |
0:32a0996dff0f
|
75
|
|
robertcook |
0:32a0996dff0f
|
76
|
list<InPacket> m_lInPkt;
|
robertcook |
0:32a0996dff0f
|
77
|
IpAddr m_multicastGroup;
|
robertcook |
0:32a0996dff0f
|
78
|
|
robertcook |
0:32a0996dff0f
|
79
|
//Static callback : Transforms into a C++ callback
|
robertcook |
0:32a0996dff0f
|
80
|
static void sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
|
robertcook |
0:32a0996dff0f
|
81
|
|
robertcook |
0:32a0996dff0f
|
82
|
};
|
robertcook |
0:32a0996dff0f
|
83
|
|
robertcook |
0:32a0996dff0f
|
84
|
#endif
|