//Nosûta kârtçjo update klientam void sendClientUpdate(int sock, struct Header *head) { struct UpdatePlayerHeader *pHead = (struct UpdatePlayerHeader*)malloc(sizeof(*pHead)); struct UpdatePlayer *pUpdate = (struct UpdatePlayer*)malloc(sizeof(*pUpdate)); struct UpdateBulletHeader *bHead = (struct UpdateBulletHeader*)malloc(sizeof(*bHead)); struct UpdateBullet *bUpdate = (struct UpdateBullet*)malloc(sizeof(*bUpdate)); uint32_t next_size; int forW = 0; getPHead(pHead); head->type = PCKT_UPDATE; next_size = sizeof(*pHead); head->length = htonl(next_size); sendHead(sock, head); if( send(sock , pHead , next_size , 0) < 0) { puts("Sent failed."); //exit(1); } for(forW = 0; forW < ntohl(pHead->playerCount); forW++) { getPUpdate(pUpdate); head->type = PCKT_UPDATE; next_size = sizeof(*pUpdate); head->length = htonl(next_size); sendHead(sock, head); if( send(sock , pUpdate , next_size , 0) < 0) { puts("Sent failed."); //exit(1); } } getBHead(bHead); head->type = PCKT_UPDATE; next_size = sizeof(*bHead); head->length = htonl(next_size); sendHead(sock, head); if( send(sock , bHead , next_size , 0) < 0) { puts("Sent failed."); //exit(1); } for(forW = 0; forW < ntohl(bHead->bulletCount); forW++) { getBUpdate(bUpdate); head->type = PCKT_UPDATE; next_size = sizeof(*bUpdate); head->length = htonl(next_size); sendHead(sock, head); if( send(sock , bUpdate , next_size , 0) < 0) { puts("Sent failed."); //exit(1); } } free(pHead); free(pUpdate); free(bHead); free(bUpdate); }
void KAsyncFetchObject::connectCallBack(KHttpRequest *rq,KUpstreamSelectable *client,bool half_connection) { this->client = client; if (client && client->selector==NULL) { client->selector = rq->c->selector; } if(this->client==NULL || this->client->socket->get_socket()==INVALID_SOCKET){ if (client) { client->isBad(BadStage_Connect); } if (tryCount<=conf.errorTryCount) { //connect try again retryOpen(rq); return; } handleError(rq,STATUS_GATEWAY_TIMEOUT,"Cann't connect to remote host"); return; } if (!TEST(rq->flags,RQ_NET_BIG_OBJECT)) { rq->c->removeSocket(); } if (half_connection) { client->connect(rq,resultUpstreamConnectResult); } else { sendHead(rq); } }
int putProcess(const ExHttp * pHttp, int rType) { static const char * c100 = "HTTP/1.1 100 Continue\n\n" ; size_t size ; int code ; const char * csize = get_head_info( pHttp, "Content-Length" ) ; size = csize==NULL ? 0 : atoi( csize ) ; char buf[size] ; char *pBuf = buf ; int ret = 0 ; ex_sock_nwrite( pHttp->sock, ( char * ) c100, strlen( c100 ) ) ; ex_sock_nread( pHttp->sock, ( char * ) &buf, sizeof (buf), size ) ; switch ( rType ) { case 0: /* static file */ case 1: /* dyanamic page: '.cgi' extension */ code = 200 ; writeFileStream( pHttp->url, ( char * ) &buf, size ) ; break ; case 404: code = 201 ; writeFileStream( pHttp->url, ( char * ) &buf, size ) ; break ; default: break ; } pBuf += codeSet( pBuf, code ) ; pBuf += sprintf( pBuf, "Connection: close\n" ) ; ret = sendHead( pHttp, buf, pBuf-buf ) ; return -1 ; }
void Router::doInfo(Connection *conn, RouterMsg *pMsg) { Buffer body; char buffer[1024]; int bufflen; //clients bufflen = snprintf(buffer, 1024, "clients: %d\r\n", allSessions.size()); body.append(buffer, bufflen); //channels bufflen = snprintf(buffer, 1024, "channels: %d\r\n", allChannels.size()); body.append(buffer, bufflen); //gateways for(ConnectionSet::const_iterator it = allGateways.begin(); it != allGateways.end(); ++it) { Connection *gate = it->first; GatewayInfo *pInfo = (GatewayInfo*)gate->getData(); if(!pInfo->isauth){ continue; } bufflen = snprintf(buffer, 1024, "gateway: %d\t%d\t%d\r\n", pInfo->serverid, pInfo->sessions.size(), pInfo->channels.size()); body.append(buffer, bufflen); } //发送消息 sendHead(conn, ROUTER_MSG_INFO, 0, body.size()); conn->send(body.data(), body.size()); }
int ex_error_reply(const ExHttp *pHttp, int stscode) { char buf[128] ; char *pBuf = buf ; pBuf += codeSet( pBuf, stscode ) ; return sendHead( pHttp, buf, pBuf-buf ) ; }
void KAsyncFetchObject::handleConnectResult(KHttpRequest *rq,int got) { if (got==-1) { handleConnectError(rq,STATUS_GATEWAY_TIMEOUT,"connect to remote host time out"); return; } /////////[394] sendHead(rq); }
void echoHtmlPage(FILE *client,char *req){ //use the plain text to fill the html template //这个函数派上用场实在单独的一个web页面中(<a href=xxx,不同于其他链接) ///view?resp=html&dir=xxx,dir is absname char abspath[MAXPATH]; extern char webpage_root[]; sscanf(req,"/view?resp=html&dir=%s",abspath); chinese2host(abspath); if(strncasecmp(webpage_root,abspath,strlen(webpage_root))!=0){ fputs("Permission denied!",client); return; } int fd=open(abspath,O_RDONLY); if(fd==-1){ info("open file error:"); info(abspath); sendHead(client); fputs("open file error!",client); return; } int len=lseek(fd,0,SEEK_END); char* p=(char *)malloc(len+1);//maybe need '\0' bzero(p,len+1); lseek(fd,0,SEEK_SET); int ret=read(fd,p,len);//一次性读取文件,对于大文件会出错,有时间再改,分批读取和传送文件 if(ret<0) info("echoHtmlPage:read fail"); close(fd); sendHead(client); // char msg[128]; // sprintf(msg,"strlen of head:%d",strlen(packHead_close)); // info(msg); char title[MAX_FN];//,time[MAX_FN]; sscanf(p,"%s",title); p=formatText(p,len);//space -> etc // info("there"); fprintf(client,"<html lang=\"zh-cn\"><html><head><meta charset=\"utf-8\"><title>Online</title></head><body><font color=\"115599\" size=+3>%s</font><br><hr width=\"100%%\"><br><font color=\"119966\" size=+2>%s</body></html>",title,p); // fwrite(p,len,1,client);//send file content fflush(client); free(p); }
StreamState KHttpTransfer::write_end() { if (preventWriteEnd) { return STREAM_WRITE_SUCCESS; } if (!isHeadSend) { if (sendHead(true) != STREAM_WRITE_SUCCESS) { SET(rq->flags,RQ_CONNECTION_CLOSE); return STREAM_WRITE_FAILED; } } return KHttpStream::write_end(); }
void Widget::defaultSetting() { //test...2014-08-23 // QSettings setting(QStandardPaths::writableLocation(QStandardPaths::DataLocation)+"/data/config.ini",QSettings::IniFormat); QSettings setting; setting.beginGroup("GroupData"); for(int i = 0;i<setting.childGroups().count();i++){//TODO:test area. QString strip = setting.value(QString("%1/IpAdd").arg(setting.childGroups().at(i)),i>9?QString("192.168.1.1%1").arg(i):QString("192.168.1.10%1").arg(i)).toString(); ui->combGroup->addItem(QString::number(i+1)); // ui->combGroup->setItemText(i,strip); ui->combGroup->setItemData(i,strip); } setting.endGroup(); for(int i=0;i<ui->combGroup->count();i++){ _BarreStatus.insert(ui->combGroup->itemText(i).toInt(), QPair<QPair<sendHead,sendHead>,bool> (QPair<sendHead,sendHead>(sendHead(),sendHead()),true)); } loadSetting(); }
StreamState KHttpTransfer::write_all(const char *str, int len) { if (len<=0) { return STREAM_WRITE_SUCCESS; } if (!isHeadSend) { if (sendHead(false) != STREAM_WRITE_SUCCESS) { SET(rq->flags,RQ_CONNECTION_CLOSE); return STREAM_WRITE_FAILED; } } if (st == NULL) { return STREAM_WRITE_FAILED; } return st->write_all(str,len); }
void Router::doSendAllMsg(Connection *conn, RouterMsg *pMsg) { ConnectionSet::const_iterator it; for(it = allGateways.begin(); it != allGateways.end(); ++it){ Connection *gate = it->first; GatewayInfo *pInfo = (GatewayInfo*)gate->getData(); LOG("send allmsg to gateway=%p, clients=%d", gate, pInfo->sessions.size()); if(!pInfo->sessions.size()){ continue; } sendHead(gate, ROUTER_MSG_SEND_ALL, 0, pMsg->len); gate->send(pMsg->data, pMsg->len); } }
/* * This will handle connection for each client * */ void *connection_handler(void *socket_desc) { //Get the socket descriptor int sock = *(int*)socket_desc; int read_size; uint32_t next_size; struct Header *head = (struct Header*)malloc(sizeof(*head)); size_t packet_size = sizeof(*head); int packet_type; struct ConnectionRequest *request = (struct ConnectionRequest*)malloc(sizeof(*request)); struct ConnectionResponse *rResp = (struct ConnectionResponse*)malloc(sizeof(*rResp)); //Saòemam ziòu no klienta if( (read_size = recv(sock , head , packet_size , 0)) > 0 ) { packet_size = ntohl(head->length); packet_type = head->type; //Nosûtam pirmo responsu if( packet_type == PCKT_CONNECTION_REQUEST ) { getrResp(rResp); head->type = PCKT_CONNECTION_RESPONSE; next_size = sizeof(*rResp); head->length = htonl(next_size); //Send some data sendHead(sock, head); if( send(sock , rResp , next_size , 0) < 0) { puts("Sent failed."); //exit(1); } } } sendClientUpdate(sock, head); //Free the socket pointer puts("Worker ending."); free(socket_desc); free(head); free(request); free(rResp); return 0; }
void Router::doKick(Connection *conn, RouterMsg *pMsg) { LOG("kick"); //session分组 SessionGroup groups; getSessionGroups(groups, pMsg->slen, pMsg->data); //发送 for(SessionGroup::const_iterator it = groups.begin(); it != groups.end(); ++it){ Connection *gate = it->first; const std::string &sidlist = it->second; sendHead(gate, ROUTER_MSG_KICK, sidlist.size(), 0); gate->send(sidlist.c_str(), sidlist.size()); } }
void Router::doChannelDel(Connection *conn, RouterMsg *pMsg) { //session分组 SessionGroup groups; getSessionGroups(groups, pMsg->slen, pMsg->data); //频道名称在data域 for(SessionGroup::const_iterator it = groups.begin(); it != groups.end(); ++it){ Connection *gate = it->first; const std::string &sidlist = it->second; sendHead(gate, ROUTER_MSG_CH_DEL, sidlist.size(), pMsg->len); gate->send(sidlist.c_str(), sidlist.size()); gate->send(pMsg->data + pMsg->slen, pMsg->len); } }
void Router::doSendMsg(Connection *conn, RouterMsg *pMsg) { //session分组 SessionGroup groups; getSessionGroups(groups, pMsg->slen, pMsg->data); //发送 for(SessionGroup::const_iterator it = groups.begin(); it != groups.end(); ++it){ Connection *gate = it->first; const std::string &sidlist = it->second; LOG("send msg to: %s, data=%s", sidlist.c_str()); sendHead(gate, ROUTER_MSG_SEND_MSG, sidlist.size(), pMsg->len); gate->send(sidlist.c_str(), sidlist.size()); gate->send(pMsg->data + pMsg->slen, pMsg->len); } }
bool KHttpTransfer::support_sendfile() { if (st != static_cast<KWStream *>(rq)) { return false; } if (rq->c->isSSL()) { return false; } /////////[302] if (!isHeadSend) { if (sendHead(false) != STREAM_WRITE_SUCCESS) { SET(rq->flags, RQ_CONNECTION_CLOSE); return false; } } return true; }
int ex_send_msg(ExHttp *pHttp, const char *type, const char *buf, size_t len) { char hBuf[BUFSIZ] ; char *pBuf = hBuf ; int ret ; pBuf += codeSet( pBuf, 200 ) ; pBuf += typeSet( pBuf, type ) ; pBuf += lengthSet( pBuf, len ) ; do { if ( (ret = sendHead( pHttp, hBuf, pBuf-hBuf ))<0 ) break ; if ( (ret = ex_sock_nwrite( pHttp->sock, ( char * ) buf, len ))<0 ) break ; } while ( 0 ) ; return ret ; }
int ex_send_file(ExHttp *pHttp, const char *filePath) { char buf[BUFSIZ] ; char *pBuf = buf ; int ret = 0 ; pBuf += codeSet( pBuf, 200 ) ; pHttp->url = ( char * ) filePath ; stat( filePath, &pHttp->st ) ; pBuf += fileSet( pBuf, pHttp ) ; do { if ( (ret = sendHead( pHttp, buf, pBuf-buf ))<0 ) break ; if ( (ret = sendFileStream( pHttp, filePath ))<0 ) break ; } while ( 0 ) ; return ret ; }
int deleteProcess(const ExHttp * pHttp, int rType) { char buf[BUFSIZ] ; char *pBuf = buf ; int code ; switch ( rType ) { case 0: /* static file */ case 1: /* dyanamic page: '.cgi' extension */ if ( remove( pHttp->url )==0 ) code = 200 ; break ; } pBuf += codeSet( pBuf, code ) ; strcat( pBuf, "\n\n" ) ; sendHead( pHttp, buf, pBuf-buf ) ; return -1 ; }
void Router::doChannelPub(Connection *conn, RouterMsg *pMsg) { //频道名称在session域 std::string name(pMsg->data, pMsg->slen); ChannelList::const_iterator it = allChannels.find(name); if(it == allChannels.end()){ return ; } LOG("send channel start"); const ConnectionSet &conns = it->second; ConnectionSet::const_iterator it2; for(it2 = conns.begin(); it2 != conns.end(); ++it2){ Connection *gate = it2->first; sendHead(gate, ROUTER_MSG_CH_PUB, pMsg->slen, pMsg->len); gate->send(pMsg->data, pMsg->slen + pMsg->len); LOG("send channel msg, gateway=%p", gate); } }
static int staticProcess(const ExHttp *pHttp) { char buf[BUFSIZ]; char *pBuf = buf; int ret = 0; int code = cacheCheck(pHttp); pBuf += codeSet(pBuf , code); if (code == 200) { pBuf += fileSet(pBuf , pHttp); pBuf += connectionSet(pBuf); } do { if ((ret = sendHead(pHttp, buf , pBuf - buf)) < 0) break; if (code == 304 || 'H' == *(pHttp->method)) break; ret = sendFileStream(pHttp, pHttp->url); } while (0); return ret; }
//------------------------------------------------------------------------- static void serveSocket(Connection *c) { sw_socket *s = c->s; IFileAccess *dw = c->dw; free(c); byte *header; char url[1024]; int length, method; bool hasRange; int64 rangeStart, rangeLength; header = (byte*) malloc(gBufferSize); while(1) { // printf("==== Waiting for request...\n"); // get the request length = getHeader(s, header, gBufferSize - 1); if (length <= 0) break; header[length] = 0; // check kind of request method = getMethod(header); if (method != METHOD_HEAD && method != METHOD_GET) { sendError(s, ERR_BAD_METHOD); continue; } // get url, host and port parseRequest((char*)header, url, &hasRange, &rangeStart, &rangeLength); if (rangeLength > 1024*1024*256) rangeLength = 1024*1024*256; #if 0 { printf("================================================\n"); printf("header:\n%s\n", header); printf("= = = = = = = = = = = = = = = = = = = = = = = = \n"); printf("url: %s\n", url); if (hasRange) printf("range: %lld - %lld (%lld)\n", rangeStart, rangeStart + rangeLength - 1, rangeLength); printf("\n"); } #endif const char *path = "/dvd.iso"; if (strcmp(url, path) != 0) { sendError(s, ERR_NOT_FOUND); continue; } int64 fileSize = dw->size(); int64 requestSize = (hasRange) ? rangeLength : fileSize; byte *data = NULL; if (method == METHOD_GET) { data = dw->lock(rangeStart, requestSize); if (!data) { fprintf(stderr, "Data read error\n"); sendError(s, ERR_NOT_FOUND); continue; } } sendHead(s, path, fileSize, hasRange, rangeStart, rangeLength); if (data) { sendRawToSocket(s, data, requestSize); dw->unlock(); } } sw_close(s); free(header); }
static int staticProcess(const ExHttp *pHttp) { char buf[BUFSIZ] ; char *pBuf = buf ; int ret = 0 ; int code = cacheCheck( pHttp ) ; pBuf += codeSet( pBuf, code ) ; if ( code==200 ) { pBuf += fileSet( pBuf, pHttp ) ; } if ( strcmp( get_mime_type( pHttp->url ), "image/png" )!=0 ) pBuf += setCookie( pBuf, pHttp ) ; do { if ( code==206 ) { char rangeTemp[128] ; char * range = ( char * ) get_head_info( pHttp, "Range" ) ; strncpy( ( char * ) &rangeTemp, range, 128 ) ; range = ( char * ) &rangeTemp ; SKIP( &range, '=' ) ; char * sRange = range ; SKIP( &range, '-' ) ; char date[35] ; strftime( date, 35, "%a, %d %b %Y %X GMT", localtime( &(pHttp->st.st_ctime) ) ) ; int startRange = (*sRange=='\0') ? -1 : atoi( sRange ) ; int endRange = (*range=='\0') ? -1 : atoi( range ) ; // printf("start :%d end :%d\n",startRange,endRange ) ; if ( startRange<0 ) { startRange = pHttp->st.st_size-endRange ; endRange = pHttp->st.st_size ; } else if ( endRange<0 ) { endRange = pHttp->st.st_size ; } pBuf += sprintf( pBuf, "connection: close\n" "Last-Modified: %s\n" "Accept-Ranges: bytes\n" "Content-Length: %d\n" "Content-Range: bytes %d-%d/%d\n", date, endRange-startRange, startRange, endRange, pHttp->st.st_size ) ; if ( (ret = sendHead( pHttp, buf, pBuf-buf ))<0 ) break ; ret = sendFileRangeStream( pHttp, pHttp->url, startRange, endRange + 1 ) ; break ; } if ( (ret = sendHead( pHttp, buf, pBuf-buf ))<0 ) break ; if ( code==304|| code == 412 || 'H'== *(pHttp->method) ) break ; ret = sendFileStream( pHttp, pHttp->url ) ; } while ( 0 ) ; return ret ; }