static void sendJoin(ServerData *data, ENetEvent *event) { Client client, *c; memset(&client, 0, sizeof(Client)); client.peer = event->peer; client.clientId = event->peer->connectID; enet_address_get_host_ip(&event->peer->address, client.host, sizeof(client.host)); event->peer->data = serverNewClient(data, &client); PacketServerClientInformation info; memset(&info, 0, sizeof(PacketServerClientInformation)); info.id = PACKET_ID_CLIENT_INFORMATION; strncpy(info.host, client.host, sizeof(info.host)); info.clientId = htonl(client.clientId); for (c = data->clients; c; c = c->next) { if (c == event->peer->data) continue; PacketServerClientInformation info2; memset(&info2, 0, sizeof(PacketServerClientInformation)); info2.id = PACKET_ID_CLIENT_INFORMATION; strncpy(info2.host, c->host, sizeof(info2.host)); info2.clientId = htonl(c->clientId); serverSend(event->peer->data, (unsigned char*)&info2, sizeof(PacketServerClientInformation), ENET_PACKET_FLAG_RELIABLE); sendFullState(data, c, event->peer->data); serverSend(c, (unsigned char*)&info, sizeof(PacketServerClientInformation), ENET_PACKET_FLAG_RELIABLE); } c = (Client*)event->peer->data; printf("%s [%u] connected.\n", c->host, c->clientId); }
void MainWindow::handleConnected() { serverSend(locUser.name.toLatin1(), 'U'); serverSend(locUser.image, 'I'); chessModel.initStatus(); chessBoard->changeStatus(chessModel.getStatus(player)); char buf[BUFSIZE + 1]; memset(buf, 0, BUFSIZE + 1); buf[0] = 0; memcpy(buf + 1, &chessModel, sizeof(ChessModel)); server.sendData(buf, BUFSIZE + 1); statusBar()->showMessage(QString("Please Organize Your Chess Pieces")); startButton->setEnabled(1); }
static void sendFullState(ServerData *data, Client *target, Client *client) { PacketServerActorFullState state; memset(&state, 0, sizeof(PacketActorFullState)); state.id = PACKET_ID_ACTOR_FULL_STATE; state.clientId = htonl(target->clientId); state.flags = target->actor.flags; state.rotation = target->actor.rotation; memcpy(&state.position, &target->actor.position, sizeof(Vector3f)); serverSend(client, (unsigned char*)&state, sizeof(PacketActorFullState), ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT); }
static void sendPart(ServerData *data, ENetEvent *event) { Client *c; PacketServerClientPart part; c = (Client*)event->peer->data; memset(&part, 0, sizeof(PacketServerClientPart)); part.id = PACKET_ID_CLIENT_PART; part.clientId = htonl(c->clientId); for (c = data->clients; c; c = c->next) { if (c == event->peer->data) continue; serverSend(c, (unsigned char*)&part, sizeof(PacketServerClientPart), ENET_PACKET_FLAG_RELIABLE); } c = (Client*)event->peer->data; printf("%s [%u] disconnected.\n", c->host, c->clientId); }
static void handleState(ServerData *data, ENetEvent *event) { PacketServerActorState state; PacketActorState *p = (PacketActorState*)event->packet->data; Client *c, *client = (Client*)event->peer->data; state.id = p->id; state.clientId = htonl(client->clientId); state.flags = p->flags; state.rotation = p->rotation; for (c = data->clients; c; c = c->next) { if (c == client) continue; serverSend(c, (unsigned char*)&state, sizeof(PacketServerActorState), ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT); } client->actor.flags = p->flags; client->actor.rotation = p->rotation; }
void maitre() { int i, n, bl; int clnt; char tmp[MAXLEN], *buf; #ifdef DEBUG char dbg[16]; #endif struct flock request; struct s_test tLock; enum etat_t etat; int offset; /* A test sentence written in the file */ char phraseTest[] = "Ceci est une phrase test ecrite par le maitre dans le fichier"; bl = -1; clnt = dp.nclnt; maitreLecteur = dp.maitre[0]; etat = SELECT; /* On commence par le premier test. C'est original ;) */ /* Start with the first test ;) */ n = 0; printf("\n--------------------------------------\n"); while (1) { switch (etat) { case SELECT: /* Selection du test a effectuer */ /* Select the test to perform */ printf("\n"); E("Maitre: SELECT"); selectTest(n, &tLock); etat = tLock.type; bl = 0; if (n < MAXTEST) { memset(tmp, 0, MAXLEN); sprintf(tmp, "TEST : TRY TO %s:", LISTE_NOMS_TESTS[n]); write(0, tmp, strlen(tmp)); } else etat = FIN; P("etat=%d\n", etat); n += 1; continue; case RDONLY: case WRONLY: case READLOCK: P("Read lock :%d\n", etat); request.l_type = F_RDLCK; etat = LOCK; continue; case WRITELOCK: P("Write lock :%d\n", etat); request.l_type = F_WRLCK; etat = LOCK; continue; case LOCK: /* On applique le lock que l'on veut */ /* Apply the wanted lock */ E("Maitre: LOCK"); write(dp.fd, phraseTest, strlen(phraseTest)); lockWholeFile(&request); if (fcntl(dp.fd, F_SETLK, &request) < 0) { perror("Master: can't set lock\n"); perror("Echec\n"); exit(0); } E("Maitre"); etat = SYNC; continue; case BYTELOCK_READ: bl = 1; request.l_type = F_RDLCK; etat = SYNC; continue; case BYTELOCK_WRITE: bl = 1; request.l_type = F_WRLCK; etat = SYNC; continue; case BYTELOCK: /* * L'idee est de faire locker l'ensemble du fichier octet par octet par un ensemble de sous processus * Il nous faut donc * -creer un fichier ayant autant d'octet que le nombre de processus passe en parametres * -passer les sections a locker a chacun des esclaves * -verifier que les locks sont bien appliques * */ /* The main idea is to lock all the bytes in a file. Each slave process locks one byte. * * We need : * - To create a file of a length equal to the total number of slave processes * - send the exact section to lock to each slave * - ensure locks have been correctly set */ /* On cree une chaine de caracteres a enregistrer dans le fichier qui contienne exactement un octet par * processus. */ /* Create a string to record in the test file. Length is exactly the number of sub process */ P("Maitre: BYTELOCK: %d\n", etat); buf = malloc(clnt * (maxClients + 1)); memset(buf, '*', clnt); write(dp.fd, buf, clnt); free(buf); /* Chaque processus esclave reecrit son champs a locker. */ /* Each slave process re-writes its own field to lock */ request.l_whence = SEEK_SET; request.l_start = 0; request.l_len = 1; /* On commence par les envois reseau */ /* Start to send sections to lock to remote process (network clients) */ for (i = 0; i < maxClients; i++) { /* On renseigne la structure avec le lock qui va bien */ /* Set the correct byte to lock */ offset = (i + 1) * clnt; request.l_start = (off_t) offset; serverSendLockClient(&request, i); } /* Puis les envois locaux */ /* Now send sections to local processes */ for (i = 0; i < clnt; i++) { request.l_start = i; serverSendLockLocal(&request, i); } etat = RESULTAT; continue; case SYNC: sendLockTest(&tLock); if (bl) { etat = BYTELOCK; continue; } if (n < MAXTEST + 1) etat = RESULTAT; else etat = FIN; continue; case RESULTAT: /* On lit les resultats un par un */ /* Read results by one */ getResults(n - 1); if (bl) validationResultats(n - 1); etat = CLEAN; continue; case CLEAN: /* On demande aux clients d'arreter le test */ /* Ask the clients to stop testing ... */ tLock.test = CLEAN; serialiseTLock(&tLock); serverSend(); /* ... et on attend un accuse de reception avant de fermer */ /* ... and wait for an ack before closing */ serverReceive(); /* On ignore les resultats, ce n'est qu'un accuse de reception */ /* Ignore message content : that is only an ack */ /* close and open file */ close(dp.fd); initTest(dp); etat = SELECT; continue; case FIN: tLock.test = FIN; serialiseTLock(&tLock); serverSend(); sleep(2); break; printf("(end)\n"); exit(0); } /* switch */ break; } /* while */ rapport(clnt); }
int sendLockTest(struct s_test *tLock) { serialiseTLock(tLock); serverSend(); return 0; }
//use this if you are client int UdpSocket::clientSend(const void* buf, int bufLen) { return serverSend(buf,bufLen,&m_sockAddr,sizeof(m_sockAddr)); }