Multiplayer: We can now send files from server to all clients. I just send two dummy files (Items.xml, German.Items.xml) from server to all clients and save them on the client in the JA2 root directory.

The files are send as soon as the client connects.

git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@2640 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
Wanne
2009-03-29 18:17:33 +00:00
parent 24d426c7d9
commit bb6e7c4521
5 changed files with 185 additions and 156 deletions
+111 -3
View File
@@ -77,6 +77,13 @@
#include "RakNetStatistics.h"
#include "RakNetTypes.h"
#include "FileListTransfer.h"
#include "FileListTransferCBInterface.h"
#include "FileOperations.h"
#include "SuperFastHash.h"
#include "RakAssert.h"
#include "IncrementalReadInterface.h"
#include "BitStream.h"
#include <assert.h>
#include <cstdio>
@@ -92,6 +99,86 @@
#include "tactical placement gui.h"
#include "prebattle interface.h"
// WANNE: FILE TRANSFER
STRING512 executableDirectory;
class ClientTransferCB : public FileListTransferCBInterface
{
public:
// This method gets called when all the data is received on the client.
// Now the file will be saved on the client
bool OnFile(OnFileStruct *onFileStruct)
{
STRING512 targetFileName;
ScreenMsg( FONT_BCOLOR_ORANGE, MSG_CHAT, L"(100%%) Got file from server: %S", onFileStruct->fileName);
//ScreenMsg( FONT_BCOLOR_ORANGE, MSG_CHAT, L"%i. (100%%) %i/%i %S %ib->%ib / %ib->%ib\n", onFileStruct->setID, onFileStruct->fileIndex+1, onFileStruct->setCount, onFileStruct->fileName, onFileStruct->compressedTransmissionLength, onFileStruct->finalDataLength, onFileStruct->setTotalCompressedTransmissionLength, onFileStruct->setTotalFinalLength);
// Save the file in the clients executable dir!
strcpy(targetFileName, executableDirectory);
strcat(targetFileName, "\\");
// Only get the filename
char* file = ExtractFilename(onFileStruct->fileName);
strcat(targetFileName, file);
FILE *fp = fopen(targetFileName, "wb");
fwrite(onFileStruct->fileData, onFileStruct->finalDataLength, 1, fp);
fclose(fp);
/*
// Make sure it worked
unsigned int hash1 = SuperFastHashFile(fileToSend);
if (RakNet::BitStream::DoEndianSwap())
RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &hash1, sizeof(hash1));
unsigned int hash2 = SuperFastHashFile(targetFileName);
if (RakNet::BitStream::DoEndianSwap())
RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &hash2, sizeof(hash2));
RakAssert(hash1==hash2);
*/
ScreenMsg( FONT_BCOLOR_ORANGE, MSG_CHAT, L"Saved file local in %S", targetFileName);
// Return true to have RakNet delete the memory allocated to hold this file.
// False if you hold onto the memory, and plan to delete it yourself later
return true;
}
virtual void OnFileProgress(OnFileStruct *onFileStruct,unsigned int partCount,unsigned int partTotal,unsigned int partLength, char *firstDataChunk)
{
//ScreenMsg( FONT_BLUE, MSG_CHAT, L"(%i%%) %S", 100*partCount/partTotal, onFileStruct->fileName);
//ScreenMsg( FONT_BLUE, MSG_CHAT, L"%i (%i%%) %i/%i %S %ib->%ib / %ib->%ib\n", onFileStruct->setID, 100*partCount/partTotal, onFileStruct->fileIndex+1, onFileStruct->setCount, onFileStruct->fileName, onFileStruct->compressedTransmissionLength, onFileStruct->finalDataLength, onFileStruct->setTotalCompressedTransmissionLength, onFileStruct->setTotalFinalLength, firstDataChunk);
//printf("%i (%i%%) %i/%i %s %ib->%ib / %ib->%ib\n", onFileStruct->setID, 100*partCount/partTotal, onFileStruct->fileIndex+1, onFileStruct->setCount, onFileStruct->fileName, onFileStruct->compressedTransmissionLength, onFileStruct->finalDataLength, onFileStruct->setTotalCompressedTransmissionLength, onFileStruct->setTotalFinalLength, firstDataChunk);
}
virtual bool OnDownloadComplete(void)
{
//printf("Download complete.\n");
ScreenMsg( FONT_RED, MSG_CHAT, L"Download complete");
// Returning false automatically deallocates the automatically allocated handler that was created by DirectoryDeltaTransfer
return false;
}
private:
char *ExtractFilename(char *pathname)
{
char *s;
if ((s=strrchr(pathname, '\\')) != NULL) s++;
else if ((s=strrchr(pathname, '/')) != NULL) s++;
else if ((s=strrchr(pathname, ':')) != NULL) s++;
else s = pathname;
return s;
}
} transferCallback;
unsigned char GetPacketIdentifier(Packet *p);
unsigned char packetIdentifier;
@@ -124,6 +211,11 @@ unsigned char packetIdentifier;
extern INT8 SquadMovementGroups[ ];
RakPeerInterface *client;
// WANNE: FILE TRANSFER
FileListTransfer fltClient; // flt2
ClientTransferCB transferCBClient;
typedef struct
{
UINT8 ubProfileID;
@@ -326,6 +418,9 @@ UINT32 iCCStartGameTime = 0;
bool is_game_started = false;
// OJW - added 20081130
// <TODO> - add retry timer and notification
void NetworkAutoStart()
@@ -1968,6 +2063,7 @@ void overide_callback( UINT8 ubResult )
}
}
void requestSETTINGS(void)
{
client_info cl_name;
@@ -2201,6 +2297,8 @@ void recieveSETTINGS (RPCParameters *rpcParameters) //recive settings from serve
}
}
void recieveTEAMCHANGE( RPCParameters *rpcParameters )
@@ -3379,7 +3477,12 @@ void connect_client ( void )
{
//ScreenMsg( FONT_LTGREEN, MSG_CHAT, L"Client started, waiting for connections...");
is_client=true;
/*repo=0;*/
GetExecutableDirectory(executableDirectory);
// WANNE: FILE TRANSFER
client->AttachPlugin(&fltClient);
client->SetSplitMessageProgressInterval(1);
}
else
{
@@ -3537,6 +3640,8 @@ void connect_client ( void )
MPDebugMsg( String ( "connect_client()\n" ) );
#endif
}
else if (is_connecting)
@@ -3630,9 +3735,11 @@ void client_packet ( void )
is_connected=true;
is_connecting=false;
requestSETTINGS();
//request_settings();//ask server for game settings...
// WANNE: FILE TRANSFER
setID = fltClient.SetupReceive(&transferCBClient, false, p->systemAddress);
break;
case ID_NEW_INCOMING_CONNECTION:
case ID_NEW_INCOMING_CONNECTION:
//tells server client has connected
ScreenMsg( FONT_LTGREEN, MSG_CHAT, L"ID_NEW_INCOMING_CONNECTION");
break;
@@ -3702,6 +3809,7 @@ void client_disconnect (void)
allowlaptop=false;
// clear local client cache
memset(client_names,0,sizeof(char)*4*30);
memset(client_edges,0,sizeof(int)*4);
+4
View File
@@ -6,6 +6,10 @@ extern char CLIENT_NAME[30];
extern char SERVER_NAME[30];
extern bool Sawarded;
//extern char *fileToSend;
extern unsigned int setID;
//extern char *fileToSendCopy;
typedef struct
{
+69 -5
View File
@@ -5,6 +5,14 @@
#include "RakNetStatistics.h"
#include "RakNetTypes.h"
#include "FileListTransfer.h"
#include "FileListTransferCBInterface.h"
#include "FileOperations.h"
#include "SuperFastHash.h"
#include "RakAssert.h"
#include "IncrementalReadInterface.h"
#include "BitStream.h"
#include "RakSleep.h"
#include <assert.h>
@@ -16,9 +24,27 @@
#include <time.h>
#include <list>
#include "connect.h"
#include "types.h"
#include "gamesettings.h"
//char *fileToSend;
unsigned int setID;
//char *fileToSendCopy;
// Sender progress notification
class TestFileListProgress : public FileListProgress
{
virtual void OnFilePush(const char *fileName, unsigned int fileLengthBytes, unsigned int offset, unsigned int bytesBeingSent, bool done, SystemAddress targetSystem)
{
printf("Sending %s bytes=%i offset=%i\n", fileName, bytesBeingSent, offset);
}
} testFileListProgress;
char kbag[100];
char net_div[30];
INT32 gsMAX_MERCS;
@@ -29,7 +55,7 @@ int gsREPORT_NAME;
#include "text.h"
#include "network.h"
#include "connect.h"
#include "message.h"
#include "overhead.h"
#include "fresh_header.h"
@@ -61,6 +87,16 @@ unsigned char SpacketIdentifier;
RakPeerInterface *server;
// WANNE: FILE TRANSFER
FileListTransfer fltServer; // flt1
IncrementalReadInterface incrementalReadInterface;
FileList fileList;
int numreadyteams;
int readyteamreg[10];
@@ -584,6 +620,9 @@ void requestSETTINGS(RPCParameters *rpcParameters )
server->RPC("recieveSETTINGS",(const char*)&lan, (int)sizeof(settings_struct)*8, HIGH_PRIORITY, RELIABLE, 0, UNASSIGNED_SYSTEM_ADDRESS, true, 0, UNASSIGNED_NETWORK_ID,0);
} // end if(can_joingame)
}
@@ -826,14 +865,35 @@ void start_server (void)
REGISTER_STATIC_RPC(server, sendCHATMSG);
//REGISTER_STATIC_RPC(server, rINT);
//
if (b)
{
ScreenMsg( FONT_LTBLUE, MSG_CHAT, MPServerMessage[1]);
ScreenMsg( FONT_LTBLUE, MSG_CHAT, MPServerMessage[2]);
is_server = true;
// WANNE: FILE TRANSFER
server->AttachPlugin(&fltServer);
server->SetSplitMessageProgressInterval(1);
fltServer.SetCallback(&testFileListProgress);
STRING512 executableDir;
STRING512 fileToSend;
GetExecutableDirectory(executableDir);
// 1st Testfile to send to the client
strcpy(fileToSend, executableDir);
strcat(fileToSend, "\\Data-1.13\\TableData\\Items.xml");
unsigned int fileLength = GetFileLength(fileToSend);
fileList.AddFile(fileToSend, 0, fileLength, fileLength, FileListNodeContext(0,0), true);
// 2nd Testfile to send to the client
strcpy(fileToSend, executableDir);
strcat(fileToSend, "\\Data-1.13\\TableData\\German.Items.xml");
fileLength = GetFileLength(fileToSend);
fileList.AddFile(fileToSend, 0, fileLength, fileLength, FileListNodeContext(0,0), true);
connect_client();//connect client to server
}
else
@@ -917,6 +977,9 @@ void server_packet ( void )
// make sure they can connect
CheckIncomingConnection(p);
//send_settings();//send off server set settings
// TODO.RW: Send the files to the connect clients
fltServer.Send(&fileList,server,p->systemAddress,setID,HIGH_PRIORITY,0,false, &incrementalReadInterface, 5000);
break;
case ID_MODIFIED_PACKET:
// Cheater!
@@ -958,6 +1021,7 @@ void server_disconnect (void)
{
server->Shutdown(300);
is_server = false;
fileList.Clear();
// We're done with the network
RakNetworkFactory::DestroyRakPeerInterface(server);
ScreenMsg( FONT_LTBLUE, MSG_CHAT, MPServerMessage[6]);
@@ -966,4 +1030,4 @@ void server_disconnect (void)
{
ScreenMsg( FONT_LTBLUE, MSG_CHAT, MPServerMessage[7]);
}
}
}
-143
View File
@@ -1,143 +0,0 @@
#include "RakPeerInterface.h"
#include "FileListTransfer.h"
#include "RakSleep.h"
#include "RakNetworkFactory.h"
#include "MessageIdentifiers.h"
#include "FileListTransferCBInterface.h"
#include "FileOperations.h"
#include "SuperFastHash.h"
#include "RakAssert.h"
#include "BitStream.h"
#include "IncrementalReadInterface.h"
#include <stdio.h>
RakNet::RakString file;
RakNet::RakString fileCopy;
//const char *file = "c:/temp/unittest.vcproj";
//const char *fileCopy = "c:/temp/unittest_copy.vcproj";
class TestCB : public FileListTransferCBInterface
{
public:
bool OnFile(
OnFileStruct *onFileStruct)
{
printf("%i. (100%%) %i/%i %s %ib->%ib / %ib->%ib\n", onFileStruct->setID, onFileStruct->fileIndex+1, onFileStruct->setCount, onFileStruct->fileName, onFileStruct->compressedTransmissionLength, onFileStruct->finalDataLength, onFileStruct->setTotalCompressedTransmissionLength, onFileStruct->setTotalFinalLength);
FILE *fp = fopen(fileCopy.C_String(), "wb");
fwrite(onFileStruct->fileData, onFileStruct->finalDataLength, 1, fp);
fclose(fp);
// Make sure it worked
unsigned int hash1 = SuperFastHashFile(file.C_String());
if (RakNet::BitStream::DoEndianSwap())
RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &hash1, sizeof(hash1));
unsigned int hash2 = SuperFastHashFile(fileCopy.C_String());
if (RakNet::BitStream::DoEndianSwap())
RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &hash2, sizeof(hash2));
RakAssert(hash1==hash2);
// Return true to have RakNet delete the memory allocated to hold this file.
// False if you hold onto the memory, and plan to delete it yourself later
return true;
}
virtual void OnFileProgress(OnFileStruct *onFileStruct,unsigned int partCount,unsigned int partTotal,unsigned int partLength, char *firstDataChunk)
{
printf("%i (%i%%) %i/%i %s %ib->%ib / %ib->%ib\n", onFileStruct->setID, 100*partCount/partTotal, onFileStruct->fileIndex+1, onFileStruct->setCount, onFileStruct->fileName, onFileStruct->compressedTransmissionLength, onFileStruct->finalDataLength, onFileStruct->setTotalCompressedTransmissionLength, onFileStruct->setTotalFinalLength, firstDataChunk);
}
virtual bool OnDownloadComplete(void)
{
printf("Download complete.\n");
// Returning false automatically deallocates the automatically allocated handler that was created by DirectoryDeltaTransfer
return false;
}
} transferCallback;
// Sender progress notification
class TestFileListProgress : public FileListProgress
{
virtual void OnFilePush(const char *fileName, unsigned int fileLengthBytes, unsigned int offset, unsigned int bytesBeingSent, bool done, SystemAddress targetSystem)
{
printf("Sending %s bytes=%i offset=%i\n", fileName, bytesBeingSent, offset);
}
} testFileListProgress;
void TestFileTransfer(void)
{
printf("This sample demonstrates incrementally sending a file with\n");
printf("the FileListTransferPlugin. Incremental sends will read and send the.\n");
printf("file only as needed, rather than putting the whole file in memory.\n");
printf("This is to support sending large files to many users at the same time.\n");
printf("Difficulty: Intermediate\n\n");
TestCB testCB;
RakPeerInterface *peer1 = RakNetworkFactory::GetRakPeerInterface();
RakPeerInterface *peer2 = RakNetworkFactory::GetRakPeerInterface();
peer1->Startup(1,0,&SocketDescriptor(60000,0),1);
peer2->Startup(1,0,&SocketDescriptor(60001,0),1);
peer1->SetMaximumIncomingConnections(1);
peer2->Connect("127.0.0.1",60000,0,0,0);
FileListTransfer flt1, flt2;
peer1->AttachPlugin(&flt1);
peer2->AttachPlugin(&flt2);
peer1->SetSplitMessageProgressInterval(1);
peer2->SetSplitMessageProgressInterval(1);
// Print sending progress notifications
flt1.SetCallback(&testFileListProgress);
FileList fileList;
IncrementalReadInterface incrementalReadInterface;
printf("Enter complete filename with path to test:\n");
char str[256];
gets(str);
file=str;
fileCopy=file+"_copy";
// Reference this file, rather than add it in memory. Will send 1000 byte chunks. The reason to do this is so the whole file does not have to be in memory at once
unsigned int fileLength = GetFileLength(file.C_String());
if (fileLength==0)
{
printf("Test file %s not found.\n", file.C_String());
RakNetworkFactory::DestroyRakPeerInterface(peer1);
RakNetworkFactory::DestroyRakPeerInterface(peer1);
return;
}
fileList.AddFile(file.C_String(), 0, fileLength, fileLength, FileListNodeContext(0,0), true);
// Wait for the connection
RakSleep(100);
Packet *packet1, *packet2;
while (1)
{
// Wait for the connection request to be accepted
packet2=peer2->Receive();
if (packet2->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
{
flt2.SetupReceive(&testCB, false, packet2->systemAddress);
peer2->DeallocatePacket(packet2);
break;
}
peer2->DeallocatePacket(packet2);
RakSleep(30);
}
// When connected, send the file. Since the file is a reference, it will be sent incrementally
while (1)
{
packet1=peer1->Receive();
packet2=peer2->Receive();
if (packet1 && packet1->data[0]==ID_NEW_INCOMING_CONNECTION)
flt1.Send(&fileList,peer1,packet1->systemAddress,0,HIGH_PRIORITY,0,false, &incrementalReadInterface, 5000);
peer1->DeallocatePacket(packet1);
peer2->DeallocatePacket(packet2);
RakSleep(30);
}
RakNetworkFactory::DestroyRakPeerInterface(peer1);
RakNetworkFactory::DestroyRakPeerInterface(peer1);
}
+1 -5
View File
@@ -17,7 +17,7 @@
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="\games\jagged alliance 2"
OutputDirectory="C:\games\jagged alliance 2 EN"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
InheritedPropertySheets=".\ja2_2005Express.vsprops;.\ja2_2005ExpressDebug.vsprops"
@@ -817,10 +817,6 @@
/>
</FileConfiguration>
</File>
<File
RelativePath=".\TestCB.cpp"
>
</File>
</Filter>
</Filter>
<File