[Cruce-commit] SF.net SVN: cruce:[44] C/trunk
Status: Beta
Brought to you by:
caiusb
|
From: <ali...@us...> - 2010-03-19 11:01:21
|
Revision: 44
http://cruce.svn.sourceforge.net/cruce/?rev=44&view=rev
Author: alinposho
Date: 2010-03-19 11:01:09 +0000 (Fri, 19 Mar 2010)
Log Message:
-----------
Am modificat functia unpad(message) - Acum functioneaza.
Mici modificari la server. Acum ajnge pana la faza in care un jucator trebuie sa dea start game.
Modified Paths:
--------------
C/trunk/protocol/protocol.c
C/trunk/server/server.c
Modified: C/trunk/protocol/protocol.c
===================================================================
--- C/trunk/protocol/protocol.c 2010-03-18 18:26:35 UTC (rev 43)
+++ C/trunk/protocol/protocol.c 2010-03-19 11:01:09 UTC (rev 44)
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
+#include <stdlib.h>
#include "protocol.h"
@@ -15,10 +16,9 @@
code = atoi(message);
strcpy(message, message + 2);
}
- printf("Decoded this message:\n");
+ printf("\nDecoded this package:\n");
printf("code: %d\n", code);
- printf("message: %s\n", message);
- printf("---end---\n");
+ printf("message: %s\n\n", message);
return code;
}
@@ -34,8 +34,14 @@
{
int i = strlen(msg) - 1;
for (; i >= 0; i--)
- if (msg[i] != ' ')
+ {
+ if (msg[i] == ' ')
+ {
+ msg[i] = '\0';
+ }
+ else
+ {
break;
-
- msg[i+1] = '\0';
+ }
+ }
}
Modified: C/trunk/server/server.c
===================================================================
--- C/trunk/server/server.c 2010-03-18 18:26:35 UTC (rev 43)
+++ C/trunk/server/server.c 2010-03-19 11:01:09 UTC (rev 44)
@@ -4,7 +4,9 @@
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
+#include <arpa/inet.h>
+
#include "server.h"
#include "../protocol/protocol.h"
@@ -16,6 +18,8 @@
struct sockaddr_in addr;
char ip[16];
+
+
int port;
int sfd;
@@ -33,6 +37,15 @@
}
}
+/**
+ * Prints the message to the output file making sure to flush the buffer
+ */
+void displayInfoMessage(FILE* infoMessageOutput, char *message)
+{
+ fprintf(infoMessageOutput, "%s", message);
+ fflush(infoMessageOutput);
+}
+
/** reads the config file and sets ip and port **/
void readConfig()
{
@@ -48,6 +61,7 @@
perror("Eroare creare socket");
exit(EXIT_FAILURE);
}
+ return s;
}
void bindAndListen()
@@ -202,7 +216,7 @@
switch(code)
{
- case JOIN_GAME:
+ case JOIN_GAME://msg va contine numele jucatorului care a dat join
{
char buffer[200];
sprintf(buffer,"%s joined the game\n",msg);
@@ -216,9 +230,12 @@
{
if(echipe[i].jucatori[j].sfd != fd)
{
- char message[100];
- //sprintf(message, "%s s-a alaturat joucului!\n", jucator.nume);
- sendInfoMessage(echipe[i].jucatori[i].sfd, message);
+ if(echipe[i].jucatori[i].sfd != -1)
+ {
+ char message[100];
+ sprintf(message, "%s s-a alaturat joucului!\n", msg);
+ sendInfoMessage(echipe[i].jucatori[i].sfd, message);
+ }
}
else
echipe[i].jucatori[j].joined = true;
@@ -311,7 +328,7 @@
printf("Message from player %d of team %d ready\n",j,i);
}
if (!bla)
- return; /* daca nu a venit mesaj ies din functie */
+ return 0; /* daca nu a venit mesaj ies din functie */
else
break; /* daca a venit il tratez */
}
@@ -319,21 +336,25 @@
while(1);
/* a venit mesajul de la cine trebe */
- int read_rt;
+ int messageLength;
- read_rt = read(fd,msg,MSG_SIZE);
- printf("Message read. It has %d bytes\n",read_rt);
- if (read_rt == 0)
+ messageLength = read(fd,msg,MSG_SIZE);
+ char infoMSG[1024];
+ sprintf(infoMSG, "Message read. It has %d bytes\n",messageLength);
+ //printf("Message read. It has %d bytes\n",messageLength);
+ displayInfoMessage(stdout, infoMSG);
+
+ if (messageLength == 0)
{
printf("Connection closed\n");
leave_game();
}
- if (read_rt != MSG_SIZE);
+ if (messageLength != MSG_SIZE)
{
displayInfoMessage(stdout, "Warning: too few bytes read\n");
}
- printf("%s\n",msg);
+
//scanf("%*c");
int code = decode_message(msg);
@@ -343,12 +364,8 @@
}
-void displayInfoMessage(FILE* infoMessageOutput, char message[])
-{
- fprintf(infoMessageOutput, "%s", message);
- fflush(infoMessageOutput);
-}
+
int main(int argc, char *argv[])
{
displayInfoMessage(stdout, "Server started!\n");
@@ -378,15 +395,23 @@
getMSG(NULL,ex_msg);
}
- printf("All players are connected\n");
+ sprintf(infoMessage, "All players are connected\n");
+ displayInfoMessage(stdout, infoMessage);
while(!joinGame())
getMSG(NULL,NULL);
- printf("All players have joined\n");
+ sprintf(infoMessage, "All players have joined\n");
+ displayInfoMessage(stdout, infoMessage);
while(!startGame())
getMSG(NULL,NULL);
- printf("Game has started\n");
+ sprintf(infoMessage, "Game has started\n");
+ displayInfoMessage(stdout, infoMessage);
+
+ displayInfoMessage(stdout, "Press any key to close server\n" );
+ getchar();
+
+ return 1;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|