Пример #1
0
        public UserError(PyTuple tuple, PyDict match)
        {
            if (match == null)
            {
                throw new InvalidDataException("UserError: No matching set found.");
            }
            if (tuple.Items.Count > 2 || tuple.Items.Count < 1)
            {
                throw new InvalidDataException("UserError: Invalid tuple size expected 1 or 2 got " + tuple.Items.Count);
            }
            PyString msgString = tuple.Items[0] as PyString;

            if (msgString == null)
            {
                throw new InvalidDataException("UserError: No message found.");
            }
            message = msgString.Value;
            if (tuple.Items.Count == 2)
            {
                dict = tuple.Items[1] as PyDict;
                if (dict == null)
                {
                    throw new InvalidDataException("UserError: Invalid dictionary.");
                }
            }
            msgString = match.Get("msg") as PyString;
            if (msgString == null || msgString.StringValue != message)
            {
                throw new InvalidDataException("UserError: Message name mismatch.");
            }
            PyRep matchRep = match.Get("dict");

            if (dict != null)
            {
                PyDict matchDict = matchRep as PyDict;
                if (matchDict == null)
                {
                    throw new InvalidDataException("UserError: No matching dictionary.");
                }
                if (matchDict.Dictionary.Count != dict.Dictionary.Count)
                {
                    throw new InvalidDataException("UserError: Dictionary size mismatch.");
                }
                //foreach(var kvp in dict.Dictionary)
                //{
                // To-DO: compare each entry.
                //}
            }
            else
            {
                if (matchRep != null && !(matchRep is PyNone))
                {
                    throw new InvalidDataException("UserError: No Dictionary to match.");
                }
            }
        }
Пример #2
0
        private PyObject OldHandle(PyObject packet)
        {
            // Check for login packets and just forward them
            if (packet is PyTuple)
            {
                // Those are the first packets, sent by both client and server
                return(packet);
            }
            else if (packet is PyInt)
            {
                // This is only sent by the server
                return(packet);
            }
            else if (packet is PyString)
            {
                // This is only sent by the server
                return(packet);
            }
            else if (packet is PyDict)
            {
                // Packet sent by the client(HandshakeAck)
                // We need to modify it in order to put our own client address, as it isnt the same as the address that the server sends
                PyDict handshake = packet as PyDict;

                PyDict session = handshake.Get("session_init") as PyDict;

                session.Set("address", new PyString(socket.GetAddress()));

                handshake.Set("session_init", session);

                return(handshake);
            }
            else if (packet is PyObjectEx) // Exceptions... just check the type and decide what to do
            {
                PyException exception = new PyException();

                if (exception.Decode(packet) == true) // Ignore the error
                {
                    Log.Debug("Exceptions", "Got exception of type " + exception.exception_type);
                }

                return(packet);
            }
            else // Normal packets
            {
                PyPacket p = new PyPacket();
                if (p.Decode(packet) == false)
                {
                    // Big problem here, we dont know who to send this
                    Log.Error("Client", "Cannot decode PyPacket");
                }
                else
                {
                    return(HandlePyPacket(p));
                }

                return(packet);
            }
        }
Пример #3
0
 /*
 * [PyObjectEx Type2]
 *   header:
 *     [PyTuple 1]
 *       [PyToken "carbon.common.script.sys.crowset.CIndexedRowset"]
 *     [PyDict]
 *       Key=header
 *       Value=[DBRowDescriptor]
 *       key=columnName
 *       value=[PyString columnName]
 *   dict:
 *     rows
 * create with: DBResultToCIndexedRowset()
 */
 public CIndexedRowset(PyDict dict, Dictionary<PyRep, PyRep> nRows)
 {
     rows = nRows;
     if(rows == null)
     {
         rows = new Dictionary<PyRep, PyRep>();
     }
     descriptor = dict.Get("header") as DBRowDescriptor;
     if (descriptor == null)
     {
         throw new InvalidDataException("CIndexedRowSet: Invalid DBRowDescriptor.");
     }
     PyRep name = dict.Get("columnName");
     if(name == null)
     {
         throw new InvalidDataException("CIndexedRowSet: Could not find index name.");
     }
     columnName = name.StringValue;
 }
Пример #4
0
 /*
  * [PyObjectEx Type2]
  *   header:
  *     [PyTuple 1]
  *       [PyToken "carbon.common.script.sys.crowset.CRowset"]
  *     [PyDict]
  *       Key=header
  *       Value=[DBRowDescriptor]
  *   list:
  *     rows
  * create with: DBResultToCRowset
  */
 public CRowSet(PyDict dict, List <PyRep> list)
 {
     rows = list;
     if (rows == null)
     {
         rows = new List <PyRep>();
     }
     descriptor = dict.Get("header") as DBRowDescriptor;
     if (descriptor == null)
     {
         throw new InvalidDataException("CRowSet: Invalid DBRowDescriptor.");
     }
 }
