private IEnumerator doPingCheck(Action <NetworkConnectionState> callback) { float pingStartTime = Time.unscaledTime; NetworkConnectionState connectionState = ConnectionState; if (Service.Get <GameSettings>().OfflineMode) { callback(NetworkConnectionState.BasicConnection); yield break; } bool isPinging = true; Ping ping = new Ping("8.8.8.8"); while (isPinging) { if (ping.isDone) { connectionState = ((ping.time >= 0) ? NetworkConnectionState.BasicConnection : NetworkConnectionState.NoConnection); isPinging = false; } else if (Time.unscaledTime - pingStartTime >= 2f) { connectionState = NetworkConnectionState.NoConnection; isPinging = false; } yield return(null); } callback(connectionState); }
public BluetoothConnectionHelper(Guid crossPlatformServiceUid) { NetworkConnectionStatus = NetworkConnectionState.NotSearching; rfcommServiceUuid = crossPlatformServiceUid; PeerFinder.TriggeredConnectionStateChanged += PeerFinderTriggeredConnectionStateChanged; PeerFinder.ConnectionRequested += PeerFinderConnectionRequested; }
public NetworkWriter(NetworkManager parent, Socket socket, ServerType serverType) { _parent = parent; _parent.WriterAlive = true; _socket = socket; _serverType = serverType; _connectionState = NetworkConnectionState.Connected; _sharedDataSource = SharedDataSource.GetInstance(); }
private void Client_Receive_OnReady() { if (networkSocket.IsHost) { DevTools.Logger.LogError(NetworkService.NetworkLog, "Client Method called on a host."); return; } currentState = NetworkConnectionState.Ready; OnReady(this); }
public void Disconnect() { currentState = NetworkConnectionState.Disconnecting; ServiceLocator.SceneService.OnLoadScene -= Host_OnLoadScene; ServiceLocator.SceneService.OnFinishedLoading -= Client_OnFinishedLoading; if (!networkSocket.IsHost) { networkSocket.Close(); } DevTools.Logger.Log(NetworkService.NetworkLog, "Disconnect: {}", () => new object[] { connectionID }); }
public void Run() { int bytesReceived; byte[] buffer = new byte[2048]; //while we are still connected. while (_connectionState == NetworkConnectionState.Connected) { if (!_sharedDataSource.SocketDied) { try { bytesReceived = _socket.Receive(buffer); string toProcess = Encoding.UTF8.GetString(buffer, 0, bytesReceived); switch (_serverType) { case ServerType.Communication: CommunicationProc(toProcess); break; case ServerType.Login: LoginProc(toProcess); break; case ServerType.Streaming: StreamingProc(toProcess); break; default: //something has gone terribly wrong. _parent.NextServer = null; break; } } catch (SocketException socketException) { if (_connectionState != NetworkConnectionState.Disconnecting) { //If we quit out of the reader with the next server as null, the network manager will reconnect us to the communication server. _parent.NextServer = null; } } } else { _connectionState = NetworkConnectionState.Disconnecting; } } _parent.ReaderAlive = false; }
private void Client_Receive_OnLoadScene(NetworkReader reader) { if (networkSocket.IsHost) { DevTools.Logger.LogError(NetworkService.NetworkLog, "Client Method called on a host."); return; } currentState = NetworkConnectionState.Loading; friendlyName = reader.ReadString(); Scenes.SceneLoadInfo loadInfo = new Scenes.SceneLoadInfo(reader); ServiceLocator.SceneService.LoadScene(loadInfo); }
public void Run() { while (_connectionState == NetworkConnectionState.Connected) { string messageToSend; if (!_sharedDataSource.SocketDied) { try { messageToSend = _sharedDataSource.GetMessage(); while (_sharedDataSource.IsStreaming) //dont send anything while streaming. { } if (messageToSend != null) { string[] parameters = messageToSend.Split(':'); switch (parameters[0]) { case "DISCONNECT": _connectionState = NetworkConnectionState.Disconnecting; break; case "GETSERVER": ServerType nextServer; nextServer = Enum.TryParse(parameters[1], true, out nextServer) ? nextServer : ServerType.None; _parent.NextServerType = nextServer; break; } byte[] buffer = new byte[messageToSend.Length]; int bytesToSend = Encoding.UTF8.GetBytes(messageToSend, 0, messageToSend.Length, buffer, 0); _socket.Send(buffer, bytesToSend, SocketFlags.None); } } catch (SocketException socketException) { if (_connectionState != NetworkConnectionState.Disconnecting) { //the socket died so we need to die too. _sharedDataSource.SocketDied = true; _connectionState = NetworkConnectionState.Disconnecting; } } } else { _connectionState = NetworkConnectionState.Disconnecting; } } _parent.WriterAlive = false; }
// // Ready Events // private void Host_OnReady() { if (!networkSocket.IsHost) { DevTools.Logger.LogError(NetworkService.NetworkLog, "Host Method called on a client."); return; } currentState = NetworkConnectionState.Ready; NetworkWriter writer = networkSocket.Writer.Setup(networkSocket.ReliableSequencedChannel, MessageTypes.BRF_Client_Receive_OnReady); networkSocket.Writer.Send(this, () => "Send client ready"); OnReady(this); }
// Called when the client has finished loading the scene. private void Client_OnFinishedLoading(Scenes.SceneLoadInfo sceneLoadInfo) { if (networkSocket.IsHost) { DevTools.Logger.LogError(NetworkService.NetworkLog, "Client Method called on a host."); return; } // Tell host we have finished loading. currentState = NetworkConnectionState.Waiting; NetworkWriter writer = networkSocket.Writer.Setup(networkSocket.ReliableSequencedChannel, MessageTypes.BRF_Host_Receive_OnFinishedLoading); writer.Write(ServiceLocator.PlatformService.Username); // Tell host what our name is. networkSocket.Writer.Send(this, () => "Send host finished loading"); }
// // Level Load Events // // Called when the host starts to load a new scene. private void Host_OnLoadScene(Scenes.SceneLoadInfo sceneLoadInfo) { if (!networkSocket.IsHost) { DevTools.Logger.LogError(NetworkService.NetworkLog, "Host Method called on a client."); return; } currentState = NetworkConnectionState.Loading; NetworkWriter writer = networkSocket.Writer.Setup(networkSocket.ReliableSequencedChannel, MessageTypes.BRF_Client_Receive_OnLoadScene); writer.Write(ServiceLocator.PlatformService.Username); // Tell client what our name is. sceneLoadInfo.NetworkWrite(writer); networkSocket.Writer.Send(this, () => "Send Client SceneLoadInfo"); }
private void CommunicationProc(string message) { string[] splitMessage = message.Split(':'); switch (splitMessage[0]) { case "TYPESTORED": //handle this break; case "ERROR": //errors can be handled by the UI _sharedDataSource.AddUserQueue(message); break; case "IP": _sharedDataSource.AddUserQueue("ERROR:Ready"); if (splitMessage.Length == 4) { string ipAddress = splitMessage[1]; int portNumber; portNumber = int.TryParse(splitMessage[3], out portNumber) ? portNumber : -1; if (portNumber != -1) { IPAddress ipAddr = IPAddress.Parse(ipAddress); _parent.NextServer = new IPEndPoint(ipAddr, portNumber); _sharedDataSource.AddMessageStart("DISCONNECT"); } } else { //there are problems - handle this. } break; case "DISCONNECT": _connectionState = NetworkConnectionState.Disconnecting; break; default: //we probably sent a bad message - we should handle this. break; } }
private void Host_Receive_OnFinishedLoading(NetworkReader reader) { if (!networkSocket.IsHost) { DevTools.Logger.LogError(NetworkService.NetworkLog, "Host Method called on a client."); return; } currentState = NetworkConnectionState.Waiting; friendlyName = reader.ReadString(); if (ServiceLocator.SceneService.CurrentState == Scenes.SceneLoadingState.Loaded) { Host_OnReady(); } else { ServiceLocator.SceneService.OnFinishedLoading += Host_OnFinishedLoading; } }
private void LoginProc(string message) { string[] splitMessage = message.Split(':'); switch (splitMessage[0]) { case "ADDED": _sharedDataSource.AddUserQueue(message); break; case "AUTH": _sharedDataSource.AddUserQueue(message); //we need to go back to the connection server. _sharedDataSource.AddMessage("DISCONNECT"); _parent.NextServer = _parent.CommServerDetails; _parent.NextServerType = ServerType.Communication; //we then need to send these messages when we have disconnected the writer to ensure that these do not get sent to the wrong server. while (_parent.WriterAlive) { } _sharedDataSource.AddMessage("CLIENT"); _sharedDataSource.AddMessage("GETSERVER:STREAMING"); break; case "ERROR": _sharedDataSource.AddUserQueue(message); break; case "DISCONNECT": _connectionState = NetworkConnectionState.Disconnecting; break; default: //we probably sent a bad message - we should handle this. break; } }
private static extern bool InternetGetConnectedState(out NetworkConnectionState lpdwFlags, uint dwReserved);
private void StreamingProc(string message) { string[] splitMessage = message.Split(':'); switch (splitMessage[0]) { case "ADDED": case "REMOVED": case "SONGS": case "ERROR": _sharedDataSource.AddUserQueue(message); break; case "RECOMMENDATION": _sharedDataSource.AddUserQueue(message); break; case "SONG": _sharedDataSource.IsStreaming = true; _sharedDataSource.NewSong(); try { while (_sharedDataSource.IsStreaming) { byte[] songBuffer = new byte[4096]; int receive = _socket.Receive(songBuffer); string eofCheck = Encoding.UTF8.GetString(songBuffer); if (eofCheck.Contains("EOF:EOF:EOF")) { if (!eofCheck.StartsWith("EOF:EOF:EOF")) { _sharedDataSource.AddMemoryStream(songBuffer, eofCheck.IndexOf("EOF:EOF:EOF")); } _sharedDataSource.IsStreaming = false; _sharedDataSource.OnSongReady(); } else { _sharedDataSource.AddMemoryStream(songBuffer, receive); } } } catch (SocketException socketException) { if (_connectionState != NetworkConnectionState.Disconnecting) { //If we quit out of the reader with the next server as null, the network manager will reconnect us to the communication server. _parent.NextServer = null; } } break; case "DISCONNECT": _connectionState = NetworkConnectionState.Disconnecting; _parent.NextServer = _parent.CommServerDetails; _parent.NextServerType = ServerType.Communication; //we then need to send these messages when we have disconnected the writer to ensure that these do not get sent to the wrong server. while (_parent.WriterAlive) { } _sharedDataSource.AddMessage("CLIENT"); _sharedDataSource.AddMessage("GETSERVER:LOGIN"); break; default: //we probably sent a bad message - we should handle this. break; } }