Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NetworkVirtualFileProvider.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if !SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
4 using System;
5 using System.IO;
6 
7 using SiliconStudio.Core.Serialization;
8 using SiliconStudio.Core.IO;
9 
10 namespace SiliconStudio.Paradox.Engine.Network
11 {
13  {
14  public string Url { get; set; }
15  }
16 
18  {
19  public string Url { get; set; }
20  }
21 
23  {
24  public bool FileExists { get; set; }
25  }
26 
28  {
29  public byte[] Data { get; set; }
30  }
31 
32  public class UploadFilePacket
33  {
34  public string Url { get; set; }
35  public byte[] Data { get; set; }
36  }
37 
39  {
40  private SocketContext socketContext;
41 
42  public NetworkVirtualFileProvider(SocketContext socketContext, string rootPath, string remoteUrl) : base(rootPath)
43  {
44  this.socketContext = socketContext;
45  RemoteUrl = remoteUrl;
46  if (!RemoteUrl.EndsWith(VirtualFileSystem.DirectorySeparatorChar.ToString()))
47  RemoteUrl += VirtualFileSystem.DirectorySeparatorChar;
48  }
49 
50  public string RemoteUrl { get; private set; }
51 
52  public static void RegisterServer(SocketContext socketContext)
53  {
54  socketContext.AddPacketHandler<DownloadFileQuery>(
55  async (packet) =>
56  {
57  var stream = await VirtualFileSystem.OpenStreamAsync(packet.Url, VirtualFileMode.Open, VirtualFileAccess.Read);
58  var data = new byte[stream.Length];
59  await stream.ReadAsync(data, 0, data.Length);
60  stream.Close();
61  socketContext.Send(new DownloadFileAnswer { StreamId = packet.StreamId, Data = data });
62  });
63 
64  socketContext.AddPacketHandler<UploadFilePacket>(
65  async (packet) =>
66  {
67  var stream = await VirtualFileSystem.OpenStreamAsync(packet.Url, VirtualFileMode.Create, VirtualFileAccess.Write);
68  await stream.WriteAsync(packet.Data, 0, packet.Data.Length);
69  stream.Close();
70  });
71 
72  socketContext.AddPacketHandler<FileExistsQuery>(
73  async (packet) =>
74  {
75  var fileExists = await VirtualFileSystem.FileExistsAsync(packet.Url);
76  socketContext.Send(new FileExistsAnswer { StreamId = packet.StreamId, FileExists = fileExists });
77  });
78  }
79 
80  public override string GetAbsolutePath(string path)
81  {
82  return RemoteUrl + path;
83  }
84 
85  public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
86  {
87  switch (access)
88  {
89  case VirtualFileAccess.Write:
90  return new NetworkWriteStream(socketContext, RemoteUrl + url);
91  case VirtualFileAccess.Read:
92  var downloadFileAnswer = (DownloadFileAnswer)socketContext.SendReceiveAsync(new DownloadFileQuery { Url = RemoteUrl + url }).Result;
93  return new MemoryStream(downloadFileAnswer.Data);
94  default:
95  throw new NotSupportedException();
96  }
97  }
98 
99  public override bool FileExists(string url)
100  {
101  var fileExistsAnswer = (FileExistsAnswer)socketContext.SendReceiveAsync(new FileExistsQuery { Url = RemoteUrl + url }).Result;
102  return fileExistsAnswer.FileExists;
103  }
104 
105  internal class NetworkWriteStream : VirtualFileStream
106  {
107  private string url;
108  private SocketContext socketContext;
109  private MemoryStream memoryStream;
110 
111  public NetworkWriteStream(SocketContext socketContext, string url)
112  : base(new MemoryStream())
113  {
114  this.memoryStream = (MemoryStream)InternalStream;
115  this.url = url;
116  this.socketContext = socketContext;
117  }
118 
119  protected override void Dispose(bool disposing)
120  {
121  socketContext.Send(new UploadFilePacket { Url = url, Data = memoryStream.ToArray() });
122  base.Dispose(disposing);
123  }
124  }
125  }
126 }
127 #endif
Virtual abstraction over a file system. It handles access to files, http, packages, path rewrite, etc...
NetworkVirtualFileProvider(SocketContext socketContext, string rootPath, string remoteUrl)
override bool FileExists(string url)
Determines whether the specified path points to an existing file. The path.
Abstract base class for IVirtualFileProvider.
VirtualFileShare
File share capabilities, equivalent of System.IO.FileShare.
VirtualFileAccess
File access equivalent of System.IO.FileAccess.
VirtualFileMode
File mode equivalent of System.IO.FileMode.
override string GetAbsolutePath(string path)
Gets the absolute path for the specified local path from this provider. The path local to this instan...
A multithreaded wrapper over a Stream, used by the VirtualFileSystem. It also allows restricted acces...
StreamFlags
Describes the different type of streams.
Definition: StreamFlags.cs:11
override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share=VirtualFileShare.Read, StreamFlags streamFlags=StreamFlags.None)
Opens a Stream from the specified path. The path.The mode.The access.The process sharing mode...