/// <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); } } } } }
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); } }
/// <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()); } }
/// <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); } }
public override string ToString() { return(string.Format("{0} : {1}", FrameBase.GetType(), FrameBase.InstanceName)); }
/// <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); } } }
public void Transmit(FrameBase frame) { Console.WriteLine(frame.GetType().Name); }