示例#1
0
 public void Dispose()
 {
     cli.Dispose();
     cli    = null;
     codec  = null;
     typeDb = null;
 }
示例#2
0
        public UDPSession()
        {
            codec  = new FrameCodec();
            typeDb = new PBTypeDB(Assembly.GetExecutingAssembly());
            cli    = new UDPClient();

            cli.OnConnect = peerId =>
            {
                Debug.Assert(this.peerId == 0);

                Connecting  = false;
                Connected   = true;
                this.peerId = peerId;

                OnConnect?.Invoke();
            };

            cli.OnDisconnect = peerId =>
            {
                Debug.Assert(this.peerId == peerId);


                Connecting  = false;
                Connected   = false;
                this.peerId = 0;

                OnDisconnect?.Invoke();
            };
            cli.OnReceive = (peerId, bytes, channelId) =>
            {
                Debug.Assert(this.peerId == peerId);

                codec.Feed(bytes);
            };

            codec.FrameCb = frame =>
            {
                try
                {
                    var msg = PBSerializer.Deserialize(typeDb, frame);
                    OnMsg?.Invoke(msg);
                }
                catch (Exception e)
                {
                    this.Fatal("{0}:{1}", peerId, e.ToString());
                }
            };
        }