Example #1
0
        public static Protocol Decode(Octets content)
        {
			Protocol prtc = null;
            int iType = 0;
            OctetsStream os = new OctetsStream(content);
            OctetsStream data = new OctetsStream();

            try
            {
                os.Begin();
                iType = (int)os.uncompact_uint32();
                os.unmarshal(data);
                os.Commit();

				prtc = Create(iType);
				if (prtc == null)
				{
					prtc = new ProtocolDefault();
				    Debug.Log(string.Format("Protocol type {0} not found", (NetProtocolType)iType));
				}
				else
					prtc.unmarshal(data);
            }
            catch (MarshalException)
            {
                os.Rollback();
                Debug.Log(string.Format("Protocol decode error, type {0}", (NetProtocolType)iType));
                prtc = null;
            }

            return prtc;
        }
Example #2
0
        public static Protocol Decode(Stream ism)
        {
            if (ism.eos())
                return null;

			Protocol prtc = null;
            int iType = 0;
            int iSize = 0;

            try
            {
                if (ism.check_policy)
                {
                    ism.Begin();
                    iType = (int)ism.uncompact_uint32();
                    iSize = (int)ism.uncompact_uint32();
                    ism.Rollback();

                    if (!ism.session.StatePolicy(iType) || !ism.session.manager.InputPolicy(iType, iSize))
                    {
                        Debug.Log(string.Format("Protocol Decode CheckPolicy Error: type={0}, size={1}", (NetProtocolType)iType, iSize));
                        throw new ProtocolException();
                    }

                    ism.check_policy = false;
                    ism.checked_size = iSize;
                }

                Stream data = new Stream(ism.session, ism.checked_size);
                ism.Begin();
                iType = (int)ism.uncompact_uint32();
                ism.unmarshal(data);
                ism.Commit();

				prtc = Create(iType);
                 if (prtc == null)
                 {
                     prtc = new ProtocolDefault(iType);
                     Debug.Log(string.Format("Protocol type {0} not found", (NetProtocolType)iType));
                 }
                 else
                     prtc.unmarshal(data);

                ism.check_policy = true;
            }
            catch (MarshalException)
            {
                ism.Rollback();
                if (prtc == null)
                {}//Debug.Log(string.Format("Protocol Decode Warning: uncompleted data, protocol type={0}, size={1}", (NetProtocolType)iType, iSize));
                else
                {
                    Debug.Log(string.Format("Protocol Decode Unmarshal Error: type={0}, size={1}", (NetProtocolType)iType, iSize));
                    throw new ProtocolException();
                }
            }

            return prtc;
        }