Пример #5
0
        /*
         * [PyObjectEx Normal]
         *   Header:
         *     [PyTuple 3 items]
         *       [PyToken carbon.common.script.net.machoNetExceptions.WrongMachoNode]
         *       [PyTuple 0 items]
         *       [PyDict 1 kvp]
         *         Key:[PyString "payload"]
         *         ==Value:[PyInt correctNode]
         *   List:
         *   Dictionary:
         */
        public WrongMachoNode(PyDict obj)
        {
            if (obj == null)
            {
                throw new InvalidDataException("WrongMachoNode: null dictionary.");
            }
            if (!obj.Contains("payload"))
            {
                throw new InvalidDataException("WrongMachoNode: Could not find key 'payload'.");
            }
            if (obj.Dictionary.Count > 1)
            {
                throw new InvalidDataException("WrongMachoNode: Too many values in dictionary.");
            }
            PyRep value = obj.Get("payload");

            correctNode = value.IntValue;
        }
Пример #6
0
        private PyObject HandlePyPacket(PyPacket data)
        {
            // Just forward it if we dont want to look for a specific one
            if (data.dest.type == PyAddress.AddrType.Client)
            {
                // Look for SessionChangeNotifications
                if (data.type == Macho.MachoNetMsg_Type.SESSIONCHANGENOTIFICATION)
                {
                    // Search for address change in session
                    PyTuple payload = data.payload;
                    PyDict  session = payload.Items[0].As <PyTuple>().Items[1].As <PyDict>();

                    if (session.Contains("address") == true)
                    {
                        PyTuple address = session.Get("address").As <PyTuple>();

                        address[0] = new PyString(socket.GetAddress());
                        address[1] = new PyString(socket.GetAddress());
                        session.Set("address", address);
                    }

                    payload.Items[0].As <PyTuple>().Items[1] = session;

                    data.payload = payload;
                }

                // SendClient(data);
            }
            else if (data.dest.type == PyAddress.AddrType.Node)
            {
                // SendServer(data);
            }
            else if (data.dest.type == PyAddress.AddrType.Broadcast)
            {
                // What to do now ?
                Log.Error("Client", "Broadcast packets not supported yet");
                // throw new NotImplementedException("Broadcast packets are not supported yet");
            }

            return(data.Encode());
        }
Пример #7
0
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.ObjectEx)
            {
                Log.Error("PyException", "Wrong container type");
                return(false);
            }

            PyObjectEx p = data.As <PyObjectEx>();

            if (p.IsType2 == true)
            {
                Log.Error("PyException", "Wrong PyObjectEx type, expected Normal, but got Type2");
                return(false);
            }

            if (p.Header.Type != PyObjectType.Tuple)
            {
                Log.Error("PyException", "Wrong item 1 type");
                return(false);
            }

            PyTuple args = p.Header.As <PyTuple>();

            if (args.Items.Count != 3)
            {
                Log.Error("PyException", "Wrong tuple 1 item count, expected 3 but got " + args.Items.Count);
                return(false);
            }

            if (args.Items[0].Type != PyObjectType.Token)
            {
                Log.Error("PyException", "Wrong tuple item 1 type");
                return(false);
            }

            PyToken type = args.Items[0].As <PyToken>();

            exception_type = type.Token;

            if (exception_type.StartsWith("exceptions.") == false)
            {
                Log.Warning("PyException", "Trying to decode a non-exception packet: " + exception_type);
                return(false);
            }

            if (args.Items[1].Type != PyObjectType.Tuple)
            {
                Log.Error("PyException", "Wrong tuple item 2 type");
                return(false);
            }

            PyTuple msg = args.Items[1].As <PyTuple>();

            if (msg.Items.Count != 1)
            {
                Log.Error("PyException", "Wrong item 2 tuple count, expected 1 but got " + msg.Items.Count);
                return(false);
            }

            if (msg.Items[0].Type != PyObjectType.String)
            {
                Log.Error("PyException", "Wrong tuple 2 item 1 type");
                return(false);
            }

            PyString msg_data = msg.Items[0].As <PyString>();

            message = msg_data.Value;

            if (args.Items[2].Type != PyObjectType.Dict)
            {
                Log.Error("PyException", "Wrong tuple 1 item 3 type");
                return(false);
            }

            PyDict info = args.Items[2].As <PyDict>();

            if (info.Contains("origin") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key origin");
                return(false);
            }

            origin = info.Get("origin").As <PyString>().Value;

            if (info.Contains("reasonArgs") == false)
            {
                Log.Error("PyException", "Dict item 1 doesn has key reasonArgs");
                return(false);
            }

            reasonArgs = info.Get("reasonArgs").As <PyDict>();

            if (info.Contains("clock") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key clock");
                return(false);
            }

            clock = info.Get("clock").IntValue;

            if (info.Contains("loggedOnUserCount") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key loggedOnUserCount");
                return(false);
            }

            loggedOnUserCount = info.Get("loggedOnUserCount");

            if (info.Contains("region") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key region");
                return(false);
            }

            region = info.Get("region").As <PyString>().Value;

            if (info.Contains("reason") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key reason");
                return(false);
            }

            reason = info.Get("reason").As <PyString>().Value;

            if (info.Contains("version") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key version");
                return(false);
            }

            version = info.Get("version").As <PyFloat>().Value;

            if (info.Contains("build") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key build");
                return(false);
            }

            build = info.Get("build").As <PyInt>().Value;

            if (info.Contains("reasonCode") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key reasonCode");
                return(false);
            }

            reasonCode = info.Get("reasonCode").StringValue;

            if (info.Contains("codename") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key codename");
                return(false);
            }

            codename = info.Get("codename").As <PyString>().Value;

            if (info.Contains("machoVersion") == false)
            {
                Log.Error("PyException", "Dict item 1 doesnt has key machoVersion");
                return(false);
            }

            machoVersion = info.Get("machoVersion").As <PyInt>().Value;

            return(true);
        }
