Beispiel #1
0
 public BaseRequest(RequestType tp)
 {
     this.sfso             = AWObject.NewInstance();
     this.targetController = 0;
     this.isEncrypted      = false;
     this.id = (int)tp;
 }
Beispiel #2
0
        public IAWObject Cs2Sfs(object csObj)
        {
            IAWObject iSFSObject = AWObject.NewInstance();

            this.ConvertCsObj(csObj, iSFSObject);
            return(iSFSObject);
        }
Beispiel #3
0
 public BaseRequest(int id)
 {
     this.sfso             = AWObject.NewInstance();
     this.targetController = 0;
     this.isEncrypted      = false;
     this.id = id;
 }
Beispiel #4
0
        private void DispatchRequest(IAWObject requestObject)
        {
            IMessage message = new Message();

            if (requestObject.IsNull(AWProtocolCodec.CONTROLLER_ID))
            {
                throw new AWCodecError("Request rejected: No Controller ID in request!");
            }
            if (requestObject.IsNull(AWProtocolCodec.ACTION_ID))
            {
                throw new AWCodecError("Request rejected: No Action ID in request!");
            }
            message.Id      = Convert.ToInt32(requestObject.GetShort(AWProtocolCodec.ACTION_ID));
            message.Content = requestObject.GetSFSObject(AWProtocolCodec.PARAM_ID);
            message.IsUDP   = requestObject.ContainsKey(AWProtocolCodec.UDP_PACKET_ID);
            if (message.IsUDP)
            {
                message.PacketId = requestObject.GetLong(AWProtocolCodec.UDP_PACKET_ID);
            }
            int @byte = (int)requestObject.GetByte(AWProtocolCodec.CONTROLLER_ID);

            UnityEngine.Debug.Log("@byte" + @byte);
            IController controller = this.bitSwarm.GetController(@byte);

            if (controller == null)
            {
                throw new AWError("Cannot handle server response. Unknown controller, id: " + @byte);
            }
            controller.HandleMessage(message);
        }
        private void FnClientDisconnection(IMessage msg)
        {
            IAWObject content = msg.Content;
            int       @byte   = (int)content.GetByte("dr");

            this.sfs.HandleClientDisconnection(ClientDisconnectionReason.GetReason(@byte));
        }
Beispiel #6
0
        private IAWObject UnrollDictionary(Hashtable dict)
        {
            IAWObject   iSFSObject = AWObject.NewInstance();
            IEnumerator enumerator = dict.Keys.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    string        text           = (string)enumerator.Current;
                    AWDataWrapper sFSDataWrapper = this.WrapField(dict[text]);
                    if (sFSDataWrapper == null)
                    {
                        throw new AWCodecError(string.Concat(new object[]
                        {
                            "Cannot serialize field of dictionary with key: ",
                            text,
                            ", ",
                            dict[text],
                            " -- unsupported type!"
                        }));
                    }
                    iSFSObject.Put(text, sFSDataWrapper);
                }
            }
            finally
            {
                IDisposable disposable;
                if ((disposable = (enumerator as IDisposable)) != null)
                {
                    disposable.Dispose();
                }
            }
            return(iSFSObject);
        }
 public PrivateMessageRequest(string message, int recipientId, IAWObject parameters)
 {
     this.type       = 1;
     this.message    = message;
     this.recipient  = recipientId;
     this.parameters = parameters;
 }
Beispiel #8
0
        public ByteArray Object2Binary(IAWObject obj)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteByte(Convert.ToByte(18));
            byteArray.WriteShort(Convert.ToInt16(obj.Size()));
            return(this.Obj2bin(obj, byteArray));
        }
Beispiel #9
0
        private Hashtable RebuildDict(IAWObject sfsObj)
        {
            Hashtable hashtable = new Hashtable();

            string[] keys = sfsObj.GetKeys();
            for (int i = 0; i < keys.Length; i++)
            {
                string key = keys[i];
                hashtable[key] = this.UnwrapField(sfsObj.GetData(key));
            }
            return(hashtable);
        }
