
/*---------------------------------------------------------------------------*/
/* webserver_websocket_handler.h                                             */
/* copyright (c) innovaphone 2016                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

class WebserverWebsocketHandler : public WebserverMessageHandler, public IWebsocket {
protected:
    UWebsocket * user;
    char * host;

    void DoClose();

    void AcceptComplete() override {} // Not used
    void SendResult()  override{}  // Not used
    void * RecvBuffer(size_t size) override {
        return user->WebsocketRecvBuffer(size);
    }
    void RecvResult(byte * buffer, size_t len) override {} // Not used
    void RecvCanceled(byte * buffer) override { user->WebsocketRecvCanceled(this, buffer); }
    void CloseComplete() override {} // Not used

public:
    WebserverWebsocketHandler(UWebserverMessageHandler * baseUser,
                              byte * extHeaderBuffer,
                              dword connectionID,
                              char * host,
                              char * path,
                              char * resReqPath,
                              class ExtendedHeader & extHeader,
                              IWebserverMessage * const webserverMessageSend,
                              class IInstanceLog * const log, 
                              word protocolVersion);
    virtual ~WebserverWebsocketHandler();

    void SetUser(UWebsocket * user)
    {
        this->userSet = true;
        this->user = user;
    }

	dword GetConnectionID() override { return WebserverMessageHandler::GetConnectionID(); }
    const char * GetHost() override { return host; }
    const char * GetPath() override { return WebserverMessageHandler::GetResourceName(); }
	const char * GetHeaderFieldValue(const char * fieldName) override { return WebserverMessageHandler::GetHeaderFieldValue(fieldName); }
    virtual const char * GetRemoteAddress() { return WebserverMessageHandler::GetRemoteAddress(); }
    virtual word GetRemotePort() { return WebserverMessageHandler::GetRemotePort(); }
    bool IsSysClientAuthenticated() override { return connFlags.sysClient; }
    bool IsLocalHost() override { return connFlags.localHost; }
    bool IsEncryptedConnection() override { return this->connFlags.connectionIsTLS; }

    void HandleMessage(class IWebserverMessage * const message, void * buffer, size_t len) override;
    void WebserverPluginWillClose(wsp_close_reason_t reason) override;

    void Send(const void * buffer, size_t len, bool text = true) override;
    void Recv(void * buf = NULL, size_t len = 0) override;
    void Close() override;
};
