Fixed propulsion applying force to _every limb except_ the ones it's supposed to, fixed pressure building up in enclosed rooms that are full of water, fixed netlobby displaying multiple votes for clients, progress on file transfer

This commit is contained in:
Regalis
2016-02-26 22:21:00 +02:00
parent 77d3d22810
commit 9f8f4e290e
19 changed files with 330 additions and 133 deletions
@@ -1,11 +1,14 @@
using Lidgren.Network;
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Barotrauma.Networking
{
class FileStreamReceiver : IDisposable
{
const int MaxFileSize = 1000000;
public delegate void OnFinished(FileStreamReceiver fileStreamReceiver);
private OnFinished onFinished;
@@ -18,12 +21,17 @@ namespace Barotrauma.Networking
private string filePath;
private FileTransferType fileType;
public string FileName
{
get;
private set;
}
public string FilePath
{
get { return filePath; }
}
public ulong FileSize
{
@@ -35,12 +43,23 @@ namespace Barotrauma.Networking
get { return received; }
}
public FileTransferType FileType
{
get { return fileType; }
}
public FileTransferStatus Status
{
get;
private set;
}
public string ErrorMessage
{
get;
private set;
}
public float BytesPerSecond
{
get;
@@ -49,13 +68,13 @@ namespace Barotrauma.Networking
public float Progress
{
get { return length / (float)received; }
get { return (float)received / (float)length; }
}
public FileStreamReceiver(NetClient client, string filePath, FileTransferType fileType, OnFinished onFinished)
{
client = client;
this.client = client;
this.filePath = filePath;
this.fileType = fileType;
@@ -78,8 +97,75 @@ namespace Barotrauma.Networking
}
}
private bool ValidateInitialData(byte type, string fileName, ulong fileSize)
{
if (fileSize > MaxFileSize)
{
ErrorMessage = "File too large (" + MathUtils.GetBytesReadable((long)fileSize) + ")";
Status = FileTransferStatus.Error;
return false;
}
if (type != (byte)fileType)
{
ErrorMessage = "Unexpected file type ''"+type+"'' (expected "+fileType+")";
Status = FileTransferStatus.Error;
return false;
}
if (!Regex.Match(fileName, @"^[\w\- ]+[\w\-. ]*$").Success)
{
ErrorMessage = "Illegal characters in file name ''"+fileName+"''";
Status = FileTransferStatus.Error;
return false;
}
switch (type)
{
case (byte)FileTransferType.Submarine:
if (Path.GetExtension(fileName) != ".sub")
{
ErrorMessage = "Wrong file extension ''" + Path.GetExtension(fileName)+"''! (Expected .sub)";
Status = FileTransferStatus.Error;
return false;
}
break;
}
return true;
}
public void DeleteFile()
{
string file = Path.Combine(filePath, FileName);
writeStream.Flush();
writeStream.Close();
writeStream.Dispose();
writeStream = null;
Status = FileTransferStatus.Canceled;
if (File.Exists(file))
{
try
{
File.Delete(file);
}
catch (Exception e)
{
DebugConsole.ThrowError("Couldn't delete file ''" + file + "''!", e);
}
}
}
private void TryReadMessage(NetIncomingMessage inc)
{
if (Status == FileTransferStatus.Error ||
Status == FileTransferStatus.Finished ||
Status == FileTransferStatus.Canceled) return;
//int chunkLen = inc.LengthBytes;
if (length == 0)
{
@@ -90,14 +176,17 @@ namespace Barotrauma.Networking
}
byte fileTypeByte = inc.ReadByte();
if (fileTypeByte != (byte)fileType)
{
Status = FileTransferStatus.Error;
return;
}
length = inc.ReadUInt64();
FileName = inc.ReadString();
if (!ValidateInitialData(fileTypeByte, FileName, length))
{
Status = FileTransferStatus.Error;
if (onFinished != null) onFinished(this);
return;
}
writeStream = new FileStream(Path.Combine(filePath, FileName), FileMode.Create, FileAccess.Write, FileShare.None);
timeStarted = Environment.TickCount;
@@ -106,6 +195,15 @@ namespace Barotrauma.Networking
return;
}
if (received + (ulong)inc.LengthBytes > length*1.1f)
{
ErrorMessage = "Receiving more data than expected (> " + MathUtils.GetBytesReadable((long)(received + (ulong)inc.LengthBytes)) + ")";
Status = FileTransferStatus.Error;
if (onFinished != null) onFinished(this);
return;
}
byte[] all = inc.ReadBytes(inc.LengthBytes - inc.PositionInBytes);
received += (ulong)all.Length;
writeStream.Write(all, 0, all.Length);
@@ -117,6 +215,7 @@ namespace Barotrauma.Networking
Status = FileTransferStatus.Receiving;
if (received >= length)
{
Status = FileTransferStatus.Finished;
@@ -133,9 +232,12 @@ namespace Barotrauma.Networking
protected virtual void Dispose(bool disposing)
{
writeStream.Flush();
writeStream.Close();
writeStream.Dispose();
if (writeStream != null)
{
writeStream.Flush();
writeStream.Close();
writeStream.Dispose();
}
}
}