public void ReadSingleObject()
        {
            byte[]     data = MiscellaneousUtils.HexToBytes("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00");
            BSONObject obj  = SimpleBSON.Load(data);

            Assert.AreEqual(obj ["Blah"].int32Value, 1);
        }
Beispiel #2
0
    private void readSocket()
    {
        //Console.WriteLine(" - Try to read!");
        if (!socket_ready)
        {
            return;
        }

        if (net_stream.DataAvailable && net_stream.CanRead)
        {
            //Debug.Log("Read Socket");
            try {
                // decrypt ?
                BSONObject obj = null;
                obj = SimpleBSON.Load(ByteStreamParser.parseStream(net_stream));



                IClientCmd clientCmd = cmdMgr.decodeBSON(obj);
                if (clientCmd != null)
                {
                    this.observer.addClientCmd(clientCmd);
                }
                else
                {
                    Debug.LogError("ClientCmd not parsed !");
                }
            } catch (Exception e) {
                Debug.LogError("Exception:" + e);
            }
        }
    }
        public void ReadArray()
        {
            byte[] data = MiscellaneousUtils.HexToBytes("31-00-00-00-04-42-53-4f-4e-00-26-00-00-00-02-30-00-08-00-00-00-61-77-65-73-6f-6d-65-00-01-31-00-33-33-33-33-33-33-14-40-10-32-00-c2-07-00-00-00-00");

            BSONObject obj = SimpleBSON.Load(data);

            Assert.IsTrue(obj ["BSON"] is BSONArray);
            Assert.AreEqual(obj ["BSON"][0].stringValue, "awesome");
            Assert.AreEqual(obj ["BSON"][1].doubleValue, 5.05);
            Assert.AreEqual(obj ["BSON"][2].int32Value, 1986);
        }
Beispiel #4
0
        private static UpdateInfo ReadInfo()
        {
            int        XOR_CYPHER = IonConsts.XOR[(int)Consts.ProductID];
            var        bson       = SimpleBSON.Load(File.ReadAllBytes(PathUpdateInfo));
            UpdateInfo upinfo     = new UpdateInfo()
            {
                v = bson["v"].int32Value ^ XOR_CYPHER,                // last version
                n = bson["n"].int32Value ^ XOR_CYPHER,                // NTP
                //r = bson["r"].int32Value^ XOR_CYPHER,// last update date
            };

            return(upinfo);
        }
Beispiel #5
0
        private byte[] OnPacket(byte[] revBuffer, String from)
        {
            // Remove padding and load the bson.
            byte[] data = new byte[revBuffer.Length - 4];
            Buffer.BlockCopy(revBuffer, 4, data, 0, data.Length);

            BSONObject packets = null;

            try
            {
                packets = SimpleBSON.Load(data);
            }catch { }

            if (packets == null || !packets.ContainsKey("mc"))
            {
                return(revBuffer);
            }

            // Modify the packet?
            Console.WriteLine(from + " ========================================================================================");
            for (int i = 0; i < packets["mc"]; i++)
            {
                BSONObject packet = packets["m" + i] as BSONObject;
                ReadBSON(packet);

                if (packet["ID"].stringValue == "OoIP")
                {
                    PIXEL_IP     = (packet["IP"].stringValue == "prod.gamev70.portalworldsgame.com" ? "3.220.252.91" : packet["IP"].stringValue);
                    packet["IP"] = "prod.gamev70.portalworldsgame.com";
                }
            }

            // Dump the BSON and add padding.
            MemoryStream memoryStream = new MemoryStream();

            using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
            {
                byte[] bsonDump = SimpleBSON.Dump(packets);

                binaryWriter.Write(bsonDump.Length + 4);
                binaryWriter.Write(bsonDump);
            }
            return(memoryStream.ToArray());
        }
