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