Axeda Corp
/
AxedaGo-ubloxC027
Axeda demo software for u-blox C027 (GSM)
C027_Support/Socket/TCPSocketConnection.h@0:a725e8eab383, 2014-08-11 (annotated)
- Committer:
- AxedaCorp
- Date:
- Mon Aug 11 19:02:42 2014 +0000
- Revision:
- 0:a725e8eab383
1st commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:a725e8eab383 | 1 | #ifndef TCPSOCKET_H |
AxedaCorp | 0:a725e8eab383 | 2 | #define TCPSOCKET_H |
AxedaCorp | 0:a725e8eab383 | 3 | |
AxedaCorp | 0:a725e8eab383 | 4 | #include "Socket.h" |
AxedaCorp | 0:a725e8eab383 | 5 | |
AxedaCorp | 0:a725e8eab383 | 6 | /** TCP socket connection |
AxedaCorp | 0:a725e8eab383 | 7 | */ |
AxedaCorp | 0:a725e8eab383 | 8 | class TCPSocketConnection: public Socket |
AxedaCorp | 0:a725e8eab383 | 9 | { |
AxedaCorp | 0:a725e8eab383 | 10 | friend class TCPSocketServer; |
AxedaCorp | 0:a725e8eab383 | 11 | |
AxedaCorp | 0:a725e8eab383 | 12 | public: |
AxedaCorp | 0:a725e8eab383 | 13 | /** TCP socket connection |
AxedaCorp | 0:a725e8eab383 | 14 | */ |
AxedaCorp | 0:a725e8eab383 | 15 | TCPSocketConnection() {} |
AxedaCorp | 0:a725e8eab383 | 16 | |
AxedaCorp | 0:a725e8eab383 | 17 | /** Connects this TCP socket to the server |
AxedaCorp | 0:a725e8eab383 | 18 | \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. |
AxedaCorp | 0:a725e8eab383 | 19 | \param port The host's port to connect to. |
AxedaCorp | 0:a725e8eab383 | 20 | \return 0 on success, -1 on failure. |
AxedaCorp | 0:a725e8eab383 | 21 | */ |
AxedaCorp | 0:a725e8eab383 | 22 | int connect(const char* host, const int port) |
AxedaCorp | 0:a725e8eab383 | 23 | { |
AxedaCorp | 0:a725e8eab383 | 24 | if (_mdm == NULL) |
AxedaCorp | 0:a725e8eab383 | 25 | _mdm = MDMParser::getInstance(); |
AxedaCorp | 0:a725e8eab383 | 26 | if (_mdm == NULL) |
AxedaCorp | 0:a725e8eab383 | 27 | return -1; |
AxedaCorp | 0:a725e8eab383 | 28 | |
AxedaCorp | 0:a725e8eab383 | 29 | if (_socket < 0) { |
AxedaCorp | 0:a725e8eab383 | 30 | _socket = _mdm->socketSocket(MDMParser::IPPROTO_TCP); |
AxedaCorp | 0:a725e8eab383 | 31 | if (_socket < 0) { |
AxedaCorp | 0:a725e8eab383 | 32 | return -1; |
AxedaCorp | 0:a725e8eab383 | 33 | } |
AxedaCorp | 0:a725e8eab383 | 34 | } |
AxedaCorp | 0:a725e8eab383 | 35 | |
AxedaCorp | 0:a725e8eab383 | 36 | _mdm->socketSetBlocking(_socket, _timeout_ms); |
AxedaCorp | 0:a725e8eab383 | 37 | if (!_mdm->socketConnect(_socket, host, port)) { |
AxedaCorp | 0:a725e8eab383 | 38 | return -1; |
AxedaCorp | 0:a725e8eab383 | 39 | } |
AxedaCorp | 0:a725e8eab383 | 40 | return 0; |
AxedaCorp | 0:a725e8eab383 | 41 | } |
AxedaCorp | 0:a725e8eab383 | 42 | /** Check if the socket is connected |
AxedaCorp | 0:a725e8eab383 | 43 | \return true if connected, false otherwise. |
AxedaCorp | 0:a725e8eab383 | 44 | */ |
AxedaCorp | 0:a725e8eab383 | 45 | bool is_connected(void) { return _mdm->socketIsConnected(_socket); } |
AxedaCorp | 0:a725e8eab383 | 46 | |
AxedaCorp | 0:a725e8eab383 | 47 | /** Send data to the remote host. |
AxedaCorp | 0:a725e8eab383 | 48 | \param data The buffer to send to the host. |
AxedaCorp | 0:a725e8eab383 | 49 | \param length The length of the buffer to send. |
AxedaCorp | 0:a725e8eab383 | 50 | \return the number of written bytes on success (>=0) or -1 on failure |
AxedaCorp | 0:a725e8eab383 | 51 | */ |
AxedaCorp | 0:a725e8eab383 | 52 | int send(char* data, int length) { return _mdm->socketSend(_socket, data, length); } |
AxedaCorp | 0:a725e8eab383 | 53 | |
AxedaCorp | 0:a725e8eab383 | 54 | /** Send all the data to the remote host. |
AxedaCorp | 0:a725e8eab383 | 55 | \param data The buffer to send to the host. |
AxedaCorp | 0:a725e8eab383 | 56 | \param length The length of the buffer to send. |
AxedaCorp | 0:a725e8eab383 | 57 | \return the number of written bytes on success (>=0) or -1 on failure |
AxedaCorp | 0:a725e8eab383 | 58 | */ |
AxedaCorp | 0:a725e8eab383 | 59 | int send_all(char* data, int length) { return send(data,length); } |
AxedaCorp | 0:a725e8eab383 | 60 | |
AxedaCorp | 0:a725e8eab383 | 61 | /** Receive data from the remote host. |
AxedaCorp | 0:a725e8eab383 | 62 | \param data The buffer in which to store the data received from the host. |
AxedaCorp | 0:a725e8eab383 | 63 | \param length The maximum length of the buffer. |
AxedaCorp | 0:a725e8eab383 | 64 | \return the number of received bytes on success (>=0) or -1 on failure |
AxedaCorp | 0:a725e8eab383 | 65 | */ |
AxedaCorp | 0:a725e8eab383 | 66 | int receive(char* data, int length) { return _mdm->socketRecv(_socket, data, length); } |
AxedaCorp | 0:a725e8eab383 | 67 | |
AxedaCorp | 0:a725e8eab383 | 68 | /** Receive all the data from the remote host. |
AxedaCorp | 0:a725e8eab383 | 69 | \param data The buffer in which to store the data received from the host. |
AxedaCorp | 0:a725e8eab383 | 70 | \param length The maximum length of the buffer. |
AxedaCorp | 0:a725e8eab383 | 71 | \return the number of received bytes on success (>=0) or -1 on failure |
AxedaCorp | 0:a725e8eab383 | 72 | */ |
AxedaCorp | 0:a725e8eab383 | 73 | int receive_all(char* data, int length) { return receive(data,length); } |
AxedaCorp | 0:a725e8eab383 | 74 | |
AxedaCorp | 0:a725e8eab383 | 75 | }; |
AxedaCorp | 0:a725e8eab383 | 76 | |
AxedaCorp | 0:a725e8eab383 | 77 | #endif |