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();
}
}
}
@@ -6,7 +6,7 @@ namespace Barotrauma.Networking
{
enum FileTransferStatus
{
NotStarted, Sending, Receiving, Finished, Error
NotStarted, Sending, Receiving, Finished, Error, Canceled
}
enum FileTransferType
@@ -22,6 +22,8 @@ namespace Barotrauma.Networking
private byte[] tempBuffer;
private NetConnection connection;
float waitTimer;
private FileTransferType fileType;
@@ -56,7 +58,7 @@ namespace Barotrauma.Networking
chunkLen = connection.Peer.Configuration.MaximumTransmissionUnit - 100;
tempBuffer = new byte[chunkLen];
sentOffset = 0;
FileName = fileName;
this.fileType = fileType;
@@ -64,10 +66,13 @@ namespace Barotrauma.Networking
Status = FileTransferStatus.NotStarted;
}
public void Update()
public void Update(float deltaTime)
{
if (inputStream == null) return;
waitTimer -= deltaTime;
if (waitTimer > 0.0f) return;
if (!connection.CanSendImmediately(NetDeliveryMethod.ReliableOrdered, 1)) return;
// send another part of the file!
@@ -98,6 +103,8 @@ namespace Barotrauma.Networking
connection.SendMessage(message, NetDeliveryMethod.ReliableOrdered, 1);
sentOffset += sendBytes;
waitTimer = connection.AverageRoundtripTime + 0.05f;
//Program.Output("Sent " + m_sentOffset + "/" + m_inputStream.Length + " bytes to " + m_connection);
if (remaining - sendBytes <= 0)
+29 -9
View File
@@ -697,6 +697,25 @@ namespace Barotrauma.Networking
{
base.Draw(spriteBatch);
if (fileStreamReceiver != null &&
(fileStreamReceiver.Status == FileTransferStatus.Receiving || fileStreamReceiver.Status == FileTransferStatus.NotStarted))
{
Vector2 pos = Screen.Selected == GameMain.NetLobbyScreen ?
new Vector2(GameMain.NetLobbyScreen.SubList.Rect.X, GameMain.NetLobbyScreen.SubList.Rect.Bottom+5) : new Vector2(GameMain.GraphicsWidth / 2 - 200, 10);
GUI.DrawString(spriteBatch, pos, "Downloading " + fileStreamReceiver.FileName, Color.White);
GUI.DrawString(spriteBatch, pos + Vector2.UnitX*300,
MathUtils.GetBytesReadable((long)fileStreamReceiver.Received) + " / " + MathUtils.GetBytesReadable((long)fileStreamReceiver.FileSize), Color.White);
GUI.DrawProgressBar(spriteBatch, new Vector2(pos.X, -pos.Y - 20), new Vector2(300, 15), fileStreamReceiver.Progress, Color.Green);
if (GUI.DrawButton(spriteBatch, new Rectangle((int)pos.X + 310, (int)pos.Y + 20, 100, 15), "Cancel", new Color(0.88f, 0.25f, 0.15f, 0.8f)))
{
fileStreamReceiver.DeleteFile();
fileStreamReceiver.Dispose();
fileStreamReceiver = null;
}
}
if (!GameMain.DebugDraw) return;
int width = 200, height = 300;
@@ -711,27 +730,28 @@ namespace Barotrauma.Networking
spriteBatch.DrawString(GUI.SmallFont, "Sent bytes: " + client.Statistics.SentBytes, new Vector2(x + 10, y + 75), Color.White);
spriteBatch.DrawString(GUI.SmallFont, "Sent packets: " + client.Statistics.SentPackets, new Vector2(x + 10, y + 90), Color.White);
if (fileStreamReceiver!=null)
{
GUI.DrawString(spriteBatch, new Vector2(GameMain.GraphicsWidth / 2 - 100, 20), "Downloading "+fileStreamReceiver.FileName, Color.White);
GUI.DrawString(spriteBatch, new Vector2(GameMain.GraphicsWidth / 2 - 100, 20),
MathUtils.GetBytesReadable((long)fileStreamReceiver.Received)+" / "+MathUtils.GetBytesReadable((long)fileStreamReceiver.FileSize), Color.White);
GUI.DrawProgressBar(spriteBatch, new Vector2(GameMain.GraphicsWidth / 2 - 100, 20), new Vector2(200, 15), fileStreamReceiver.Progress, Color.Green);
}
}
private void OnFileReceived(FileStreamReceiver receiver)
{
if (receiver.Status == FileTransferStatus.Error)
{
new GUIMessageBox("Error while receiving file from server", receiver.ErrorMessage);
receiver.DeleteFile();
}
else if (receiver.Status == FileTransferStatus.Finished)
{
new GUIMessageBox("Download finished", "File ''"+receiver.FileName+"'' was downloaded succesfully.");
switch (receiver.FileType)
{
case FileTransferType.Submarine:
Submarine.Preload();
break;
}
}
receiver.Dispose();
fileStreamReceiver = null;
}
+15 -6
View File
@@ -312,9 +312,9 @@ namespace Barotrauma.Networking
foreach (Client c in ConnectedClients)
{
if (c.FileStreamSender!=null)
if (c.FileStreamSender!=null && Rand.Range(0.0f, 1.0f)<0.01f)
{
c.FileStreamSender.Update();
c.FileStreamSender.Update(deltaTime);
if (c.FileStreamSender.Status == FileTransferStatus.Finished ||
c.FileStreamSender.Status == FileTransferStatus.Error)
@@ -516,8 +516,17 @@ namespace Barotrauma.Networking
ReadCharacterData(inc);
break;
case (byte)PacketTypes.RequestFile:
string fileName = inc.ReadString();
if (!allowFileTransfers)
{
var outmsg = server.CreateMessage();
outmsg.Write((byte)PacketTypes.RequestFile);
outmsg.Write(false);
break;
}
byte fileType = inc.ReadByte();
string fileName = inc.ReadString();
switch (fileType)
{
@@ -552,11 +561,11 @@ namespace Barotrauma.Networking
case (byte)PacketTypes.Vote:
Voting.RegisterVote(inc, ConnectedClients);
if (Voting.AllowEndVoting && EndVoteMax > 0 &&
if (Voting.AllowEndVoting && EndVoteMax > 0 &&
((float)EndVoteCount / (float)EndVoteMax) >= EndVoteRequiredRatio)
{
Log("Ending round by votes ("+EndVoteCount+"/"+(EndVoteMax-EndVoteCount)+")", Color.Cyan);
EndButtonHit(null,null);
Log("Ending round by votes (" + EndVoteCount + "/" + (EndVoteMax - EndVoteCount) + ")", Color.Cyan);
EndButtonHit(null, null);
}
break;
case (byte)PacketTypes.RequestNetLobbyUpdate:
@@ -45,6 +45,8 @@ namespace Barotrauma.Networking
private bool saveServerLogs = true;
private bool allowFileTransfers = true;
public bool AutoRestart
{
get { return (ConnectedClients.Count == 0) ? false : autoRestart; }
+1 -1
View File
@@ -9,7 +9,7 @@ namespace Barotrauma.Networking
{
class ServerLog
{
const int LinesPerFile = 300;
const int LinesPerFile = 800;
public const string SavePath = "ServerLogs";