class Poco::Net::WebSocketImpl

Overview

This class implements a WebSocket, according to the WebSocket protocol described in RFC 6455. More…

#include <WebSocketImpl.h>

class WebSocketImpl: public Poco::Net::StreamSocketImpl
{
public:
    // construction

    WebSocketImpl(
        StreamSocketImpl* pStreamSocketImpl,
        HTTPSession& session,
        bool mustMaskPayload
        );

    // methods

    virtual
    int
    sendBytes(
        const void* buffer,
        int length,
        int flags
        );

    virtual
    int
    receiveBytes(
        void* buffer,
        int length,
        int flags
        );

    virtual
    SocketImpl*
    acceptConnection(SocketAddress& clientAddr);

    virtual
    void
    connect(const SocketAddress& address);

    virtual
    void
    connect(
        const SocketAddress& address,
        const Poco::Timespan& timeout
        );

    virtual
    void
    connectNB(const SocketAddress& address);

    virtual
    void
    bind(
        const SocketAddress& address,
        bool reuseAddress = false
        );

    virtual
    void
    bind6(
        const SocketAddress& address,
        bool reuseAddress = false,
        bool ipV6Only = false
        );

    virtual
    void
    listen(int backlog = 64);

    virtual
    void
    close();

    virtual
    void
    shutdownReceive();

    virtual
    void
    shutdownSend();

    virtual
    void
    shutdown();

    virtual
    int
    sendTo(
        const void* buffer,
        int length,
        const SocketAddress& address,
        int flags = 0
        );

    virtual
    int
    receiveFrom(
        void* buffer,
        int length,
        SocketAddress& address,
        int flags = 0
        );

    virtual
    void
    sendUrgent(unsigned char data);

    virtual
    int
    available();

    virtual
    bool
    secure() const;

    virtual
    void
    setSendTimeout(const Poco::Timespan& timeout);

    virtual
    Poco::Timespan
    getSendTimeout();

    virtual
    void
    setReceiveTimeout(const Poco::Timespan& timeout);

    virtual
    Poco::Timespan
    getReceiveTimeout();

    int
    frameFlags() const;

    bool
    mustMaskPayload() const;

protected:
    // enums

    enum
    {
        FRAME_FLAG_MASK   = 0x80,
        MAX_HEADER_LENGTH = 14,
    };

    // methods

    int
    receiveNBytes(
        void* buffer,
        int bytes
        );

    int
    receiveSomeBytes(
        char* buffer,
        int bytes
        );
};

Inherited Members

