wip
Fork of Socket by
UDPSocket.h@11:3d83c348fb8b, 2012-08-01 (annotated)
- Committer:
- emilmont
- Date:
- Wed Aug 01 13:02:32 2012 +0000
- Revision:
- 11:3d83c348fb8b
- Parent:
- 10:d24738f4ef99
- Child:
- 16:2d471deff212
[Socket library] Remove redundant UDPPacket class. Use centralized socket destructor. Add is_connected check.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:1f77255a22f5 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 0:1f77255a22f5 | 2 | * |
donatien | 0:1f77255a22f5 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 0:1f77255a22f5 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 0:1f77255a22f5 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 0:1f77255a22f5 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 0:1f77255a22f5 | 7 | * furnished to do so, subject to the following conditions: |
donatien | 0:1f77255a22f5 | 8 | * |
donatien | 0:1f77255a22f5 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 0:1f77255a22f5 | 10 | * substantial portions of the Software. |
donatien | 0:1f77255a22f5 | 11 | * |
donatien | 0:1f77255a22f5 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 0:1f77255a22f5 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 0:1f77255a22f5 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 0:1f77255a22f5 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:1f77255a22f5 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 0:1f77255a22f5 | 17 | */ |
donatien | 0:1f77255a22f5 | 18 | |
donatien | 0:1f77255a22f5 | 19 | #ifndef UDPSOCKET_H |
donatien | 0:1f77255a22f5 | 20 | #define UDPSOCKET_H |
donatien | 0:1f77255a22f5 | 21 | |
emilmont | 3:e6474399e057 | 22 | #include "Socket/Socket.h" |
emilmont | 11:3d83c348fb8b | 23 | #include "Socket/Endpoint.h" |
emilmont | 11:3d83c348fb8b | 24 | |
donatien | 0:1f77255a22f5 | 25 | #include <cstdint> |
donatien | 0:1f77255a22f5 | 26 | |
donatien | 0:1f77255a22f5 | 27 | /** |
emilmont | 8:9cf9c2d45264 | 28 | UDP Socket |
donatien | 0:1f77255a22f5 | 29 | */ |
emilmont | 3:e6474399e057 | 30 | class UDPSocket : public Socket { |
emilmont | 5:300e7ad2dc1d | 31 | |
emilmont | 5:300e7ad2dc1d | 32 | public: |
emilmont | 8:9cf9c2d45264 | 33 | /** Instantiate an UDP Socket. |
donatien | 0:1f77255a22f5 | 34 | */ |
donatien | 0:1f77255a22f5 | 35 | UDPSocket(); |
donatien | 0:1f77255a22f5 | 36 | |
emilmont | 8:9cf9c2d45264 | 37 | /** Init the UDP Client Socket without binding it to any specific port |
emilmont | 8:9cf9c2d45264 | 38 | \return 0 on success, -1 on failure. |
emilmont | 8:9cf9c2d45264 | 39 | */ |
emilmont | 5:300e7ad2dc1d | 40 | int init(void); |
emilmont | 5:300e7ad2dc1d | 41 | |
emilmont | 3:e6474399e057 | 42 | /** Bind a UDP Server Socket to a specific port |
emilmont | 8:9cf9c2d45264 | 43 | \param port The port to listen for incoming connections on |
donatien | 0:1f77255a22f5 | 44 | \return 0 on success, -1 on failure. |
donatien | 0:1f77255a22f5 | 45 | */ |
donatien | 0:1f77255a22f5 | 46 | int bind(int port); |
emilmont | 5:300e7ad2dc1d | 47 | |
emilmont | 11:3d83c348fb8b | 48 | /** Send a packet to a remote endpoint |
emilmont | 11:3d83c348fb8b | 49 | \param remote The remote endpoint |
emilmont | 11:3d83c348fb8b | 50 | \param packet The packet to be sent |
emilmont | 11:3d83c348fb8b | 51 | \param length The length of the packet to be sent |
donatien | 0:1f77255a22f5 | 52 | \return the number of written bytes on success (>=0) or -1 on failure |
donatien | 0:1f77255a22f5 | 53 | */ |
emilmont | 11:3d83c348fb8b | 54 | int sendTo(Endpoint &remote, char *packet, int length); |
emilmont | 5:300e7ad2dc1d | 55 | |
emilmont | 11:3d83c348fb8b | 56 | /** Receive a packet from a remote endpoint |
emilmont | 11:3d83c348fb8b | 57 | \param remote The remote endpoint |
emilmont | 11:3d83c348fb8b | 58 | \param buffer The buffer for storing the incoming packet data. If a packet |
emilmont | 11:3d83c348fb8b | 59 | is too long to fit in the supplied buffer, excess bytes are discarded |
emilmont | 11:3d83c348fb8b | 60 | \param length The length of the buffer |
donatien | 0:1f77255a22f5 | 61 | \return the number of received bytes on success (>=0) or -1 on failure |
donatien | 0:1f77255a22f5 | 62 | */ |
emilmont | 11:3d83c348fb8b | 63 | int receiveFrom(Endpoint &remote, char *buffer, int length); |
donatien | 0:1f77255a22f5 | 64 | }; |
donatien | 0:1f77255a22f5 | 65 | |
emilmont | 11:3d83c348fb8b | 66 | #endif |