/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal.Experimental.NetWork
{
using System;
/// A net work client.
public class NetWorkClient : IDisposable
{
/// Event queue for all listeners interested in OnJoinRoomResult events.
public event Action OnJoinRoomResult;
/// Event queue for all listeners interested in OnCameraParamUpdate events.
public event Action OnCameraParamUpdate;
/// Event queue for all listeners interested in OnMessageResponse events.
public event Action OnMessageResponse;
/// Event queue for all listeners interested in OnDisconnect events.
public event Action OnDisconnect;
/// Event queue for all listeners interested in OnConnect events.
public event Action OnConnect;
/// Default constructor.
public NetWorkClient()
{
NetworkSession.Register(MessageType.Connected, OnConnected);
NetworkSession.Register(MessageType.Disconnect, OnDisConnected);
NetworkSession.Register(MessageType.HeartBeat, HeartbeatResponse);
NetworkSession.Register(MessageType.EnterRoom, EnterRoomResponse);
NetworkSession.Register(MessageType.ExitRoom, ExitRoomResponse);
NetworkSession.Register(MessageType.UpdateCameraParam, UpdateCameraParamResponse);
NetworkSession.Register(MessageType.MessageSynchronization, MessageSynchronizationResponse);
}
/// Join the server's room.
public void EnterRoomRequest()
{
NetworkSession.Enqueue(MessageType.EnterRoom);
}
/// Exit room request.
public void ExitRoomRequest()
{
NetworkSession.Enqueue(MessageType.ExitRoom);
}
/// Updates the camera parameter request.
public void UpdateCameraParamRequest()
{
NetworkSession.Enqueue(MessageType.UpdateCameraParam);
}
/// Join the server's room.
public void SendMessage(byte[] data)
{
NetworkSession.Enqueue(MessageType.MessageSynchronization, data);
}
/// Connects.
/// The IP.
/// The port.
public void Connect(string ip, int port)
{
NetworkSession.Connect(ip, port);
}
/// Executes the 'connected' action.
/// The data.
private void OnConnected(byte[] data)
{
OnConnect?.Invoke();
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
/// resources.
public void Dispose()
{
NetworkSession.UnRegister(MessageType.Connected, OnConnected);
NetworkSession.UnRegister(MessageType.Disconnect, OnDisConnected);
NetworkSession.UnRegister(MessageType.HeartBeat, HeartbeatResponse);
NetworkSession.UnRegister(MessageType.EnterRoom, EnterRoomResponse);
NetworkSession.UnRegister(MessageType.ExitRoom, ExitRoomResponse);
NetworkSession.UnRegister(MessageType.UpdateCameraParam, UpdateCameraParamResponse);
NetworkSession.UnRegister(MessageType.MessageSynchronization, MessageSynchronizationResponse);
NetworkSession.Close();
}
/// Executes the 'dis connected' action.
/// The data.
private void OnDisConnected(byte[] data)
{
OnDisconnect?.Invoke();
}
#region Net msg response
/// Heartbeat response.
/// The data.
private void HeartbeatResponse(byte[] data)
{
NetworkSession.Received = true;
NRDebugger.Debug("Receive a heart beat package.");
}
/// Enter room response.
/// The data.
private void EnterRoomResponse(byte[] data)
{
EnterRoomData result = SerializerFactory.Create().Deserialize(data);
if (result.result)
{
NRDebugger.Debug("Join the room success.");
OnJoinRoomResult?.Invoke(true);
}
else
{
NRDebugger.Warning("Join the room faild.");
OnJoinRoomResult?.Invoke(false);
}
}
/// Exit room response.
/// The data.
private void ExitRoomResponse(byte[] data)
{
ExitRoomData result = SerializerFactory.Create().Deserialize(data);
if (result.Suc)
{
NRDebugger.Debug("Exit the room success.");
}
else
{
NRDebugger.Warning("Exit the room faild.");
}
}
/// Updates the camera parameter response described by data.
/// The data.
private void UpdateCameraParamResponse(byte[] data)
{
CameraParam result = SerializerFactory.Create().Deserialize(data);
OnCameraParamUpdate?.Invoke(result);
NRDebugger.Info(result.fov.ToString());
}
private void MessageSynchronizationResponse(byte[] data)
{
OnMessageResponse?.Invoke(data);
}
#endregion
}
}