15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_UDP_DATAGRAM_SERVER_SOCKET_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_UDP_DATAGRAM_SERVER_SOCKET_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/completion_callback.h" 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "net/base/net_util.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/udp/datagram_socket.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net { 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IPEndPoint; 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IOBuffer; 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A UDP Socket. 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT DatagramServerSocket : public DatagramSocket { 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~DatagramServerSocket() {} 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Initialize this socket as a server socket listening at |address|. 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a network error code. 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int Listen(const IPEndPoint& address) = 0; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Read from a socket and receive sender address information. 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |buf| is the buffer to read data into. 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |buf_len| is the maximum amount of data to read. 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |address| is a buffer provided by the caller for receiving the sender 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // address information about the received data. This buffer must be kept 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // alive by the caller until the callback is placed. 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |address_length| is a ptr to the length of the |address| buffer. This 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is an input parameter containing the maximum size |address| can hold 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and also an output parameter for the size of |address| upon completion. 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |callback| is the callback on completion of the RecvFrom. 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and |address_length| alive until the callback is called. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int RecvFrom(IOBuffer* buf, 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int buf_len, 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IPEndPoint* address, 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const CompletionCallback& callback) = 0; 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Send to a socket with a particular destination. 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |buf| is the buffer to send 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |buf_len| is the number of bytes to send 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |address| is the recipient address. 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |address_length| is the size of the recipient address 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |callback| is the user callback function to call on complete. 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // alive until the callback is called. 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int SendTo(IOBuffer* buf, 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int buf_len, 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const IPEndPoint& address, 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const CompletionCallback& callback) = 0; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set the receive buffer size (in bytes) for the socket. 59c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch // Returns a net error code. 60c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch virtual int SetReceiveBufferSize(int32 size) = 0; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set the send buffer size (in bytes) for the socket. 63c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch // Returns a net error code. 64c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch virtual int SetSendBufferSize(int32 size) = 0; 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Allow the socket to share the local address to which the socket will 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // be bound with other processes. Should be called before Listen(). 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AllowAddressReuse() = 0; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Allow sending and receiving packets to and from broadcast addresses. 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Should be called before Listen(). 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AllowBroadcast() = 0; 73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Join the multicast group with address |group_address|. 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Returns a network error code. 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual int JoinGroup(const IPAddressNumber& group_address) const = 0; 77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Leave the multicast group with address |group_address|. 79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // If the socket hasn't joined the group, it will be ignored. 80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // It's optional to leave the multicast group before destroying 81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // the socket. It will be done by the OS. 82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Returns a network error code. 83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual int LeaveGroup(const IPAddressNumber& group_address) const = 0; 84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Set interface to use for multicast. If |interface_index| set to 0, default 86a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // interface is used. 87a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Should be called before Bind(). 88a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Returns a network error code. 89a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) virtual int SetMulticastInterface(uint32 interface_index) = 0; 90a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Set the time-to-live option for UDP packets sent to the multicast 92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // group address. The default value of this option is 1. 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Cannot be negative or more than 255. 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Should be called before Bind(). 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Returns a network error code. 96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual int SetMulticastTimeToLive(int time_to_live) = 0; 97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Set the loopback flag for UDP socket. If this flag is true, the host 99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // will receive packets sent to the joined group from itself. 100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // The default value of this option is true. 101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Should be called before Bind(). 102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Returns a network error code. 103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual int SetMulticastLoopbackMode(bool loopback) = 0; 1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Set the Differentiated Services Code Point. May do nothing on 1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // some platforms. Returns a network error code. 1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) virtual int SetDiffServCodePoint(DiffServCodePoint dscp) = 0; 108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Resets the thread to be used for thread-safety checks. 110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void DetachFromThread() = 0; 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace net 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // NET_UDP_DATAGRAM_SERVER_SOCKET_H_ 116