Home

Awesome

Unity Async Await TCP

中文

Notice: If you enjoy this repository and plan to use it, please consider using TinyRPC instead!

1. Description

  1. Base on async/await(TAP) syntax sugar.
  2. This Demo is a Remote Video Controller base on tcp , support play, pause, and stop command, and can read the remarks of MP4 file as well.
  3. Environment:Windows10 & Unity 2019.2.12f1
  4. Circularbuffer and a simple packet strategy (header + content) inside for avoiding message segmentation those caused by hard-coded buffer size.
  5. Disconnection notification, not the best maybe, but at least there is.
  6. Friendly reminder: Task will open threads on demand. It is a good habit to always pay attention to context thread id, API: System.Threading.Thread.CurrentThread.ManagedThreadId, to avoid exceptions caused by using Unity components in non-main threads.

2. Project structure

Controller

Player

Common

3. Demonstrate

注意: 如果您喜欢这个存储库并打算使用它,请考虑改用TinyRPC

1. 简要说明

  1. 测试基于async/await(TAP) 模式下的TCP 通信。
  2. 这个Demo 是一个基于TCP通信的Remotecontroller for video player,实现了客户端控制服务端视频播放器的播放、暂停、停止,也实现了MP4文件备注信息的获取。
  3. Windows10 & Unity 2020.2.12f1 (使用较多新语法糖)
  4. 加入了环形缓冲器和简单的分包策略(记录包体长度的包头+包体)避免了写死缓存尺寸带来的消息被分割的异常。
  5. 加强了断线提醒,逻辑可能不是最优,但至少是有。
  6. 友情提示: Task 会按需开线程,时刻关注上下文线程是一个很好的习惯,API:System.Threading.Thread.CurrentThread.ManagedThreadId ,避免在非主线程中使用Unity 组件导致的异常,在本示例中使用 LOOM 跨线程推送数据。

2. 工程结构

Controller

Player

Common

3. 演示