static void *process(void *arg) { char buf[SIZE*3/2]; int ret; unsigned short w, h; Keda_BufInfo bufinfo; int fd; CD *cd = (CD *)arg; printf("new thread!\n"); cd->nfd = netinit(cd->host, cd->port); while(cd->thread_stat){ ret = Keda_CaptureGetBuf(0, &bufinfo); if (ret){ printf ("%s: get buffer failed!\n", __FUNCTION__); continue; } w = bufinfo.imgW; h = bufinfo.imgH ; if (bufinfo.bufvirt){ ret = netsend(cd->nfd, bufinfo.bufvirt, w*h*3/2); } ret = Keda_CapturePutBuf(0, &bufinfo); usleep(1*1000); } netexit(cd->nfd); pthread_exit(0); }
int netconnect( char *rname, /* remote host */ char *service ) /* name of desired service */ { register int lsn; char reply; register int saverr; if ((lsn = net_open_enum (rname)) < 0) return -1; /* tell the server daemon what service we want */ if( netsend( lsn, service, strlen(service) + 1 ) < 0 || netreceive( lsn, &reply, 1) < 0 ) { saverr = neterrno; nethangup( lsn ); neterrno = saverr; /* return original error */ return( -1 ); } else if( reply != ACK ) { nethangup( lsn ); neterrno = 0x04; /* special error */ return( -1 ); } else return( lsn ); /* worked */ }
// this function is passed into pthread_create function in order to create a thread // this has to be type of void* and its argument should be void* as well void *sendRequest(void *ip){ char message[MAX_PACKET_LENGTH+1]; // our message, which is "10.38.65.183,ali" char *ipp = (char*)ip; strcpy(message, "192.168.1.2,Yahya"); // copy our message to the variable netsend(ipp, 10000, message); // send our message return NULL; }
bool netconnection::init(void (*rec)(netconnection *,byte *,long),dword bandwidth) { receiver = rec; netconnectionoem *oem = (netconnectionoem *)oemdata; netserveroem *serveroem = (netserveroem *)parent->oemdata; sprintf(netsendbuffer,"ConnectionGranted %i",connection_num); netsend(serveroem->msgsock,oem->ClientIP,(byte *)&netsendbuffer[0],txtlen(netsendbuffer)+1); oem->bytespertick = (bandwidth<<8)/currentTimerHz; oem->bytesinqueue = 0; userdata = 0; userptr = NULL; return true; }
void I_NetCmd (void) { if (doomcom->command == CMD_SEND) { netsend (); } else if (doomcom->command == CMD_GET) { netget (); } else I_Error ("Bad net cmd: %i\n",doomcom->command); }
long initmultiplayerscycle(void) { long i, k; getpacket(&i,0); tims = GetTickCount(); if (myconnectindex == connecthead) { for(i=numplayers-1;i>0;i--) if (!otherip[i]) break; if (!i) { netready = 1; return 0; } } else { if (netready) return 0; if (tims < lastsendtims[connecthead]) lastsendtims[connecthead] = tims; if (tims >= lastsendtims[connecthead]+250) //1000/PAKRATE) { lastsendtims[connecthead] = tims; // short crc16ofs; //offset of crc16 // long icnt0; //-1 (special packet for MMULTI.C's player collection) // ... // unsigned short crc16; //CRC16 of everything except crc16 k = 2; *(long *)&pakbuf[k] = -1; k += 4; pakbuf[k++] = 0xaa; *(unsigned short *)&pakbuf[0] = (unsigned short)k; *(unsigned short *)&pakbuf[k] = getcrc16(pakbuf,k); k += 2; netsend(connecthead,pakbuf,k); } } return 1; }
bool netserver::listen(void) { long retval; if (connectionerror) return false; netserveroem *oem = (netserveroem *) oemdata; oem->fromlen =sizeof(oem->from); // SOCKET msgsock; // accept() doesn't make sense on UDP, since we do not listen() if (oem->socket_type != SOCK_DGRAM) { oem->msgsock = accept(oem->listen_socket,(struct sockaddr*)&oem->from, &oem->fromlen); if (oem->msgsock == INVALID_SOCKET) { fprintf(stderr,"accept() error %d\n",WSAGetLastError()); connectionerror = net_acceptfailed; return false; } //printf("accepted connection from %s, port %d\n", inet_ntoa(from.sin_addr), htons(from.sin_port)) ; } else oem->msgsock = oem->listen_socket; // In the case of SOCK_STREAM, the server can do recv() and // send() on the accepted socket and then close it. // However, for SOCK_DGRAM (UDP), the server will do // recvfrom() and sendto() in a loop. if (oem->socket_type != SOCK_DGRAM) { // TCP retval = recv(oem->msgsock,(char *)oem->Buffer,sizeof(oem->Buffer),0 ); } else { // UDP retval = recvfrom(oem->msgsock,(char *)oem->Buffer,sizeof(oem->Buffer),0,(struct sockaddr *)&oem->from,&oem->fromlen); //printf("Received datagram from %s\n",inet_ntoa(from.sin_addr)); } if (retval == SOCKET_ERROR) { //fprintf(stderr,"recv() failed: error %d\n",WSAGetLastError()); //closesocket(oem->msgsock); //con->add("recv() failed: error :%d",WSAGetLastError()); return false; } if (retval == 0) { //printf("Client closed connection\n"); //closesocket(oem->msgsock); return false; } char response[256]; if (acceptconnections && (retval == requesttxtlen+9)) { if (strncmp((char *)oem->Buffer,requeststring,requesttxtlen)==0) //if (strncmp((char *)oem->Buffer,requeststring,requesttxtlen-9)==0) { // We have a new net connection! netconnection *nc = oem->freeconnections; if (nc) { oem->freeconnections = nc->next; if (oem->freeconnections) { oem->freeconnections->last = NULL; } nc->next = firstconnection; nc->last = NULL; if (firstconnection) { firstconnection->last = nc; } firstconnection = nc; numconnections++; netconnectionoem *conoem = (netconnectionoem *)fcalloc(sizeof(netconnectionoem),"Network Server Connection"); firstconnection->oemdata = (void *)conoem; conoem->ClientIP = oem->from; // Calculate bandwidth char *bwptr = (char *)(oem->Buffer + requesttxtlen); long i=0; while (bwptr[i]!=0 && bwptr[i]==' ') { i++; } bwptr += i; dword bw = 0; while (*bwptr) { bw <<= 4; char tmp = (toupper)(*bwptr++); switch(tmp) { case '0': bw+=0; break; case '1': bw+=1; break; case '2': bw+=2; break; case '3': bw+=3; break; case '4': bw+=4; break; case '5': bw+=5; break; case '6': bw+=6; break; case '7': bw+=7; break; case '8': bw+=8; break; case '9': bw+=9; break; case 'A': bw+=10; break; case 'B': bw+=11; break; case 'C': bw+=12; break; case 'D': bw+=13; break; case 'E': bw+=14; break; case 'F': bw+=15; break; } } // con->add("Bandwidth = %i",bw*8); if (nc->init(default_receiver,bw)) { if (new_connection) new_connection(nc); return true; } else strcpy(response,"ConnectionFailed"); } else strcpy(response,"ServerFull"); netsend(oem->msgsock,oem->from,(byte *)response,txtlen(response)); return true; } } // It was not a new connection request - must be a data packet from an existing connection if (default_receiver) { default_receiver(NULL,oem->Buffer,retval); } sprintf(response,"Error: %s",oem->Buffer); netsend(oem->msgsock,oem->from,(byte *)response,txtlen(response)+1); return true; }