Beispiel #10
0
 private ByteArray Obj2bin(IAWObject obj, ByteArray buffer)
 {
     string[] keys  = obj.GetKeys();
     string[] array = keys;
     for (int i = 0; i < array.Length; i++)
     {
         string        text = array[i];
         AWDataWrapper data = obj.GetData(text);
         buffer = this.EncodeSFSObjectKey(buffer, text);
         buffer = this.EncodeObject(buffer, data.Type, data.Data);
     }
     return(buffer);
 }
Beispiel #11
0
 private void ConvertSFSObject(IAWArray fieldList, object csObj, Type objType)
 {
     for (int i = 0; i < fieldList.Size(); i++)
     {
         IAWObject     sFSObject = fieldList.GetAWObject(i);
         string        utfString = sFSObject.GetUtfString(DefaultAWDataSerializer.FIELD_NAME_KEY);
         AWDataWrapper data      = sFSObject.GetData(DefaultAWDataSerializer.FIELD_VALUE_KEY);
         object        value     = this.UnwrapField(data);
         FieldInfo     field     = objType.GetField(utfString, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
         if (field != null)
         {
             field.SetValue(csObj, value);
         }
     }
 }
Beispiel #12
0
        private void ConvertCsObj(object csObj, IAWObject sfsObj)
        {
            Type   type     = csObj.GetType();
            string fullName = type.FullName;

            if (!(csObj is SerializableAWType))
            {
                throw new AWCodecError(string.Concat(new object[]
                {
                    "Cannot serialize object: ",
                    csObj,
                    ", type: ",
                    fullName,
                    " -- It doesn't implement the SerializableAWType interface"
                }));
            }
            IAWArray iSFSArray = AWArray.NewInstance();

            sfsObj.PutUtfString(DefaultAWDataSerializer.CLASS_MARKER_KEY, fullName);
            sfsObj.PutSFSArray(DefaultAWDataSerializer.CLASS_FIELDS_KEY, iSFSArray);
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            FieldInfo[] array  = fields;
            for (int i = 0; i < array.Length; i++)
            {
                FieldInfo     fieldInfo      = array[i];
                string        name           = fieldInfo.Name;
                object        value          = fieldInfo.GetValue(csObj);
                IAWObject     iSFSObject     = AWObject.NewInstance();
                AWDataWrapper sFSDataWrapper = this.WrapField(value);
                if (sFSDataWrapper == null)
                {
                    throw new AWCodecError(string.Concat(new object[]
                    {
                        "Cannot serialize field of object: ",
                        csObj,
                        ", field: ",
                        name,
                        ", type: ",
                        fieldInfo.GetType().Name,
                        " -- unsupported type!"
                    }));
                }
                iSFSObject.PutUtfString(DefaultAWDataSerializer.FIELD_NAME_KEY, name);
                iSFSObject.Put(DefaultAWDataSerializer.FIELD_VALUE_KEY, sFSDataWrapper);
                iSFSArray.AddSFSObject(iSFSObject);
            }
        }
Beispiel #13
0
        public void HandleHandShake(BaseEvent evt)
        {
            IAWObject iAWObject = evt.Params["message"] as IAWObject;

            if (iAWObject.IsNull(BaseRequest.KEY_ERROR_CODE))
            {
                this.sessionToken = iAWObject.GetUtfString(HandshakeRequest.KEY_SESSION_TOKEN);
                this.bitSwarm.CompressionThreshold = iAWObject.GetInt(HandshakeRequest.KEY_COMPRESSION_THRESHOLD);
                this.bitSwarm.MaxMessageSize       = iAWObject.GetInt(HandshakeRequest.KEY_MAX_MESSAGE_SIZE);
                if (this.debug)
                {
                    this.log.Debug(new string[]
                    {
                        string.Format("Handshake response: tk => {0}, ct => {1}", this.sessionToken, this.bitSwarm.CompressionThreshold)
                    });
                }
                if (this.bitSwarm.IsReconnecting)
                {
                    this.bitSwarm.IsReconnecting = false;
                    this.DispatchEvent(new AWEvent(AWEvent.CONNECTION_RESUME));
                }
                else
                {
                    this.isConnecting = false;
                    Hashtable hashtable = new Hashtable();
                    hashtable["success"] = true;
                    this.DispatchEvent(new AWEvent(AWEvent.CONNECTION, hashtable));
                }
            }
            else
            {
                short     @short       = iAWObject.GetShort(BaseRequest.KEY_ERROR_CODE);
                string    errorMessage = AWErrorCodes.GetErrorMessage((int)@short, this.log, iAWObject.GetUtfStringArray(BaseRequest.KEY_ERROR_PARAMS));
                Hashtable hashtable2   = new Hashtable();
                hashtable2["success"]      = false;
                hashtable2["errorMessage"] = errorMessage;
                hashtable2["errorCode"]    = @short;
                this.DispatchEvent(new AWEvent(AWEvent.CONNECTION, hashtable2));
            }
        }
Beispiel #14
0
        private object UnwrapField(AWDataWrapper wrapper)
        {
            object result = null;
            int    type   = wrapper.Type;

            if (type <= 8)
            {
                result = wrapper.Data;
            }
            else
            {
                if (type == 17)
                {
                    result = this.RebuildArray(wrapper.Data as IAWArray);
                }
                else
                {
                    if (type == 18)
                    {
                        IAWObject iSFSObject = wrapper.Data as IAWObject;
                        if (iSFSObject.ContainsKey(DefaultAWDataSerializer.CLASS_MARKER_KEY) && iSFSObject.ContainsKey(DefaultAWDataSerializer.CLASS_FIELDS_KEY))
                        {
                            result = this.Sfs2Cs(iSFSObject);
                        }
                        else
                        {
                            result = this.RebuildDict(wrapper.Data as IAWObject);
                        }
                    }
                    else
                    {
                        if (type == 19)
                        {
                            result = wrapper.Data;
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #15
0
        public object Sfs2Cs(IAWObject sfsObj)
        {
            if (!sfsObj.ContainsKey(DefaultAWDataSerializer.CLASS_MARKER_KEY) || !sfsObj.ContainsKey(DefaultAWDataSerializer.CLASS_FIELDS_KEY))
            {
                throw new AWCodecError("The AWObject passed does not represent any serialized class.");
            }
            string utfString = sfsObj.GetUtfString(DefaultAWDataSerializer.CLASS_MARKER_KEY);
            Type   type;

            if (DefaultAWDataSerializer.runningAssembly == null)
            {
                type = Type.GetType(utfString);
            }
            else
            {
                type = DefaultAWDataSerializer.runningAssembly.GetType(utfString);
            }
            if (type == null)
            {
                throw new AWCodecError("Cannot find type: " + utfString);
            }
            object obj = Activator.CreateInstance(type);

            if (!(obj is SerializableAWType))
            {
                throw new AWCodecError(string.Concat(new object[]
                {
                    "Cannot deserialize object: ",
                    obj,
                    ", type: ",
                    utfString,
                    " -- It doesn't implement the SerializableAWType interface"
                }));
            }
            this.ConvertSFSObject(sfsObj.GetSFSArray(DefaultAWDataSerializer.CLASS_FIELDS_KEY), obj, type);
            return(obj);
        }
Beispiel #16
0
 public void PutSFSObject(string key, IAWObject val)
 {
     this.dataHolder[key] = new AWDataWrapper(AWDataType.SFS_OBJECT, val);
 }
Beispiel #17
0
 public void AddSFSObject(IAWObject val)
 {
     this.AddObject(val, AWDataType.SFS_OBJECT);
 }
Beispiel #18
0
 public void OnPacketRead(IAWObject packet)
 {
     this.DispatchRequest(packet);
 }
Beispiel #19
0
 public AdminMessageRequest(string message, MessageRecipientMode recipientMode, IAWObject parameters)
 {
     if (recipientMode == null)
     {
         throw new ArgumentException("RecipientMode cannot be null!");
     }
     this.type       = 3;
     this.message    = message;
     this.parameters = parameters;
     this.recipient  = recipientMode.Target;
     this.sendMode   = recipientMode.Mode;
 }
Beispiel #20
0
        public void OnPacketRead(ByteArray packet)
        {
            IAWObject requestObject = AWObject.NewFromBinaryData(packet);

            this.DispatchRequest(requestObject);
        }
Beispiel #21
0
        private AWDataWrapper DecodeObject(ByteArray buffer)
        {
            AWDataType    sFSDataType = (AWDataType)Convert.ToInt32(buffer.ReadByte());
            AWDataWrapper result;

            if (sFSDataType == AWDataType.NULL)
            {
                result = this.BinDecode_NULL(buffer);
            }
            else
            {
                if (sFSDataType == AWDataType.BOOL)
                {
                    result = this.BinDecode_BOOL(buffer);
                }
                else
                {
                    if (sFSDataType == AWDataType.BOOL_ARRAY)
                    {
                        result = this.BinDecode_BOOL_ARRAY(buffer);
                    }
                    else
                    {
                        if (sFSDataType == AWDataType.BYTE)
                        {
                            result = this.BinDecode_BYTE(buffer);
                        }
                        else
                        {
                            if (sFSDataType == AWDataType.BYTE_ARRAY)
                            {
                                result = this.BinDecode_BYTE_ARRAY(buffer);
                            }
                            else
                            {
                                if (sFSDataType == AWDataType.SHORT)
                                {
                                    result = this.BinDecode_SHORT(buffer);
                                }
                                else
                                {
                                    if (sFSDataType == AWDataType.SHORT_ARRAY)
                                    {
                                        result = this.BinDecode_SHORT_ARRAY(buffer);
                                    }
                                    else
                                    {
                                        if (sFSDataType == AWDataType.INT)
                                        {
                                            result = this.BinDecode_INT(buffer);
                                        }
                                        else
                                        {
                                            if (sFSDataType == AWDataType.INT_ARRAY)
                                            {
                                                result = this.BinDecode_INT_ARRAY(buffer);
                                            }
                                            else
                                            {
                                                if (sFSDataType == AWDataType.LONG)
                                                {
                                                    result = this.BinDecode_LONG(buffer);
                                                }
                                                else
                                                {
                                                    if (sFSDataType == AWDataType.LONG_ARRAY)
                                                    {
                                                        result = this.BinDecode_LONG_ARRAY(buffer);
                                                    }
                                                    else
                                                    {
                                                        if (sFSDataType == AWDataType.FLOAT)
                                                        {
                                                            result = this.BinDecode_FLOAT(buffer);
                                                        }
                                                        else
                                                        {
                                                            if (sFSDataType == AWDataType.FLOAT_ARRAY)
                                                            {
                                                                result = this.BinDecode_FLOAT_ARRAY(buffer);
                                                            }
                                                            else
                                                            {
                                                                if (sFSDataType == AWDataType.DOUBLE)
                                                                {
                                                                    result = this.BinDecode_DOUBLE(buffer);
                                                                }
                                                                else
                                                                {
                                                                    if (sFSDataType == AWDataType.DOUBLE_ARRAY)
                                                                    {
                                                                        result = this.BinDecode_DOUBLE_ARRAY(buffer);
                                                                    }
                                                                    else
                                                                    {
                                                                        if (sFSDataType == AWDataType.UTF_STRING)
                                                                        {
                                                                            result = this.BinDecode_UTF_STRING(buffer);
                                                                        }
                                                                        else
                                                                        {
                                                                            if (sFSDataType == AWDataType.UTF_STRING_ARRAY)
                                                                            {
                                                                                result = this.BinDecode_UTF_STRING_ARRAY(buffer);
                                                                            }
                                                                            else
                                                                            {
                                                                                if (sFSDataType == AWDataType.SFS_ARRAY)
                                                                                {
                                                                                    buffer.Position--;
                                                                                    result = new AWDataWrapper(17, this.DecodeSFSArray(buffer));
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (sFSDataType != AWDataType.SFS_OBJECT)
                                                                                    {
                                                                                        throw new Exception("Unknow AWDataType ID: " + sFSDataType);
                                                                                    }
                                                                                    buffer.Position--;
                                                                                    IAWObject iSFSObject = this.DecodeSFSObject(buffer);
                                                                                    byte      type       = Convert.ToByte(18);
                                                                                    object    data       = iSFSObject;
                                                                                    if (iSFSObject.ContainsKey(DefaultAWDataSerializer.CLASS_MARKER_KEY) && iSFSObject.ContainsKey(DefaultAWDataSerializer.CLASS_FIELDS_KEY))
                                                                                    {
                                                                                        type = Convert.ToByte(19);
                                                                                        data = this.Sfs2Cs(iSFSObject);
                                                                                    }
                                                                                    result = new AWDataWrapper((int)type, data);
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }