14cf62172SAdrien Destugues/* 2*c8127e2bSAxel Dörfler * Copyright 2013-2014 Haiku, Inc. All rights reserved. 34cf62172SAdrien Destugues * Distributed under the terms of the MIT License. 44cf62172SAdrien Destugues * 54cf62172SAdrien Destugues * Authors: 64cf62172SAdrien Destugues * Adrien Destugues, pulkomandy@pulkomandy.tk 7*c8127e2bSAxel Dörfler * Axel Dörfler, axeld@pinc-software.de 84cf62172SAdrien Destugues * 94cf62172SAdrien Destugues * Corresponds to: 104cf62172SAdrien Destugues * headers/os/net/DatagramSocket.h rev 43302 114cf62172SAdrien Destugues * src/kits/network/libnetapi/DatagramSocket.cpp rev 43302 124cf62172SAdrien Destugues */ 134cf62172SAdrien Destugues 144cf62172SAdrien Destugues/*! 154cf62172SAdrien Destugues \file DatagramSocket.h 164cf62172SAdrien Destugues \ingroup network 17*c8127e2bSAxel Dörfler \brief BAbstractSocket implementation for datagram connections. 184cf62172SAdrien Destugues*/ 194cf62172SAdrien Destugues 204cf62172SAdrien Destugues/*! 214cf62172SAdrien Destugues \class BDatagramSocket 224cf62172SAdrien Destugues \ingroup network 23*c8127e2bSAxel Dörfler \brief BAbstractSocket implementation for datagram connections. 244cf62172SAdrien Destugues 254cf62172SAdrien Destugues Datagrams are atomic messages. There is no notion of sequence and the data 26*c8127e2bSAxel Dörfler sent in a sequence of write calls may not get to the other end of the 274cf62172SAdrien Destugues connections in the same order. There is no flow control, so some of them 28*c8127e2bSAxel Dörfler may not even make it to the peer. The most well known datagram protocol 29*c8127e2bSAxel Dörfler is UDP, which also happens to be the only one that Haiku currently supports. 304cf62172SAdrien Destugues 314cf62172SAdrien Destugues The main uses for datagram sockets are when performance is more important 324cf62172SAdrien Destugues than safety (the lack of acknowledge mechanism allows to send a lot of 334cf62172SAdrien Destugues datagram packets at once, whereas TCP is limited by its sliding window 344cf62172SAdrien Destugues mechanism), when the application wants to manage flow control and 35*c8127e2bSAxel Dörfler acknowledges itself (ie. when you want to implement your own protocol on 36*c8127e2bSAxel Dörfler top of UDP), and when lost packets don't matter (for example, in 374cf62172SAdrien Destugues a video stream, there is no use for receiving late video frames if they 384cf62172SAdrien Destugues were already skipped to play the following ones). 394cf62172SAdrien Destugues 40*c8127e2bSAxel Dörfler Since UDP is a connectionless protocol, in order to specify the target, 41*c8127e2bSAxel Dörfler or to be able to know from where you got a packet, this class provides 42*c8127e2bSAxel Dörfler you with the extra methods SendTo() and ReceiveFrom(). 434cf62172SAdrien Destugues*/ 444cf62172SAdrien Destugues 454cf62172SAdrien Destugues/*! 464cf62172SAdrien Destugues \fn BDatagramSocket::BDatagramSocket() 474cf62172SAdrien Destugues \brief Default constructor. 484cf62172SAdrien Destugues 49*c8127e2bSAxel Dörfler Does nothing. Call Bind() or Connect() to actually start network 50*c8127e2bSAxel Dörfler communications. 514cf62172SAdrien Destugues 524cf62172SAdrien Destugues \see BAbstractSocket::BAbstractSocket(). 534cf62172SAdrien Destugues*/ 544cf62172SAdrien Destugues 554cf62172SAdrien Destugues/*! 564cf62172SAdrien Destugues \fn BDatagramSocket::BDatagramSocket(const BNetworkAddress& peer, 574cf62172SAdrien Destugues bigtime_t timeout) 584cf62172SAdrien Destugues \brief Create and connect a datagram socket. 594cf62172SAdrien Destugues 60*c8127e2bSAxel Dörfler The socket is immediately connected to the given peer. Use InitCheck() to 614cf62172SAdrien Destugues make sure the connection was successful. 624cf62172SAdrien Destugues 634cf62172SAdrien Destugues \param peer host to connect to 644cf62172SAdrien Destugues \param timeout connection timeout, in microsecond. 654cf62172SAdrien Destugues*/ 664cf62172SAdrien Destugues 674cf62172SAdrien Destugues/*! 684cf62172SAdrien Destugues \fn BDatagramSocket::BDatagramSocket(const BDatagramSocket& other) 694cf62172SAdrien Destugues \brief Copy constructor. 704cf62172SAdrien Destugues*/ 714cf62172SAdrien Destugues 724cf62172SAdrien Destugues/*! 734cf62172SAdrien Destugues \fn BDatagramSocket::~BDatagramSocket() 744cf62172SAdrien Destugues \brief Destructor. 754cf62172SAdrien Destugues 764cf62172SAdrien Destugues The socket is disconnected. 774cf62172SAdrien Destugues*/ 784cf62172SAdrien Destugues 794cf62172SAdrien Destugues/*! 804cf62172SAdrien Destugues \fn BDatagramSocket::SetBroadcast(bool broadcast) 814cf62172SAdrien Destugues \brief enables or disable broadcast mode 824cf62172SAdrien Destugues 834cf62172SAdrien Destugues In broadcast mode, datagrams can be sent to multiple peers at once. 844cf62172SAdrien Destugues Calling this method is not enough, you must also set your peer address to 85*c8127e2bSAxel Dörfler be \c INADDR_BROADCAST to effectively send a broadcast message. 864cf62172SAdrien Destugues 874cf62172SAdrien Destugues Note that broadcast messages usually don't propagate on Internet as they 884cf62172SAdrien Destugues would generate too much traffic. Their use is thus restricted to local 894cf62172SAdrien Destugues networks. 904cf62172SAdrien Destugues 914cf62172SAdrien Destugues \param broadcast the requested state for broadcast permissions. 92*c8127e2bSAxel Dörfler \return \c B_OK on success, or other error codes on failure. 934cf62172SAdrien Destugues*/ 944cf62172SAdrien Destugues 954cf62172SAdrien Destugues/*! 964cf62172SAdrien Destugues \fn void BDatagramSocket::SetPeer(const BNetworkAddress& peer) 974cf62172SAdrien Destugues \brief Change the remote host for this connections. 984cf62172SAdrien Destugues 994cf62172SAdrien Destugues Datagram connections are not statically bound to a remote address, so it is 1004cf62172SAdrien Destugues possible to change the destination of packets at runtime. 1014cf62172SAdrien Destugues 1024cf62172SAdrien Destugues Note that packets coming to the right local address, no matter where they 1034cf62172SAdrien Destugues come from, will always be accepted. 1044cf62172SAdrien Destugues 1054cf62172SAdrien Destugues \param peer the address to which following Write calls will send datagrams 1064cf62172SAdrien Destugues*/ 1074cf62172SAdrien Destugues 1084cf62172SAdrien Destugues/*! 1094cf62172SAdrien Destugues \fn size_t BDatagramSocket::MaxTransmissionSize() const 1104cf62172SAdrien Destugues 1114cf62172SAdrien Destugues The maximum size for datagram sockets is 32768 bytes. 1124cf62172SAdrien Destugues 1134cf62172SAdrien Destugues \returns 32768 1144cf62172SAdrien Destugues*/ 1154cf62172SAdrien Destugues 1164cf62172SAdrien Destugues/*! 1174cf62172SAdrien Destugues \fn ssize_t BDatagramSocket::SendTo(const BNetworkAddress& address, 1184cf62172SAdrien Destugues const void* buffer, size_t size) 1194cf62172SAdrien Destugues \brief Send a single datagram to the given address 1204cf62172SAdrien Destugues 121*c8127e2bSAxel Dörfler Unlike the Write() method, which always sends to the same peer, this method 1224cf62172SAdrien Destugues can be used to send messages to different destinations. 1234cf62172SAdrien Destugues 1244cf62172SAdrien Destugues \param address the host to send the datagram to 1254cf62172SAdrien Destugues \param buffer datagram contents 1264cf62172SAdrien Destugues \param size size of the buffer 1274cf62172SAdrien Destugues \returns the number of bytes sent, which may be less than requested, or a 1284cf62172SAdrien Destugues negative error code. 1294cf62172SAdrien Destugues*/ 1304cf62172SAdrien Destugues 1314cf62172SAdrien Destugues/*! 1324cf62172SAdrien Destugues \fn ssize_t BDatagramSocket::ReceiveFrom(void* buffer, size_t bufferSize, 1334cf62172SAdrien Destugues BNetworkAddress& from) 1344cf62172SAdrien Destugues \brief receive a single datagram from a given host 1354cf62172SAdrien Destugues 136*c8127e2bSAxel Dörfler Receives a datagram, and fills the \a from address with the host that 137*c8127e2bSAxel Dörfler sent it. 1384cf62172SAdrien Destugues If the buffer is too small, extra bytes from the datagram will be lost. 1394cf62172SAdrien Destugues 1404cf62172SAdrien Destugues \param buffer the buffer to store the datagram in 1414cf62172SAdrien Destugues \param bufferSize size of the buffer 142*c8127e2bSAxel Dörfler \param from the datagram sender address 1434cf62172SAdrien Destugues*/ 1444cf62172SAdrien Destugues 1454cf62172SAdrien Destugues/*! 1464cf62172SAdrien Destugues \fn ssize_t BDatagramSocket::Read(void* buffer, size_t size) 1474cf62172SAdrien Destugues \brief Receive a datagram from any sender 1484cf62172SAdrien Destugues 149*c8127e2bSAxel Dörfler This is similar to ReceiveFrom(), but there is no way to know who sent 150*c8127e2bSAxel Dörfler the message. 1514cf62172SAdrien Destugues 1524cf62172SAdrien Destugues If the buffer is too small, the remaining part of the datagram is lost. 1534cf62172SAdrien Destugues 1544cf62172SAdrien Destugues \param buffer memory to store the datagram in 1554cf62172SAdrien Destugues \param size the size of the buffer 1564cf62172SAdrien Destugues \return the number of bytes actually written, or a negative error code. 1574cf62172SAdrien Destugues*/ 1584cf62172SAdrien Destugues 1594cf62172SAdrien Destugues/*! 1604cf62172SAdrien Destugues \fn ssize_t BDatagramSocket::Write(const void* buffer, size_t size) 1614cf62172SAdrien Destugues \brief Send a datagram to the default target 1624cf62172SAdrien Destugues 1634cf62172SAdrien Destugues If the socket is connected, send a datagram to the connected host. 164*c8127e2bSAxel Dörfler If it's not, send to the peer given to the SetPeer() function. 1654cf62172SAdrien Destugues 1664cf62172SAdrien Destugues \param buffer the datagram to send 1674cf62172SAdrien Destugues \param size the size of the message 1684cf62172SAdrien Destugues \return the number of bytes written, which may be less than requested, or 1694cf62172SAdrien Destugues a negative error code. 1704cf62172SAdrien Destugues*/ 171