int adhocSync(void) { int size = 0; int retry = 60; if (Server) { while (retry--) { adhocSend(adhoc_work, 1, ADHOC_DATATYPE_SYNC); if (adhocRecv(adhoc_work, 1000000, ADHOC_DATATYPE_SYNC) == 1) goto check_packet; } } else { while (retry--) { if (adhocRecv(adhoc_work, 1000000, ADHOC_DATATYPE_SYNC) == 1) { adhocSend(adhoc_work, 1, ADHOC_DATATYPE_SYNC); goto check_packet; } } } return -1; check_packet: while (1) { pdpStatStruct pdpStat; size = sizeof(pdpStat); if (sceNetAdhocGetPdpStat(&size, &pdpStat) >= 0) { // 余分なパケットを破棄 if (pdpStat.rcvdData == ADHOC_DATASIZE_SYNC) adhocRecv(adhoc_work, 0, ADHOC_DATATYPE_SYNC); else break; } if (Loop != LOOP_EXEC) return 0; sceKernelDelayThread(100); } return 0; }
int adhocRecvSendAck(void *buffer, int length, int timeout, int type) { int temp_length = length; int rcvd_length = 0; int error = 0; unsigned char *buf = (unsigned char *)buffer; do { if (temp_length > ADHOC_BUFFER_SIZE - 1) temp_length = ADHOC_BUFFER_SIZE - 1; if ((error = adhocRecv(buf, timeout, type)) != temp_length) return error; *(int *)adhoc_work = rcvd_length + temp_length; adhocSend(adhoc_work, 4, ADHOC_DATATYPE_ACK); buf += temp_length; rcvd_length += temp_length; temp_length = length - rcvd_length; } while (rcvd_length < length); return rcvd_length; }
int adhocSendRecvAck(void *buffer, int length, int timeout, int type) { int temp_length = length; int sent_length = 0; int error = 0; unsigned char *buf = (unsigned char *)buffer; do { if (temp_length > ADHOC_BUFFER_SIZE - 1) temp_length = ADHOC_BUFFER_SIZE - 1; adhocSend(buf, temp_length, type); if ((error = adhocRecv(adhoc_work, timeout, ADHOC_DATATYPE_ACK)) != 4) return error; if (*(int *)adhoc_work != sent_length + temp_length) return -1; buf += temp_length; sent_length += temp_length; temp_length = length - sent_length; } while (sent_length < length); return sent_length; }
int adhocRecvBlocked(void *buffer, unsigned int *length,int max_retry) { char mac[20]; int port=309; int err=0; int retry_cpt=0; do { err = adhocRecv(buffer, length); if (err>0) break; sceKernelDelayThread(RECV_DELAY); if (max_retry>=0) { retry_cpt++; if (retry_cpt>max_retry) return 0; } else { if (get_pad()&PSP_CTRL_TRIANGLE) return -1; } } while (err == 0); return err; }
void adhocWait(int data_size) { pdpStatStruct pdpStat; int size = sizeof(pdpStat); if (data_size > ADHOC_BUFFER_SIZE) data_size = ADHOC_BUFFER_SIZE; while (1) { if (sceNetAdhocGetPdpStat(&size, &pdpStat) >= 0) { if (pdpStat.rcvdData == 0 || (int)pdpStat.rcvdData == data_size) break; else adhocRecv(adhoc_work, 0, ADHOC_DATATYPE_ANY); } if (Loop != LOOP_EXEC) break; sceKernelDelayThread(100); } }