#include "CRM32Pro.h"
#define EXAMPLE_VERSION "Example 11: INetwork on Authoritative mode"
void ExecuteClient(char *server,char *myname);
void ExecuteServer();
int ReadLine(void *);
void PrintHelp(char *sErrMsg = NULL);
int myCoreServer(void *data,int size);
int done;
int main(int argc,char **argv)
{
Uint8 bMode = 0;
if(argc < 2)
{
PrintHelp();
return 1;
}
if(!strcmp(argv[1],"-s"))
{
bMode = 1;
}
else if(!strcmp(argv[1],"-c"))
{
if(argc > 2)
{
bMode = 2;
}
}
if(bMode == 0)
{
PrintHelp("\n Error: invalid arguments\n");
return 1;
}
{
ILogSystem.
Msg(
LOG_NORMAL,
" · [LOG] - Could not initialize CRM32Pro: %s\n",SDL_GetError());
return 1;
}
{
ILogSystem.
Msg(
LOG_NORMAL,
" · [LOG] - Could not initialize INetworkSystem\n");
return 1;
}
if(bMode == 1) ExecuteServer();
else
{
if(argc > 3) ExecuteClient(argv[2],argv[3]);
else ExecuteClient(argv[2],"Anonymous");
}
return 0;
}
void ExecuteClient(char *server,char *myname)
{
char *buff = NULL;
Uint32 size = 0, result = 0;
SDL_Thread *ReadLineT = NULL;
if(!INetwork->
ConnectTo(server,2220,myname,1001))
return;
ReadLineT = SDL_CreateThread(ReadLine,NULL);
done = 0;
while(!done)
{
switch(result)
{
puts("Error detected!");
puts("Closing application. Press ENTER to finish");
done = 1;
SDL_WaitThread(ReadLineT,NULL);
ReadLineT = NULL;
break;
break;
break;
break;
break;
break;
break;
break;
}
SDL_Delay(250);
}
}
#define MAXLEN (1*1024) // 1 KB - adequate for text!
int ReadLine(void *data)
{
char message[MAXLEN];
Uint32 len = 0, nc, i;
char *str;
strcpy(message,"");
while(!done)
{
strcpy(message,"");
fgets(message,1024,stdin);
len = (int)strlen(message);
SDL_Delay(250);
if((len) && (!done))
{
message[len - 1] = '\0';
#define HOW_KB 32*1024
else if(message[0] == '4')
{
len = HOW_KB;
str = new char[len];
memset(str,0,len);
ILogSystem.
Msg(
LOG_NORMAL,
"[Sending a block of %dKb raw data]\n",len / 1024);
delete str;
}
else if(message[0] == '5')
{
for(i = 0; i < nc; i++)
{
ILogSystem.
Msg(
LOG_NORMAL,
"Client '%s' - IP '%d.%d.%d.%d' - Current %dms - Average %dms.\n",
cInfo[i].szName,cInfo[i].IP >> 24,(cInfo[i].IP >> 16) & 0xff, (cInfo[i].IP >> 8) & 0xff,
cInfo[i].IP & 0xff, cInfo[i].iLatency, cInfo[i].iLatencyAvg);
}
}
else if(message[0] == '6')
{
}
else if(message[0] == '7')
{
ILogSystem.
Msg(
LOG_NORMAL,
"[Sending a text line of %d bytes bypassing the CoreServer]\n",len);
}
else
{
ILogSystem.
Msg(
LOG_NORMAL,
"[Sending a text line of %d bytes]\n",len);
}
}
}
return 1;
}
void ExecuteServer()
{
}
int myCoreServer(void *data,int size)
{
if(!strcmp((const char*)data,"hello")) return 0;
if(!strcmp((const char*)data,"bye")) return 0;
return 1;
}
void PrintHelp(char *sErrMsg)
{
printf("-----------------------------------------------------------\n");
printf(" CRM32Pro SDK - HelpScreen\n %s\n",EXAMPLE_VERSION);
printf("-----------------------------------------------------------\n\n");
printf(" Use:\n");
printf(" - 'NetChat2 -s' to run as server\n");
printf(" - 'NetChat2 -c ServerIP [my_name]' to connect to a server\n\n");
printf(" Once you are connected to a server, you can press [ENTER]\n");
printf(" key to send any text to others clients.\n");
printf(" There are a few words that are reserved internally:\n");
printf(" > '1' the client wants to exit\n");
printf(" > '2' the client wants to close the server\n");
printf(" > '3' the client has asked for info about others clients(including itself)\n");
printf(" > '4' the client sends a raw block data of %dKB\n",HOW_KB/1024);
printf(" > '5' the client dumps the local clients information\n");
printf(" > '6' the client shows the current statistics\n");
if(sErrMsg != NULL) printf("%s",sErrMsg);
else printf("\n");
puts("Press [ENTER] key to continue");
getchar();
}