SocketObserver (Naga API)
Date: 2016-06-28

naga

## Interface SocketObserver

- All Known Implementing Classes:
    SocketObserverAdapter

      public interface SocketObserver

  This interface contains the callbacks used by a NIOSocket to inform its observer of events.

  All callbacks will be run on the NIOService-thread, so callbacks should try to return as quickly as possible since the callback blocks communication on all sockets of the service.

  Author:
    Christoffer Lerno

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static SocketObserver` | `NULL` A null object used as the default observer

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `connectionBroken(NIOSocket nioSocket, java.lang.Exception exception)` Called by the NIOService on the NIO thread when a connection is disconnected.
    `void` | `connectionOpened(NIOSocket nioSocket)` Called by the NIOService on the NIO thread when a connection completes on a socket.
    `void` | `packetReceived(NIOSocket socket, byte[] packet)` Called by the NIOService on the NIO thread when a packet is finished reading.
    `void` | `packetSent(NIOSocket socket, java.lang.Object tag)` Called by the NIOService on the NIO thread when a packet has finished writing.

- - ### Field Detail

    - #### NULL

          static final SocketObserver NULL

      A null object used as the default observer

  - ### Method Detail

    - #### connectionOpened

          void connectionOpened(NIOSocket nioSocket)

      Called by the NIOService on the NIO thread when a connection completes on a socket.

      Note: Since this is a direct callback on the NIO thread, this method will suspend IO on all other connections until the method returns. It is therefore strongly recommended that the implementation of this method returns as quickly as possible to avoid blocking IO.

      Parameters:
        `nioSocket` - the socket that completed its connect.

    - #### connectionBroken

          void connectionBroken(NIOSocket nioSocket,
                              java.lang.Exception exception)

      Called by the NIOService on the NIO thread when a connection is disconnected.

      This may be sent even if a `connectionOpened(NIOSocket)` wasn't ever called, since the connect itself may fail.

      Note: Since this is a direct callback on the NIO thread, this method will suspend IO on all other connections until the method returns. It is therefore strongly recommended that the implementation of this method returns as quickly as possible to avoid blocking IO.

      Parameters:
        `nioSocket` - the socket that was disconnected.
        `exception` - the exception that caused the connection to break, may be null.

    - #### packetReceived

          void packetReceived(NIOSocket socket,
                            byte[] packet)

      Called by the NIOService on the NIO thread when a packet is finished reading. The byte array contains the packet as parsed by the current PacketReader.

      Note: Since this is a direct callback on the NIO thread, this method will suspend IO on all other connections until the method returns. It is therefore strongly recommended that the implementation of this method returns as quickly as possible to avoid blocking IO.

      Parameters:
        `socket` - the socket we received a packet on.
        `packet` - the packet we received.

    - #### packetSent

          void packetSent(NIOSocket socket,
                        java.lang.Object tag)

      Called by the NIOService on the NIO thread when a packet has finished writing.

      Note: Since this is a direct callback on the NIO thread, this method will suspend IO on all other connections until the method returns. It is therefore strongly recommended that the implementation of this method returns as quickly as possible to avoid blocking IO.

      Parameters:
        `socket` - the socket we sent the packet on.
        `tag` - the (optional) tag associated with the packet.