internal void EnsureConnected() { if (_stopped) { return; } if (!GameSparksUtil.ShouldConnect) { return; } lock (this) { if (_WebSocketClient == null) { _WebSocketClient = _gSPlatform.GetSocket(url, GSConnection_OnMessageReceived, OnClosed, OnOpened, OnError); } lock (_WebSocketClient) { GameSparksWebSocketState state = _WebSocketClient.State; if (mustConnectBy < DateTime.Now.Ticks && state == GameSparksWebSocketState.Connecting) { Terminate(); } else if (state != GameSparksWebSocketState.Open && state != GameSparksWebSocketState.Connecting) { _WebSocketClient.Open(); } } } }
internal override void StopInternal() { if (socket != null && socket.State == GameSparksWebSocketState.Open) { socket.Terminate(); socket = null; } session = null; }
public ReliableWSConnection(string remotehost, int remoteport, IRTSessionInternal session) : base(remotehost, remoteport, session) { //GameSparks.Core.GameSparksUtil.LogMessageHandler = Console.WriteLine; session.Log("ReliableWSConnection", GameSparksRT.LogLevel.DEBUG, "wss://" + remotehost + ":" + remoteport); if (session.GetGSInstance() != null) { socket = session.GetGSInstance().GSPlatform.GetBinarySocket("wss://" + remotehost + ":" + remoteport, OnBinaryMessageReceived, OnClosed, OnOpened, OnError); } else { socket = new GameSparksWebSocket(); socket.Initialize("wss://" + remotehost + ":" + remoteport, OnBinaryMessageReceived, OnClosed, OnOpened, OnError); } socket.Open(); }
internal void OnOpened() { if (stopped || session == null) { if (socket != null) { socket.Terminate(); socket = null; } return; } session.Log("ReliableWSConnection", GameSparksRT.LogLevel.DEBUG, " TCP Connection Established"); LoginCommand loginCmd = new LoginCommand(session.ConnectToken); Send(loginCmd); }
internal void OnBinaryMessageReceived(byte[] message) { if (stopped) { if (socket != null) { socket.Terminate(); socket = null; } return; } BinaryWriteMemoryStream ms = new BinaryWriteMemoryStream(); int length = message.Length; ms.Write(message, 0, length); ms.Position = 0; while (ms.Position < length) { int bytesRead; Packet p = PooledObjects.PacketPool.Pop(); if (stopped) { return; } p.Session = session; p.Reliable = true; bytesRead = Packet.DeserializeLengthDelimited(ms, ms.BinaryReader, p); p.Reliable = p.Reliable.GetValueOrDefault(true); OnPacketReceived(p, bytesRead); PooledObjects.PacketPool.Push(p); } }
internal void OnClosed() { _stopped = true; _WebSocketClient = null; }