/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsISupports.idl"
#include "nsINetAddr.idl"

interface nsIUDPSocketInternal;
interface nsIInputStream;
interface nsIPrincipal;
interface nsIEventTarget;

%{ C++
namespace mozilla {
namespace net {
union NetAddr;
}
}
%}
native NetAddr(mozilla::net::NetAddr);
[ptr] native NetAddrPtr(mozilla::net::NetAddr);

[scriptable, uuid(1e6ad73b-6c05-4d78-9a88-2d357b88f58b)]
interface nsIUDPSocketChild : nsISupports
{
  readonly attribute unsigned short localPort;
  readonly attribute AUTF8String localAddress;
  attribute AUTF8String filterName;

  // Allow hosting this over PBackground instead of PNecko
  [noscript] void setBackgroundSpinsEvents();

  // Tell the chrome process to bind the UDP socket to a given local host and port
  void bind(in nsIUDPSocketInternal socket, in nsIPrincipal principal,
            in AUTF8String host, in unsigned short port,
            in bool addressReuse, in bool loopback, in uint32_t recvBufferSize,
            in uint32_t sendBufferSize,
            [optional] in nsIEventTarget mainThreadTarget);

  // Tell the chrome process to connect the UDP socket to a given remote host and port
  void connect(in nsIUDPSocketInternal socket, in AUTF8String host, in unsigned short port);

  // Tell the chrome process to perform equivalent operations to all following methods
  void send(in AUTF8String host, in unsigned short port,
            [const, array, size_is(byteLength)] in uint8_t bytes,
            in unsigned long byteLength);
  // Send without DNS query
  void sendWithAddr(in nsINetAddr addr,
                    [const, array, size_is(byteLength)] in uint8_t bytes,
                    in unsigned long byteLength);
  [noscript] void sendWithAddress([const] in NetAddrPtr addr,
                                  [const, array, size_is(byteLength)] in uint8_t bytes,
                                  in unsigned long byteLength);
  // Send input stream. This must be a buffered stream implementation.
  void sendBinaryStream(in AUTF8String host, in unsigned short port, in nsIInputStream stream);

  void close();
  void joinMulticast(in AUTF8String multicastAddress, in AUTF8String iface);
  void leaveMulticast(in AUTF8String multicastAddress, in AUTF8String iface);
};

/*
 * Internal interface for callback from chrome process
 */
[scriptable, uuid(613dd3ad-598b-4da9-ad63-bbda50c20098)]
interface nsIUDPSocketInternal : nsISupports
{
  // callback while socket is opened. localPort and localAddress is ready until this time.
  void callListenerOpened();
  // callback while socket is connected.
  void callListenerConnected();
  // callback while socket is closed.
  void callListenerClosed();
  // callback while incoming packet is received.
  void callListenerReceivedData(in AUTF8String host, in unsigned short port,
                                [const, array, size_is(dataLength)] in uint8_t data,
                                in unsigned long dataLength);
  // callback while any error happened.
  void callListenerError(in AUTF8String message, in AUTF8String filename, in uint32_t lineNumber);
};
