takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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
00030  */
00031 class UDPSocket : public InternetSocket {
00032 public:
00033     /** Create an uninitialized socket
00034      *
00035      *  Must call open to initialize the socket on a network stack.
00036      */
00037     UDPSocket();
00038 
00039     /** Create a socket on a network interface
00040      *
00041      *  Creates and opens a socket on the network stack of the given
00042      *  network interface.
00043      *
00044      *  @param stack    Network stack as target for socket
00045      */
00046     template <typename S>
00047     UDPSocket(S *stack)
00048     {
00049         open(stack);
00050     }
00051 
00052     /** Destroy a socket
00053      *
00054      *  Closes socket if the socket is still open
00055      */
00056     virtual ~UDPSocket();
00057 
00058     /** Send a packet over a UDP socket
00059      *
00060      *  Sends data to the specified address specified by either a domain name
00061      *  or an IP address and port. Returns the number of bytes sent from the
00062      *  buffer.
00063      *
00064      *  By default, sendto blocks until data is sent. If socket is set to
00065      *  non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
00066      *  immediately.
00067      *
00068      *  @param host     Hostname of the remote host
00069      *  @param port     Port of the remote host
00070      *  @param data     Buffer of data to send to the host
00071      *  @param size     Size of the buffer in bytes
00072      *  @return         Number of sent bytes on success, negative error
00073      *                  code on failure
00074      */
00075     virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port,
00076                                          const void *data, nsapi_size_t size);
00077 
00078     /** Send a packet over a UDP socket
00079      *
00080      *  Sends data to the specified address. Returns the number of bytes
00081      *  sent from the buffer.
00082      *
00083      *  By default, sendto blocks until data is sent. If socket is set to
00084      *  non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
00085      *  immediately.
00086      *
00087      *  @param address  The SocketAddress of the remote host
00088      *  @param data     Buffer of data to send to the host
00089      *  @param size     Size of the buffer in bytes
00090      *  @return         Number of sent bytes on success, negative error
00091      *                  code on failure
00092      */
00093     virtual nsapi_size_or_error_t sendto(const SocketAddress &address,
00094                                          const void *data, nsapi_size_t size);
00095 
00096     /** Receive a datagram over a UDP socket
00097      *
00098      *  Receives a datagram and stores the source address in address if address
00099      *  is not NULL. Returns the number of bytes written into the buffer. If the
00100      *  datagram is larger than the buffer, the excess data is silently discarded.
00101      *
00102      *  If socket is connected, only packets coming from connected peer address
00103      *  are accepted.
00104      *
00105      *  @note recvfrom() is allowed write to address and data buffers even if error occurs.
00106      *
00107      *  By default, recvfrom blocks until a datagram is received. If socket is set to
00108      *  non-blocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
00109      *  is returned.
00110      *
00111      *  @param address  Destination for the source address or NULL
00112      *  @param data     Destination buffer for datagram received from the host
00113      *  @param size     Size of the buffer in bytes
00114      *  @return         Number of received bytes on success, negative error
00115      *                  code on failure
00116      */
00117     virtual nsapi_size_or_error_t recvfrom(SocketAddress *address,
00118                                            void *data, nsapi_size_t size);
00119 
00120     /** Set remote peer address
00121      *
00122      *  Set the remote address for next send() call and filtering
00123      *  for incomming packets. To reset the address, zero initialised
00124      *  SocketAddress must be in the address parameter.
00125      *
00126      *  @param address  The SocketAddress of the remote host
00127      *  @return         0 on success, negative error code on failure
00128      */
00129     virtual nsapi_error_t connect(const SocketAddress &address);
00130 
00131     /** Send a datagram to pre-specified remote.
00132      *
00133      *  The socket must be connected to a remote host before send() call.
00134      *  Returns the number of bytes sent from the buffer.
00135      *
00136      *  By default, send blocks until all data is sent. If socket is set to
00137      *  non-blocking or times out, a partial amount can be written.
00138      *  NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
00139      *
00140      *  @param data     Buffer of data to send to the host
00141      *  @param size     Size of the buffer in bytes
00142      *  @return         Number of sent bytes on success, negative error
00143      *                  code on failure.
00144      */
00145     virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
00146 
00147     /** Receive data from a socket.
00148      *
00149      *  This is equivalent of calling recvfrom(NULL, data, size).
00150      *
00151      *  If socket is connected, only packets coming from connected peer address
00152      *  are accepted.
00153      *
00154      *  @note recv() is allowed write to data buffer even if error occurs.
00155      *
00156      *  By default, recv blocks until some data is received. If socket is set to
00157      *  non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
00158      *  indicate no data.
00159      *
00160      *  @param data     Destination buffer for data received from the host
00161      *  @param size     Size of the buffer in bytes
00162      *  @return         Number of received bytes on success, negative error
00163      *                  code on failure.
00164      */
00165     virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
00166 
00167     /** Not implemented for UDP
00168      *
00169      *  @param error      unused
00170      *  @return           NSAPI_ERROR_UNSUPPORTED
00171      */
00172     virtual Socket *accept(nsapi_error_t *error = NULL);
00173 
00174     /** Not implemented for UDP
00175      *
00176      *  @param backlog    unused
00177      *  @return           NSAPI_ERROR_UNSUPPORTED
00178      */
00179     virtual nsapi_error_t listen(int backlog = 1);
00180 
00181 protected:
00182     virtual nsapi_protocol_t get_proto();
00183 };
00184 
00185 
00186 #endif
00187 
00188 /** @}*/