Beispiel #1
0
        public TimeoutMonitor SendMsg(object msg, bool handoverRelibility = false, ICipherPolicy cipherPolicy = null)
        {
            if (null == _endpoint)
            {
                Debug.LogError("[Session.SendMsg] invalid destination address");
                return(null);
            }
            if (true == handoverRelibility)
            {
                if (Application.internetReachability != _networkReachability)
                {
                    _networkReachability = Application.internetReachability;
                    Disconnect();
                    _disconnectState = DisconnectState.Handover;
                }
            }
            try
            {
                System.IO.MemoryStream ms   = new System.IO.MemoryStream();
                System.Type            type = msg.GetType();
                type.GetMethod("Store").Invoke(msg, new object[] { ms });
                System.Reflection.FieldInfo fi = type.GetField("MSG_ID");

                if (null != cipherPolicy)
                {
                    ms = cipherPolicy.Encrypt(ms);
                }

                uint   msgID        = (uint)(int)fi.GetValue(msg);
                ushort packetLength = (ushort)(Packet.HEADER_SIZE + (int)ms.Length);

                if (packetLength > Gamnet.Buffer.BUFFER_SIZE)
                {
                    throw new System.Exception(string.Format("Overflow the send buffer max size : {0}", packetLength));
                }

                Reconnect();

                Gamnet.Packet packet = new Gamnet.Packet();
                packet.length = packetLength;
                packet.msg_id = msgID;
                packet.Append(ms);
                packet.msg_seq  = ++_send_seq;
                packet.reliable = handoverRelibility;

                SendMsg(packet);
                return(_timeout_monitor);
            }
            catch (System.Exception e) {
                Debug.LogError("[Session.SendMsg] exception:" + e.ToString());
                Error(new Gamnet.Exception(ErrorCode.SendMsgFailError, e.ToString()));
            }
            return(null);
        }
Beispiel #2
0
        public void RegisterHandler <T> (Action <T> handler, ICipherPolicy cipherPolicy = null) where T : new()
        {
            System.Type msg_type = typeof(T);
            System.Reflection.FieldInfo fieldInfo = msg_type.GetField("MSG_ID");
            int            msg_id      = (int)fieldInfo.GetValue(null);
            MsgHandler <T> msg_handler = null;

            if (true == _handlers.ContainsKey((uint)msg_id))
            {
                msg_handler = (MsgHandler <T>)_handlers [(uint)msg_id];
            }
            else
            {
                msg_handler = new MsgHandler <T> (this, cipherPolicy);
                _handlers.Add((uint)msg_id, msg_handler);
            }
            msg_handler.onReceive += handler;
        }
Beispiel #3
0
 public MsgHandler(Session session, ICipherPolicy cipher_policy = null)
 {
     this.session       = session;
     this.cipher_policy = cipher_policy;
 }