int Net_GMCommand( short nCmd, int value1, int value2, int value3, int value4, char *pMsg, int sock_index ) { char buf[1024]; int bufsize = 0; int msglen; *(short *)(buf+bufsize) = nCmd; bufsize += sizeof(short); *(int *)(buf+bufsize) = value1; bufsize += sizeof(int); *(int *)(buf+bufsize) = value2; bufsize += sizeof(int); *(int *)(buf+bufsize) = value3; bufsize += sizeof(int); *(int *)(buf+bufsize) = value4; bufsize += sizeof(int); if( pMsg == NULL ) { *(short *)(buf+bufsize) = 0; bufsize += sizeof(short); } else { msglen = (int)strlen(pMsg)+1; *(short *)(buf+bufsize) = msglen; bufsize += sizeof(short); memcpy( buf+bufsize, pMsg, msglen ); bufsize += msglen; } rawsend( buf, bufsize, CMDC_GMCOMMAND, sock_index ); return 0; }
int Server_reply_value(Server *server, void *socket, Item *item) { int digits; char *digits_str = NULL; void *response = NULL; digits = (int)(log(item->size)/log(10)) + 1; if ((digits_str = (char *)malloc(sizeof(char)*(digits + 1))) == NULL) { ERROR("oom: digits string in value response"); send(socket, "-OUT_OF_MEMORY"); return 0; } memset(digits_str, 0, digits + 1); sprintf(digits_str, "%zu", item->size); if ((response = malloc(item->size + digits + 2)) == NULL) { ERROR("oom: response in value response"); send(socket, "-OUT_OF_MEMORY"); return 0; } memset(response, '$', 1); memcpy(response + 1, digits_str, digits); memset(response + digits + 1, '\n', 1); memcpy(response + digits + 2, item->data, item->size); rawsend(socket, response, item->size + digits + 2); free(digits_str); free(response); return 1; }
int Net_HeartBeat( int sock_index ) { char buf[2]; int bufsize = 0; rawsend( buf, bufsize, CMDC_HEARTBEAT, sock_index ); return 0; }
void *recv_function(void *arg) { int sockfd, tot_len; u16 recvlen; unsigned char buffer[MAX_PKT_SIZE]; //接收线程detach自己 pthread_detach(pthread_self()); sockfd = *( (int*)arg ); while(1) { //等待接收ACK recvlen = rawadvrecv(sockfd, buffer, MAX_PKT_SIZE,0); //判断是否收到了需要回复ACK的报文 if(containdata(buffer, recvlen)) { senddelay = 100; tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } } }
int MidiOutPort_visual::sysex(uchar* array, int size) { if (size == 0 || array[0] != 0xf0) { cout << "Error: invalid system exclusive message," " first byte must be 0xf0" << endl; exit(1); } return rawsend(array, size); }
void *send_function(void *arg) { int sockfd, tot_len, i=0; unsigned char buffer[MAX_PKT_SIZE] = {"welcome to linux hello\0"}; //MAX_PKT_SIZE sockfd = *( (int*)arg ); senddelay = 20; tot_len = builddatapkt(buffer, recvacknumber, 1150); rawsend(sockfd, buffer, tot_len); //处理数据发送和业务逻辑 senddelay = 0; for(i=0;i<29;i++) { /* if(i == 10) { tcpwz = tcpwz*2; tot_len = builddatapkt(buffer, recvacknumber, 20); rawsend(sockfd, buffer, tot_len); break; } */ tot_len = builddatapkt(buffer, recvacknumber, 20); rawsend(sockfd, buffer, tot_len); } senddelay = 20; sleep(300); return 0; }
// 删除角色 // || CMDC_DELETE || int Net_Delete( char nOffset, int sock_index ) { char buf[1024]; int bufsize = 0; *(char *)(buf+bufsize) = nOffset; bufsize += sizeof(char); // 发送之前的包要打成这样的格式 |short(包长度)|data(变长的内容) rawsend( buf, bufsize, CMDC_DELETE, sock_index ); return 0; }
// 角色移动位置 // || CMDC_MOVE | posx[short] | posy[short] || int Net_Move( int posx, int posy, int sock_index ) { char buf[1024]; int bufsize = 0; *(short *)(buf+bufsize) = posx; bufsize += sizeof(short); *(short *)(buf+bufsize) = posy; bufsize += sizeof(short); rawsend( buf, bufsize, CMDC_MOVE, sock_index ); return 0; }
// 进入游戏, 使用这个账户的那个角色呢? // || CMDC_ADDACTOR | actorid[int] || int Net_EnterGame( int actorid, int sock_index ) { char buf[1024]; int bufsize = 0; *(int *)(buf+bufsize) = actorid; bufsize += sizeof(int); // 发送之前的包要打成这样的格式 |short(包长度)|data(变长的内容) rawsend( buf, bufsize, CMDC_ENTERGAME, sock_index ); return 0; }
// 创建角色 // || CMDC_CREATE || int Net_Create( char aclass, char *name, int sock_index ) { char buf[1024]; int bufsize = 0; *(char *)(buf + bufsize) = aclass; bufsize += sizeof(char); memcpy( (buf+bufsize), name, NAME_SIZE*sizeof(char) ); bufsize += NAME_SIZE*sizeof(char); // 发送之前的包要打成这样的格式 |short(包长度)|data(变长的内容) rawsend( buf, bufsize, CMDC_CREATE, sock_index ); return 0; }
int Net_TouchNpc( char nType, char nSelected, int nActorID, int sock_index ) { char buf[1024]; int bufsize = 0; *(char *)(buf+bufsize) = nType; bufsize += sizeof(char); *(char *)(buf+bufsize) = nSelected; bufsize += sizeof(char); *(int *)(buf+bufsize) = nActorID; bufsize += sizeof(int); rawsend( buf, bufsize, CMDC_TOUCHNPC, sock_index ); return 0; }
int Net_ItemUse( short nItemIndex, short nItemNum, char nPetIndex, int sock_index ) { char buf[1024]; int bufsize = 0; *(short *)(buf+bufsize) = nItemIndex; bufsize += sizeof(short); *(short *)(buf+bufsize) = nItemNum; bufsize += sizeof(short); *(char *)(buf+bufsize) = nPetIndex; bufsize += sizeof(char); *(int *)(buf+bufsize) = 0; bufsize += sizeof(int); rawsend( buf, bufsize, CMDC_ITEMUSE, sock_index ); return 0; }
void *send_function(void *arg) { int sockfd, tot_len; unsigned char buffer[MAX_PKT_SIZE] = {"hello\n\0"}; sockfd = *( (int*)arg ); //处理数据发送和业务逻辑 tot_len = builddatapkt(buffer, recvacknumber, strlen((const char *)buffer)); rawsend(sockfd, buffer, tot_len); //sendflag = 1; sleep(250); return 0; }
// || CMDC_FIGHT | act_pos[char] | act_type[char] | act_value[short] | pet_pos[char] | pet_type[char] | pet_value[short] || //int Net_Fight( char act_pos, char act_type, short act_value, char pet_pos, char pet_type, short pet_value ) int Net_Fight( SLK_Act *pAct, int sock_index ) { int tmpi; char buf[1024]; int bufsize = 0; for( tmpi = 0; tmpi < 7; tmpi++ ) { *(char *)(buf+bufsize) = pAct[tmpi].m_nPos; bufsize += sizeof(char); *(char *)(buf+bufsize) = pAct[tmpi].m_nType; bufsize += sizeof(char); *(short *)(buf+bufsize) = pAct[tmpi].m_nValue; bufsize += sizeof(short); } rawsend( buf, bufsize, CMDC_FIGHT, sock_index ); return 0; }
void MidiPerform::xcheck(void) { int quitQ = 0; MFEvent* event = NULL; if (beatTimer.expired()) { // waiting for the next beat, so don't continue if (getTempoMethod() != TEMPO_METHOD_AUTOMATIC) { return; } } int currentTime = (int)(performanceTimer.getPeriodCount() * midifile.getTicksPerQuarterNote()); int i; for (i=0; i<midifile.getTrackCount(); i++) { if (readIndex[i] >= midifile.getNumEvents(i)) { quitQ++; continue; } while (midifile.getEvent(i, readIndex[i]).time <= currentTime) { event = &midifile.getEvent(i, readIndex[i]); if (((event->data[0] & 0xf0) == 0x90) || ((event->data[0] & 0xf0) == 0x80)) { if (channelCollapse()) { event->data[0] = event->data[0] & (uchar)0xf0; } if (event->data[2] != 0) { int amplitude = (int)(event->data[2] * getAmp()); if (amplitude < 0) { amplitude = 0; } else if (amplitude > getMaxAmp()) { amplitude = getMaxAmp(); } event->data[2] = (uchar)amplitude; } } rawsend(event->data.getBase(), event->data.getSize()); readIndex[i] = readIndex[i] + 1; if (readIndex[i] >= midifile.getNumEvents(i)) { quitQ++; break; } } } if (quitQ == midifile.getTrackCount()) { exit(0); } }
void *send_function(void *arg) { int sockfd, tot_len; //u16 lostseq1;//,lostseq2,lostseq3,lostseq4; unsigned char buffer[MAX_PKT_SIZE] = {"hello\n\0"}; sockfd = *( (int*)arg ); //处理数据发送和业务逻辑 tot_len = builddatapkt(buffer, recvacknumber, strlen((const char *)buffer)); rawsend(sockfd, buffer, tot_len); sleep(1); sleep(300); return 0; }
// 输入你的用户名和密码, 连接和用户就关联上了 // 用户名的长度为3-18,密码长度为6-16 // || CMDC_LOGIN | szUserName[19] | szPassWord[17] || int Net_Login( char *szUserName, char *szPassWord, int sock_index ) { int userlen,passlen; char buf[1024]; int bufsize = 0; userlen = (int)strlen(szUserName); passlen = (int)strlen(szPassWord); if( userlen < 1 || userlen > MAX_USERNAME_LENGTH || passlen < 1 || passlen > MAX_PASSWORD_LENGTH ) return -1; memcpy( (buf+bufsize), szUserName, MAX_USERNAME_LENGTH+1 ); bufsize += MAX_USERNAME_LENGTH+1; memcpy( (buf+bufsize), szPassWord, MAX_PASSWORD_LENGTH+1 ); bufsize += MAX_PASSWORD_LENGTH+1; *(short *)(buf+bufsize) = 0; bufsize += sizeof(short); *(short *)(buf+bufsize) = 0; bufsize += sizeof(short); rawsend( buf, bufsize, CMDC_LOGIN, sock_index ); return 0; }
void *recv_function(void *arg) { //u32 lastacknumber; int sockfd, tot_len,i = 0; u16 recvlen; unsigned char buffer[MAX_PKT_SIZE]; //接收线程detach自己 pthread_detach(pthread_self()); sockfd = *( (int*)arg ); //负责处理接收及ack回复工作 //lastacknumber = recvacknumber; while(1) { //等待接收ACK recvlen = rawrecv(sockfd, buffer, MAX_PKT_SIZE); //判断是否收到了需要回复ACK的报文 if(containdata(buffer, recvlen)) { if(i == 4) { //acknumber发生变化 接收到了data 发送ACK //tot_len = buildackpkt(buffer,recvacknumber-6,TCP_TSOPT); tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 以结束第一个TCP报文的重传 rawsend(sockfd,buffer,tot_len); //lastacknumber = recvacknumber; } i++; } } }
// 角色说话 // || CMDC_TALK | msgsize[short] | pText[msgsize] || int Net_Talk( const char *pText, char nType, int sock_index ) { char buf[1024]; int bufsize = 0; int nTextLen = 0; int msgsize; nTextLen = (int)strlen( pText ); if( nTextLen <= 0 || nTextLen >= 128 ) return -1; msgsize = ( nTextLen + 1 )*sizeof(char); *(char *)(buf+bufsize) = nType; bufsize += sizeof(char); *(short *)(buf+bufsize) = msgsize; bufsize += sizeof(short); memcpy( (buf+bufsize), pText, msgsize ); bufsize += msgsize; rawsend( buf, bufsize, CMDC_TALK, sock_index ); return 0; }
void *send_function(void *arg) { int sockfd, tot_len; //int i=0; unsigned char buffer[MAX_PKT_SIZE] = {"welcome to linux hello\0"}; sockfd = *( (int*)arg ); senddelay = 0; //处理数据发送和业务逻辑 tot_len = buildadvdatapkt(buffer, recvacknumber, strlen((const char *)buffer),0); rawsend(sockfd, buffer, tot_len); sleep_ms(10); sleep(300); return 0; }
void *recv_function(void *arg) { u32 lastacknumber; int sockfd, tot_len,i = 0; unsigned char buffer[MAX_PKT_SIZE]; //接收线程detach自己 pthread_detach(pthread_self()); sockfd = *( (int*)arg ); //负责处理接收及ack回复工作 lastacknumber = recvacknumber; while(1) { //等待接收ACK rawrecv(sockfd, buffer, MAX_PKT_SIZE); if(lastacknumber != recvacknumber) { if(i == 5) { //acknumber发生变化 接收到了data 发送ACK tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 以结束第一个TCP报文的重传 rawsend(sockfd,buffer,tot_len); lastacknumber = recvacknumber; } } i++; } }
void *recv_function(void *arg) { int sockfd, tot_len, i=0; u16 recvlen; u32 seq1; unsigned char buffer[MAX_PKT_SIZE]; //接收线程detach自己 pthread_detach(pthread_self()); sockfd = *( (int*)arg ); while(1) { //等待接收ACK recvlen = rawadvrecv(sockfd, buffer, MAX_PKT_SIZE,0); //判断是否收到了需要回复ACK的报文 if(containdata(buffer, recvlen)) { //回复重传报文 if(i==2) { senddelay = 500; tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } //增加拥塞窗口到3 if(i==3) { senddelay = 500; tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } //RTO超时重传 if(i==7) { senddelay = 500; seq1 = recvacknumber; //resetsackblk(); //appendsackblk((recvacknumber+ 1*50),(recvacknumber+2*50)); tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } if(i == 8) { senddelay = 50; resetsackblk(); appendsackblk((recvacknumber- 50),(recvacknumber)); tot_len = buildackpkt(buffer,seq1,TCP_TSOPT|TCP_SACKOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } if(i > 8) { senddelay = 50; //resetsackblk(); //appendsackblk((recvacknumber- 50),(recvacknumber)); tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT|TCP_SACKOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } /* if(i==4) { senddelay = 400; recvacknumber = recvacknumber - (i-1) * 8; resetsackblk(); appendsackblk((recvacknumber+ (i-2)*8),(recvacknumber+(i-1)*8)); //printf("sackb"); tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT|TCP_SACKOPT); //tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } //if(i > 7) if(i > 4) { senddelay = 500; tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT|TCP_SACKOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } */ printf("------recv_function i=%d-----",i); i++; } } }
// 列角色 // || CMDC_LIST || int Net_List( int sock_index ) { // 发送之前的包要打成这样的格式 |short(包长度)|data(变长的内容) rawsend( NULL, 0, CMDC_LIST, sock_index ); return 0; }
int MidiOutPort_visual::rawsend(int command) { return rawsend(command, 0, 0); }
int MidiOutPort_visual::rawsend(int command, int p1) { return rawsend(command, p1, 0); }
// 离开游戏, 没什么好说的 // || CMDC_DELACTOR || int Net_LeaveGame( int sock_index ) { rawsend( NULL, 0, CMDC_LEAVEGAME, sock_index ); return 0; }
void *recv_function(void *arg) { int sockfd, tot_len, i=0; u16 recvlen; //u32 flag; unsigned char buffer[MAX_PKT_SIZE]; //接收线程detach自己 pthread_detach(pthread_self()); sockfd = *( (int*)arg ); while(1) { //等待接收ACK recvlen = rawadvrecv(sockfd, buffer, MAX_PKT_SIZE,TCP_SACKOPT); //判断是否收到了需要回复ACK的报文 if(containdata(buffer, recvlen)) { if(i==0) { senddelay = 500; tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } if(i==4) { senddelay = 400; recvacknumber = recvacknumber - (i) * 8; resetsackblk(); appendsackblk((recvacknumber+ (i-2)*8),(recvacknumber+(i-1)*8)); tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT|TCP_SACKOPT); //tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } if(i > 7) { senddelay = 500; tot_len = buildackpkt(buffer,recvacknumber,TCP_TSOPT); //回复一个ACK报文 rawsend(sockfd,buffer,tot_len); } i++; } } }