ap
Dependencies: DnsQuery
Dependents: WizFi310_TCP_Echo_Server_Example
Fork of NetworkSocketAPI by
Socket.cpp@58:1caa187fa5af, 2016-02-25 (annotated)
- Committer:
- Christopher Haster
- Date:
- Thu Feb 25 21:20:25 2016 -0600
- Revision:
- 58:1caa187fa5af
- Parent:
- 57:3c873fab4207
- Child:
- 65:ca337f9ebdab
Removed setURL/IPAddress/Port set of functions from Socket and SocketInterface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
25:ed7b2a52e8ac | 1 | /* Socket |
Christopher Haster |
25:ed7b2a52e8ac | 2 | * Copyright (c) 2015 ARM Limited |
Christopher Haster |
25:ed7b2a52e8ac | 3 | * |
Christopher Haster |
25:ed7b2a52e8ac | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Christopher Haster |
25:ed7b2a52e8ac | 5 | * you may not use this file except in compliance with the License. |
Christopher Haster |
25:ed7b2a52e8ac | 6 | * You may obtain a copy of the License at |
Christopher Haster |
25:ed7b2a52e8ac | 7 | * |
Christopher Haster |
25:ed7b2a52e8ac | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
25:ed7b2a52e8ac | 9 | * |
Christopher Haster |
25:ed7b2a52e8ac | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
25:ed7b2a52e8ac | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Christopher Haster |
25:ed7b2a52e8ac | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
25:ed7b2a52e8ac | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
25:ed7b2a52e8ac | 14 | * limitations under the License. |
Christopher Haster |
25:ed7b2a52e8ac | 15 | */ |
Christopher Haster |
25:ed7b2a52e8ac | 16 | |
Christopher Haster |
25:ed7b2a52e8ac | 17 | #include "Socket.h" |
geky | 29:7bcec3189a93 | 18 | #include <string.h> |
Christopher Haster |
25:ed7b2a52e8ac | 19 | |
Christopher Haster |
57:3c873fab4207 | 20 | Socket::Socket(NetworkInterface *iface, ns_protocol_t proto) |
geky | 30:3cc78f5db99d | 21 | : _iface(iface) |
geky | 30:3cc78f5db99d | 22 | , _proto(proto) |
geky | 30:3cc78f5db99d | 23 | , _socket(0) |
geky | 30:3cc78f5db99d | 24 | { |
Christopher Haster |
42:49893d13c432 | 25 | memset(_ip_address, 0, NS_IP_SIZE); |
Christopher Haster |
28:163fbe3263f4 | 26 | _port = 0; |
Christopher Haster |
25:ed7b2a52e8ac | 27 | } |
Christopher Haster |
25:ed7b2a52e8ac | 28 | |
geky | 30:3cc78f5db99d | 29 | Socket::~Socket() |
geky | 30:3cc78f5db99d | 30 | { |
Christopher Haster |
43:09ea32f2eb54 | 31 | if (_socket) { |
Christopher Haster |
43:09ea32f2eb54 | 32 | close(); |
Christopher Haster |
43:09ea32f2eb54 | 33 | } |
Christopher Haster |
25:ed7b2a52e8ac | 34 | } |
Christopher Haster |
25:ed7b2a52e8ac | 35 | |
Christopher Haster |
38:157fb2ab965f | 36 | |
Christopher Haster |
43:09ea32f2eb54 | 37 | int32_t Socket::open(const char *address, uint16_t port) |
Christopher Haster |
32:2c5fc105fc50 | 38 | { |
Christopher Haster |
41:3ec1c97e9bbf | 39 | int32_t err; |
Christopher Haster |
41:3ec1c97e9bbf | 40 | |
Christopher Haster |
41:3ec1c97e9bbf | 41 | err = close(); |
Christopher Haster |
43:09ea32f2eb54 | 42 | if (err) { |
Christopher Haster |
43:09ea32f2eb54 | 43 | return err; |
Christopher Haster |
43:09ea32f2eb54 | 44 | } |
Christopher Haster |
34:c17745683385 | 45 | |
Christopher Haster |
58:1caa187fa5af | 46 | err = _iface->getHostByName(address, _ip_address); |
Christopher Haster |
58:1caa187fa5af | 47 | _port = port; |
Christopher Haster |
58:1caa187fa5af | 48 | if (err) { |
Christopher Haster |
58:1caa187fa5af | 49 | return err; |
Christopher Haster |
35:838393fbc2ca | 50 | } |
Christopher Haster |
32:2c5fc105fc50 | 51 | |
Christopher Haster |
32:2c5fc105fc50 | 52 | _socket = _iface->createSocket(_proto); |
Christopher Haster |
43:09ea32f2eb54 | 53 | if (!_socket) { |
Christopher Haster |
45:c8aca7c1e93f | 54 | return NS_ERROR_NO_SOCKET; |
Christopher Haster |
43:09ea32f2eb54 | 55 | } |
Christopher Haster |
41:3ec1c97e9bbf | 56 | |
Christopher Haster |
41:3ec1c97e9bbf | 57 | err = _socket->open(_ip_address, _port); |
Christopher Haster |
34:c17745683385 | 58 | |
Christopher Haster |
32:2c5fc105fc50 | 59 | if (err) { |
Christopher Haster |
32:2c5fc105fc50 | 60 | _iface->destroySocket(_socket); |
Christopher Haster |
54:0c764f654352 | 61 | _socket = 0; |
Christopher Haster |
32:2c5fc105fc50 | 62 | } |
Christopher Haster |
32:2c5fc105fc50 | 63 | |
Christopher Haster |
32:2c5fc105fc50 | 64 | return err; |
Christopher Haster |
32:2c5fc105fc50 | 65 | } |
Christopher Haster |
32:2c5fc105fc50 | 66 | |
Christopher Haster |
32:2c5fc105fc50 | 67 | int32_t Socket::close() |
Christopher Haster |
32:2c5fc105fc50 | 68 | { |
Christopher Haster |
43:09ea32f2eb54 | 69 | if (!_socket) { |
Christopher Haster |
43:09ea32f2eb54 | 70 | return 0; |
Christopher Haster |
43:09ea32f2eb54 | 71 | } |
Christopher Haster |
32:2c5fc105fc50 | 72 | |
Christopher Haster |
32:2c5fc105fc50 | 73 | int32_t err = _socket->close(); |
Christopher Haster |
32:2c5fc105fc50 | 74 | |
Christopher Haster |
32:2c5fc105fc50 | 75 | if (!err) { |
Christopher Haster |
32:2c5fc105fc50 | 76 | _iface->destroySocket(_socket); |
Christopher Haster |
54:0c764f654352 | 77 | _socket = 0; |
Christopher Haster |
32:2c5fc105fc50 | 78 | } |
Christopher Haster |
32:2c5fc105fc50 | 79 | |
Christopher Haster |
32:2c5fc105fc50 | 80 | return err; |
Christopher Haster |
32:2c5fc105fc50 | 81 | } |
Christopher Haster |
32:2c5fc105fc50 | 82 | |
Christopher Haster |
48:b3bbe28a7963 | 83 | int32_t Socket::send(const void *data, uint32_t size) |
geky | 30:3cc78f5db99d | 84 | { |
Christopher Haster |
43:09ea32f2eb54 | 85 | if (!_socket) { |
Christopher Haster |
45:c8aca7c1e93f | 86 | return NS_ERROR_NO_CONNECTION; |
Christopher Haster |
43:09ea32f2eb54 | 87 | } |
Christopher Haster |
48:b3bbe28a7963 | 88 | return _socket->send(data, size); |
Christopher Haster |
25:ed7b2a52e8ac | 89 | } |
Christopher Haster |
25:ed7b2a52e8ac | 90 | |
Christopher Haster |
48:b3bbe28a7963 | 91 | int32_t Socket::recv(void *data, uint32_t size, bool blocking) |
geky | 30:3cc78f5db99d | 92 | { |
Christopher Haster |
43:09ea32f2eb54 | 93 | if (!_socket) { |
Christopher Haster |
45:c8aca7c1e93f | 94 | return NS_ERROR_NO_CONNECTION; |
Christopher Haster |
43:09ea32f2eb54 | 95 | } |
Christopher Haster |
48:b3bbe28a7963 | 96 | |
Christopher Haster |
48:b3bbe28a7963 | 97 | while (true) { |
Christopher Haster |
49:85fe0b99948d | 98 | int32_t recv = _socket->recv(data, size); |
Christopher Haster |
48:b3bbe28a7963 | 99 | |
Christopher Haster |
49:85fe0b99948d | 100 | if (recv != 0 || !blocking) { |
Christopher Haster |
49:85fe0b99948d | 101 | return recv; |
Christopher Haster |
48:b3bbe28a7963 | 102 | } |
Christopher Haster |
48:b3bbe28a7963 | 103 | } |
Christopher Haster |
25:ed7b2a52e8ac | 104 | } |
Christopher Haster |
25:ed7b2a52e8ac | 105 | |
Christopher Haster |
58:1caa187fa5af | 106 | |
Christopher Haster |
58:1caa187fa5af | 107 | const char *Socket::getIPAddress() const |
Christopher Haster |
58:1caa187fa5af | 108 | { |
Christopher Haster |
58:1caa187fa5af | 109 | if (_ip_address[0]) { |
Christopher Haster |
58:1caa187fa5af | 110 | return _ip_address; |
Christopher Haster |
58:1caa187fa5af | 111 | } else { |
Christopher Haster |
58:1caa187fa5af | 112 | return 0; |
Christopher Haster |
58:1caa187fa5af | 113 | } |
Christopher Haster |
58:1caa187fa5af | 114 | } |
Christopher Haster |
58:1caa187fa5af | 115 | |
Christopher Haster |
58:1caa187fa5af | 116 | uint16_t Socket::getPort() const |
Christopher Haster |
58:1caa187fa5af | 117 | { |
Christopher Haster |
58:1caa187fa5af | 118 | return _port; |
Christopher Haster |
58:1caa187fa5af | 119 | } |
Christopher Haster |
58:1caa187fa5af | 120 | |
Christopher Haster |
58:1caa187fa5af | 121 | bool Socket::isConnected() |
Christopher Haster |
58:1caa187fa5af | 122 | { |
Christopher Haster |
58:1caa187fa5af | 123 | if (!_socket) { |
Christopher Haster |
58:1caa187fa5af | 124 | return false; |
Christopher Haster |
58:1caa187fa5af | 125 | } |
Christopher Haster |
58:1caa187fa5af | 126 | |
Christopher Haster |
58:1caa187fa5af | 127 | return _socket->isConnected(); |
Christopher Haster |
58:1caa187fa5af | 128 | } |
Christopher Haster |
58:1caa187fa5af | 129 |