public:
    // enums

    enum SelectMode;

    // methods

    void
    duplicate() const;

    void
    release() const;

    int
    referenceCount() const;

    virtual
    SocketImpl*
    acceptConnection(SocketAddress& clientAddr);

    virtual
    void
    connect(const SocketAddress& address);

    virtual
    void
    connect(
        const SocketAddress& address,
        const Poco::Timespan& timeout
        );

    virtual
    void
    connectNB(const SocketAddress& address);

    virtual
    void
    bind(
        const SocketAddress& address,
        bool reuseAddress = false
        );

    virtual
    void
    bind6(
        const SocketAddress& address,
        bool reuseAddress = false,
        bool ipV6Only = false
        );

    virtual
    void
    listen(int backlog = 64);

    virtual
    void
    close();

    virtual
    void
    shutdownReceive();

    virtual
    void
    shutdownSend();

    virtual
    void
    shutdown();

    virtual
    int
    sendBytes(
        const void* buffer,
        int length,
        int flags = 0
        );

    virtual
    int
    receiveBytes(
        void* buffer,
        int length,
        int flags = 0
        );

    virtual
    int
    sendTo(
        const void* buffer,
        int length,
        const SocketAddress& address,
        int flags = 0
        );

    virtual
    int
    receiveFrom(
        void* buffer,
        int length,
        SocketAddress& address,
        int flags = 0
        );

    virtual
    void
    sendUrgent(unsigned char data);

    virtual
    int
    available();

    virtual
    bool
    poll(
        const Poco::Timespan& timeout,
        int mode
        );

    virtual
    void
    setSendBufferSize(int size);

    virtual
    int
    getSendBufferSize();

    virtual
    void
    setReceiveBufferSize(int size);

    virtual
    int
    getReceiveBufferSize();

    virtual
    void
    setSendTimeout(const Poco::Timespan& timeout);

    virtual
    Poco::Timespan
    getSendTimeout();

    virtual
    void
    setReceiveTimeout(const Poco::Timespan& timeout);

    virtual
    Poco::Timespan
    getReceiveTimeout();

    virtual
    SocketAddress
    address();

    virtual
    SocketAddress
    peerAddress();

    void
    setOption(
        int level,
        int option,
        int value
        );

    void
    setOption(
        int level,
        int option,
        unsigned value
        );

    void
    setOption(
        int level,
        int option,
        unsigned char value
        );

    void
    setOption(
        int level,
        int option,
        const Poco::Timespan& value
        );

    void
    setOption(
        int level,
        int option,
        const IPAddress& value
        );

    virtual
    void
    setRawOption(
        int level,
        int option,
        const void* value,
        poco_socklen_t length
        );

    void
    getOption(
        int level,
        int option,
        int& value
        );

    void
    getOption(
        int level,
        int option,
        unsigned& value
        );

    void
    getOption(
        int level,
        int option,
        unsigned char& value
        );

    void
    getOption(
        int level,
        int option,
        Poco::Timespan& value
        );

    void
    getOption(
        int level,
        int option,
        IPAddress& value
        );

    virtual
    void
    getRawOption(
        int level,
        int option,
        void* value,
        poco_socklen_t& length
        );

    void
    setLinger(
        bool on,
        int seconds
        );

    void
    getLinger(
        bool& on,
        int& seconds
        );

    void
    setNoDelay(bool flag);

    bool
    getNoDelay();

    void
    setKeepAlive(bool flag);

    bool
    getKeepAlive();

    void
    setReuseAddress(bool flag);

    bool
    getReuseAddress();

    void
    setReusePort(bool flag);

    bool
    getReusePort();

    void
    setOOBInline(bool flag);

    bool
    getOOBInline();

    void
    setBroadcast(bool flag);

    bool
    getBroadcast();

    virtual
    void
    setBlocking(bool flag);

    virtual
    bool
    getBlocking() const;

    virtual
    bool
    secure() const;

    int
    socketError();

    poco_socket_t
    sockfd() const;

    void
    ioctl(
        poco_ioctl_request_t request,
        int& arg
        );

    void
    ioctl(
        poco_ioctl_request_t request,
        void* arg
        );

    bool
    initialized() const;

    virtual
    int
    sendBytes(
        const void* buffer,
        int length,
        int flags = 0
        );

protected:
    // methods

    virtual
    void
    init(int af);

    void
    initSocket(
        int af,
        int type,
        int proto = 0
        );

    void
    reset(poco_socket_t fd = POCO_INVALID_SOCKET);

    static
    int
    lastError();

    static
    void
    error();

    static
    void
    error(const std::string& arg);

    static
    void
    error(int code);

    static
    void
    error(
        int code,
        const std::string& arg
        );

Detailed Documentation

This class implements a WebSocket, according to the WebSocket protocol described in RFC 6455.

Construction

WebSocketImpl(
    StreamSocketImpl* pStreamSocketImpl,
    HTTPSession& session,
    bool mustMaskPayload
    )

Creates a WebSocketImpl.

Methods

virtual
int
sendBytes(
    const void* buffer,
    int length,
    int flags
    )

Sends a WebSocket protocol frame.

virtual
int
receiveBytes(
    void* buffer,
    int length,
    int flags
    )

Receives a WebSocket protocol frame.

