示例#1
0
        // Simple implementation of packing the stream however you want from the Message you Queued to send
        // Custom bit packing along with pooled object can provide the optimal performance for this
        public async Task PackMessage(AsyncPeer peer, int type, object payload, Stream packToStream)
        {
            using (var inputStream = AsyncTcp.StreamManager.GetStream())
            {
                // Serialize object into the managed stream
                Utf8Json.JsonSerializer.Serialize(inputStream, payload);

                // Reset stream position for next copy
                inputStream.Position = 0;

                // If UseCompression
                if (inputStream.Length >= 860)
                {
                    packToStream.WriteByte(Convert.ToByte(true));

                    using (var compressionStream = new GZipStream(packToStream, CompressionMode.Compress, true))
                    {
                        await inputStream.CopyToAsync(compressionStream).ConfigureAwait(false);
                    }
                }
                else
                {
                    // Stupid Utf8Json won't serialize from an offset, so we need to do this double copy
                    packToStream.WriteByte(Convert.ToByte(false));
                    await inputStream.CopyToAsync(packToStream).ConfigureAwait(false);
                }
            }
        }
示例#2
0
        public Task HandleUDPPacket(AsyncPeer peer, Memory <byte> buffer)
        {
            var waypoint = MemoryMarshal.Read <Waypoint>(buffer.Span);

            //_ = HandleWaypoint(peer, waypoint); // TODO make value task?
            return(Task.CompletedTask);
        }
示例#3
0
        public Task Receive(AsyncPeer peer, int type, ReadOnlySequence <byte> buffer)
        {
            return(Task.CompletedTask);

            /*
             *  if (type == AsyncTcp.AsyncTcp.KeepAliveType)
             *      return;
             *
             *  // Handle Zero-Length Messages
             *  if (buffer.Length == 0)
             *  {
             *      await HandleMessage(peer, type, null).ConfigureAwait(false);
             *      return;
             *  }
             *
             *  using (var stream = StreamManager.GetStream(null, (int)buffer.Length))
             *  {
             *      foreach (var segment in buffer)
             *      {
             *          await stream.WriteAsync(segment).ConfigureAwait(false);
             *      }
             *
             *      stream.Position = 0;
             *
             *      var compressed = Convert.ToBoolean(stream.ReadByte());
             *
             *      // Stupid Utf8Json Wont Serialize a Stream with an offset, so we need to make this either way
             *      using (var outputStream = StreamManager.GetStream())
             *      {
             *          if (compressed)
             *          {
             *              using (var compressionStream = new GZipStream(stream, CompressionMode.Decompress))
             *              {
             *                  await compressionStream.CopyToAsync(outputStream).ConfigureAwait(false);
             *
             *                  // Reset stream position for the deserialize
             *                  outputStream.Position = 0;
             *
             *                  // Handle the decompressed stream
             *                  await HandleMessage(peer, type, outputStream).ConfigureAwait(false);
             *              }
             *          }
             *          else
             *          {
             *              // Stupid Utf8Json Wont Serialize a Stream with an offset, so we need to make this either way
             *              await stream.CopyToAsync(outputStream).ConfigureAwait(false);
             *
             *              // Reset stream position for the deserialize
             *              outputStream.Position = 0;
             *
             *              // Handle the stream
             *              await HandleMessage(peer, type, outputStream).ConfigureAwait(false);
             *          }
             *      }
             *  }
             */
        }
示例#4
0
        public static void Init(ClientInitializer initializer)
        {
            var user = new User(initializer.Nick, initializer.NickColor);

            if (Interlocked.CompareExchange(ref model, new ClientModel(user), null) != null)
            {
                throw new InvalidOperationException("model already inited");
            }

            Api    = new ClientApi();
            Client = new AsyncClient(initializer.Nick);
            Peer   = new AsyncPeer();

            Plugins = new ClientPluginManager(initializer.PluginsPath);
            Plugins.LoadPlugins(initializer.ExcludedPlugins);
        }
