This commit is contained in:
Sofia 2017-05-11 22:00:52 +03:00
parent d634e862ea
commit 1f87d83492
15 changed files with 238 additions and 22 deletions

View File

@ -39,10 +39,8 @@ namespace Cyber.Controls {
Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (Move.sqrMagnitude != 0) {
Character.Move(Character.transform.TransformDirection(Move));
Client.Send(PktType.MoveCreature, new MoveCreaturePkt(Character.transform.TransformDirection(Move), Character.ID));
} else if (Character.Moving()) {
Character.Stop();
Client.Send(PktType.MoveCreature, new MoveCreaturePkt(new Vector3(), Character.ID));
}
// Rotation
@ -58,7 +56,7 @@ namespace Cyber.Controls {
if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) {
LookingAt.Interact(Character);
if (LookingAt.GetInteractableSyncdata().PublicInteractions) {
Client.Send(PktType.InteractPkt, new InteractionPkt(LookingAt.ID));
Client.Send(PktType.Interact, new InteractionPkt(LookingAt.ID));
}
}
}

View File

@ -83,6 +83,14 @@ namespace Cyber.Entities.SyncBases {
return MovementDirection.sqrMagnitude != 0;
}
/// <summary>
/// Returns the current movement direction;
/// </summary>
/// <returns>The Movement Direction Vector3</returns>
public Vector3 GetMovementDirection() {
return MovementDirection;
}
/// <summary>
/// The character's rotation. Intended to be given as an input to
/// <see cref="SetRotation"/>.

View File

