NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Socket.cpp@66:c84a4c76cb94, 2016-04-01 (annotated)
- Committer:
- geky
- Date:
- Fri Apr 01 17:18:27 2016 +0000
- Revision:
- 66:c84a4c76cb94
- Parent:
- 65:ca337f9ebdab
- Child:
- 68:a52251517491
Changed API to better match Posix sockets; ; - Send returns size on success; - Returns NS_ERROR_WOULD_BLOCK when recv would block
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 | } |
geky | 65:ca337f9ebdab | 72 | |
geky | 65:ca337f9ebdab | 73 | SocketInterface *socket = _socket; |
geky | 65:ca337f9ebdab | 74 | _socket = 0; |
Christopher Haster |
32:2c5fc105fc50 | 75 | |
geky | 65:ca337f9ebdab | 76 | int32_t err = socket->close(); |
Christopher Haster |
32:2c5fc105fc50 | 77 | if (!err) { |
geky | 65:ca337f9ebdab | 78 | _iface->destroySocket(socket); |
Christopher Haster |
32:2c5fc105fc50 | 79 | } |
Christopher Haster |
32:2c5fc105fc50 | 80 | |
Christopher Haster |
32:2c5fc105fc50 | 81 | return err; |
Christopher Haster |
32:2c5fc105fc50 | 82 | } |
Christopher Haster |
32:2c5fc105fc50 | 83 | |
Christopher Haster |
48:b3bbe28a7963 | 84 | int32_t Socket::send(const void *data, uint32_t size) |
geky | 30:3cc78f5db99d | 85 | { |
Christopher Haster |
43:09ea32f2eb54 | 86 | if (!_socket) { |
Christopher Haster |
45:c8aca7c1e93f | 87 | return NS_ERROR_NO_CONNECTION; |
Christopher Haster |
43:09ea32f2eb54 | 88 | } |
Christopher Haster |
48:b3bbe28a7963 | 89 | return _socket->send(data, size); |
Christopher Haster |
25:ed7b2a52e8ac | 90 | } |
Christopher Haster |
25:ed7b2a52e8ac | 91 | |
Christopher Haster |
48:b3bbe28a7963 | 92 | int32_t Socket::recv(void *data, uint32_t size, bool blocking) |
geky | 30:3cc78f5db99d | 93 | { |
Christopher Haster |
48:b3bbe28a7963 | 94 | while (true) { |
geky | 65:ca337f9ebdab | 95 | if (!_socket) { |
geky | 65:ca337f9ebdab | 96 | return NS_ERROR_NO_CONNECTION; |
geky | 65:ca337f9ebdab | 97 | } |
geky | 65:ca337f9ebdab | 98 | |
Christopher Haster |
49:85fe0b99948d | 99 | int32_t recv = _socket->recv(data, size); |
Christopher Haster |
48:b3bbe28a7963 | 100 | |
geky | 66:c84a4c76cb94 | 101 | if (recv != NS_ERROR_WOULD_BLOCK || !blocking) { |
Christopher Haster |
49:85fe0b99948d | 102 | return recv; |
Christopher Haster |
48:b3bbe28a7963 | 103 | } |
Christopher Haster |
48:b3bbe28a7963 | 104 | } |
Christopher Haster |
25:ed7b2a52e8ac | 105 | } |
Christopher Haster |
25:ed7b2a52e8ac | 106 | |
Christopher Haster |
58:1caa187fa5af | 107 | |
Christopher Haster |
58:1caa187fa5af | 108 | const char *Socket::getIPAddress() const |
Christopher Haster |
58:1caa187fa5af | 109 | { |
Christopher Haster |
58:1caa187fa5af | 110 | if (_ip_address[0]) { |
Christopher Haster |
58:1caa187fa5af | 111 | return _ip_address; |
Christopher Haster |
58:1caa187fa5af | 112 | } else { |
Christopher Haster |
58:1caa187fa5af | 113 | return 0; |
Christopher Haster |
58:1caa187fa5af | 114 | } |
Christopher Haster |
58:1caa187fa5af | 115 | } |
Christopher Haster |
58:1caa187fa5af | 116 | |
Christopher Haster |
58:1caa187fa5af | 117 | uint16_t Socket::getPort() const |
Christopher Haster |
58:1caa187fa5af | 118 | { |
Christopher Haster |
58:1caa187fa5af | 119 | return _port; |
Christopher Haster |
58:1caa187fa5af | 120 | } |
Christopher Haster |
58:1caa187fa5af | 121 | |
Christopher Haster |
58:1caa187fa5af | 122 | bool Socket::isConnected() |
Christopher Haster |
58:1caa187fa5af | 123 | { |
Christopher Haster |
58:1caa187fa5af | 124 | if (!_socket) { |
Christopher Haster |
58:1caa187fa5af | 125 | return false; |
Christopher Haster |
58:1caa187fa5af | 126 | } |
Christopher Haster |
58:1caa187fa5af | 127 | |
Christopher Haster |
58:1caa187fa5af | 128 | return _socket->isConnected(); |
Christopher Haster |
58:1caa187fa5af | 129 | } |
Christopher Haster |
58:1caa187fa5af | 130 |