Beispiel #6
0
        public void ReadBSON(BSONObject SinglePacket, string Parent = "")
        {
            foreach (string Key in SinglePacket.Keys)
            {
                try
                {
                    BSONValue Packet = SinglePacket[Key];

                    switch (Packet.valueType)
                    {
                    case BSONValue.ValueType.String:
                        Console.WriteLine($"{Parent} = {Key} | {Packet.valueType} = {Packet.stringValue}");
                        break;

                    case BSONValue.ValueType.Boolean:
                        Console.WriteLine($"{Parent} = {Key} | {Packet.valueType} = {Packet.boolValue}");
                        break;

                    case BSONValue.ValueType.Int32:
                        Console.WriteLine($"{Parent} = {Key} | {Packet.valueType} = {Packet.int32Value}");
                        break;

                    case BSONValue.ValueType.Int64:
                        Console.WriteLine($"{Parent} = {Key} | {Packet.valueType} = {Packet.int64Value}");
                        break;

                    case BSONValue.ValueType.Binary:     // BSONObject
                        Console.WriteLine($"{Parent} = {Key} | {Packet.valueType}");
                        ReadBSON(SimpleBSON.Load(Packet.binaryValue), Key);
                        break;

                    case BSONValue.ValueType.Double:
                        Console.WriteLine($"{Parent} = {Key} | {Packet.valueType} = {Packet.doubleValue}");
                        break;

                    default:
                        Console.WriteLine($"{Parent} = {Key} = {Packet.valueType}");
                        break;
                    }
                }
                catch { }
            }
        }
Beispiel #7
0
        private static BSONObject ExtractServerStatusFromResponse(TcpClient client)
        {
            using (var binaryReader = new BinaryReader(client.GetStream(), new UTF8Encoding(), true))
            {
                //We don't care about the message length and DB RequestId, skip 2 int32's.
                binaryReader.ReadBytes(8);

                //Response To
                int responseTo = binaryReader.ReadInt32();
                if (responseTo != 0)
                {
                    throw new NAMEException($"{SupportedDependencies.MongoDb}: The server responed with an unexpected response code ({responseTo}).", NAMEStatusLevel.Error);
                }

                //Op Code
                int opCode = binaryReader.ReadInt32();
                if (opCode != 1)
                {
                    throw new NAMEException($"{SupportedDependencies.MongoDb}: The server responed with an unexpected operation code ({opCode}).", NAMEStatusLevel.Error);
                }

                //We don't care about responseFlags, cursorID or startingFrom. Skip them.
                binaryReader.ReadBytes(4 + 8 + 4);

                //Number of documents
                var numberOfDocuments = binaryReader.ReadInt32();
                if (numberOfDocuments != 1)
                {
                    throw new NAMEException($"{SupportedDependencies.MongoDb}: The server responded with an unexpected number of documents ({numberOfDocuments}).", NAMEStatusLevel.Error);
                }

                //The ServerStatus document
                int    size   = binaryReader.ReadInt32();
                byte[] buffer = new byte[size];
                BitConverter.GetBytes(size).CopyTo(buffer, 0);
                binaryReader.Read(buffer, 4, size - 4);

                BSONObject obj = SimpleBSON.Load(buffer);
                return(obj);
            }
        }
        public void WriteAndRead()
        {
            var obj = new BSONObject();

            obj["hello"] = 123;

            obj["where"]          = new BSONObject();
            obj["where"]["Korea"] = "Asia";
            obj["where"]["USA"]   = "America";
            obj["bytes"]          = new byte[41223];

            byte [] buf = SimpleBSON.Dump(obj);
            Console.WriteLine(buf);

            obj = SimpleBSON.Load(buf);

            Console.WriteLine(obj["hello"].int32Value);             // => 123
            Console.WriteLine(obj["where"]["Korea"].stringValue);   // => "Asia"
            Console.WriteLine(obj["where"]["USA"].stringValue);     // => "America"
            Console.WriteLine(obj["bytes"].binaryValue.Length);     // => 128-length bytesbytes
        }
Beispiel #9
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                StateObject state  = (StateObject)ar.AsyncState;
                Socket      client = state.workSocket;

                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    if (bytesRead < 1024)
                    {
                        byte[] data = new byte[bytesRead];
                        Array.Copy(state.buffer, data, bytesRead);

                        BSONObject bObj    = SimpleBSON.Load(data);
                        string     message = bObj["msg"].stringValue;
                        string     note    = bObj["note"].stringValue;


                        switch (message)
                        {
                        case "auth":
                        {
                            int authState = bObj["auth_state"].int32Value;
                            userInfo.username = bObj["username"].stringValue;



                            if (authState == 0)
                            {
                                MessageBox.Show(note, "Growbrew Server");
                            }
                            else
                            {
                                //Environment.Exit(authState);
                            }
                            break;
                        }

                        case "get_online":
                        {
                            int count = bObj["c"];
                            UpdateUserCount(count);
                            break;
                        }

                        case "chat_res":
                        {
                            AppendChat(note);
                            break;
                        }

                        default:
                            break;
                        }
                    }
                    tClient.Client.BeginReceive(stateObj.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), stateObj);
                }
            }
            catch
            {
            }
        }