Пример #8
0
        public bool Decode(PyObject data)
        {
            if (data.Type != PyObjectType.Tuple)
            {
                Log.Error("AuthenticationReq", "Wrong type");
                return(false);
            }

            PyTuple tmp = data.As <PyTuple>();

            if (tmp.Items.Count != 2)
            {
                Log.Error("AuthenticationReq", "Wrong size, expected 2 but got " + tmp.Items.Count);
                return(false);
            }

            if (tmp.Items[0].Type != PyObjectType.String)
            {
                Log.Error("AuthenticationReq", "Wrong type for item 1");
                return(false);
            }

            if (tmp.Items[1].Type != PyObjectType.Dict)
            {
                Log.Error("AuthenticationReq", "Wrong type for item 2");
                return(false);
            }

            PyDict info = tmp.Items[1].As <PyDict>();

            if (info.Contains("boot_version") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_version");
                return(false);
            }

            boot_version = info.Get("boot_version").As <PyFloat>().Value;

            if (info.Contains("boot_region") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_region");
                return(false);
            }

            boot_region = info.Get("boot_region").As <PyString>().Value;

            if (info.Contains("user_password") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_password");
                return(false);
            }

            if (info.Get("user_password").Type == PyObjectType.None)
            {
                user_password = null;
            }
            else
            {
                // user_password = info.Get("user_password").As<PyString>().Value;
                PyObjectEx obj = info.Get("user_password").As <PyObjectEx>();
                user_password = obj.Header.As <PyTuple>().Items[0].As <PyTuple>().Items[1].As <PyString>().Value;
            }

            if (info.Contains("user_affiliateid") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_affiliateid");
                return(false);
            }

            user_affiliateid = info.Get("user_affiliateid").As <PyInt>().Value;

            if (info.Contains("user_password_hash") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_password_hash");
                return(false);
            }

            if (info.Get("user_password_hash").Type == PyObjectType.None)
            {
                user_password_hash = null;
            }
            else
            {
                user_password_hash = info.Get("user_password_hash").As <PyString>().Value;
            }

            if (info.Contains("macho_version") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key macho_version");
                return(false);
            }

            macho_version = info.Get("macho_version").As <PyInt>().Value;

            if (info.Contains("boot_codename") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_codename");
                return(false);
            }

            boot_codename = info.Get("boot_codename").As <PyString>().Value;

            if (info.Contains("boot_build") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key boot_build");
                return(false);
            }

            boot_build = info.Get("boot_build").As <PyInt>().Value;

            if (info.Contains("user_name") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_name");
                return(false);
            }

            user_name = info.Get("user_name").As <PyString>().Value;

            if (info.Contains("user_languageid") == false)
            {
                Log.Error("AuthenticationReq", "Dict item 1 doesnt has key user_languageid");
                return(false);
            }

            user_languageid = info.Get("user_languageid").As <PyString>().Value;

            return(true);
        }