Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.h Source File

UDPSocket.h

00001 
00002 /** \addtogroup netsocket */
00003 /** @{*/
00004 /* UDPSocket
00005  * Copyright (c) 2015 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 #ifndef UDPSOCKET_H
00021 #define UDPSOCKET_H
00022 
00023 #include "netsocket/InternetSocket.h"
00024 #include "netsocket/NetworkStack.h"
00025 #include "netsocket/NetworkInterface.h"
00026 #include "rtos/EventFlags.h"
00027 
00028 
00029 /** UDP socket implementation.
00030  */
00031 class UDPSocket : public InternetSocket {
00032 public:
00033     /** Create an uninitialized socket.
00034      *
00035      *  @note Must call open to initialize the socket on a network stack.
00036      */
00037     UDPSocket();
00038 
00039     /** Create and open a socket on the network stack of the given
00040      *  network interface.
00041      *
00042      *  @tparam S    Type of the Network stack.
00043      *  @param stack Network stack as target for socket.
00044      */
00045     template <typename S>
00046     UDPSocket(S *stack)
00047     {
00048         open(stack);
00049     }
00050 
00051     /** Destroy a socket.
00052      *
00053      *  @note Closes socket if the socket is still open.
00054      */
00055     virtual ~UDPSocket();
00056 
00057     /** Send data to the specified host and port.
00058      *
00059      *  By default, sendto blocks until data is sent. If socket is set to
00060      *  nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
00061      *  immediately.
00062      *
00063      *  @param host     Domain name of the remote host or a dotted notation IP address.
00064      *  @param port     Port of the remote host.
00065      *  @param data     Buffer of data to send to the host.
00066      *  @param size     Size of the buffer in bytes.
00067      *  @return         Number of sent bytes on success, negative error
00068      *                  code on failure.
00069      */
00070     virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port,
00071                                          const void *data, nsapi_size_t size);
00072 
00073     /** Send data to the specified address.
00074      *
00075      *  By default, sendto blocks until data is sent. If socket is set to
00076      *  nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
00077      *  immediately.
00078      *
00079      *  @param address  The SocketAddress of the remote host.
00080      *  @param data     Buffer of data to send to the host.
00081      *  @param size     Size of the buffer in bytes.
00082      *  @return         Number of sent bytes on success, negative error
00083      *                  code on failure.
00084      */
00085     virtual nsapi_size_or_error_t sendto(const SocketAddress &address,
00086                                          const void *data, nsapi_size_t size);
00087 
00088     /** Receive a datagram and store the source address in address if it's not NULL.
00089      *
00090      *  By default, recvfrom blocks until a datagram is received. If socket is set to
00091      *  nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
00092      *  is returned.
00093      *
00094      *  @note If the datagram is larger than the buffer, the excess data is silently discarded.
00095      *
00096      *  @note If socket is connected, only packets coming from connected peer address
00097      *  are accepted.
00098      *
00099      *  @note recvfrom() is allowed write to address and data buffers even if error occurs.
00100      *
00101      *  @param address  Destination for the source address or NULL.
00102      *  @param data     Destination buffer for datagram received from the host.
00103      *  @param size     Size of the buffer in bytes.
00104      *  @return         Number of received bytes on success, negative error
00105      *                  code on failure.
00106      */
00107     virtual nsapi_size_or_error_t recvfrom(SocketAddress *address,
00108                                            void *data, nsapi_size_t size);
00109 
00110     /** Set the remote address for next send() call and filtering
00111      *  of incoming packets. To reset the address, zero initialized
00112      *  SocketAddress must be in the address parameter.
00113      *
00114      *  @param address  The SocketAddress of the remote host.
00115      *  @return         0 on success, negative error code on failure.
00116      */
00117     virtual nsapi_error_t connect(const SocketAddress &address);
00118 
00119     /** Send a datagram to connected remote address.
00120      *
00121      *  By default, send blocks until all data is sent. If socket is set to
00122      *  nonblocking or times out, a partial amount can be written.
00123      *  NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
00124      *
00125      *  @note The socket must be connected to a remote host before send() call.
00126      *
00127      *  @param data     Buffer of data to send to the host.
00128      *  @param size     Size of the buffer in bytes.
00129      *  @return         Number of sent bytes on success, negative error
00130      *                  code on failure.
00131      */
00132     virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
00133 
00134     /** Receive data from a socket.
00135      *
00136      *  This is equivalent to calling recvfrom(NULL, data, size).
00137      *
00138      *  If the socket is connected, only packets coming from a connected peer address
00139      *  are accepted.
00140      *
00141      *  By default, recv blocks until some data is received. If socket is set to
00142      *  nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
00143      *  indicate no data.
00144      *
00145      *  @note recv() is allowed write to data buffer even if error occurs.
00146      *
00147      *  @param data     Pointer to buffer for data received from the host.
00148      *  @param size     Size of the buffer in bytes.
00149      *  @return         Number of received bytes on success, negative error
00150      *                  code on failure.
00151      */
00152     virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
00153 
00154     /** Not implemented for UDP.
00155      *
00156      *  @param error      Not used.
00157      *  @return           NSAPI_ERROR_UNSUPPORTED
00158      */
00159     virtual Socket *accept(nsapi_error_t *error = NULL);
00160 
00161     /** Not implemented for UDP.
00162      *
00163      *  @param backlog    Not used.
00164      *  @return           NSAPI_ERROR_UNSUPPORTED
00165      */
00166     virtual nsapi_error_t listen(int backlog = 1);
00167 
00168 #if !defined(DOXYGEN_ONLY)
00169 
00170 protected:
00171     virtual nsapi_protocol_t get_proto();
00172 
00173 #endif //!defined(DOXYGEN_ONLY)
00174 };
00175 
00176 
00177 #endif
00178 
00179 /** @}*/