1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_UDP_DATAGRAM_SERVER_SOCKET_H_ 6 #define NET_UDP_DATAGRAM_SERVER_SOCKET_H_ 7 8 #include "net/base/completion_callback.h" 9 #include "net/base/net_util.h" 10 #include "net/udp/datagram_socket.h" 11 12 namespace net { 13 14 class IPEndPoint; 15 class IOBuffer; 16 17 // A UDP Socket. 18 class NET_EXPORT DatagramServerSocket : public DatagramSocket { 19 public: 20 virtual ~DatagramServerSocket() {} 21 22 // Initialize this socket as a server socket listening at |address|. 23 // Returns a network error code. 24 virtual int Listen(const IPEndPoint& address) = 0; 25 26 // Read from a socket and receive sender address information. 27 // |buf| is the buffer to read data into. 28 // |buf_len| is the maximum amount of data to read. 29 // |address| is a buffer provided by the caller for receiving the sender 30 // address information about the received data. This buffer must be kept 31 // alive by the caller until the callback is placed. 32 // |address_length| is a ptr to the length of the |address| buffer. This 33 // is an input parameter containing the maximum size |address| can hold 34 // and also an output parameter for the size of |address| upon completion. 35 // |callback| the callback on completion of the Recv. 36 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. 37 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, 38 // and |address_length| alive until the callback is called. 39 virtual int RecvFrom(IOBuffer* buf, 40 int buf_len, 41 IPEndPoint* address, 42 const CompletionCallback& callback) = 0; 43 44 // Send to a socket with a particular destination. 45 // |buf| is the buffer to send 46 // |buf_len| is the number of bytes to send 47 // |address| is the recipient address. 48 // |address_length| is the size of the recipient address 49 // |callback| is the user callback function to call on complete. 50 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. 51 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| 52 // alive until the callback is called. 53 virtual int SendTo(IOBuffer* buf, 54 int buf_len, 55 const IPEndPoint& address, 56 const CompletionCallback& callback) = 0; 57 58 // Set the receive buffer size (in bytes) for the socket. 59 virtual bool SetReceiveBufferSize(int32 size) = 0; 60 61 // Set the send buffer size (in bytes) for the socket. 62 virtual bool SetSendBufferSize(int32 size) = 0; 63 64 // Allow the socket to share the local address to which the socket will 65 // be bound with other processes. Should be called before Listen(). 66 virtual void AllowAddressReuse() = 0; 67 68 // Allow sending and receiving packets to and from broadcast addresses. 69 // Should be called before Listen(). 70 virtual void AllowBroadcast() = 0; 71 72 // Join the multicast group with address |group_address|. 73 // Returns a network error code. 74 virtual int JoinGroup(const IPAddressNumber& group_address) const = 0; 75 76 // Leave the multicast group with address |group_address|. 77 // If the socket hasn't joined the group, it will be ignored. 78 // It's optional to leave the multicast group before destroying 79 // the socket. It will be done by the OS. 80 // Returns a network error code. 81 virtual int LeaveGroup(const IPAddressNumber& group_address) const = 0; 82 83 // Set the time-to-live option for UDP packets sent to the multicast 84 // group address. The default value of this option is 1. 85 // Cannot be negative or more than 255. 86 // Should be called before Bind(). 87 // Returns a network error code. 88 virtual int SetMulticastTimeToLive(int time_to_live) = 0; 89 90 // Set the loopback flag for UDP socket. If this flag is true, the host 91 // will receive packets sent to the joined group from itself. 92 // The default value of this option is true. 93 // Should be called before Bind(). 94 // Returns a network error code. 95 virtual int SetMulticastLoopbackMode(bool loopback) = 0; 96 }; 97 98 } // namespace net 99 100 #endif // NET_UDP_DATAGRAM_SERVER_SOCKET_H_ 101