Beispiel #1
0
        public void Send(byte[] data)
        {
            if (_connected)
            {
                // 8 + 12 + 4 + data
                //byte[] crypt_head = new byte[8];
                byte[] head   = new byte[12];
                byte[] buffer = new byte[8 + 12 + data.Length];

                int   local  = _timeSync.LocalTime();
                int[] global = _timeSync.GlobalTime();
                NetPack.PacklI(head, 0, (uint)local);
                NetPack.PacklI(head, 4, (uint)global[0]);
                NetPack.PacklI(head, 8, (uint)_session);
                byte[] crypt_head = Crypt.hmac_hash(_secret, head);

                Array.Copy(crypt_head, buffer, 8);
                Array.Copy(head, 0, buffer, 8, 12);
                Array.Copy(data, 0, buffer, 20, data.Length);

                UnityEngine.Debug.Log(string.Format("localtime:{0}, eventtime:{1}, session:{2}", local, global[0], _session));
                _u.Send(data, 0, data.Length);
            }
            else
            {
                UnityEngine.Debug.Assert(false);
            }
        }
Beispiel #2
0
        public void Sync()
        {
            int now = _timeSync.LocalTime();

            byte[] buffer = new byte[12];
            NetPack.PacklI(buffer, 0, (uint)now);
            NetPack.PacklI(buffer, 4, 0xffffffff);
            NetPack.PacklI(buffer, 8, (uint)_session);
            byte[] head = Crypt.hmac_hash(_secret, buffer);
            byte[] data = new byte[8 + buffer.Length];
            Array.Copy(head, data, 8);
            Array.Copy(buffer, 0, data, 8, buffer.Length);

            _u.Send(data, 0, data.Length);
        }
Beispiel #3
0
        private int _delta      = 25; // 0.025s


        public PackageSocketUdp(Context ctx, byte[] secret, uint session)
        {
            _ctx      = ctx;
            _timeSync = _ctx.TiSync;

            _so      = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            _secret  = secret;
            _session = session;

            _u        = new Rudp.Rudp(_ctx.SharpC, 1, 5);
            _u.OnRecv = RRecv;
            _u.OnSend = RSend;

            int now = _timeSync.LocalTime();

            _last = now;
        }