/** Copyright 2013-2014 Haiku, Inc. All rights reserved.* Distributed under the terms of the MIT License.** Authors:* Adrien Destugues, pulkomandy@pulkomandy.tk* Axel DΓΆrfler, axeld@pinc-software.de** Corresponds to:* headers/os/net/DatagramSocket.h rev 43302* src/kits/network/libnetapi/DatagramSocket.cpp rev 43302*//*!\file DatagramSocket.h\ingroup network\brief BAbstractSocket implementation for datagram connections.*//*!\class BDatagramSocket\ingroup network\brief BAbstractSocket implementation for datagram connections.Datagrams are atomic messages. There is no notion of sequence and the datasent in a sequence of write calls may not get to the other end of theconnections in the same order. There is no flow control, so some of themmay not even make it to the peer. The most well known datagram protocolis UDP, which also happens to be the only one that Haiku currently supports.The main uses for datagram sockets are when performance is more importantthan safety (the lack of acknowledge mechanism allows to send a lot ofdatagram packets at once, whereas TCP is limited by its sliding windowmechanism), when the application wants to manage flow control andacknowledges itself (ie. when you want to implement your own protocol ontop of UDP), and when lost packets don't matter (for example, ina video stream, there is no use for receiving late video frames if theywere already skipped to play the following ones).Since UDP is a connectionless protocol, in order to specify the target,or to be able to know from where you got a packet, this class providesyou with the extra methods SendTo() and ReceiveFrom().*//*!\fn BDatagramSocket::BDatagramSocket()\brief Default constructor.Does nothing. Call Bind() or Connect() to actually start networkcommunications.\see BAbstractSocket::BAbstractSocket().*//*!\fn BDatagramSocket::BDatagramSocket(const BNetworkAddress& peer,bigtime_t timeout)\brief Create and connect a datagram socket.The socket is immediately connected to the given peer. Use InitCheck() tomake sure the connection was successful.\param peer host to connect to\param timeout connection timeout, in microsecond.*//*!\fn BDatagramSocket::BDatagramSocket(const BDatagramSocket& other)\brief Copy constructor.*//*!\fn BDatagramSocket::~BDatagramSocket()\brief Destructor.The socket is disconnected.*//*!\fn BDatagramSocket::SetBroadcast(bool broadcast)\brief enables or disable broadcast modeIn broadcast mode, datagrams can be sent to multiple peers at once.Calling this method is not enough, you must also set your peer address tobe \c INADDR_BROADCAST to effectively send a broadcast message.Note that broadcast messages usually don't propagate on Internet as theywould generate too much traffic. Their use is thus restricted to localnetworks.\param broadcast the requested state for broadcast permissions.\return \c B_OK on success, or other error codes on failure.*//*!\fn void BDatagramSocket::SetPeer(const BNetworkAddress& peer)\brief Change the remote host for this connections.Datagram connections are not statically bound to a remote address, so it ispossible to change the destination of packets at runtime.Note that packets coming to the right local address, no matter where theycome from, will always be accepted.\param peer the address to which following Write calls will send datagrams*//*!\fn size_t BDatagramSocket::MaxTransmissionSize() constThe maximum size for datagram sockets is 32768 bytes.\returns 32768*//*!\fn ssize_t BDatagramSocket::SendTo(const BNetworkAddress& address,const void* buffer, size_t size)\brief Send a single datagram to the given addressUnlike the Write() method, which always sends to the same peer, this methodcan be used to send messages to different destinations.\param address the host to send the datagram to\param buffer datagram contents\param size size of the buffer\returns the number of bytes sent, which may be less than requested, or anegative error code.*//*!\fn ssize_t BDatagramSocket::ReceiveFrom(void* buffer, size_t bufferSize,BNetworkAddress& from)\brief receive a single datagram from a given hostReceives a datagram, and fills the \a from address with the host thatsent it.If the buffer is too small, extra bytes from the datagram will be lost.\param buffer the buffer to store the datagram in\param bufferSize size of the buffer\param from the datagram sender address*//*!\fn ssize_t BDatagramSocket::Read(void* buffer, size_t size)\brief Receive a datagram from any senderThis is similar to ReceiveFrom(), but there is no way to know who sentthe message.If the buffer is too small, the remaining part of the datagram is lost.\param buffer memory to store the datagram in\param size the size of the buffer\return the number of bytes actually written, or a negative error code.*//*!\fn ssize_t BDatagramSocket::Write(const void* buffer, size_t size)\brief Send a datagram to the default targetIf the socket is connected, send a datagram to the connected host.If it's not, send to the peer given to the SetPeer() function.\param buffer the datagram to send\param size the size of the message\return the number of bytes written, which may be less than requested, ora negative error code.*/