virtual
SocketImpl*
acceptConnection(SocketAddress& clientAddr)

Get the next completed connection from the socket’s completed connection queue.

If the queue is empty, waits until a connection request completes.

Returns a new TCP socket for the connection with the client.

The client socket’s address is returned in clientAddr.

virtual
void
connect(const SocketAddress& address)

Initializes the socket and establishes a connection to the TCP server at the given address.

Can also be used for UDP sockets. In this case, no connection is established. Instead, incoming and outgoing packets are restricted to the specified address.

virtual
void
connect(
    const SocketAddress& address,
    const Poco::Timespan& timeout
    )

Initializes the socket, sets the socket timeout and establishes a connection to the TCP server at the given address.

virtual
void
connectNB(const SocketAddress& address)

Initializes the socket and establishes a connection to the TCP server at the given address.

Prior to opening the connection the socket is set to nonblocking mode.

virtual
void
bind(
    const SocketAddress& address,
    bool reuseAddress = false
    )

Bind a local address to the socket.

This is usually only done when establishing a server socket. TCP clients should not bind a socket to a specific address.

If reuseAddress is true, sets the SO_REUSEADDR socket option.

virtual
void
bind6(
    const SocketAddress& address,
    bool reuseAddress = false,
    bool ipV6Only = false
    )

Bind a local IPv6 address to the socket.

This is usually only done when establishing a server socket. TCP clients should not bind a socket to a specific address.

If reuseAddress is true, sets the SO_REUSEADDR socket option.

The given address must be an IPv6 address. The IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket according to the ipV6Only parameter.

If the library has not been built with IPv6 support, a Poco::NotImplementedException will be thrown.

virtual
void
listen(int backlog = 64)

Puts the socket into listening state.

The socket becomes a passive socket that can accept incoming connection requests.

The backlog argument specifies the maximum number of connections that can be queued for this socket.

virtual
void
close()

Close the socket.

virtual
void
shutdownReceive()

Shuts down the receiving part of the socket connection.

virtual
void
shutdownSend()

Shuts down the sending part of the socket connection.

virtual
void
shutdown()

Shuts down both the receiving and the sending part of the socket connection.

virtual
int
sendTo(
    const void* buffer,
    int length,
    const SocketAddress& address,
    int flags = 0
    )

Sends the contents of the given buffer through the socket to the given address.

Returns the number of bytes sent, which may be less than the number of bytes specified.

virtual
int
receiveFrom(
    void* buffer,
    int length,
    SocketAddress& address,
    int flags = 0
    )

Receives data from the socket and stores it in buffer.

Up to length bytes are received. Stores the address of the sender in address.

Returns the number of bytes received.

virtual
void
sendUrgent(unsigned char data)

Sends one byte of urgent data through the socket.

The data is sent with the MSG_OOB flag.

The preferred way for a socket to receive urgent data is by enabling the SO_OOBINLINE option.

virtual
int
available()

Returns the number of bytes available that can be read without causing the socket to block.

virtual
bool
secure() const

Returns true iff the socket’s connection is secure (using SSL or TLS).

virtual
void
setSendTimeout(const Poco::Timespan& timeout)

Sets the send timeout for the socket.

virtual
Poco::Timespan
getSendTimeout()

Returns the send timeout for the socket.

The returned timeout may be different than the timeout previously set with setSendTimeout(), as the system is free to adjust the value.

virtual
void
setReceiveTimeout(const Poco::Timespan& timeout)

Sets the send timeout for the socket.

On systems that do not support SO_RCVTIMEO, a workaround using poll() is provided.

virtual
Poco::Timespan
getReceiveTimeout()

Returns the receive timeout for the socket.

The returned timeout may be different than the timeout previously set with setReceiveTimeout(), as the system is free to adjust the value.

int
frameFlags() const

Returns the frame flags of the most recently received frame.

bool
mustMaskPayload() const

Returns true if the payload must be masked.