void TCPConnection::CallBack_Read_Body(const boost::system::error_code &ec, size_t size) { if ( ! ec ) { Server *srv = Server::GetInstance(); char *buf = (char*)srv->malloc(); memcpy(buf,m_buf,size + sizeof(STR_PackHead)); //srv->RunTask(boost::bind(&CommandParse,&m_socket,buf)); CommandParse(shared_from_this(),buf); srv->free(buf); //至此,已处理完一个完整数据包 m_read_lock.unlock(); ReadHead(); } else if ( size == 0 || ec == boost::asio::error::eof || ec == boost::asio::error::shut_down) { Logger::GetLogger()->Debug("Client body Disconnected"); // int fd = m_socket.native_handle(); Server::GetInstance()->GetPlayerLogin()->SavePlayerOfflineData(shared_from_this() ); Server::GetInstance()->GetPlayerLogin()->DeleteNameSock(shared_from_this() ); SessionMgr::Instance()->RemoveSession(shared_from_this()); m_read_lock.unlock(); } }
/******************************************************************* * FUNCTION: CommandBegin * AUTHOR = TRAMPAS STERN * FILE = command.c * DATE = 1/25/2004 3:43:22 PM * * PARAMETERS: * * DESCRIPTION: Starts up the serial command line * * RETURNS: * * *******************************************************************/ UINT CommandBegin() { UBYTE data; CHAR buffer[MAX_STRING]; UBYTE buffIndex; //first clear the screen and push command prompt CLEAR_SCREEN(); printf(CMD_PROMPT); //now spin reading command and waiting on CR CommandExit=0; buffIndex=0; buffer[0]=0; while(!CommandExit) { if(kbhit()) { data=getch2(); //echo the data putch(data); //if the data is the CR we need to process buffer if (data==0x0D) { putch(0x0A); if (strlen(buffer)>0) CommandParse(buffer); printf("\n\r"); printf(CMD_PROMPT); buffIndex=0; buffer[buffIndex]=0; } if (data != 0x0A && data !=0x0D) { buffer[buffIndex++]=data; buffer[buffIndex]=0; } if (buffIndex>=(MAX_STRING-1)) { printf("\n\rERROR: Command buffer overflow\n\r"); buffIndex=0; buffer[0]=0; printf(CMD_PROMPT); } } } return 0; }
/* ********************************************************************************************************* * App_TaskGenieShell() * * Description : Realize a command shell interface on Debug_UART for real time debug purpose. * * Argument(s) : p_arg Argument passed to 'App_TaskGenieShell()' by 'OSTaskCreate()'. * * Return(s) : none. * * Note(s) : (1) The first line of code is used to prevent a compiler warning because 'p_arg' is not * used. The compiler should not generate any code for this statement. ********************************************************************************************************* */ void App_TaskGenieShell( void *p_arg ) { (void)p_arg; CPU_INT08U index ; /*index is the pointer of commandbuf */ CPU_INT08U num ; CPU_CHAR ch ; CPU_CHAR CommandBuf[ MaxLenComBuf + 1 ]; /*store '\0'*/ CPU_INT08U (*Func)(CPU_INT08U argc, CPU_CHAR **argv); CPU_CHAR *argv[10]; CPU_INT08U argc; CPU_INT08U error_code; index = 0 ; CommandBuf[0] = '\0'; InitCommands(); OSTimeDly(200); /*To be done: Login & Password*/ UART_SHELL_SEND_STR(( "\n\rLaunching Genieshell, press any to continue...")); UART_SHELL_GET_BYTE(()); UART_SHELL_SEND_STR(("\n\r>")); while (DEF_TRUE) { do { //only accept a-z,0-9,A-Z,.,space,/,- ch = UART_SHELL_GET_BYTE(()); } while( !( (ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')\ ||(ch=='.')||(ch==' ')||(ch=='-')||(ch=='/')\ ||(ch=='\r')||(ch=='\b')||(ch==',') ) ); switch(ch) { case '\r': //enter if ( index == 0 ){ //commandbuf is null,begin a new line UART_SHELL_SEND_STR(("\n\r>")); } else { if(CommandBuf[index-1]==' ') { index--; //get rid of the end space } CommandBuf[index] = '\0'; //UART_SHELL_SEND_STR("\n\rThe command is %s",CommandBuf); num = CommandParse( CommandBuf,&argc,argv ); //analys the argv in the commandbuf if( num == ERRORCOMMAND ){ //error or none exist command index = 0; CommandBuf[index] = '\0'; //UART_SHELL_SEND_STR("\n\rError command is %s",CommandBuf); UART_SHELL_SEND_STR(("Error: bad command or filename.")); UART_SHELL_SEND_STR(("\n\r>")); } else { Func = ShellComms[num].CommandFunc; //call corresponding CommandFunc error_code = Func(argc,argv) ; if( error_code == 1 ) { UART_SHELL_SEND_STR(("Error : number of parameters error...")); } else if(error_code == 2 ) { UART_SHELL_SEND_STR(("Error : parameters content error ...")); } else if(error_code == 3 ) { UART_SHELL_SEND_STR(("Error : Function execution error ...")); } else if(error_code > 3) { UART_SHELL_SEND_STR(("Error : Unknown error ...")); } index = 0; CommandBuf[index] = '\0'; LED_Toggle(LED_DS2); UART_SHELL_SEND_STR(("\n\r>")); } } break; case '\b': //backspace if ( index==0 ){ //has backed to first one //do nothing } else { index--; //pointer back once UART_SHELL_SEND_BYTE(('\b')); //cursor back once UART_SHELL_SEND_BYTE((' ')); //earse last char in screen UART_SHELL_SEND_BYTE(('\b')); //cursor back again } break; case ' ': //don't allow continuous or begin space(' ') if((CommandBuf[index-1] == ' ')||(index==0)||( index>MaxLenComBuf) ){ //do nothing } else { CommandBuf[index] = ch; index++; UART_SHELL_SEND_BYTE((ch)); //display and store ch } break; default: //normal key if ( index> MaxLenComBuf ){ //the buf reached MAX //do nothing } else { CommandBuf[index] = ch; index++; UART_SHELL_SEND_BYTE((ch)); //display and store ch } break; } //switch end OSTimeDly(10); } }
void* HttpRequest::RequestParse(void*arg) { int n; int again_flag = true; //again flag decide to close the fd or try again while(true) { errno = 0; //this is important memset(usr_buf,0,RIO_BUFSIZE); //reset the buf n = rio_readn(fd,(void*)usr_buf,RIO_BUFSIZE-1); if(n==-1) { debug("error."); again_flag = false; break; } else if(n==0) { if(errno==EAGAIN) { //debug("again read"); again_flag = true; } else //EOF { //debug("EOF read close fd"); again_flag = false; } //debug("there is not any more data,errno is: %d",errno); break; } read_data = usr_buf; /* cout<<"read data:--------------------------------------------"<<endl; cout<<read_data<<endl; cout<<"-------------------------------------------------------"<<endl; */ //debug_cpp(read_data.size()); CommandParse(); //requese line parse HeaderParse(); //head parse //int data_n = read_data.size(); if(read_data.size()!=0) { BodyParse(); //body parse } /*parse complete,do method,url ,version check*/ HttpError e; int check_res = e.RequireCheck(command,head,out,file_stat); if(check_res!=D_OK) { //debug("require error: %d",check_res); //then send the wrong information page command.uri = "/40x.html"; // reset the file_stat; string file_name = file_root+command.uri; stat(file_name.c_str(),&file_stat); out.SetStaticServer(true); } /**********check over,send the result to the client********/ if(out.GetStaticServer()) { //set the length out.SetFileSize(file_stat.st_size); auto out_line = out.SetOutLine_C(); out_line += out.SetOutHeaders_C(head); char* res_line_buf = const_cast<char*>(out_line.c_str()); n = rio_writen(fd,(void*)res_line_buf,strlen(res_line_buf)); //write command line //debug("send Response line and Out Header."); StaticServer(); //debug("----------static server over-----------"); } else //dynamic server { //debug("----------dynamic server on-------------"); DynamicServer(); //debug("----------dynamic server over-----------"); } //server -------**over**---------- if(head.head.find("Connection")==head.head.end()) //keep alive { again_flag = false; //debug("-----------no keep alive,will break---------"); break; } else { again_flag = true; } } if(!again_flag) { //debug("close fd ,%d",fd); close(fd); //close fd ,there is no other request } else { head.head.clear(); //clear the request head read_data=""; again_flag = true; out.clear(); //clear the output data InitHttpRequest(ep,fd,epfd); //no need to initialize.because the ep,fd,and the epfd etc do not change ep->SetEpollEvent((void*)this,D_EPOLLIN|D_EPOLLET|D_EPOLLONESHOT); //set event ep->EpollCTL(D_EPOLL_CTL_MOD,fd); //add fd to the event module } return NULL; }