using System;
using Sandbox.ModAPI;
using VRage;
using VRage.Utils;
using VRageMath;
namespace SENetworkAPI
{
public class Client : NetworkAPI
{
///
/// Handles communication with the server
///
/// Identifies the channel to pass information to and from this mod
/// identifies what chat entries should be captured and sent to the server
public Client(ushort comId, string modName, string keyword = null) : base(comId, modName, keyword)
{
}
///
/// Sends a command packet to the server
///
/// The command to be executed
/// Text that will be displayed in client chat
/// A serialized object to be sent across the network
/// The date timestamp this command was sent
/// The client reciving this packet (if 0 it sends to all clients)
/// Enture delivery of the packet
public override void SendCommand(string commandString, string message = null, byte[] data = null,
DateTime? sent = null, ulong steamId = ulong.MinValue, bool isReliable = true)
{
if (MyAPIGateway.Session?.Player != null)
SendCommand(
new Command
{
CommandString = commandString, Message = message, Data = data,
Timestamp = sent == null ? DateTime.UtcNow.Ticks : sent.Value.Ticks,
SteamId = MyAPIGateway.Session.Player.SteamUserId
}, MyAPIGateway.Session.Player.SteamUserId, isReliable);
else
MyLog.Default.Warning($"[NetworkAPI] ComID: {ComId} | Failed to send command. Session does not exist.");
}
///
/// Sends a command packet to the server
///
/// The object to be sent to the client
/// The users steam ID
/// Makes sure the message is recieved by the server
internal override void SendCommand(Command cmd, ulong steamId = ulong.MinValue, bool isReliable = true)
{
if (cmd.Data != null && cmd.Data.Length > CompressionThreshold)
{
cmd.Data = MyCompression.Compress(cmd.Data);
cmd.IsCompressed = true;
}
cmd.Timestamp = DateTime.UtcNow.Ticks;
var packet = MyAPIGateway.Utilities.SerializeToBinary(cmd);
if (LogNetworkTraffic)
MyLog.Default.Info(
$"[NetworkAPI] TRANSMITTING Bytes: {packet.Length} Command: {cmd.CommandString} User: {steamId}");
MyAPIGateway.Multiplayer.SendMessageToServer(ComId, packet, isReliable);
}
///
/// Sends a command packet to the server
///
/// The command to be executed
/// Client side send to server this is not used
/// Client side send to server this is not used
/// Text that will be displayed in client chat
/// A serialized object to be sent across the network
/// The date timestamp this command was sent
/// The client reciving this packet (if 0 it sends to all clients)
/// Enture delivery of the packet
public override void SendCommand(string commandString, Vector3D point, double radius = 0, string message = null,
byte[] data = null, DateTime? sent = null, ulong steamId = 0, bool isReliable = true)
{
SendCommand(commandString, message, data, sent, steamId, isReliable);
}
///
/// Sends a command packet to the server
///
/// The object to be sent to the client
/// Client side send to server this is not used
/// Client side send to server this is not used
/// The users steam ID
/// Makes sure the message is recieved by the server
internal override void SendCommand(Command cmd, Vector3D point, double radius = 0, ulong steamId = 0,
bool isReliable = true)
{
SendCommand(cmd, steamId, isReliable);
}
public override void Say(string message)
{
SendCommand(null, message);
}
}
}