ap
Dependencies: DnsQuery
Dependents: WizFi310_TCP_Echo_Server_Example
Fork of NetworkSocketAPI by
Socket.cpp@89:b1d417383c0d, 2016-04-19 (annotated)
- Committer:
- Christopher Haster
- Date:
- Tue Apr 19 18:20:38 2016 -0500
- Revision:
- 89:b1d417383c0d
- Parent:
- 87:94e2cf3a06be
- Child:
- 90:0a988e4abb72
Adopt mbed style doxygen comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
89:b1d417383c0d | 1 | /* Socket |
Christopher Haster |
89:b1d417383c0d | 2 | * Copyright (c) 2015 ARM Limited |
Christopher Haster |
89:b1d417383c0d | 3 | * |
Christopher Haster |
89:b1d417383c0d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Christopher Haster |
89:b1d417383c0d | 5 | * you may not use this file except in compliance with the License. |
Christopher Haster |
89:b1d417383c0d | 6 | * You may obtain a copy of the License at |
Christopher Haster |
89:b1d417383c0d | 7 | * |
Christopher Haster |
89:b1d417383c0d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
89:b1d417383c0d | 9 | * |
Christopher Haster |
89:b1d417383c0d | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
89:b1d417383c0d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Christopher Haster |
89:b1d417383c0d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
89:b1d417383c0d | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
89:b1d417383c0d | 14 | * limitations under the License. |
Christopher Haster |
89:b1d417383c0d | 15 | */ |
Christopher Haster |
89:b1d417383c0d | 16 | |
Christopher Haster |
89:b1d417383c0d | 17 | #include "Socket.h" |
Christopher Haster |
89:b1d417383c0d | 18 | |
Christopher Haster |
89:b1d417383c0d | 19 | Socket::Socket(NetworkInterface *iface, nsapi_protocol_t proto) |
Christopher Haster |
89:b1d417383c0d | 20 | : _iface(iface) |
Christopher Haster |
89:b1d417383c0d | 21 | , _blocking(true) |
Christopher Haster |
89:b1d417383c0d | 22 | , _timeout(0) |
Christopher Haster |
89:b1d417383c0d | 23 | { |
Christopher Haster |
89:b1d417383c0d | 24 | _socket = _iface->socket_create(proto); |
Christopher Haster |
89:b1d417383c0d | 25 | } |
Christopher Haster |
89:b1d417383c0d | 26 | |
Christopher Haster |
89:b1d417383c0d | 27 | Socket::~Socket() |
Christopher Haster |
89:b1d417383c0d | 28 | { |
Christopher Haster |
89:b1d417383c0d | 29 | if (_socket) { |
Christopher Haster |
89:b1d417383c0d | 30 | close(false); |
Christopher Haster |
89:b1d417383c0d | 31 | } |
Christopher Haster |
89:b1d417383c0d | 32 | } |
Christopher Haster |
89:b1d417383c0d | 33 | |
Christopher Haster |
89:b1d417383c0d | 34 | void Socket::set_blocking(bool blocking) |
Christopher Haster |
89:b1d417383c0d | 35 | { |
Christopher Haster |
89:b1d417383c0d | 36 | _blocking = blocking; |
Christopher Haster |
89:b1d417383c0d | 37 | } |
Christopher Haster |
89:b1d417383c0d | 38 | |
Christopher Haster |
89:b1d417383c0d | 39 | void Socket::set_timeout(unsigned timeout) |
Christopher Haster |
89:b1d417383c0d | 40 | { |
Christopher Haster |
89:b1d417383c0d | 41 | _timeout = timeout; |
Christopher Haster |
89:b1d417383c0d | 42 | } |
Christopher Haster |
89:b1d417383c0d | 43 | |
Christopher Haster |
89:b1d417383c0d | 44 | int Socket::set_option(int optname, const void *optval, unsigned int optlen) |
Christopher Haster |
89:b1d417383c0d | 45 | { |
Christopher Haster |
89:b1d417383c0d | 46 | if (!_socket) { |
Christopher Haster |
89:b1d417383c0d | 47 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
89:b1d417383c0d | 48 | } |
Christopher Haster |
89:b1d417383c0d | 49 | |
Christopher Haster |
89:b1d417383c0d | 50 | return _iface->socket_set_option(_socket, optname, optval, optlen); |
Christopher Haster |
89:b1d417383c0d | 51 | } |
Christopher Haster |
89:b1d417383c0d | 52 | |
Christopher Haster |
89:b1d417383c0d | 53 | int Socket::get_option(int optname, void *optval, unsigned int *optlen) |
Christopher Haster |
89:b1d417383c0d | 54 | { |
Christopher Haster |
89:b1d417383c0d | 55 | if (!_socket) { |
Christopher Haster |
89:b1d417383c0d | 56 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
89:b1d417383c0d | 57 | } |
Christopher Haster |
89:b1d417383c0d | 58 | |
Christopher Haster |
89:b1d417383c0d | 59 | return _iface->socket_get_option(_socket, optname, optval, optlen); |
Christopher Haster |
89:b1d417383c0d | 60 | } |
Christopher Haster |
89:b1d417383c0d | 61 | |
Christopher Haster |
89:b1d417383c0d | 62 | int Socket::close(bool shutdown) |
Christopher Haster |
89:b1d417383c0d | 63 | { |
Christopher Haster |
89:b1d417383c0d | 64 | if (!_socket) { |
Christopher Haster |
89:b1d417383c0d | 65 | return 0; |
Christopher Haster |
89:b1d417383c0d | 66 | } |
Christopher Haster |
89:b1d417383c0d | 67 | |
Christopher Haster |
89:b1d417383c0d | 68 | int err = _iface->socket_close(_socket, shutdown); |
Christopher Haster |
89:b1d417383c0d | 69 | if (!err) { |
Christopher Haster |
89:b1d417383c0d | 70 | void *socket = _socket; |
Christopher Haster |
89:b1d417383c0d | 71 | _socket = 0; |
Christopher Haster |
89:b1d417383c0d | 72 | _iface->socket_destroy(socket); |
Christopher Haster |
89:b1d417383c0d | 73 | } |
Christopher Haster |
89:b1d417383c0d | 74 | |
Christopher Haster |
89:b1d417383c0d | 75 | return err; |
Christopher Haster |
89:b1d417383c0d | 76 | } |
Christopher Haster |
89:b1d417383c0d | 77 | |
Christopher Haster |
89:b1d417383c0d | 78 | void Socket::thunk(void *p) |
Christopher Haster |
89:b1d417383c0d | 79 | { |
Christopher Haster |
89:b1d417383c0d | 80 | FunctionPointer *fptr = (FunctionPointer *)p; |
Christopher Haster |
89:b1d417383c0d | 81 | (*fptr)(); |
Christopher Haster |
89:b1d417383c0d | 82 | } |