Example #1
0
MO_NAMESPACE_BEGIN

//============================================================
//<T>½âÎö×Ö·û´®ÄÚÈÝ¡£</T>
// 
// @param pValue ×Ö·û´®
// @return ÊÇ·ñ½âÎö³É¹¦
//============================================================
TBool SIntRange::Parse(TCharC* pValue){
   // ¼ì²é³¤¶È
   TStringRefer value(pValue);
   TInt length = value.Length();
   if(length == 0){
      MO_ERROR(TC("It is invalid length."));
      return EFalse;
   }
   // ·Ö¸îÄÚÈÝ
   TFsText temp;
   TInt begin = 0;
   TInt index = value.IndexOf(',', begin);
   if(ENotFound  == index){
      MO_ERROR(TC("Splite value failure."));
      return EFalse;
   }
   // ½âÎöÄÚÈÝX
   temp.Assign(value.SubStrC(begin, index));
   min = RInt::Parse(temp.MemoryC());
   // ½âÎöÄÚÈÝY
   temp.Assign(value.SubStrC(index + 1, length));
   max = RInt::Parse(temp.MemoryC());
   return ETrue;
}
unsigned char TcpClient_open_hal()
{

    //handle
    int i;
    for(i=0;i<5;i++)
    {
        if(TcpClient_handle_manager[i]==0)
        {
             //creat fifo
            int ret;
            ret=mo_drv_wifi_creat_tcpc_fifo(i);
            if(ret!=MO_SUCCESS)
            {
                MO_ERROR(("mo_drv_wifi_creat_tcpc_fifo failed"));
                return MO_FAILED;
            }

            //assign handle
            TcpClient_handle_manager[i]=1;
            return i;
        }
    }

    MO_ERROR(("TcpClient_open_hal failed handle use up"));
    return MO_FAILED;
}
//============================================================
//<T>解析字符串内容。</T>
// 
// @param pValue 字符串
// @return 是否解析成功
//============================================================
TBool S{type_name}Size3::Parse(TCharC* pValue){
   // 检查长度
   TStringRefer value(pValue);
   TInt length = value.Length();
   if(length == 0){
      MO_ERROR(TC("It is invalid length."));
      return EFalse;
   }
   // 分割内容
   TFsText temp;
   TInt begin = 0;
   TInt index = value.IndexOf(',', begin);
   if(ENotFound  == index){
      MO_ERROR(TC("Splite value failure."));
      return EFalse;
   }
   // 解析内容X
   temp.Assign(value.SubStrC(begin, index));
   width = R{type_name}::Parse(temp.MemoryC());
   // 分割内容
   begin = index + 1;
   index = value.IndexOf(',', begin);
   if(ENotFound == index){
      MO_ERROR(TC("Splite value failure."));
      return EFalse;
   }
   // 解析内容Y
   temp.Assign(value.SubStrC(begin, index));
   height = R{type_name}::Parse(temp.MemoryC());
   // 解析内容Z
   temp.Assign(value.SubStrC(index + 1, length));
   deep = R{type_name}::Parse(temp.MemoryC());
   return ETrue;
}
int TcpClient_connect_hal(const char *host, uint16_t port,unsigned char handle)
{
    if( (host==NULL) || (port == NULL) ||(handle>4) )
    {
        MO_ERROR(("bad pram"));
        goto error;
    }

    char temp_cmd[64];
    memset(temp_cmd,0x00,sizeof(temp_cmd));
    sprintf(temp_cmd,"AT+CIPSTART=%d,\"TCP\",\"%s\",%d",handle,host,port);
   // MO_DEBUG(("TCP connect:%s",temp_cmd));

    char temp[16];
    memset(temp,0x00,sizeof(temp));

    if(mo_drv_wifi_cmd_transfer(temp_cmd,strlen(temp_cmd),temp,sizeof(temp),20,0x03)>0)
    {
       // MO_DEBUG(("ret {%s} ",temp));
        if(memcmp(temp,"OK",2)==0)
        {
            MO_INFO(("OK TcpClient_connect_hal %d",handle));
            goto ok;
        }
        else if(memcmp(temp,"ALREAY CONNECT",14)==0)
        {
            MO_INFO(("OK TcpClient_connect_hal(already connected) %d",handle));
            goto ok;
        }
        else
        {
            MO_ERROR(("Failed TcpClient_connect_hal %d",handle));
            goto error;
        }
    }
    else
    {
        MO_ERROR(("Time out TcpClient_connect_hal %d",handle));
        goto error;
    }


   ok:
        return MO_SUCCESS;

    error:
        return MO_FAILED;


}
void TcpClient_stop_hal(unsigned char handle)
{
    //pram check
    if(handle>4)
    {
        MO_ERROR(("bad pram"));
        return;     //error
    }

    //send close cmd
    char temp_s[16];
    memset(temp_s,0x00,sizeof(temp_s));
    sprintf(temp_s,"AT+CIPCLOSE=%d",handle);

    char temp_r[4];
    memset(temp_r,0x00,sizeof(temp_r));

    if(mo_drv_wifi_cmd_transfer(temp_s,strlen(temp_s),temp_r,sizeof(temp_r),4,0x03)>0)
    {
        if(memcmp(temp_r,"OK",2)==0)
        {
           MO_INFO(("OK TcpClient_stop_hal %d",handle));
        }
        else
        {
           MO_ERROR(("Failed TcpClient_stop_hal %d",handle));
           // return;
        }
    }
    else
    {
        MO_ERROR(("Time out TcpClient_stop_hal %d",handle));
        //return;     //error
    }

    //destory fifo  recycle handle
    unsigned char  ret=TcpClient_close_hal(handle);
    if(ret!=MO_SUCCESS)
    {
        MO_ERROR(("Failed TcpClient_close_hal %d",handle));
        return;     //error
    }
    else
    {
        MO_INFO(("OK TcpClient_close_hal %d",handle));
    }

}
Example #6
0
//============================================================
// <T>×¢ÏúÒ»¸ö²¶×½Æ÷¡£</T>
//
// @param code ´úÂë
// @return ´¦Àí½á¹û
//============================================================
TBool FThreadCatcher::Jump(TInt code){
   // ÊÇ·ñ×¢²á¹ý¼¤»î²¶×½Æ÷
   if(NULL == _pActiveCatcher){
      MO_ERROR(TC("Active catcher is not exists."));
      MO_THROW(TC("Uncatcher exception."));
      return EFalse;
   }
   // ¼ì²éÉϲã
   _pJumpCatcher = _pActiveCatcher;
   while(NULL != _pJumpCatcher){
      if(_pJumpCatcher->IsRegistered(code)){
         break;
      }
      // µ¯³ö¶¥²ã¶ÔÏó
      FCatcher* pCatcher = _pCatchers->Pop();
      if(pCatcher == _pJumpCatcher){
         MO_FATAL(TC("Pop catcher and jump not same. (jump_captcher=%s:0x%08X, captcher=%s:0x%08X)"),
               _pJumpCatcher->Name(), _pJumpCatcher, pCatcher->Name(), pCatcher);
      }
      _pJumpCatcher = _pJumpCatcher->Parent();
   }
   // ¼ì²é²ÎÊý
   if(NULL == _pJumpCatcher){
      MO_FATAL(TC("Not any catcher catch this code. (code=%d)"), code);
   }
   _pActiveCatcher = _pJumpCatcher;
   // ¸ú×Ù²¶×½Æ÷
   Track(_pJumpCatcher, code);
   // ¿ªÊ¼Ìøת
   _lockerJump.Enter();
   _pJumpCatcher->Jump();
   return ETrue;
}
Example #7
0
//============================================================
// <T>写入一个压缩路由。</T>
//
// @param pMessage 消息
// @return 处理结果
//============================================================
TBool FNetBufferedPipe::WriteRouterCompress(TNetRouter* pRouter){
   MO_ASSERT(pRouter);
   // 序列化路由数据
   TInt length = 0;
   TByte buffer[MO_NETMESSAGE_MAXLENGTH];
   if(!pRouter->SerializeRouter(buffer, MO_NETMESSAGE_MAXLENGTH, &length)){
      MO_ERROR(TC("Serialize router failure."));
      return EFalse;
   }
   // 尝试写入数据
   TInt lengthWrite = 0;
   if(EStreamResult_Success != FBufferedPipe::Write(buffer, length, &lengthWrite)){
      MO_ERROR(TC("Write router failure. (length=%d)"), length);
      return EFalse;
   }
   return ETrue;
}
Example #8
0
//============================================================
// <T>写入一个压缩消息。</T>
//
// @param pMessage 消息
// @return 处理结果
//============================================================
TBool FNetBufferedPipe::WriteMessageCompress(TNetMessage* pMessage){
   MO_ASSERT(pMessage);
   // 序列化路由数据
   TInt length = 0;
   TByte buffer[MO_NETMESSAGE_MAXLENGTH];
   if(!pMessage->CompressMessage(buffer, MO_NETMESSAGE_MAXLENGTH, &length)){
      MO_ERROR(TC("Serialize message failure."));
      return EFalse;
   }
   // 尝试写入数据
   TInt lengthWrite = 0;
   if(EStreamResult_Success != FBufferedPipe::Write(buffer, length, &lengthWrite)){
      MO_ERROR(TC("Write message failure. (length=%d)"), length);
      return EFalse;
   }
   return ETrue;
}
unsigned char TcpClient_connected_hal(unsigned char handle)
{

  //pram check
  if(handle>4)
  {
    MO_ERROR(("bad pram"));
    return 0;
  }

  char temp[24];
  #define CMD "AT+CIPSTATUS"
  memset(temp,0x00,sizeof(temp));
  if(mo_drv_wifi_cmd_transfer(CMD,strlen(CMD),temp,sizeof(temp),10,0x03)<0)
  {
    MO_ERROR(("Failed TcpClient_connected_hal"));
    return 0;
  }
  else
  {
    //debug
    //MO_DEBUG(("mo_drv_wifi_cmd_transfer:{%s}",temp));

    //right data
    char temp_right[4];
    memset(temp_right,0x00,sizeof(temp_right));
    sprintf(temp_right,"%d1",handle);
    //MO_DEBUG(("temp_right:%s",temp_right));

    //cmp result
    if( strstr(temp,temp_right)!=NULL )
    {
      MO_INFO(("OK TcpClient_connected_hal %d",handle));
      return 1;
    }
    else
    {
      MO_ERROR(("NOT TcpClient_connected_hal %d",handle));
      return 0;
    }

  }

}
int TcpClient_available_hal(unsigned char handle)
{
    //check pram
    if(handle>4)
    {
         MO_ERROR(("bad pram"));
         return 0;
    }

    return mo_drv_wifi_available_tcpc_fifo(handle);
}
int TcpClient_readn_hal(unsigned char *buff, int size,unsigned char tcp_handle)
{
    //check pram
    if( (buff==NULL) || (size==0) ||(tcp_handle>4) )
    {
        MO_ERROR(("bad pram"));
        return 0;
    }

    return mo_drv_wifi_read_tcpc_fifo((char *)buff,size,tcp_handle);
}
Example #12
0
//============================================================
// <T>Ìøת½áÊø´¦Àí¡£</T>
//
// @return ´¦Àí½á¹û
//============================================================
TBool FThreadCatcher::JumpFinish(FCatcher* pCatcher){
   MO_ASSERT(pCatcher);
   // ¼ì²éÊÇ·ñ´æÔÚ¼¤»î
   if(NULL == _pJumpCatcher){
      MO_ERROR(TC("Catcher long jump failure, lost jump captcher. (jump_catcher=0x%08X, catcher=0x%08X)"), _pJumpCatcher, pCatcher);
      return EFalse;
   }
   // ¼ì²éÌøתÆ÷
   if(_pJumpCatcher != pCatcher){
      MO_ERROR(TC("Catcher long jump failure, jump and active not same. (jump_catcher=%s@0x%08X, catcher=%s@0x%08X)"),
            _pJumpCatcher->Name(), _pJumpCatcher, pCatcher->Name(), pCatcher);
   }
   // ÉèÖñêÖ¾
   MO_WARN(TC("Trap catcher long jump success. (catcher=%s@0x%08X, jumping=%d, valid=%d)"),
         _pJumpCatcher->Name(), _pJumpCatcher, _pJumpCatcher->IsJumping(), _pJumpCatcher->IsValid());
   MO_CLEAR(_pJumpCatcher);
   // ÌøתÍê³É
   _lockerJump.Leave();
   return ETrue;
}
Example #13
0
//============================================================
// <T>检查执行错误。</T>
//
// @param pCode 代码
// @return 处理结果
//============================================================
TResult FPoRenderDevice::CheckError(TCharC* pCode, TCharC* pMessage, ...){
   // 获得错误原因
   TBool result = EFalse;
   GLenum errorCode = 0;
   TCharC* pErrorReason = NULL;
   while(ETrue){
      // 获得错误
      GLenum code = glGetError();
      if(code == GL_NO_ERROR){
         break;
      }
      // 获得原因
      switch(code){
         case GL_INVALID_OPERATION:
            pErrorReason = "Invalid operation.";
            break;
         case GL_INVALID_ENUM:
            pErrorReason = "Invalid enum.";
            break;
         case GL_INVALID_VALUE:
            pErrorReason = "Invalid value.";
            break;
         case GL_INVALID_FRAMEBUFFER_OPERATION:
            pErrorReason = "Invalid paramebuffer opeartion."; 
            break;
         case GL_OUT_OF_MEMORY:
            pErrorReason = "Out of memory.";
            break;
         default:
            pErrorReason = "Unknown"; 
            break;
      }
      result = ETrue;
      errorCode = code;
#ifdef _MO_WINDOWS
      break;
#endif // _MO_WINDOWS
   }
   //............................................................
   // 输出错误信息
   if(result){
      // 格式化可变参数字符串信息
      TFsText message;
      va_list params;
      va_start(params, pMessage);
      message.AppendFormatParameters(pMessage, params);
      va_end(params);
      // 输出错误信息
      MO_ERROR("%s (code=%s, error=0x%04X(%d), reason=%s)", (TCharC*)message, pCode, errorCode, errorCode, pErrorReason);
   }
   return result;
}
unsigned char TcpClient_close_hal(unsigned char handle)
{
    //check pram
    if(handle>4)
    {
        MO_ERROR(("bad pram"));
        return MO_FAILED;
    }

    //destory fifo
    if(mo_drv_wifi_destroy_tcpc_fifo(handle)!=MO_SUCCESS)
    {
        MO_ERROR(("mo_drv_wifi_destroy_tcpc_fifo failed"));
        return MO_FAILED;
    }

    //recycle handle
    TcpClient_handle_manager[handle]=0;

    return MO_SUCCESS;

}
Example #15
0
//============================================================
TBool FNetMessageMachine::ProcessMessage(){
   TNetMessageBuffer message;
   if(_pConnection->PopupMessage(&message)){
      // 处理消息
      TInt code = message.MessageHead().Code();
      FNetMessageHandles* pHandles = _pHanldess->Get(code);
      if(NULL != pHandles){
         // 有注册的消息处理器情况
         pHandles->Process(_pConnection, &message);
      }else{
         // 未处理消息
         TChar dump[MO_FS_DUMP_LENGTH];
         MO_ERROR("Unknown message. (%s)", message.Dump(dump, MO_FS_DUMP_LENGTH));
      }
      return ETrue;
   }
   return ETrue;
}
Example #16
0
//============================================================
// <T>执行消息处理。</T>
//============================================================
TBool FNetRouterHandles::Process(FObject* pSender, TNetRouter* pRouter){
   TBool result = ETrue;
   if(!_pProcessors->IsEmpty()){
      TListIteratorC<FNetRouterHandle*> iterator = _pProcessors->IteratorC();
      while(iterator.Next()){
         FNetRouterHandle* pHandle = *iterator;
         result = pHandle->Process(pSender, pRouter);
         if(result){
            MO_DEBUG(TC("Process router hanle success. (message=%s, invoker=%s)"),
                  pRouter->MessageInfo()->Name(), pHandle->InvokerName());
         }else{
            MO_ERROR(TC("Process router hanle failure. (message=%s, invoker=%s)"),
                  pRouter->MessageInfo()->Name(), pHandle->InvokerName());
         }
      }
   }
   return ETrue;
}
//============================================================
// <T>逻辑处理。</T>
//
// @return 处理结果
//============================================================
TBool FApplicationListener::Process(){
   switch(_listenerCd){
      case EApplicationListener_Interrrupt:
         _pListeners->Interrupt();
         break;
      case EApplicationListener_Reload:
         _pListeners->Reload();
         break;
      case EApplicationListener_Unload:
         _pListeners->Unload();
         break;
      case EApplicationListener_Shutdown:
         _pListeners->Shutdown();
         break;
      default:
         MO_ERROR(TC("Unknown listener code. (listener_cd=%d)"), _listenerCd);
         break;
   }
   return ETrue;
}
Example #18
0
//============================================================
// <T>反序列化数据区到路由数据。</T>
//
// @param pMemory 数据指针
// @param size 数据大小
// @param length 处理大小
//============================================================
TBool TNetRouter::UnserializeRouter(TAnyC* pMemory, TInt size, TInt* length){
   MO_ASSERT(pMemory);
   TByteC* pPtr = (TByteC*)pMemory;
   // 反序列化头数据
   TInt offset = 0;
   offset += _netHead.Unserialize(pPtr);
   TUint protocol = _netHead.Protocol();
   TNetSerial messageSerial = 0;
   TNetTick messageTick = 0;
   if(ENetProtocol_Message == (ENetProtocol_Message & protocol)){
      offset += _messageHead.Unserialize(pPtr + offset);
      messageSerial = _messageHead.Serial();
      messageTick = _messageHead.Tick();
   }
   if(ENetProtocol_Router == (ENetProtocol_Router & protocol)){
      offset += _routerHead.Unserialize(pPtr + offset);
   }
   TNetLength capacity = _netHead.Length();
   // 复制数据
   TByteC* pData = pPtr + offset;
   TInt dataLength = capacity - offset;
   // 验证哈希
   TNetHash hash = CalculateHash(messageSerial, messageTick, pData, dataLength);
   if(_netHead.Hash() != hash){
      MO_ERROR(TC("Unserialize router invalid hash. (head_hash=0x%08X, data_hash=0x%08X)"),
            _netHead.Hash(), hash);
      return EFalse;
   }
   // 设置返回
   if(dataLength > 0){
      memcpy(_buffer, pData, dataLength);
   }
   _dataLength = dataLength;
   *length = capacity;
   return ETrue;
}
int mo_TcpClient_peek_hal(unsigned char *p_dat)
{
    MO_ERROR(("not support"));
    return 0;
}
Example #20
0
static void runPingTest(TestRunner& tr)
{
   tr.test("Ping");

   Config cfg = tr.getApp()->getConfig()["pong"];

   // Stats and control
   PingPong pingPong(cfg);

   // create kernel
   Kernel k;

   // set thread stack size in engine (128k)
   k.getEngine()->getThreadPool()->setThreadStackSize(
      cfg["threadStackSize"]->getUInt32());

   // optional for testing --
   // limit threads to 2: one for accepting, 1 for handling
   //k.getEngine()->getThreadPool()->setPoolSize(2);
   k.getEngine()->getThreadPool()->setPoolSize(cfg["threads"]->getUInt32());

   // start engine
   k.getEngine()->start();

   // create server
   Server server;
   server.setMaxConnectionCount(cfg["maxConnections"]->getInt32());
   InternetAddress address("0.0.0.0", cfg["port"]->getUInt32());

   // create SSL/generic http connection servicer
   HttpConnectionServicer hcs;
//   SslContext context;
//   SslSocketDataPresenter presenter1(&context);
//   NullSocketDataPresenter presenter2;
//   SocketDataPresenterList list(false);
//   list.add(&presenter1);
//   list.add(&presenter2);
   //server.addConnectionService(&address, &hcs);//, &list);
   server.addConnectionService(
      &address, &hcs, NULL, "pong",
      cfg["maxConnections"]->getInt32(),
      cfg["backlog"]->getInt32());

   // create test http request servicer
   NoContentServicer noContentSrv(&pingPong, "/");
   hcs.addRequestServicer(&noContentSrv, false);

   PingServicer ping(&pingPong, "/pong");
   hcs.addRequestServicer(&ping, false);

   const int bufsize = 4096;
   char buf[bufsize];
   memset(buf, '.', bufsize);
   DataServicer data(&pingPong, "/data", buf, bufsize);
   hcs.addRequestServicer(&data, false);

   StatsServicer stats(&pingPong, "/stats");
   hcs.addRequestServicer(&stats, false);

   ResetServicer reset(&pingPong, "/reset");
   hcs.addRequestServicer(&reset, false);

   QuitServicer quit(&pingPong, "/quit");
   hcs.addRequestServicer(&quit, false);

   if(server.start(&k))
   {
      uint64_t num = cfg["num"]->getUInt64();
      MO_INFO("Server started.");
      if(num == 0)
      {
         MO_INFO("Servicing forever. CTRL-C to quit.");
      }
      {
         MO_INFO("Servicing approximately %" PRIu64 " connections.", num);
      }
   }
   else if(Exception::get() != NULL)
   {
      MO_ERROR("Server started with errors=%s\n",
         Exception::get()->getMessage());
   }

   // start timing
   pingPong.reset();

   // either serve for limited time, or wait for lock
   uint32_t time = cfg["time"]->getUInt32();
   if(time != 0)
   {
      Thread::sleep(time);
   }
   else
   {
      pingPong.getLock().wait();
   }

   server.stop();
   MO_INFO("Server stopped.");

   // stop kernel engine
   k.getEngine()->stop();

   tr.passIfNoException();
}
int8_t mo_TcpServer_accept_hal(void)
{
    MO_ERROR(("not support"));
    return 0;
}
void mo_TcpServer_write_hal(uint8_t c)
{
    MO_ERROR(("not support"));
  //  return 0;
}
int TcpClient_writen_hal(const unsigned char *p_dat,int len,unsigned char tcp_handle)
{
	/*
	MO_ERROR(("tcp_write"));
	MO_PRINTN(((const char *)p_dat,len));
	*/


    //check pram
    if( (p_dat==NULL) ||(len==0)||(tcp_handle>4) )
    {
        MO_ERROR(("bad pram"));
        return 0;
    }

    //send cmd
    char temp_w[24];
    char temp_r[32];
    memset(temp_w,0x00,sizeof(temp_w));
    sprintf(temp_w,"AT+CIPSEND=%d,%d",tcp_handle,len);

    if(mo_drv_wifi_cmd_transfer((const char *)temp_w,strlen(temp_w),temp_r,sizeof(temp_r),10,0x01)>0)
    {
        if(memcmp(temp_r,">",1)==0)
        {
            //   MO_INFO(("GET >"));
            MO_INFO(("0.5> TcpClient_writen_hal %d",tcp_handle));
        }
        else
        {
            MO_ERROR(("0.5Failed TcpClient_writen_hal %d",tcp_handle));
        }
    }
    else
    {
        MO_ERROR(("0.5Time out TcpClient_writen_hal %d",tcp_handle));
    }


    //send data
    int ret;
    if( (ret=mo_drv_wifi_cmd_transfer((const char *)p_dat,len,temp_r,sizeof(temp_r),10,0x02)) >0)
    {
        if(memcmp(temp_r,"SEND OK",7)==0)
        {
            MO_INFO(("OK TcpClient_writen_hal %d",tcp_handle));
            //usleep(5000*1000);
            return ret;
        }
        else
        {
            MO_ERROR(("Failed TcpClient_writen_hal %d",tcp_handle));
            return 0;
        }
    }
    else
    {
        MO_ERROR(("Time out TcpClient_writen_hal %d",tcp_handle));
        return 0;
    }

}
uint8_t mo_TcpServer_begin_hal(uint16_t port,uint8_t useLocalhost)
{
    MO_ERROR(("not support"));
    return 0;
}