Created
July 6, 2016 02:15
-
-
Save NVentimiglia/9cd027ac74fb3245ed09eae030f038ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading; | |
using UnityEngine; | |
using LiteNetLib; | |
using LiteNetLib.Utils; | |
using Debug = UnityEngine.Debug; | |
public class GameClient : MonoBehaviour, INetEventListener | |
{ | |
private NetClient _netClient; | |
private NetDataWriter _writer; | |
[SerializeField] | |
private GameObject _clientBall; | |
[SerializeField] | |
private GameObject _clientBallInterpolated; | |
private float _newBallPosX; | |
private float _oldBallPosX; | |
private float _lerpTime; | |
private int Total; | |
private DateTime StartTime; | |
private Stopwatch watch = new Stopwatch(); | |
private long Latency; | |
void Start() | |
{ | |
_writer = new NetDataWriter(true); | |
_netClient = new NetClient(this, "sample_app"); | |
_netClient.Start(); | |
_netClient.Connect("localhost", 5000); | |
_netClient.UpdateTime = 1; | |
//new Thread(() => | |
//{ | |
// while (true) | |
// { | |
// _netClient.PollEvents(); | |
// } | |
//}).Start(); | |
} | |
void Update() | |
{ | |
_netClient.PollEvents(); | |
} | |
void OnDestroy() | |
{ | |
_netClient.Stop(); | |
} | |
void DoSend() | |
{ | |
_writer.Reset(); | |
for (int i = 0; i < 10; i++) | |
{ | |
_writer.Put((double)UnityEngine.Random.value); | |
} | |
watch.Reset(); | |
watch.Start(); | |
_netClient.Peer.Send(_writer, SendOptions.Unreliable); | |
} | |
void Print() | |
{ | |
var time = DateTime.UtcNow - StartTime; | |
var ops = Total / time.TotalSeconds; | |
var lag = Latency / Total; | |
UnityEngine.Debug.Log("OPS " + ops.ToString("N2") + " lag " + lag.ToString("N2") + " time " + time.TotalMinutes.ToString("N2")); | |
Invoke("Print", 15); | |
} | |
public void OnPeerConnected(NetPeer peer) | |
{ | |
Debug.Log("[CLIENT] We connected to " + peer.EndPoint); | |
StartTime = DateTime.UtcNow; | |
Invoke("Print", 15); | |
DoSend(); | |
} | |
public void OnPeerDisconnected(NetPeer peer, string additionalInfo) | |
{ | |
} | |
public void OnNetworkError(NetEndPoint endPoint, string error) | |
{ | |
} | |
public void OnNetworkReceive(NetPeer peer, NetDataReader reader) | |
{ | |
Total++; | |
Latency += watch.ElapsedMilliseconds; | |
DoSend(); | |
} | |
public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader) | |
{ | |
} | |
public void OnNetworkLatencyUpdate(NetPeer peer, int latency) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading; | |
using UnityEngine; | |
using LiteNetLib; | |
using LiteNetLib.Utils; | |
public class GameServer : MonoBehaviour, INetEventListener | |
{ | |
private NetServer _netServer; | |
private NetPeer _ourPeer; | |
private NetDataWriter _dataWriter; | |
[SerializeField] private GameObject _serverBall; | |
void Start() | |
{ | |
_dataWriter = new NetDataWriter(); | |
_netServer = new NetServer(this, 100, "sample_app"); | |
_netServer.Start(5000); | |
_netServer.UpdateTime = 1; | |
new Thread(() => | |
{ | |
while (true) | |
{ | |
_netServer.PollEvents(); | |
} | |
}).Start(); | |
} | |
//void Update() | |
//{ | |
// _netServer.PollEvents(); | |
//} | |
void OnDestroy() | |
{ | |
_netServer.Stop(); | |
} | |
public void OnPeerConnected(NetPeer peer) | |
{ | |
Debug.Log("[SERVER] We have new peer " + peer.EndPoint); | |
_ourPeer = peer; | |
} | |
public void OnPeerDisconnected(NetPeer peer, string additionalInfo) | |
{ | |
if(peer == _ourPeer) | |
_ourPeer = null; | |
} | |
public void OnNetworkError(NetEndPoint endPoint, string error) | |
{ | |
} | |
public void OnNetworkReceive(NetPeer peer, NetDataReader reader) | |
{ | |
peer.Send(reader.Data, SendOptions.Unreliable); | |
} | |
public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader) | |
{ | |
} | |
public void OnNetworkLatencyUpdate(NetPeer peer, int latency) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment