bool FNetworkFileServerClientConnection::ProcessPayload(FArchive& Ar)
{
	FBufferArchive Out;
	bool Result = true;

	// first part of the payload is always the command
	uint32 Cmd;
	Ar << Cmd;

	UE_LOG(LogFileServer, Verbose, TEXT("Processing payload with Cmd %d"), Cmd);

	// what type of message is this?
	NFS_Messages::Type Msg = NFS_Messages::Type(Cmd);

	// make sure the first thing is GetFileList which initializes the game/platform
	checkf(Msg == NFS_Messages::GetFileList || Msg == NFS_Messages::Heartbeat || Sandbox != NULL, TEXT("The first client message MUST be GetFileList, not %d"), (int32)Msg);

	// process the message!
	bool bSendUnsolicitedFiles = false;

	{
		FScopeLock SocketLock(&SocketCriticalSection);

		switch (Msg)
		{
		case NFS_Messages::OpenRead:
			ProcessOpenFile(Ar, Out, false);
			break;

		case NFS_Messages::OpenWrite:
			ProcessOpenFile(Ar, Out, true);
			break;

		case NFS_Messages::Read:
			ProcessReadFile(Ar, Out);
			break;

		case NFS_Messages::Write:
			ProcessWriteFile(Ar, Out);
			break;

		case NFS_Messages::Seek:
			ProcessSeekFile(Ar, Out);
			break;

		case NFS_Messages::Close:
			ProcessCloseFile(Ar, Out);
			break;

		case NFS_Messages::MoveFile:
			ProcessMoveFile(Ar, Out);
			break;

		case NFS_Messages::DeleteFile:
			ProcessDeleteFile(Ar, Out);
			break;

		case NFS_Messages::GetFileInfo:
			ProcessGetFileInfo(Ar, Out);
			break;

		case NFS_Messages::CopyFile:
			ProcessCopyFile(Ar, Out);
			break;

		case NFS_Messages::SetTimeStamp:
			ProcessSetTimeStamp(Ar, Out);
			break;

		case NFS_Messages::SetReadOnly:
			ProcessSetReadOnly(Ar, Out);
			break;

		case NFS_Messages::CreateDirectory:
			ProcessCreateDirectory(Ar, Out);
			break;

		case NFS_Messages::DeleteDirectory:
			ProcessDeleteDirectory(Ar, Out);
			break;

		case NFS_Messages::DeleteDirectoryRecursively:
			ProcessDeleteDirectoryRecursively(Ar, Out);
			break;

		case NFS_Messages::ToAbsolutePathForRead:
			ProcessToAbsolutePathForRead(Ar, Out);
			break;

		case NFS_Messages::ToAbsolutePathForWrite:
			ProcessToAbsolutePathForWrite(Ar, Out);
			break;

		case NFS_Messages::ReportLocalFiles:
			ProcessReportLocalFiles(Ar, Out);
			break;

		case NFS_Messages::GetFileList:
			Result = ProcessGetFileList(Ar, Out);
			break;

		case NFS_Messages::Heartbeat:
			ProcessHeartbeat(Ar, Out);
			break;

		case NFS_Messages::SyncFile:
			ProcessSyncFile(Ar, Out);
			bSendUnsolicitedFiles = true;
			break;

		case NFS_Messages::RecompileShaders:
			ProcessRecompileShaders(Ar, Out);
			break;

		default:

			UE_LOG(LogFileServer, Error, TEXT("Bad incomming message tag (%d)."), (int32)Msg);
		}
	}


	// send back a reply if the command wrote anything back out
	if (Out.Num() && Result )
	{
		int32 NumUnsolictedFiles = UnsolictedFiles.Num();
		if (bSendUnsolicitedFiles)
		{
			Out << NumUnsolictedFiles;
		}

		UE_LOG(LogFileServer, Verbose, TEXT("Returning payload with %d bytes"), Out.Num());

		// send back a reply
		Result &= SendPayload( Out );

		if (bSendUnsolicitedFiles && Result )
		{
			for (int32 Index = 0; Index < NumUnsolictedFiles; Index++)
			{
				FBufferArchive OutUnsolicitedFile;
				PackageFile(UnsolictedFiles[Index], OutUnsolicitedFile);

				UE_LOG(LogFileServer, Display, TEXT("Returning unsolicited file %s with %d bytes"), *UnsolictedFiles[Index], OutUnsolicitedFile.Num());

				Result &= SendPayload(OutUnsolicitedFile);
			}

			UnsolictedFiles.Empty();
		}
	}

	UE_LOG(LogFileServer, Verbose, TEXT("Done Processing payload with Cmd %d Total Size sending %d "), Cmd,Out.TotalSize());

	return Result;
}
Esempio n. 2
0
int main(int argc , char *argv[])
{
    //Parse info
    if (argc < 4) {
        std::cout << "Format command invalid\n";
        std::cout << "./client \"UploadFile\" (host) (port) (source file) (destination file)\n";
        std::cout << "./client \"DownloadFile\" (host) (port) (source file) (destination file)\n";
		std::cout << "./client \"UploadDirectory\" (host) (port) (source dir) (destination dir)\n";
		std::cout << "./client \"GetListEntry\" (host) (port) (path directory)\n";
		std::cout << "./client \"DeleteDirectory\" (host) (port) (path directory)\n";
		std::cout << "./client \"GetFileSize\" (host) (port) (path file)\n";
		std::cout << "./client \"ReadFile\" (host) (port) (path file) (offset) (buff_size)\n";
		std::cout << "./client \"WriteFile\" (host) (port) (path file) (offset) (content)\n";
        return 0;
    }
    std::string command = argv[1];
    std::string host = argv[2];
    std::string port = argv[3];

    //Connect server
    int sock;
    struct sockaddr_in server;

    //Create socket
    sock = socket(AF_INET , SOCK_STREAM , 0);
    if (sock == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    server.sin_addr.s_addr = inet_addr(host.c_str());
    server.sin_family = AF_INET;
    server.sin_port = htons( atoi(port.c_str()) );

    //Connect to remote server
    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
        return 1;
    }
    puts("Connected\n");

    //
    if (command.compare("UploadFile") == 0) {
        if (argc < 6) {
            std::cout << "Format command invalid\n";
            return 0;
        }
        std::string sourceFile = argv[4];
        std::string destinationFile = argv[5];
        ProcessUploadFile(sock, sourceFile, destinationFile);
    } else if (command.compare("DownloadFile") == 0) {
        if (argc < 6) {
            std::cout << "Format command invalid\n";
            return 0;
        }
        std::string sourceFile = argv[4];
        std::string destinationFile = argv[5];
        ProcessDownloadFile(sock, sourceFile, destinationFile);
	} else if (command.compare("UploadDirectory") == 0) {
		if (argc < 6) {
			std::cout << "Format command invalid\n";
			return 0;
		}
		std::string sourceDir = argv[4];
		std::string destinationDir = argv[5];
		ProcessUploadDir(host, port, sourceDir, destinationDir);
	} else if (command.compare("GetListEntry") == 0) {
		if (argc < 5) {
			std::cout << "Format command invalid\n";
			return 0;
		}
		std::string pathDir = argv[4];
		ProcessGetListEntry(sock, pathDir);
	}  else if (command.compare("DeleteDirectory") == 0) {
		if (argc < 5) {
			std::cout << "Format command invalid\n";
			return 0;
		}
		std::string pathDir = argv[4];
		ProcessDeleteDir(sock, pathDir);
	}  else if (command.compare("GetFileSize") == 0) {
		if (argc < 5) {
			std::cout << "Format command invalid\n";
			return 0;
		}
		std::string pathFile = argv[4];
		ProcessGetFileSize(sock, pathFile);
	}  else if (command.compare("ReadFile") == 0) {
		if (argc < 7) {
			std::cout << "Format command invalid\n";
			return 0;
		}
		std::string pathFile = argv[4];
		size_t offset = atoi(argv[5]);
		size_t buff_size = atoi(argv[6]);
		ProcessReadFile(sock, pathFile, offset, buff_size);
	}  else if (command.compare("WriteFile") == 0) {
		if (argc < 7) {
			std::cout << "Format command invalid\n";
			return 0;
		}
		std::string pathFile = argv[4];
		size_t offset = atoi(argv[5]);
		std::string buffer = argv[6];
		ProcessWriteFile(sock, pathFile, offset, buffer);
	}

    close(sock);
    return 0;
}