Jun Furutani / libMiMic

Fork of libMiMic by Ryo Iizuka

mbed/mod/ModWebSocket.cpp

Committer:
nyatla
Date:
2013-09-27
Revision:
58:03b89038b21a
Child:
72:c118a7aa37a3

File content as of revision 58:03b89038b21a:

#include "ModWebSocket.h"
#include "../net/httpd/mod/NyLPC_cModWebSocket_protected.h"
#include "HttpdConnection.h"

namespace MiMic
{
    ModWebSocket::ModWebSocket(const char* i_path):ModBaseClass(i_path)
    {
        this->_mod=NULL;
    }
    ModWebSocket::ModWebSocket()
    {
        this->_mod=NULL;
    }
    ModWebSocket::~ModWebSocket()
    {
        if(this->_mod!=NULL){
            NyLPC_cModWebSocket_finalize(this->_mod);
            this->_mod=NULL;
        }
    }
    void ModWebSocket::setParam(const char* i_path)
    {
        ModBaseClass::setParam(i_path);
    }
    
    bool ModWebSocket::execute(HttpdConnection& i_connection)
    {
        if(this->_mod!=NULL){
            return false;
        }
        this->_mod=(NyLPC_TcModWebSocket_t*)malloc(sizeof(NyLPC_TcModWebSocket_t));
        if(this->_mod==NULL){
            return false;
        }
        //initialize websocket
        NyLPC_cModWebSocket_initialize(this->_mod,this->_path);        
        if(NyLPC_cModWebSocket_canHandle(this->_mod,i_connection._ref_inst)){
            if(NyLPC_cModWebSocket_execute(this->_mod,i_connection._ref_inst)){
                return true;
            }
        }
        NyLPC_cModWebSocket_finalize(this->_mod);
        free(this->_mod);
        this->_mod=NULL;
        return false;
    }
    bool ModWebSocket::write(const void* i_tx_buf,int i_tx_size)
    {
        if(this->_mod==NULL){
            return false;
        }
        return NyLPC_cModWebSocket_write(this->_mod,i_tx_buf,i_tx_size)?true:false;
    }

static NyLPC_TBool fmt_handler(void* i_inst,const void* i_buf,NyLPC_TUInt32 i_len)
{
    return NyLPC_iHttpPtrStream_write((NyLPC_TiHttpPtrStream_t*)i_inst,i_buf,i_len);
}   

    bool ModWebSocket::writeF(const char* i_fmt,...)
    {
        NyLPC_TInt16 l;
        va_list a;
        //ストリームの状態を更新する。
        NyLPC_cModWebSocket_update(this->_mod,0);
        
        //書式文字列の長さを計算
        va_start(a,i_fmt);
        l=NyLPC_cFormatWriter_length(i_fmt,a);
        va_end(a);
        if(!NyLPC_cModWebSocket_writePayloadHeader(this->_mod,l)){
            //CLOSE
            NyLPC_OnErrorGoto(Error);
        }
        va_start(a,i_fmt);
        if(!NyLPC_cFormatWriter_print(fmt_handler,NyLPC_cHttpdConnection_refStream(this->_mod->_ref_connection),i_fmt,a)){
            va_end(a);
            NyLPC_OnErrorGoto(Error);
        }
        va_end(a);
        NyLPC_iHttpPtrStream_flush(NyLPC_cHttpdConnection_refStream(this->_mod->_ref_connection));
            return true;
    Error:
        NyLPC_cHttpdConnection_closeSocket(this->_mod->_ref_connection);
        this->_mod->_payload_st=NyLPC_TcModWebSocket_ST_CLOSED;
            return false;
    }
    
    int ModWebSocket::read(void* i_rx_buf,int i_rx_size)
    {
        if(this->_mod==NULL){
            return false;
        }
        //write here!
        return NyLPC_cModWebSocket_read(this->_mod,i_rx_buf,i_rx_size);
    }
    bool ModWebSocket::canRead()
    {
        return NyLPC_cModWebSocket_canRead(this->_mod)?true:false;
    }

    void ModWebSocket::close()
    {
        if(this->_mod==NULL){
            return;
        }
        NyLPC_cModWebSocket_finalize(this->_mod);
        this->_mod=NULL;
        return;
    }
}