Class ServiceChannel

java.lang.Object
org.simpleframework.http.socket.service.ServiceChannel
All Implemented Interfaces:
FrameChannel

class ServiceChannel extends Object implements FrameChannel
The ServiceChannel represents a full duplex communication channel as defined by RFC 6455. Any instance of this will provide a means to perform asynchronous writes and reads to a remote client using a lightweight framing protocol. A frame is a finite length sequence of bytes that can hold either text or binary data. Also, control frames are used to perform heartbeat monitoring and closure.

For convenience frames can be consumed from the socket via a callback to a registered listener. This avoids having to poll each socket for data and provides a asynchronous event driven model of communication, which greatly reduces overhead and complication.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final FrameChannel
    This is the internal channel for full duplex communication.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the ServiceChannel object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This is used to close the connection without a specific reason.
    void
    close(Reason reason)
    This is used to close the connection with a specific reason.
    void
    This is used to register a FrameListener to this instance.
    void
    This is used to remove a FrameListener from this instance.
    void
    send(byte[] data)
    This is used to send data to the connected client.
    void
    send(String text)
    This is used to send text to the connected client.
    void
    send(Frame frame)
    This is used to send data to the connected client.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • channel

      private final FrameChannel channel
      This is the internal channel for full duplex communication.
  • Constructor Details

    • ServiceChannel

      public ServiceChannel(FrameChannel channel)
      Constructor for the ServiceChannel object. This is used to create a channel that is given to the application. This is synchronized so only one frame can be dispatched at a time.
      Parameters:
      channel - this is the channel to delegate to
  • Method Details

    • send

      public void send(byte[] data) throws IOException
      This is used to send data to the connected client. To prevent an application code from causing resource issues this will block as soon as a configured linked list of mapped memory buffers has been exhausted. Caution should be taken when writing a broadcast implementation that can write to multiple sockets as a badly behaving socket that has filled its output buffering capacity can cause congestion.
      Specified by:
      send in interface FrameChannel
      Parameters:
      data - this is the data that is to be sent
      Throws:
      IOException
    • send

      public void send(String text) throws IOException
      This is used to send text to the connected client. To prevent an application code from causing resource issues this will block as soon as a configured linked list of mapped memory buffers has been exhausted. Caution should be taken when writing a broadcast implementation that can write to multiple sockets as a badly behaving socket that has filled its output buffering capacity can cause congestion.
      Specified by:
      send in interface FrameChannel
      Parameters:
      text - this is the text that is to be sent
      Throws:
      IOException
    • send

      public void send(Frame frame) throws IOException
      This is used to send data to the connected client. To prevent an application code from causing resource issues this will block as soon as a configured linked list of mapped memory buffers has been exhausted. Caution should be taken when writing a broadcast implementation that can write to multiple sockets as a badly behaving socket that has filled its output buffering capacity can cause congestion.
      Specified by:
      send in interface FrameChannel
      Parameters:
      frame - this is the frame that is to be sent
      Throws:
      IOException
    • register

      public void register(FrameListener listener) throws IOException
      This is used to register a FrameListener to this instance. The registered listener will receive all user frames and control frames sent from the client. Also, when the frame is closed or when an unexpected error occurs the listener is notified. Any number of listeners can be registered at any time.
      Specified by:
      register in interface FrameChannel
      Parameters:
      listener - this is the listener that is to be registered
      Throws:
      IOException
    • remove

      public void remove(FrameListener listener) throws IOException
      This is used to remove a FrameListener from this instance. After removal the listener will no longer receive any user frames or control messages from this specific instance.
      Specified by:
      remove in interface FrameChannel
      Parameters:
      listener - this is the listener to be removed
      Throws:
      IOException
    • close

      public void close(Reason reason) throws IOException
      This is used to close the connection with a specific reason. The close reason will be sent as a control frame before the TCP connection is terminated.
      Specified by:
      close in interface FrameChannel
      Parameters:
      reason - the reason for closing the connection
      Throws:
      IOException
    • close

      public void close() throws IOException
      This is used to close the connection without a specific reason. The close reason will be sent as a control frame before the TCP connection is terminated.
      Specified by:
      close in interface FrameChannel
      Throws:
      IOException