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