Esempio n. 1
0
 public static void SendAll(GameMsg msg)
 {
     foreach (var client in _clients)
     {
         client.Key.Send(msg);
     }
 }
Esempio n. 2
0
 public override void Send(GameMsg msg)
 {
     SyncContext.RunOnUnityThread(() => {
         if (_pair != null)
         {
             _pair.Receive(msg);
         }
     });
 }
Esempio n. 3
0
        private async Task ReadingAsync()
        {
            while (!_token.IsCancellationRequested)
            {
                try {
                    if (_stream.DataAvailable)
                    {
                        byte[] msgLenBuf = new byte[4];
                        int    bytesRead = 0, msgLen;
                        while (bytesRead < 4)
                        {
                            bytesRead += await _stream.ReadAsync(msgLenBuf, bytesRead, 4 - bytesRead, _token).ConfigureAwait(false);
                        }
                        msgLen    = BitConverter.ToInt32(msgLenBuf, 0);
                        bytesRead = 0;
                        byte[] msg = new byte[msgLen];
                        while (bytesRead < msgLen)
                        {
                            bytesRead += await _stream.ReadAsync(msg, bytesRead, msgLen - bytesRead, _token).ConfigureAwait(false);

                            if (msgLen > 1024)
                            {
                                SyncContext.RunOnUnityThread(() => {
                                    Receive(new GameMsg((byte)MsgType.MsgProgress, bytesRead, msgLen));
                                });
                            }
                        }
                        GameMsg gm;
                        using (MemoryStream m = new MemoryStream(msg))
                            using (GZipStream g = new GZipStream(m, CompressionMode.Decompress))
                                using (MemoryStream decompressed = new MemoryStream()) {
                                    await g.CopyToAsync(decompressed);

                                    gm = new GameMsg(decompressed.ToArray());
                                }
                        SyncContext.RunOnUnityThread(() => {
                            Receive(gm);
                        });
                        Debug.Log("Message received");
                    }
                    else
                    {
                        await Task.Delay(100, _token).ConfigureAwait(false);
                    }
                } catch (TaskCanceledException e) {
                    Debug.Log("Read cancelled " + e.Source);
                } catch (Exception e) {
                    Debug.Log("Problem reading: " + e.ToString());
                    _cts.Cancel();
                }
            }
        }
Esempio n. 4
0
 public override void Send(GameMsg msg)
 {
     lock (_sendQueue)
         _sendQueue.Enqueue(msg);
 }