Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Socket.cpp
00001 /* Socket 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "Socket.h" 00018 #include "mbed.h" 00019 00020 Socket::Socket() 00021 : _stack(0) 00022 , _socket(0) 00023 , _timeout(osWaitForever) 00024 { 00025 } 00026 00027 nsapi_error_t Socket::open(NetworkStack *stack) 00028 { 00029 _lock.lock(); 00030 00031 if (_stack != NULL || stack == NULL) { 00032 _lock.unlock(); 00033 return NSAPI_ERROR_PARAMETER ; 00034 } 00035 _stack = stack; 00036 00037 nsapi_socket_t socket; 00038 nsapi_error_t err = _stack->socket_open(&socket, get_proto()); 00039 if (err) { 00040 _lock.unlock(); 00041 return err; 00042 } 00043 00044 _socket = socket; 00045 _event = callback(this, &Socket::event); 00046 _stack->socket_attach(_socket, Callback<void()>::thunk, &_event); 00047 00048 _lock.unlock(); 00049 return NSAPI_ERROR_OK ; 00050 } 00051 00052 nsapi_error_t Socket::close() 00053 { 00054 _lock.lock(); 00055 00056 nsapi_error_t ret = NSAPI_ERROR_OK ; 00057 if (_socket) { 00058 _stack->socket_attach(_socket, 0, 0); 00059 nsapi_socket_t socket = _socket; 00060 _socket = 0; 00061 ret = _stack->socket_close(socket); 00062 } 00063 _stack = 0; 00064 00065 // Wakeup anything in a blocking operation 00066 // on this socket 00067 event(); 00068 00069 _lock.unlock(); 00070 return ret; 00071 } 00072 00073 nsapi_error_t Socket::bind(uint16_t port) 00074 { 00075 // Underlying bind is thread safe 00076 SocketAddress addr(0, port); 00077 return bind(addr); 00078 } 00079 00080 nsapi_error_t Socket::bind(const char *address, uint16_t port) 00081 { 00082 // Underlying bind is thread safe 00083 SocketAddress addr(address, port); 00084 return bind(addr); 00085 } 00086 00087 nsapi_error_t Socket::bind(const SocketAddress &address) 00088 { 00089 _lock.lock(); 00090 nsapi_error_t ret; 00091 00092 if (!_socket) { 00093 ret = NSAPI_ERROR_NO_SOCKET ; 00094 } else { 00095 ret = _stack->socket_bind(_socket, address); 00096 } 00097 00098 _lock.unlock(); 00099 return ret; 00100 } 00101 00102 void Socket::set_blocking(bool blocking) 00103 { 00104 // Socket::set_timeout is thread safe 00105 set_timeout(blocking ? -1 : 0); 00106 } 00107 00108 void Socket::set_timeout(int timeout) 00109 { 00110 _lock.lock(); 00111 00112 if (timeout >= 0) { 00113 _timeout = (uint32_t)timeout; 00114 } else { 00115 _timeout = osWaitForever; 00116 } 00117 00118 _lock.unlock(); 00119 } 00120 00121 nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen) 00122 { 00123 _lock.lock(); 00124 nsapi_error_t ret; 00125 00126 if (!_socket) { 00127 ret = NSAPI_ERROR_NO_SOCKET ; 00128 } else { 00129 ret = _stack->setsockopt(_socket, level, optname, optval, optlen); 00130 } 00131 00132 _lock.unlock(); 00133 return ret; 00134 } 00135 00136 nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen) 00137 { 00138 _lock.lock(); 00139 nsapi_error_t ret; 00140 00141 if (!_socket) { 00142 ret = NSAPI_ERROR_NO_SOCKET ; 00143 } else { 00144 ret = _stack->getsockopt(_socket, level, optname, optval, optlen); 00145 } 00146 00147 _lock.unlock(); 00148 return ret; 00149 00150 } 00151 00152 void Socket::sigio(Callback<void()> callback) 00153 { 00154 _lock.lock(); 00155 _callback = callback; 00156 _lock.unlock(); 00157 } 00158 00159 void Socket::attach(Callback<void()> callback) 00160 { 00161 sigio(callback); 00162 }
Generated on Tue Jul 12 2022 20:03:23 by
