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 NETTCPSOCKET_H
|
robertcook |
0:32a0996dff0f
|
25
|
#define NETTCPSOCKET_H
|
robertcook |
0:32a0996dff0f
|
26
|
|
robertcook |
0:32a0996dff0f
|
27
|
#include "net.h"
|
robertcook |
0:32a0996dff0f
|
28
|
#include "host.h"
|
robertcook |
0:32a0996dff0f
|
29
|
|
robertcook |
0:32a0996dff0f
|
30
|
#include <queue>
|
robertcook |
0:32a0996dff0f
|
31
|
using std::queue;
|
robertcook |
0:32a0996dff0f
|
32
|
|
robertcook |
0:32a0996dff0f
|
33
|
//Implements a Berkeley-like socket if
|
robertcook |
0:32a0996dff0f
|
34
|
//Can be interfaced either to lwip or a Telit module
|
robertcook |
0:32a0996dff0f
|
35
|
|
robertcook |
0:32a0996dff0f
|
36
|
enum NetTcpSocketErr
|
robertcook |
0:32a0996dff0f
|
37
|
{
|
robertcook |
0:32a0996dff0f
|
38
|
__NETTCPSOCKET_MIN = -0xFFFF,
|
robertcook |
0:32a0996dff0f
|
39
|
NETTCPSOCKET_SETUP, //NetTcpSocket not properly configured
|
robertcook |
0:32a0996dff0f
|
40
|
NETTCPSOCKET_TIMEOUT,
|
robertcook |
0:32a0996dff0f
|
41
|
NETTCPSOCKET_IF, //If has problems
|
robertcook |
0:32a0996dff0f
|
42
|
NETTCPSOCKET_MEM, //Not enough mem
|
robertcook |
0:32a0996dff0f
|
43
|
NETTCPSOCKET_INUSE, //If/Port is in use
|
robertcook |
0:32a0996dff0f
|
44
|
NETTCPSOCKET_EMPTY, //Connections queue is empty
|
robertcook |
0:32a0996dff0f
|
45
|
NETTCPSOCKET_RST, // Connection was reset by remote host
|
robertcook |
0:32a0996dff0f
|
46
|
//...
|
robertcook |
0:32a0996dff0f
|
47
|
NETTCPSOCKET_OK = 0
|
robertcook |
0:32a0996dff0f
|
48
|
};
|
robertcook |
0:32a0996dff0f
|
49
|
|
robertcook |
0:32a0996dff0f
|
50
|
enum NetTcpSocketEvent
|
robertcook |
0:32a0996dff0f
|
51
|
{
|
robertcook |
0:32a0996dff0f
|
52
|
NETTCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening
|
robertcook |
0:32a0996dff0f
|
53
|
NETTCPSOCKET_ACCEPT, //Connected to client
|
robertcook |
0:32a0996dff0f
|
54
|
NETTCPSOCKET_READABLE, //Data in buf
|
robertcook |
0:32a0996dff0f
|
55
|
NETTCPSOCKET_WRITEABLE, //Can write data to buf
|
robertcook |
0:32a0996dff0f
|
56
|
NETTCPSOCKET_CONTIMEOUT,
|
robertcook |
0:32a0996dff0f
|
57
|
NETTCPSOCKET_CONRST,
|
robertcook |
0:32a0996dff0f
|
58
|
NETTCPSOCKET_CONABRT,
|
robertcook |
0:32a0996dff0f
|
59
|
NETTCPSOCKET_ERROR,
|
robertcook |
0:32a0996dff0f
|
60
|
NETTCPSOCKET_DISCONNECTED
|
robertcook |
0:32a0996dff0f
|
61
|
};
|
robertcook |
0:32a0996dff0f
|
62
|
|
robertcook |
0:32a0996dff0f
|
63
|
|
robertcook |
0:32a0996dff0f
|
64
|
class NetTcpSocket
|
robertcook |
0:32a0996dff0f
|
65
|
{
|
robertcook |
0:32a0996dff0f
|
66
|
public:
|
robertcook |
0:32a0996dff0f
|
67
|
NetTcpSocket();
|
robertcook |
0:32a0996dff0f
|
68
|
virtual ~NetTcpSocket(); //close()
|
robertcook |
0:32a0996dff0f
|
69
|
|
robertcook |
0:32a0996dff0f
|
70
|
virtual NetTcpSocketErr bind(const Host& me) = 0;
|
robertcook |
0:32a0996dff0f
|
71
|
virtual NetTcpSocketErr listen() = 0;
|
robertcook |
0:32a0996dff0f
|
72
|
virtual NetTcpSocketErr connect(const Host& host) = 0;
|
robertcook |
0:32a0996dff0f
|
73
|
virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) = 0;
|
robertcook |
0:32a0996dff0f
|
74
|
|
robertcook |
0:32a0996dff0f
|
75
|
virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len) = 0;
|
robertcook |
0:32a0996dff0f
|
76
|
virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len) = 0;
|
robertcook |
0:32a0996dff0f
|
77
|
|
robertcook |
0:32a0996dff0f
|
78
|
virtual NetTcpSocketErr close() = 0;
|
robertcook |
0:32a0996dff0f
|
79
|
|
robertcook |
0:32a0996dff0f
|
80
|
virtual NetTcpSocketErr poll() = 0;
|
robertcook |
0:32a0996dff0f
|
81
|
|
robertcook |
0:32a0996dff0f
|
82
|
class CDummy;
|
robertcook |
0:32a0996dff0f
|
83
|
//Callbacks
|
robertcook |
0:32a0996dff0f
|
84
|
template<class T>
|
robertcook |
0:32a0996dff0f
|
85
|
//Linker bug : Must be defined here :(
|
robertcook |
0:32a0996dff0f
|
86
|
void setOnEvent( T* pItem, void (T::*pMethod)(NetTcpSocketEvent) )
|
robertcook |
0:32a0996dff0f
|
87
|
{
|
robertcook |
0:32a0996dff0f
|
88
|
m_pCbItem = (CDummy*) pItem;
|
robertcook |
0:32a0996dff0f
|
89
|
m_pCbMeth = (void (CDummy::*)(NetTcpSocketEvent)) pMethod;
|
robertcook |
0:32a0996dff0f
|
90
|
}
|
robertcook |
0:32a0996dff0f
|
91
|
|
robertcook |
0:32a0996dff0f
|
92
|
void resetOnEvent(); //Disable callback
|
robertcook |
0:32a0996dff0f
|
93
|
|
robertcook |
0:32a0996dff0f
|
94
|
protected:
|
robertcook |
0:32a0996dff0f
|
95
|
void queueEvent(NetTcpSocketEvent e);
|
robertcook |
0:32a0996dff0f
|
96
|
void discardEvents();
|
robertcook |
0:32a0996dff0f
|
97
|
void flushEvents(); //to be called during polling
|
robertcook |
0:32a0996dff0f
|
98
|
|
robertcook |
0:32a0996dff0f
|
99
|
Host m_host;
|
robertcook |
0:32a0996dff0f
|
100
|
Host m_client;
|
robertcook |
0:32a0996dff0f
|
101
|
|
robertcook |
0:32a0996dff0f
|
102
|
friend class Net;
|
robertcook |
0:32a0996dff0f
|
103
|
int m_refs;
|
robertcook |
0:32a0996dff0f
|
104
|
|
robertcook |
0:32a0996dff0f
|
105
|
bool m_closed;
|
robertcook |
0:32a0996dff0f
|
106
|
bool m_removed;
|
robertcook |
0:32a0996dff0f
|
107
|
|
robertcook |
0:32a0996dff0f
|
108
|
private:
|
robertcook |
0:32a0996dff0f
|
109
|
//We do not want to execute user code in interrupt routines, so we queue events until the server is polled
|
robertcook |
0:32a0996dff0f
|
110
|
//If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) )
|
robertcook |
0:32a0996dff0f
|
111
|
void onEvent(NetTcpSocketEvent e); //To be called on poll
|
robertcook |
0:32a0996dff0f
|
112
|
CDummy* m_pCbItem;
|
robertcook |
0:32a0996dff0f
|
113
|
void (CDummy::*m_pCbMeth)(NetTcpSocketEvent);
|
robertcook |
0:32a0996dff0f
|
114
|
queue<NetTcpSocketEvent> m_events;
|
robertcook |
0:32a0996dff0f
|
115
|
|
robertcook |
0:32a0996dff0f
|
116
|
};
|
robertcook |
0:32a0996dff0f
|
117
|
|
robertcook |
0:32a0996dff0f
|
118
|
#endif
|