www.pudn.com > Net_apps.rar > HttpServer_0_0_1_.c
//*************************************************************
// Copyright 2003 Service & Quality Technology CO., LTD.
// ALL RIGHTS RESERVED.
// This software is provided under license and contains proprietary
// and confidential material which is the property of SQ tech.
//
// FileName : HttpServerTestSendPerformance.c
// Description : test send function
// Reversion : 0.1 ,Date : 2004/03/18 ,Author : gofly
// Comment : first implementation
//
//*************************************************************
#include "opt.h"
#if HTTP_OPEN
#include "..\include\L3\net_apps\httpd\HttpServer.h"
#include "..\include\L2\system\wdt.h"
#if SEARCH_PLC
#include "..\include\L2\system\plc.h"
#endif
//INT8U huge** GSendBuf = SEND_BUFFER_ADDRS; //add by jgy 07/11/05
INT8U huge GSendBuf[HTTP_MAX_SOCKET][HTTP_MAX_SEND_BUFF]; // Dynamic html Max size
INT8U huge GRecvBuf[HTTP_MAX_SOCKET][HTTP_MAX_RECV_BUFF]; // http request Max size
extern INT8U PPPoE_Status;
#define FPS_IDLE 2 //add by jgy 8/24/05 when no motion detected, send image per half second
THttpCtrlBlack GtHCB;
#if SEARCH_PLC
static INT16U idleCnt; //add by jgy 11/14/05 when idleCnt reach HTTP_IDLE_TIMEOUT then start searching modem.
#endif
//***********************************************
// HttpThread()
// Description :
// Parameters :
// Returns :
//***********************************************
void HttpThread(void *data)
{
THttpCtrlBlack *ptHCB = &GtHCB;
INT8U *ServerSocket = data;
*ServerSocket = HttpInit((void*)ptHCB);
ASSERT_EX1(HTTP_DEBUG, "HTTP INIT ERR ", *ServerSocket != INVALID_SOCKET);
SHOW_EX1(HTTP_DEBUG, "SERVICE HTTPD START \n");
while(1)
{
//===========================================================================//
// SELECT which socket can recv & send
//===========================================================================//
HttpSelect((void*)ptHCB);
} /*while(1)*/
}
//***********************************************
// HttpAccept()
// Description :
// Parameters :
// Returns :
//***********************************************
void HttpAccept(void *data)
{
INT8U maxfdp1;
INT8U i;
INT8U AcceptTempSocket;
struct sockaddr cliaddr;
fd_set readset;
INT32U NowTime;
INT32U PidCount = 0;
THttpCtrlBlack *ptHCB = &GtHCB ;
INT8U listenfd = *(INT8U*)data;
socklen_t addrlen = sizeof(cliaddr); // Add &addrelen by fly 050603 for can not open IE
maxfdp1 = listenfd + 1;
while(1)
{
FD_ZERO(&readset);
FD_SET(listenfd, &readset);
//OSTimeDlyHMSM(0, 0, 0, 20);
if( 0 == select(maxfdp1, &readset, 0, 0, 0))
{
continue;
}
NowTime = OSTimeGet();
if (FD_ISSET(listenfd, &readset))
{
//AcceptTempSocket = accept(listenfd, &cliaddr,0);
AcceptTempSocket = accept(listenfd, &cliaddr,&addrlen); // Add &addrelen by fly 050603 for can not open IE
}
else
{
continue;
}
//==========================================================//
// new sock join action
//==========================================================//
if(AcceptTempSocket == INVALID_SOCKET)
continue;
for(i = 0; i < HTTP_MAX_SOCKET ; i++)
{
if(ptHCB->Sock[i] == INVALID_SOCKET)
break;
}
if( i == HTTP_MAX_SOCKET)
{
send(AcceptTempSocket
,"system is busy"
,14
,0);
close(AcceptTempSocket);
continue;
}
ptHCB->Sock[i] = AcceptTempSocket;
ptHCB->PID[i] = PidCount++;
if(PidCount > 10000)
PidCount = 0;
ptHCB->AccessTime[i] = NowTime;
ptHCB->Stat[i] = HTTP_NEW;
#if SEARCH_PLC
idleCnt = 0;
#endif
}
}
//***********************************************
// HttpSelect()
// Description :
// Parameters :
// Returns :
//***********************************************
INT8U HttpSelect( void *data )
{
INT8U maxfdp1;
INT8U i,j;
fd_set readset;
fd_set writeset;
struct sockaddr cliaddr;
INT8U clilen;
struct timeval timeout = {1, 0};
THttpCtrlBlack *ptHCB = data ;
static INT32U PidCount = 0;
INT32U NowTime;
INT8U ActionSocketCount = 0;
maxfdp1 = 0;
FD_ZERO(&readset);
FD_ZERO(&writeset);
//==========================================================//
// check action sock
//==========================================================//
for(i = 0; i < HTTP_MAX_SOCKET; i++)
{
if(ptHCB->Sock[i] != INVALID_SOCKET)
{
if (maxfdp1 < ptHCB->Sock[i] + 1)
maxfdp1 = ptHCB->Sock[i] + 1;
if(ptHCB->Stat[i] == HTTP_NEW || ptHCB->Stat[i] == HTTP_RECV )
{
ptHCB->Stat[i] = HTTP_RECV;
FD_SET(ptHCB->Sock[i], &readset);
}
if(ptHCB->Stat[i] == HTTP_CONT) //check hold now continue next time
{
int j=0,k;
//INT8U videomax;
for(k=0;kOrder[i]+7, StImageBuf+42,&ptHCB->TotalLen[i]);
ptHCB->Stat[i] = HTTP_SEND;
ptHCB->TotalLen[i] += 42; //headsize
FD_SET(ptHCB->Sock[i], &writeset);
}
else
{
continue;
}
}
if(ptHCB->Stat[i] == HTTP_SEND)
{
FD_SET(ptHCB->Sock[i], &writeset);
}
ActionSocketCount++;
}
else
{
imgmax[i]=0;
}
}//for(i = 0; i < HTTP_MAX_SOCKET; i++)
//==========================================================//
// no action sock return
//==========================================================//
#if Back_GetImg
/*
j = 0;
for(i=0;i HTTP_IDLE_TIMEOUT)
{//启动搜猫操作
idleCnt = 0;
OSMboxPost(searchPLCMbox, (void*)1);
}
#endif
OSTimeDlyHMSM(0, 0, 0, 20);
return;
}
//==========================================================//
//
//==========================================================//
if( 0 == select(maxfdp1, &readset, &writeset, 0, &timeout))
{
// return;
}
NowTime = OSTimeGet();
//==========================================================//
// recv even
//==========================================================//
for(i = 0; i < HTTP_MAX_SOCKET; i++)
{
if(ptHCB->Sock[i] == INVALID_SOCKET ||ptHCB->Stat[i] == HTTP_NEW )
continue;
if(FD_ISSET(ptHCB->Sock[i], &readset))
{
ptHCB->AccessTime[i] = NowTime;
if(HttpRecv(data, i) == HTTP_R_ERR)
HttpCloseConnect((void*)data, i);
}
else if(FD_ISSET(ptHCB->Sock[i], &writeset))
{
ptHCB->AccessTime[i] = NowTime;
if(HttpSend(data, i) == HTTP_R_ERR)
HttpCloseConnect((void*)data, i);
}
else // check timeout
{
if( NowTime - ptHCB->AccessTime[i] > HTTP_SOCK_IDLE_TIMEOUT)
{
if (ptHCB->FileType[i] == FILE_TYPE_STREAM)
{
if(jpgsmp[i])
{
if(video_server_ref > 0) video_server_ref--;
//jpgsmp[i] = 0;
}
imgmax[i]=0;
}
HttpCloseConnect((void*)data, i);
}
}
} /*for(i = 0; i < HTTP_MAX_SOCKET; i++)*/
}
//***********************************************
// HttpInit()
// Description : init THttpCtrlBlack
// Creat Server Socket
// Parameters :
// Returns : sucess 1 - HTTP_MAX_SOCKET
// fail INVALID_SOCKET
//***********************************************
INT8S HttpInit(void *data)
{
INT8U i;
struct sockaddr_in Seversa;
INT8U sock;
THttpCtrlBlack *ptHCB = data;
INT16U port;
//==========================================================
// init THttpCtrlBlack
//==========================================================
for(i = 0; i < HTTP_MAX_SOCKET; i++)
{
HttpClearHCBByIndex(data, i);
}
//==========================================================
// Open Socket (server)
//==========================================================
if( INVALID_SOCKET == ( sock = socket(0, SOCK_STREAM, 0) ) )
{
return INVALID_SOCKET;
}
//===========================================================
// BIND Socket (server)
//===========================================================
port = atoi(IoHttpPort(NULL,NULL, IOR, 0));
Seversa.sin_family = AF_INET;
Seversa.sin_addr.s_addr = htonl(INADDR_ANY);
Seversa.sin_port = htons( port) ;
if(bind(sock,&Seversa,0) == INVALID_SOCKET)
{
close(sock);
return INVALID_SOCKET;
}
if(listen(sock,HTTP_MAX_SOCKET) == INVALID_SOCKET)
{
close(sock);
return INVALID_SOCKET;
}
return sock;
}
//***********************************************
// HttpClearHCBByIndex()
// Description : init individual HCB
// Parameters :
// Returns : sucess 0
// fail INVALID_SOCKET
//***********************************************
INT8S HttpClearHCBByIndex(void *data, INT8U index)
{
THttpCtrlBlack *ptHCB = (INT8U *)data ;
ptHCB->Stat[index] = HTTP_STOP;
ptHCB->Sock[index] = INVALID_SOCKET;
ptHCB->SockSendBuf[index] = GSendBuf[index];
ptHCB->SockRecvBuf[index] = GRecvBuf[index];
ptHCB->Order[index] = NULL;
ptHCB->FileType[index] = FILE_TYPE_NULL;
ptHCB->TotalLen[index] = 0;
ptHCB->SendLen[index] = 0;
ptHCB->RecvLen[index] = 0;
ptHCB->UID[index] = OTHER;
ptHCB->File[index] = NULL;
ptHCB->ExHttpHead[index][0] = 0x00;
ptHCB->PID[index] = 0xff;
ptHCB->AccessTime[index] = 0;
ptHCB->send_jpg[index].jpg_file = NULL;
ptHCB->ref[index] = 0;
return -1;
}
//***********************************************
// HttpRecv(void *data, INT8U index)
// Description : recv request
// Parameters :
// Returns :
//***********************************************
INT8S HttpRecv(void *data, INT8U index)
{
THttpCtrlBlack *ptHCB = (INT8U *)data ;
INT32S len;
len = recv(ptHCB->Sock[index]
,ptHCB->SockRecvBuf[index] + ptHCB->RecvLen[index]
,sizeof(GRecvBuf)/HTTP_MAX_SOCKET - ptHCB->RecvLen[index]
,0);
//---- gofly debug for test open & close socket--//
#if 0
{
INT8U Msg[256];
sprintf((char*)Msg, "HTTP requested %s was not found UID %d PID %d\n"
,"test", ptHCB->UID[index], ptHCB->PID[index]);
HttpAddHead(ptHCB->SockSendBuf[index], ptHCB->ExHttpHead[index], Msg);
ptHCB->File[index] = ptHCB->SockSendBuf[index];
ptHCB->TotalLen[index] = strlen((const char*)ptHCB->File[index]);
ptHCB->Stat[index] = HTTP_SEND;
return HTTP_R_OK;
}
#endif
//-----------------------------------------//
//----------------------------------------------------------//
// recv general len > 0
//----------------------------------------------------------//
if(len > 0)
{
ptHCB->RecvLen[index] += len;
//--------check recv data--------------//
if(HttpRecvComplete(ptHCB, index) == HTTP_R_OK)
{
// recv over||recv midway
return HTTP_R_OK;
}
else
{
return HTTP_R_ERR;
}
}
else
{
return HTTP_R_ERR;
}
}
//***********************************************
// HttpSend(void *data, INT8U index)
// Description : send file
// Parameters :
// Returns :
//***********************************************
#define packsize 1300
INT8S HttpSend(void *data, INT8U index)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
THttpCtrlBlack *ptHCB = (INT8U *)data ;
INT32S SendLen;
INT32S len;
SendLen = ptHCB->TotalLen[index] - ptHCB->SendLen[index];
if(PPPoE_Status == OK)
{
//if(ptHCB->FileType[index] == FILE_TYPE_STREAM && HttpStreamCount(data) >= 3)
//{
SendLen = min(SendLen, packsize);
//}
// if(SendLen >packsize)
// {
//
// SendLen = (SendLen/packsize)*packsize;
// //SendLen = packsize;
// SendLen = min(SendLen , TCP_SND_BUF-socket_check_sendbuf(ptHCB->Sock[index])) ;
// SendLen = 100;
// }
}
else
{
if(ptHCB->FileType[index] == FILE_TYPE_STREAM && HttpStreamCount(data) >= 3)
{
SendLen = min(SendLen, 1460);
}
if(SendLen >1460)
{
SendLen = min(SendLen , TCP_SND_BUF-socket_check_sendbuf(ptHCB->Sock[index])) ;
SendLen = (SendLen/1460)*1460;
// SendLen = 100;
}
}
//if(ptHCB->FileType[index] == FILE_TYPE_IMAGE && index >= 7)
//{
// printf("%d",SendLen);
//}
/*
SendLen = min(ptHCB->TotalLen[index] - ptHCB->SendLen[index] ,TCP_SND_BUF);
// socket_check_sendbuf(ptHCB->Sock[i])) ;
//if(SendLen > 100) SendLen=100;
if(SendLen > 1460)
{
SendLen = (SendLen/1460)*1460;
}
*/
len = send(ptHCB->Sock[index]
,ptHCB->File[index] + ptHCB->SendLen[index]
,SendLen
,0);
ptHCB->SendLen[index] += len;
//if (ptHCB->FileType[index] == FILE_TYPE_STREAM){printf("%d:%d\n\r",index,len);}
if(len < 0)
return HTTP_R_ERR;
if( ptHCB->TotalLen[index] == ptHCB->SendLen[index])
{
//----------------------------------------------------------//
// send success
//----------------------------------------------------------//
//-------- FILE_TYPE_CAB ---------//
//Mark By Changes 20041001l,
//// if(ptHCB->FileType[index] == FILE_TYPE_CAB)
//// {
//// if(ptHCB->File[index] == ViewCab)
//// {
////
//// ptHCB->File[index] = ViewCab_1;
//// ptHCB->TotalLen[index] = sizeof(ViewCab_1);
//// ptHCB->SendLen[index] = 0;
////
//// }
//// /*
//// else if(ptHCB->File[index] == FwudCab)
//// {
//// ptHCB->File[index] = FwudCab_1;
//// ptHCB->TotalLen[index] = sizeof(FwudCab_1);
//// ptHCB->SendLen[index] = 0;
////
//// }
//// */
//// else
//// {
////
//// return HTTP_R_ERR;
//// }//if(ptHCB->File[i] == ViewCab)
////
//// }
/*
//-------- FILE_TYPE_FWUD ---------//
else if(ptHCB->FileType[index] == FILE_TYPE_FWUD&& !firware_packet_success(0, IOR))
{
ptHCB->Stat[index] = HTTP_RECV;
ptHCB->Order[index] = NULL;
// ptHCB->FileType[index] = FILE_TYPE_NULL;
ptHCB->TotalLen[index] = 0;
ptHCB->SendLen[index] = 0;
ptHCB->RecvLen[index] = 0;
ptHCB->File[index] = NULL;
ptHCB->ExHttpHead[index][0] = 0x00;
}
*/
//-------- FILE_TYPE_MT ---------//
/*else*/ if(ptHCB->FileType[index] == FILE_TYPE_MT)
{
ptHCB->Stat[index] = HTTP_RECV;
ptHCB->Order[index] = NULL;
ptHCB->TotalLen[index] = 0;
ptHCB->SendLen[index] = 0;
ptHCB->RecvLen[index] = 0;
ptHCB->File[index] = NULL;
ptHCB->ExHttpHead[index][0] = 0x00;
}
//-------- FILE_TYPE_INFINITE ---------//
else if(ptHCB->FileType[index] == FILE_TYPE_INFINITE)
{
ptHCB->SendLen[index] = 0;
}
//-------- FILE_TYPE_STREAM ---------//
else if(ptHCB->FileType[index] == FILE_TYPE_STREAM)
{
if(imgmax[index] == 1)
{
//if(ptHCB->File[index] != NULL)
if(ptHCB->ref[index] != 0)
{
#if VIDEO_FREE
//video_image_free(&ptHCB->send_jpg[index]);
video_bkimage_free(data, index); //add by jgy 9/3/05
#endif
}
//--- get image
HttpGetImage_20040712(data, index) ;
//--- free image
}
else if(imgmax[index] == 2)
{
HttpGetHistImage(data, index); //add by jgy 10/11/05
}
else
{
return HTTP_R_ERR;
}
}
/*
else if(!strncmp((const char*)ptHCB->SockRecvBuf[index],"POST /Reboot.html",17))
{
// printf("HELLO\n");
update_start();
reset(FIRMWARE);
return HTTP_R_ERR;
}
*/
else
{
return HTTP_R_ERR;
}// if(ptHCB->FileType[i] == CAB)
}/*if(len < 0 || ptHCB->TotalLen[i] == ptHCB->SendLen[i])*/
return HTTP_R_OK;
}
//***********************************************
//void HttpCloseConnect(void *data, INT8U index)
// Description : close http clinet socket
// Parameters :
// Returns :
//
//***********************************************
void HttpCloseConnect(void *data, INT8U index)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
char Msg[256];
THttpCtrlBlack *ptHCB = data ;
sprintf(Msg, "HTTP HttpCloseConnect PID %d\n", ptHCB->PID[index]);
close(ptHCB->Sock[index]);
SHOW_EX1(HTTP_DEBUG, Msg);
//if(ptHCB->send_jpg[index].jpg_file != NULL)
if(ptHCB->ref[index] != 0)
{
//video_image_free(&ptHCB->send_jpg[index]); //masked by jgy 9/3/05
video_bkimage_free(ptHCB, index); //add by jgy 9/3/05
}
imgmax[index]=0;
jpgsmp[index] = 0;
sdimg_critical_section[index]=0;
//双语网页:中文网页名称由相应英文网页名称加"_gb"后缀形成
if(!strncmp((const char*)ptHCB->SockRecvBuf[index], "POST /Re", 8))
{
if(!strncmp((const char*)ptHCB->SockRecvBuf[index] + 8, "boot", 4))
{//软件更新
FWUDReBoot(NULL, NULL, IOR, ROOT, NULL, 0);
}
//if(!strncmp((const char*)ptHCB->SockRecvBuf[index],"POST /Reboot.html",17))
//{
// FWUDReBoot(NULL, NULL, IOR, ROOT, NULL, 0) ;
//}
if(!strncmp((const char*)ptHCB->SockRecvBuf[index] + 8, "store", 5))
{//恢复出厂设置
reset(SYSTEM); //此时的网络连接已经关闭!
}
if(!strncmp((const char*)ptHCB->SockRecvBuf[index] + 8, "set", 3))
{//重启
reset(FIRMWARE);
}
}
HttpClearHCBByIndex((void*)data, index);
}
#if Back_GetImg
/*
test for background get image
*//*
INT8S BackGetTest(void *data, INT8U index)
{
THttpCtrlBlack *ptHCB = (INT8U *)data ;
if(ptHCB->File[index] != NULL)
{
video_image_free(&ptHCB->send_jpg[index]);
}
//--- get image
HttpGetImage_20040712(data, index) ;
//--- free image
OSTimeDlyHMSM(0,0,0,40);
//}
} */
#endif
#endif /*HTTP_OPEN*/