30 private const string DefaultUserName =
"User ";
42 [Tooltip(
"Address of the sharing server")]
43 public string ServerAddress =
"localhost";
48 public string SessionName
52 return Manager.GetSessionManager() != null && Manager.GetSessionManager().GetCurrentSession() != null
53 ? Manager.GetSessionManager().GetCurrentSession().GetName().GetString()
59 [Tooltip(
"Name of the session to join.")]
60 private string defaultSessionName =
"DefaultSession";
65 public string RoomName
69 return CurrentRoomManager != null && CurrentRoomManager.GetCurrentRoom() != null
70 ? CurrentRoomManager.GetCurrentRoom().GetName().GetString()
76 [Tooltip(
"Name of the room to join.")]
77 private string defaultRoomName =
"DefaultRoom";
87 [Tooltip(
"Port of the sharing server")]
88 public int ServerPort = 20602;
90 [Tooltip(
"Should the app connect to the server at startup")]
92 private bool connectOnAwake =
true;
101 [Tooltip(
"Determines how often the discovery service should ping the network in search of a server.")]
102 public float PingIntervalSec = 2;
107 public bool IsAudioEndpoint =
true;
140 public string AppInstanceUniqueId {
get;
private set; }
160 private float pingIntervalCurrent;
165 private bool isTryingToFindServer;
170 [Tooltip(
"Show Detailed Information for sharing services.")]
173 public string UserName
177 using (
User user = Manager.GetLocalUser())
187 using (var userName =
new XString(value))
189 Manager.SetUserName(userName);
192 UserNameChanged.RaiseEvent(value);
201 public RoomManager CurrentRoomManager {
get {
return Manager != null ? Manager.GetRoomManager() : null; } }
203 public Room CurrentRoom
205 get {
return CurrentRoomManager != null ? CurrentRoomManager.GetCurrentRoom() : null; }
212 get {
return Manager != null ? Manager.GetServerConnection() : null; }
218 public bool IsConnected
220 get {
return Manager != null && Connection != null && Connection.
IsConnected(); }
223 #region Unity Methods 229 AppInstanceUniqueId = Guid.NewGuid().ToString();
232 if (AutoDiscoverServer)
238 ManagerInit(connectOnAwake);
242 private void OnEnable()
244 Application.logMessageReceived += OnLogReceived;
247 private void LateUpdate()
249 if (isTryingToFindServer)
251 AutoDiscoverUpdate();
261 private void OnDisable()
263 Application.logMessageReceived -= OnLogReceived;
268 if (discoveryClient != null)
272 discoveryClient = null;
274 if (discoveryClientAdapter != null)
276 discoveryClientAdapter.
Dispose();
277 discoveryClientAdapter = null;
287 if (SessionsTracker != null)
290 SessionsTracker = null;
293 if (Connection != null)
295 Connection.RemoveListener((byte)
MessageID.StatusOnly, networkConnectionAdapter);
296 Connection.Dispose();
298 if (networkConnectionAdapter != null)
300 networkConnectionAdapter.
Dispose();
301 networkConnectionAdapter = null;
308 Manager.GetPairedConnection().Disconnect();
309 Manager.GetServerConnection().Disconnect();
320 #endregion // Unity Methods 322 #region Event Callbacks 328 if (SharingManagerConnected != null)
330 SharingManagerConnected(
this, EventArgs.Empty);
335 if (SharingManagerDisconnected != null)
337 SharingManagerDisconnected(
this, EventArgs.Empty);
348 isTryingToFindServer =
false;
351 if (ShowDetailedLogs)
353 Debug.Log(
"Server discovered at: " + ServerAddress);
358 if (ShowDetailedLogs)
360 Debug.LogFormat(
"Connected to: {0}:{1}", ServerAddress, ServerPort.ToString());
364 private void OnLogReceived(
string logString,
string stackTrace, LogType type)
370 case LogType.Exception:
371 Log.
Error(
string.Format(
"{0} \n {1}", logString, stackTrace));
374 case LogType.Warning:
375 Log.
Warning(
string.Format(
"{0} \n {1}", logString, stackTrace));
379 if (ShowDetailedLogs)
385 throw new ArgumentOutOfRangeException(
"type", type,
"Invalid Message Type");
389 #endregion // Event Callbacks 391 private void ManagerInit(
bool setConnection)
394 config.SetIsAudioEndpoint(IsAudioEndpoint);
395 config.SetLogWriter(logWriter);
399 config.SetServerAddress(ServerAddress);
400 config.SetServerPort(ServerPort);
409 Connection.AddListener((byte)
MessageID.StatusOnly, networkConnectionAdapter);
414 Root =
new SyncRoot(Manager.GetRootSyncObject());
419 RoomManagerAdapter =
new RoomManagerAdapter();
421 CurrentRoomManager.AddListener(RoomManagerAdapter);
423 using (var userName =
new XString(DefaultUserName))
425 #if UNITY_WSA && !UNITY_EDITOR 426 Manager.SetUserName(SystemInfo.deviceName);
428 if (!
string.IsNullOrEmpty(Environment.UserName))
430 Manager.SetUserName(Environment.UserName);
434 User localUser = Manager.GetLocalUser();
435 Manager.SetUserName(userName + localUser.
GetID().ToString());
441 private void AutoDiscoverInit()
443 if (ShowDetailedLogs)
445 Debug.Log(
"Looking for servers...");
452 discoveryClient.
AddListener(discoveryClientAdapter);
455 isTryingToFindServer =
true;
458 private void AutoDiscoverUpdate()
461 pingIntervalCurrent += Time.deltaTime;
462 if (pingIntervalCurrent > PingIntervalSec)
464 if (ShowDetailedLogs)
466 Debug.Log(
"Looking for servers...");
469 pingIntervalCurrent = 0;
470 discoveryClient.
Ping();
478 ServerAddress = serverAddress;
485 SessionsTracker.LeaveCurrentSession();
486 Manager.SetServerConnectionInfo(ServerAddress, (uint)ServerPort);