Exemple #1
0
        /// <summary>
        /// 从流中接收并处理帧。
        /// </summary>
        /// <exception cref="Exception"></exception>
        protected async Task Receive(Stream stream)
        {
            using (Timer timer = new Timer((object obj) => { stream.Close(); }, null, Timeout.Infinite, Timeout.Infinite))
            {
                while (true)
                {
                    timer.Change(Common.Configs.HeartbeatInterval * 3, Timeout.Infinite);
                    FrameBase frame = await ReadFrame(stream).ConfigureAwait(false);

                    timer.Change(Timeout.Infinite, Timeout.Infinite);

                    if (Common.Configs.Debug)
                    {
                        Console.WriteLine("R >" + frame.GetType().ToString());
                    }
                    if (!Handlers.ContainsKey(frame.GetType()))
                    {
                        await SendAsync(new ErrorFrame(frame.Guid) { Message = "当前客户端不支持该操作。" }).ConfigureAwait(false);
                    }
                    else
                    {
                        foreach (Type handlerType in Handlers[frame.GetType()])
                        {
                            ConstructorInfo constructor = handlerType.GetConstructor(new Type[] { typeof(ClientBase) });
                            HandlerBase     handler     = constructor.Invoke(new object[] { this }) as HandlerBase;
                            handler.Start(frame);
                        }
                    }
                }
            }
        }
Exemple #2
0
        public void OnFrame(FrameBase frame)
        {
            AudioFrame audioFrame;
            VideoFrame videoFrame;

            if ((audioFrame = (frame as AudioFrame)) != null)
            {
                Console.WriteLine("a: {1}", frame.GetType().Name, audioFrame.Data.Length);
            }
            else if ((videoFrame = (frame as VideoFrame)) != null)
            {
                Console.WriteLine("v: {1}", frame.GetType().Name, videoFrame.Data.Length);
            }
        }
Exemple #3
0
        /// <summary>
        /// 向给定客户端发送帧。
        /// </summary>
        /// <exception cref="Exception"></exception>
        public override async Task Send(ClientDataBase client, FrameBase frame)
        {
            await base.Send(client, frame).ConfigureAwait(false);

            if (Common.Configs.Debug)
            {
                Console.WriteLine("S >" + frame.GetType().ToString());
            }
        }
Exemple #4
0
 /// <summary>
 /// 线程安全地向服务端发送帧。
 /// </summary>
 /// <exception cref="Exception"></exception>
 public async Task SendAsync(FrameBase frame)
 {
     using (LockGuard lg = await LockGuard.WaitAsync(SemaphoreSend).ConfigureAwait(false))
     {
         if (Common.Configs.Debug)
         {
             Console.WriteLine("S >" + frame.GetType().ToString());
         }
         await WriteFrame(Tcp.GetStream(), frame).ConfigureAwait(false);
     }
 }
Exemple #5
0
 public override string ToString()
 {
     return(string.Format("{0} : {1}", FrameBase.GetType(), FrameBase.InstanceName));
 }
Exemple #6
0
        /// <summary>
        /// 服务端处理客户端信息循环。
        /// </summary>
        protected async void ClientLoop(TcpClient tc)
        {
            try
            {
                using (NetworkStream stream = tc.GetStream())
                    using (Timer timer = new Timer((object obj) => { stream.Close(); }, null, Timeout.Infinite, Timeout.Infinite))
                    {
                        await WriteFrame(stream, new Common.Frames.Information.RequestFrame()).ConfigureAwait(false);

                        timer.Change(Common.Configs.HelloTimeout, Timeout.Infinite);
                        var informationFrame = await ReadFrame(stream).ConfigureAwait(false) as Common.Frames.Information.AnswerFrame;

                        timer.Change(Timeout.Infinite, Timeout.Infinite);

                        if (informationFrame == null)
                        {
                            throw new Exception("Invalid Hello.");
                        }

                        ClientData client = new ClientData(this, tc, informationFrame);

                        try
                        {
                            OnConnect?.Invoke(client);
                        }
                        catch { }

                        try
                        {
                            Heartbeat(stream);
                            while (true)
                            {
                                timer.Change(Common.Configs.HeartbeatInterval * 3, Timeout.Infinite);
                                FrameBase frame = await ReadFrame(stream).ConfigureAwait(false);

                                timer.Change(Timeout.Infinite, Timeout.Infinite);

                                if (Common.Configs.Debug)
                                {
                                    Console.WriteLine("R >" + frame.GetType().ToString());
                                }
                                OnFrame?.Invoke(client, frame);
                            }
                        }
                        catch { }

                        try
                        {
                            OnDisconnect?.Invoke(client);
                        }
                        catch { }
                    }
            }
            catch (Exception ex)
            {
                if (Common.Configs.Debug)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Exemple #7
0
 public void Transmit(FrameBase frame)
 {
     Console.WriteLine(frame.GetType().Name);
 }