示例#5
0
        public static void Init(ClientInitializer initializer)
        {
            if (Interlocked.CompareExchange(ref model, new ClientModel(), null) != null)
            {
                throw new InvalidOperationException("model already inited");
            }

            using (var client = Get())
                model.User = new User(initializer.Nick, initializer.NickColor);

            // API установится автоматически при подключении к серверу (согласно версии на сервере)
            Client  = new AsyncClient(initializer.Nick);
            Peer    = new AsyncPeer();
            Plugins = new ClientPluginManager(initializer.PluginsPath);
            Plugins.LoadPlugins(initializer.ExcludedPlugins);
        }
示例#6
0
        private async Task HandleMessage(AsyncPeer peer, int type, Stream stream)
        {
            try
            {
                switch (type)
                {
                case 1:
                    var test = Utf8Json.JsonSerializer.Deserialize <Test>(stream);
                    await DoTestStuff(test).ConfigureAwait(false);

                    break;

                default:
                    var error = Utf8Json.JsonSerializer.Deserialize <Error>(stream);
                    Console.WriteLine("Error Message : " + error.Err);
                    break;
                }
            }
            catch { }
        }
示例#7
0
        // Simple implementation of unpacking the stream into whatever usable format you want
        // Custom bit packing along with pooled object can provide the optimal performance for this
        public async Task UnpackMessage(AsyncPeer peer, int type, Stream unpackFromStream)
        {
            // Handle Zero-Length Messages
            if (unpackFromStream == null)
            {
                await HandleMessage(peer, type, unpackFromStream).ConfigureAwait(false);

                return;
            }

            var compressed = Convert.ToBoolean(unpackFromStream.ReadByte());

            // Stupid Utf8Json Wont Serialize a Stream with an offset, so we need to make this either way
            using (var outputStream = AsyncTcp.StreamManager.GetStream())
            {
                if (compressed)
                {
                    using (var compressionStream = new GZipStream(unpackFromStream, CompressionMode.Decompress))
                    {
                        await compressionStream.CopyToAsync(outputStream).ConfigureAwait(false);

                        // Reset stream position for the deserialize
                        outputStream.Position = 0;

                        // Handle the decompressed stream
                        await HandleMessage(peer, type, outputStream).ConfigureAwait(false);
                    }
                }
                else
                {
                    // Stupid Utf8Json Wont Serialize a Stream with an offset, so we need to make this either way
                    await unpackFromStream.CopyToAsync(outputStream).ConfigureAwait(false);

                    // Reset stream position for the deserialize
                    outputStream.Position = 0;

                    // Handle the stream
                    await HandleMessage(peer, type, outputStream).ConfigureAwait(false);
                }
            }
        }
示例#8
0
        public static void Init(ClientInitializer initializer)
        {
            if (!initializer.Certificate.HasPrivateKey)
            {
                throw new ArgumentException("Initializer should have certificate with private key.");
            }

            var userId = new UserId(initializer.Nick, initializer.Certificate.Thumbprint);
            var user   = new ClientUser(userId, initializer.NickColor, initializer.Certificate);

            if (Interlocked.CompareExchange(ref _chat, new ClientChat(user), null) != null)
            {
                throw new InvalidOperationException("model already inited");
            }

            TrustedCertificates = new ClientCertificatesStorage(initializer.TrustedCertificatesPath, _notifier, _logger);

            Api    = new ClientApi();
            Client = new AsyncClient(userId, TrustedCertificates, initializer.Certificate, Api, _notifier, _logger);
            Peer   = new AsyncPeer(userId, initializer.Certificate, Api, _notifier, _logger);

            Plugins = new ClientPluginManager(initializer.PluginsPath);
            Plugins.LoadPlugins(initializer.ExcludedPlugins);
        }
示例#9
0
 // Implement this to dispose of the message after a send if you are pooling objects
 public Task DisposeMessage(AsyncPeer peer, int type, object payload)
 {
     return(Task.CompletedTask);
 }
示例#10
0
 public async Task PeerDisconnected(AsyncPeer peer)
 {
     await Console.Out.WriteLineAsync($"Client (PeerId: {peer.PeerId}) disconnected...").ConfigureAwait(false);
 }
示例#11
0
        public Task ReceiveUnreliable(AsyncPeer peer, ReadOnlyMemory <byte> buffer)
        {
            var waypoint = MemoryMarshal.Read <Waypoint>(buffer.Span);

            return(Task.CompletedTask);
        }
示例#12
0
 public Task PeerDisconnected(AsyncPeer peer)
 {
     return(Task.CompletedTask);
 }