Remove old LuaCsNetworking
This commit is contained in:
@@ -1,143 +0,0 @@
|
|||||||
using Barotrauma.Networking;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Barotrauma
|
|
||||||
{
|
|
||||||
partial class LuaCsNetworking
|
|
||||||
{
|
|
||||||
private Dictionary<ushort, Queue<IReadMessage>> receiveQueue = new Dictionary<ushort, Queue<IReadMessage>>();
|
|
||||||
|
|
||||||
public void SendSyncMessage()
|
|
||||||
{
|
|
||||||
if (GameMain.Client == null) { return; }
|
|
||||||
|
|
||||||
WriteOnlyMessage message = new WriteOnlyMessage();
|
|
||||||
message.WriteByte((byte)ClientPacketHeader.LUA_NET_MESSAGE);
|
|
||||||
message.WriteByte((byte)LuaCsClientToServer.RequestAllIds);
|
|
||||||
GameMain.Client.ClientPeer.Send(message, DeliveryMethod.Reliable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void NetMessageReceived(IReadMessage netMessage, ServerPacketHeader header, Client client = null)
|
|
||||||
{
|
|
||||||
if (header != ServerPacketHeader.LUA_NET_MESSAGE)
|
|
||||||
{
|
|
||||||
GameMain.LuaCs.Hook.Call("netMessageReceived", netMessage, header, client);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LuaCsServerToClient luaCsHeader = (LuaCsServerToClient)netMessage.ReadByte();
|
|
||||||
|
|
||||||
switch (luaCsHeader)
|
|
||||||
{
|
|
||||||
case LuaCsServerToClient.NetMessageString:
|
|
||||||
HandleNetMessageString(netMessage);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LuaCsServerToClient.NetMessageId:
|
|
||||||
HandleNetMessageId(netMessage);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LuaCsServerToClient.ReceiveIds:
|
|
||||||
ReadIds(netMessage);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IWriteMessage Start(string netMessageName)
|
|
||||||
{
|
|
||||||
var message = new WriteOnlyMessage();
|
|
||||||
|
|
||||||
message.WriteByte((byte)ClientPacketHeader.LUA_NET_MESSAGE);
|
|
||||||
|
|
||||||
if (stringToId.ContainsKey(netMessageName))
|
|
||||||
{
|
|
||||||
message.WriteByte((byte)LuaCsClientToServer.NetMessageId);
|
|
||||||
message.WriteUInt16(stringToId[netMessageName]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
message.WriteByte((byte)LuaCsClientToServer.NetMessageString);
|
|
||||||
message.WriteString(netMessageName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Receive(string netMessageName, LuaCsAction callback)
|
|
||||||
{
|
|
||||||
RequestId(netMessageName);
|
|
||||||
|
|
||||||
netReceives[netMessageName] = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RequestId(string netMessageName)
|
|
||||||
{
|
|
||||||
if (stringToId.ContainsKey(netMessageName)) { return; }
|
|
||||||
|
|
||||||
if (GameMain.Client == null) { return; }
|
|
||||||
|
|
||||||
WriteOnlyMessage message = new WriteOnlyMessage();
|
|
||||||
message.WriteByte((byte)ClientPacketHeader.LUA_NET_MESSAGE);
|
|
||||||
message.WriteByte((byte)LuaCsClientToServer.RequestSingleId);
|
|
||||||
|
|
||||||
message.WriteString(netMessageName);
|
|
||||||
|
|
||||||
Send(message, DeliveryMethod.Reliable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Send(IWriteMessage netMessage, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable)
|
|
||||||
{
|
|
||||||
GameMain.Client.ClientPeer.Send(netMessage, deliveryMethod);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleNetMessageId(IReadMessage netMessage, Client client = null)
|
|
||||||
{
|
|
||||||
ushort id = netMessage.ReadUInt16();
|
|
||||||
|
|
||||||
if (idToString.ContainsKey(id))
|
|
||||||
{
|
|
||||||
string name = idToString[id];
|
|
||||||
|
|
||||||
HandleNetMessage(netMessage, name, client);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!receiveQueue.ContainsKey(id)) { receiveQueue[id] = new Queue<IReadMessage>(); }
|
|
||||||
receiveQueue[id].Enqueue(netMessage);
|
|
||||||
|
|
||||||
if (GameSettings.CurrentConfig.VerboseLogging)
|
|
||||||
{
|
|
||||||
LuaCsLogger.LogMessage($"Received NetMessage with unknown id {id} from server, storing in queue in case we receive the id later.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ReadIds(IReadMessage netMessage)
|
|
||||||
{
|
|
||||||
ushort size = netMessage.ReadUInt16();
|
|
||||||
|
|
||||||
for (int i = 0; i < size; i++)
|
|
||||||
{
|
|
||||||
ushort id = netMessage.ReadUInt16();
|
|
||||||
string name = netMessage.ReadString();
|
|
||||||
|
|
||||||
idToString[id] = name;
|
|
||||||
stringToId[name] = id;
|
|
||||||
|
|
||||||
if (!receiveQueue.ContainsKey(id))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (receiveQueue[id].TryDequeue(out var queueMessage))
|
|
||||||
{
|
|
||||||
if (netReceives.ContainsKey(name))
|
|
||||||
{
|
|
||||||
netReceives[name](queueMessage, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,194 +0,0 @@
|
|||||||
using Barotrauma.Networking;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Barotrauma
|
|
||||||
{
|
|
||||||
partial class LuaCsNetworking
|
|
||||||
{
|
|
||||||
private const int MaxRegisterPerClient = 1000;
|
|
||||||
|
|
||||||
private Dictionary<string, int> clientRegisterCount = new Dictionary<string, int>();
|
|
||||||
|
|
||||||
private ushort currentId = 0;
|
|
||||||
|
|
||||||
public void NetMessageReceived(IReadMessage netMessage, ClientPacketHeader header, Client client = null)
|
|
||||||
{
|
|
||||||
if (header != ClientPacketHeader.LUA_NET_MESSAGE)
|
|
||||||
{
|
|
||||||
GameMain.LuaCs.Hook.Call("netMessageReceived", netMessage, header, client);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LuaCsClientToServer luaCsHeader = (LuaCsClientToServer)netMessage.ReadByte();
|
|
||||||
|
|
||||||
switch (luaCsHeader)
|
|
||||||
{
|
|
||||||
case LuaCsClientToServer.NetMessageString:
|
|
||||||
HandleNetMessageString(netMessage, client);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LuaCsClientToServer.NetMessageId:
|
|
||||||
HandleNetMessageId(netMessage, client);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LuaCsClientToServer.RequestAllIds:
|
|
||||||
WriteAllIds(client);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LuaCsClientToServer.RequestSingleId:
|
|
||||||
RequestIdSingle(netMessage, client);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleNetMessageId(IReadMessage netMessage, Client client = null)
|
|
||||||
{
|
|
||||||
ushort id = netMessage.ReadUInt16();
|
|
||||||
|
|
||||||
if (idToString.ContainsKey(id))
|
|
||||||
{
|
|
||||||
string name = idToString[id];
|
|
||||||
|
|
||||||
HandleNetMessage(netMessage, name, client);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (GameSettings.CurrentConfig.VerboseLogging)
|
|
||||||
{
|
|
||||||
LuaCsLogger.LogError($"Received NetMessage for unknown id {id} from {GameServer.ClientLogName(client)}.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IWriteMessage Start(string netMessageName)
|
|
||||||
{
|
|
||||||
var message = new WriteOnlyMessage();
|
|
||||||
|
|
||||||
message.WriteByte((byte)ServerPacketHeader.LUA_NET_MESSAGE);
|
|
||||||
|
|
||||||
if (stringToId.ContainsKey(netMessageName))
|
|
||||||
{
|
|
||||||
message.WriteByte((byte)LuaCsServerToClient.NetMessageId);
|
|
||||||
message.WriteUInt16(stringToId[netMessageName]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
message.WriteByte((byte)LuaCsServerToClient.NetMessageString);
|
|
||||||
message.WriteString(netMessageName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Receive(string netMessageName, LuaCsAction callback)
|
|
||||||
{
|
|
||||||
RegisterId(netMessageName);
|
|
||||||
|
|
||||||
netReceives[netMessageName] = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ushort RegisterId(string name)
|
|
||||||
{
|
|
||||||
if (stringToId.ContainsKey(name))
|
|
||||||
{
|
|
||||||
return stringToId[name];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentId >= ushort.MaxValue)
|
|
||||||
{
|
|
||||||
LuaCsLogger.LogError($"Tried to register more than {ushort.MaxValue} network ids!");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentId++;
|
|
||||||
|
|
||||||
idToString[currentId] = name;
|
|
||||||
stringToId[name] = currentId;
|
|
||||||
|
|
||||||
WriteIdToAll(currentId, name);
|
|
||||||
|
|
||||||
return currentId;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void RequestIdSingle(IReadMessage netMessage, Client client)
|
|
||||||
{
|
|
||||||
string name = netMessage.ReadString();
|
|
||||||
|
|
||||||
if (!stringToId.ContainsKey(name) && client.AccountId.TryUnwrap(out AccountId id))
|
|
||||||
{
|
|
||||||
if (!clientRegisterCount.ContainsKey(id.StringRepresentation))
|
|
||||||
{
|
|
||||||
clientRegisterCount[id.StringRepresentation] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
clientRegisterCount[id.StringRepresentation]++;
|
|
||||||
|
|
||||||
if (clientRegisterCount[id.StringRepresentation] > MaxRegisterPerClient)
|
|
||||||
{
|
|
||||||
LuaCsLogger.Log($"{GameServer.ClientLogName(client)} Tried to register more than {MaxRegisterPerClient} Ids!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
RegisterId(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WriteIdToAll(ushort id, string name)
|
|
||||||
{
|
|
||||||
WriteOnlyMessage message = new WriteOnlyMessage();
|
|
||||||
message.WriteByte((byte)ServerPacketHeader.LUA_NET_MESSAGE);
|
|
||||||
message.WriteByte((byte)LuaCsServerToClient.ReceiveIds);
|
|
||||||
|
|
||||||
message.WriteUInt16(1);
|
|
||||||
message.WriteUInt16(id);
|
|
||||||
message.WriteString(name);
|
|
||||||
|
|
||||||
Send(message, null, DeliveryMethod.Reliable);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WriteAllIds(Client client)
|
|
||||||
{
|
|
||||||
WriteOnlyMessage message = new WriteOnlyMessage();
|
|
||||||
message.WriteByte((byte)ServerPacketHeader.LUA_NET_MESSAGE);
|
|
||||||
message.WriteByte((byte)LuaCsServerToClient.ReceiveIds);
|
|
||||||
|
|
||||||
message.WriteUInt16((ushort)idToString.Count());
|
|
||||||
foreach ((ushort id, string name) in idToString)
|
|
||||||
{
|
|
||||||
message.WriteUInt16(id);
|
|
||||||
message.WriteString(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
Send(message, client.Connection, DeliveryMethod.Reliable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClientWriteLobby(Client client) => GameMain.Server.ClientWriteLobby(client);
|
|
||||||
|
|
||||||
public void Send(IWriteMessage netMessage, NetworkConnection connection = null, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable)
|
|
||||||
{
|
|
||||||
if (connection == null)
|
|
||||||
{
|
|
||||||
foreach (NetworkConnection conn in Client.ClientList.Select(c => c.Connection))
|
|
||||||
{
|
|
||||||
GameMain.Server.ServerPeer.Send(netMessage, conn, deliveryMethod);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
GameMain.Server.ServerPeer.Send(netMessage, connection, deliveryMethod);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateClientPermissions(Client client)
|
|
||||||
{
|
|
||||||
GameMain.Server.UpdateClientPermissions(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int FileSenderMaxPacketsPerUpdate
|
|
||||||
{
|
|
||||||
get { return FileSender.FileTransferOut.MaxPacketsPerUpdate; }
|
|
||||||
set { FileSender.FileTransferOut.MaxPacketsPerUpdate = value; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-177
@@ -1,177 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
|
||||||
using Barotrauma.Networking;
|
|
||||||
|
|
||||||
namespace Barotrauma
|
|
||||||
{
|
|
||||||
partial class LuaCsNetworking
|
|
||||||
{
|
|
||||||
private static readonly HttpClient client = new HttpClient();
|
|
||||||
|
|
||||||
private enum LuaCsClientToServer
|
|
||||||
{
|
|
||||||
NetMessageId,
|
|
||||||
NetMessageString,
|
|
||||||
RequestSingleId,
|
|
||||||
RequestAllIds,
|
|
||||||
}
|
|
||||||
|
|
||||||
private enum LuaCsServerToClient
|
|
||||||
{
|
|
||||||
NetMessageId,
|
|
||||||
NetMessageString,
|
|
||||||
ReceiveIds
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool RestrictMessageSize = true;
|
|
||||||
|
|
||||||
private Dictionary<string, LuaCsAction> netReceives = new Dictionary<string, LuaCsAction>();
|
|
||||||
private Dictionary<ushort, string> idToString = new Dictionary<ushort, string>();
|
|
||||||
private Dictionary<string, ushort> stringToId = new Dictionary<string, ushort>();
|
|
||||||
|
|
||||||
public void Initialize()
|
|
||||||
{
|
|
||||||
#if CLIENT
|
|
||||||
SendSyncMessage();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove(string netMessageName)
|
|
||||||
{
|
|
||||||
netReceives.Remove(netMessageName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IWriteMessage Start()
|
|
||||||
{
|
|
||||||
return new WriteOnlyMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
public string IdToString(ushort id)
|
|
||||||
{
|
|
||||||
if (idToString.ContainsKey(id)) { return idToString[id]; }
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ushort StringToId(string name)
|
|
||||||
{
|
|
||||||
if (stringToId.ContainsKey(name)) { return stringToId[name]; }
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleNetMessage(IReadMessage netMessage, string name, Client client = null)
|
|
||||||
{
|
|
||||||
if (netReceives.ContainsKey(name))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
netReceives[name](netMessage, client);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
LuaCsLogger.LogError($"Exception thrown inside NetMessageReceive({name})", LuaCsMessageOrigin.CSharpMod);
|
|
||||||
LuaCsLogger.HandleException(e, LuaCsMessageOrigin.CSharpMod);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (GameSettings.CurrentConfig.VerboseLogging)
|
|
||||||
{
|
|
||||||
#if SERVER
|
|
||||||
LuaCsLogger.LogError($"Received NetMessage for unknown name {name} from {GameServer.ClientLogName(client)}.");
|
|
||||||
#else
|
|
||||||
LuaCsLogger.LogError($"Received NetMessage for unknown name {name} from server.");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleNetMessageString(IReadMessage netMessage, Client client = null)
|
|
||||||
{
|
|
||||||
string name = netMessage.ReadString();
|
|
||||||
|
|
||||||
HandleNetMessage(netMessage, name, client);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void HttpRequest(string url, LuaCsAction callback, string data = null, string method = "POST", string contentType = "application/json", Dictionary<string, string> headers = null, string savePath = null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), url);
|
|
||||||
|
|
||||||
if (headers != null)
|
|
||||||
{
|
|
||||||
foreach (var header in headers)
|
|
||||||
{
|
|
||||||
request.Headers.Add(header.Key, header.Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data != null)
|
|
||||||
{
|
|
||||||
request.Content = new StringContent(data, Encoding.UTF8, contentType);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponseMessage response = await client.SendAsync(request);
|
|
||||||
|
|
||||||
if (savePath != null)
|
|
||||||
{
|
|
||||||
if (LuaCsFile.IsPathAllowedException(savePath))
|
|
||||||
{
|
|
||||||
byte[] responseData = await response.Content.ReadAsByteArrayAsync();
|
|
||||||
|
|
||||||
using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write))
|
|
||||||
{
|
|
||||||
fileStream.Write(responseData, 0, responseData.Length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NotImplementedException();
|
|
||||||
|
|
||||||
string responseBody = await response.Content.ReadAsStringAsync();
|
|
||||||
|
|
||||||
/*GameMain.LuaCs.Timer.Wait((object[] par) =>
|
|
||||||
{
|
|
||||||
callback(responseBody, (int)response.StatusCode, response.Headers);
|
|
||||||
}, 0);*/
|
|
||||||
}
|
|
||||||
catch (HttpRequestException e)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
//GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message, e.StatusCode, null); }, 0);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
//GameMain.LuaCs.Timer.Wait((object[] par) => { callback(e.Message, null, null); }, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void HttpPost(string url, LuaCsAction callback, string data, string contentType = "application/json", Dictionary<string, string> headers = null, string savePath = null)
|
|
||||||
{
|
|
||||||
HttpRequest(url, callback, data, "POST", contentType, headers, savePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void HttpGet(string url, LuaCsAction callback, Dictionary<string, string> headers = null, string savePath = null)
|
|
||||||
{
|
|
||||||
HttpRequest(url, callback, null, "GET", null, headers, savePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CreateEntityEvent(INetSerializable entity, NetEntityEvent.IData extraData)
|
|
||||||
{
|
|
||||||
GameMain.NetworkMember.CreateEntityEvent(entity, extraData);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ushort LastClientListUpdateID
|
|
||||||
{
|
|
||||||
get { return GameMain.NetworkMember.LastClientListUpdateID; }
|
|
||||||
set { GameMain.NetworkMember.LastClientListUpdateID = value; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user