@ -75,6 +75,15 @@ namespace Cyber.Networking.Clientside {
}
}
public static bool SendByChannel(short msgType, MessageBase message, byte channelID) {
if (Singleton.Running) {
Singleton.NetClient.SendByChannel(msgType, message, channelID);
return true;
} else {
return false;
}
}
/// <summary>
/// Returns if the client is running or not.
/// This is independant weather the client is connected or not.
@ -126,9 +135,9 @@ namespace Cyber.Networking.Clientside {
NetClient.RegisterHandler(PktType.MassIdentity, HandlePacket);
NetClient.RegisterHandler(PktType.SpawnEntity, HandlePacket);
NetClient.RegisterHandler(PktType.MoveCreature, HandlePacket);
NetClient.RegisterHandler(PktType.SyncPacket, HandlePacket);
NetClient.RegisterHandler(PktType.InteractPkt, HandlePacket);
NetClient.RegisterHandler(PktType.StaticObjectIdsPkt, HandlePacket);
NetClient.RegisterHandler(PktType.Sync, HandlePacket);
NetClient.RegisterHandler(PktType.Interact, HandlePacket);
NetClient.RegisterHandler(PktType.StaticObjectIds, HandlePacket);
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
@ -182,6 +191,10 @@ namespace Cyber.Networking.Clientside {
if (SpawnPkt.OwnerID > -1) {
Players[SpawnPkt.OwnerID].Character = Obj.GetComponent<Character>();
}
if (SpawnPkt.OwnerID == Player.ConnectionID) {
gameObject.AddComponent<ClientSyncer>();
}
break;
case (PktType.MoveCreature):
MoveCreaturePkt MoveCreature = new MoveCreaturePkt();
@ -197,7 +210,7 @@ namespace Cyber.Networking.Clientside {
}
break;
case (PktType.InteractPkt):
case (PktType.Interact):
InteractionPkt Interaction = new InteractionPkt();
Interaction.Deserialize(msg.reader);
if (Interaction.OwnerSyncBaseID == Player.Character.ID) {
@ -211,10 +224,10 @@ namespace Cyber.Networking.Clientside {
Term.Println("Server has sent an erroneus SyncBase ID!");
}
break;
case (PktType.SyncPacket):
case (PktType.Sync):
SyncHandler.HandleSyncPkt(msg);
break;
case (PktType.StaticObjectIdsPkt):
case (PktType.StaticObjectIds):
Term.Println("The static object id packet!");
IntListPkt StaticIds = new IntListPkt();
StaticIds.Deserialize(msg.reader);

View File

@ -0,0 +1,47 @@

using Cyber.Networking.Messages;
using UnityEngine;
namespace Cyber.Networking.Clientside {
/// <summary>
/// Syncer used on the clientside which sends sync packets from the client to the server containing their movement direction, rotation and possible checksums.
/// </summary>
public class ClientSyncer : MonoBehaviour {
private const int ChecksumInterval = 10; // Every 10 ticks -> 1 per second
private const float TickInterval = 1 / 10f;
private float TimeSinceLastTick = TickInterval;
private int TickCounter = 0;
private int SyncIDCounter = 0;
private void Update() {
TimeSinceLastTick += Time.deltaTime;
if (TimeSinceLastTick >= TickInterval) {
ClientSyncPkt SyncPkt = new ClientSyncPkt();
if (TickCounter % ChecksumInterval == 0) {
// Add checksums
}
var PlayerCharacter = Client.GetConnectedPlayer().Character;
SyncPkt.ClientSyncID = SyncIDCounter++;
SyncPkt.MoveDirection = PlayerCharacter.GetMovementDirection();
SyncPkt.Rotation = PlayerCharacter.GetRotation();
Client.SendByChannel(PktType.ClientSync, SyncPkt, NetworkChannelID.Unreliable);
if (TickCounter < int.MaxValue) {
TickCounter += 1;
} else {
TickCounter = 0;
}
TimeSinceLastTick -= TickInterval;
}
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d36fbc4015a1ed24ab842868cc05d5b3
timeCreated: 1494527802
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +1,6 @@

using Cyber.Entities;
using Cyber.Networking.Messages;
using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Networking.Clientside {
@ -24,7 +23,7 @@ namespace Cyber.Networking.Clientside {
}
/// <summary>
/// Handle a given Network message. Must be checked to be <see cref="PktType.SyncPacket"/> first.
/// Handle a given Network message. Must be checked to be <see cref="PktType.Sync"/> first.
/// </summary>
/// <param name="message"></param>
public void HandleSyncPkt(NetworkMessage message) {

View File

@ -0,0 +1,59 @@

using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Networking.Messages {
/// <summary>
/// This Packet contains sync data from the client to the server like movement direction and rotation.
/// </summary>
public class ClientSyncPkt : MessageBase {
/// <summary>
/// ID of this client sync packet. This is separate from the server sync id.
/// </summary>
public int ClientSyncID;
/// <summary>
/// Current movement direction of the player
/// </summary>
public Vector3 MoveDirection;
/// <summary>
/// Current rotation of the player
/// </summary>
public Vector3 Rotation;
/// <summary>
/// Create an empty client sync packet.
/// </summary>
public ClientSyncPkt() { }
/// <summary>
/// Deserializes the sync packet. Does not deserialize everything, only the ID.
/// </summary>
/// <param name="reader"></param>
public override void Deserialize(NetworkReader reader) {
ClientSyncID = reader.ReadInt32();
}
/// <summary>
/// Reads the rest of the sync packet. Must be deserialized first.
/// </summary>
/// <param name="reader"></param>
public void ReadTheRest(NetworkReader reader) {
MoveDirection = reader.ReadVector3();
Rotation = reader.ReadVector3();
}
/// <summary>
/// Serializes the entire packet for sending.
/// </summary>
/// <param name="writer"></param>
public override void Serialize(NetworkWriter writer) {
writer.Write(ClientSyncID);
writer.Write(MoveDirection);
writer.Write(Rotation);
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: a048f03c7254eee44adec0eb68be8cc5
timeCreated: 1494527802
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,7 +4,7 @@ using UnityEngine.Networking;
namespace Cyber.Networking.Messages {
/// <summary>
/// Packet containing integers, used in many packet types, such as <see cref="PktType.MassIdentity"/> and <see cref="PktType.StaticObjectIdsPkt"/>.
/// Packet containing integers, used in many packet types, such as <see cref="PktType.MassIdentity"/> and <see cref="PktType.StaticObjectIds"/>.
/// </summary>
public class IntListPkt : MessageBase {

View File

@ -35,20 +35,25 @@ namespace Cyber.Networking {
public const short MoveCreature = 204;
/// <summary>
/// Packet containing sync data.
/// Packet containing sync data from the server.
/// </summary>
public const short SyncPacket = 205;
public const short Sync = 205;
/// <summary>
/// Packet telling that someone has made an interactive remark.
/// </summary>
public const short InteractPkt = 206;
public const short Interact = 206;
/// <summary>
/// Packet containing an id list of static objects existing in the ready game.
/// This packet contains an <see cref="IntListPkt"/>.
/// </summary>
public const short StaticObjectIdsPkt = 207;
public const short StaticObjectIds = 207;
/// <summary>
/// Packet containing sync data from the client to the server, like movement direction and rotation.
/// </summary>
public const short ClientSync = 208;
}
}

View File

@ -6,7 +6,7 @@ namespace Cyber.Networking.Serverside {
/// Represents a player on the server. This class is used by the server.
/// The S stands for "Server".
/// </summary>
class SConnectedPlayer {
public class SConnectedPlayer {
/// <summary>
/// The player's connection ID.

View File

@ -25,6 +25,8 @@ namespace Cyber.Networking.Serverside {
/// </summary>
public Syncer Syncer;
private ServerSyncHandler ServerSyncHandler;
/// <summary>
/// Creates the server-component, and sets the singleton as itself.
/// </summary>
@ -125,7 +127,8 @@ namespace Cyber.Networking.Serverside {
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
NetworkServer.RegisterHandler(PktType.MoveCreature, HandlePacket);
NetworkServer.RegisterHandler(PktType.InteractPkt, HandlePacket);
NetworkServer.RegisterHandler(PktType.Interact, HandlePacket);
NetworkServer.RegisterHandler(PktType.ClientSync, HandlePacket);
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
@ -141,6 +144,8 @@ namespace Cyber.Networking.Serverside {
gameObject.AddComponent<Syncer>();
ServerSyncHandler = new ServerSyncHandler(Players);
return true;
}
@ -172,7 +177,7 @@ namespace Cyber.Networking.Serverside {
NetworkServer.SendToClient(Player.Value.ConnectionID, PktType.MoveCreature, MoveCreature);
}
break;
case PktType.InteractPkt:
case PktType.Interact:
InteractionPkt Interaction = new InteractionPkt();
Interaction.Deserialize(msg.reader);
@ -187,7 +192,7 @@ namespace Cyber.Networking.Serverside {
float ServerInteractionDistance = Sender.InteractionDistance + Sender.MovementSpeed * 0.5f;
if (Delta.magnitude <= ServerInteractionDistance) {
Interacted.Interact(Sender);
NetworkServer.SendToAll(PktType.InteractPkt, Interaction);
NetworkServer.SendToAll(PktType.Interact, Interaction);
if (Interacted.GetInteractableSyncdata().RequiresSyncing) {
Syncer.DirtSyncBase(Interacted.ID);
}
@ -196,6 +201,9 @@ namespace Cyber.Networking.Serverside {
Term.Println("Client has reported an erronous SyncBase ID!");
}
break;
case PktType.ClientSync:
ServerSyncHandler.HandleSyncPkt(msg);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
@ -241,7 +249,7 @@ namespace Cyber.Networking.Serverside {
NetworkServer.SendToAll(PktType.SpawnEntity, new SpawnEntityPkt(EntityType.NPC, Position, EntityIdList, Id));
// Send ID's of every existing static SyncBase object in the world.
NetworkServer.SendToClient(Id, PktType.StaticObjectIdsPkt, new IntListPkt(Spawner.SyncDB.GetStaticSyncBaseIDList()));
NetworkServer.SendToClient(Id, PktType.StaticObjectIds, new IntListPkt(Spawner.SyncDB.GetStaticSyncBaseIDList()));
// Send every entity to the player who just connected.
foreach (var Entry in Players) {

View File

@ -0,0 +1,43 @@

using Cyber.Entities.SyncBases;
using Cyber.Networking.Messages;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Networking.Serverside {
public class ServerSyncHandler {
private Dictionary<int, int> LastSyncIDReceived = new Dictionary<int, int>();
private Dictionary<int, SConnectedPlayer> Players;
public ServerSyncHandler(Dictionary<int, SConnectedPlayer> players) {
Players = players;
}
/// <summary>
/// Handle the given sync from a client. Must be checked to be <see cref="PktType.ClientSync"/> first.
/// </summary>
/// <param name="message"></param>
public void HandleSyncPkt(NetworkMessage message) {
ClientSyncPkt SyncPkt = new ClientSyncPkt();
SyncPkt.Deserialize(message.reader);
int LastID = -1;
if (LastSyncIDReceived.ContainsKey(message.conn.connectionId)) {
LastID = LastSyncIDReceived[message.conn.connectionId];
}
if (SyncPkt.ClientSyncID > LastID) {
SyncPkt.ReadTheRest(message.reader);
LastSyncIDReceived[message.conn.connectionId] = SyncPkt.ClientSyncID;
Character PlayerCharacter = Players[message.conn.connectionId].Character;
PlayerCharacter.Move(SyncPkt.MoveDirection);
PlayerCharacter.SetRotation(SyncPkt.Rotation);
Debug.Log("MoveDirection: " + SyncPkt.MoveDirection);
}
// Disregard the package, it's too old.
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f02e0de4b2abc7e43862d3d9d824fea9
timeCreated: 1494527802
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -65,7 +65,7 @@ namespace Cyber.Networking.Serverside {
if (QueuedSyncs.Count > 0) {
int[] SyncIDs = QueuedSyncs.ToArray();
SyncPkt SyncPacket = new SyncPkt(Database, SyncIDs, SyncPacketID++);
Server.SendToAllByChannel(PktType.SyncPacket, SyncPacket, NetworkChannelID.Unreliable);
Server.SendToAllByChannel(PktType.Sync, SyncPacket, NetworkChannelID.Unreliable);
QueuedSyncs.Clear();
DirtySyncBases.Clear();