예제 #1
0
        public override byte[] ToByteArray()
        {
            PayloadWriter writer = new PayloadWriter();

            switch (register)
            {
            case Register.EAX: writer.WriteBytes(OtherOpcodes.MOV_EAX_DWORD_PTR); break;

            case Register.ECX: writer.WriteBytes(OtherOpcodes.MOV_ECX_DWORD_PTR); break;

            case Register.EBP: writer.WriteBytes(OtherOpcodes.MOV_EBP_DWORD_PTR); break;

            case Register.EBX: writer.WriteBytes(OtherOpcodes.MOV_EBX_DWORD_PTR); break;

            case Register.EDI: writer.WriteBytes(OtherOpcodes.MOV_EDI_DWORD_PTR); break;

            case Register.EDX: writer.WriteBytes(OtherOpcodes.MOV_EDX_DWORD_PTR); break;

            case Register.ESI: writer.WriteBytes(OtherOpcodes.MOV_ESI_DWORD_PTR); break;

            case Register.ESP: writer.WriteBytes(OtherOpcodes.MOV_ESP_DWORD_PTR); break;
            }
            writer.WriteInteger(ModifiyValue.Address);
            return(writer.ToByteArray());
        }
예제 #2
0
        internal static byte[] CalculateSecretHash(byte[] Modulus, byte[] Exponent)
        {
            lock (CalculationLock)
            {
                SortedList <byte, int> NumCounter = new SortedList <byte, int>();
                for (int i = 0; i < Modulus.Length; i++)
                {
                    if (!NumCounter.ContainsKey(Modulus[i]))
                    {
                        NumCounter.Add(Modulus[i], 1);
                    }
                    else
                    {
                        NumCounter[Modulus[i]] = (NumCounter[Modulus[i]] + 1) * ((Exponent[i % Exponent.Length] + 3));
                    }
                }

                PayloadWriter pw = new PayloadWriter();
                for (int i = 0; i < NumCounter.Count; i++)
                {
                    long val = NumCounter.Keys[i] + (Modulus[(i * 3) % Modulus.Length] ^ (NumCounter.Keys[i] + NumCounter.Values[i]));
                    pw.WriteDouble(Math.Pow(val, 8));
                }

                SHA512Managed ShaHasher = new SHA512Managed();
                return(ShaHasher.ComputeHash(pw.ToByteArray()));
            }
        }
예제 #3
0
        private void DoLogin()
        {
            if (!cbRememberMe.Checked)
            {
                File.Delete(loginData);
            }
            if (!cbAutoLogin.Checked)
            {
                File.Delete(autoLogin);
            }
            if (cbRememberMe.Checked && !File.Exists(loginData))
            {
                string data = tbUsername.txtbox.Text + Environment.NewLine + tbPassword.txtbox.Text;
                File.WriteAllText(loginData, GlobalVariables.cryptor.Encrypt(data, GlobalVariables.HWID));
            }
            if (cbAutoLogin.Checked && !File.Exists(autoLogin))
            {
                File.WriteAllText(autoLogin, string.Empty);
            }

            using (PayloadWriter pw = new PayloadWriter())
            {
                tbUsername.Enabled = false;
                tbPassword.Enabled = false;
                btnLogin.Enabled   = false;
                pw.WriteByte(0x02);
                pw.WriteString(tbUsername.txtbox.Text);
                pw.WriteString(tbPassword.txtbox.Text);
                pw.WriteString(GlobalVariables.HWID);
                pw.WriteString(GlobalVariables.version);
                GlobalVariables.Username = tbUsername.txtbox.Text;
                GlobalVariables.SendData(pw.ToByteArray());
            }
        }
예제 #4
0
        public override byte[] ToByteArray()
        {
            PayloadWriter writer = new PayloadWriter();

            writer.WriteBytes(OtherOpcodes.MOV_VARIABLE_VALUE);
            writer.WriteInteger(ModifiyValue.Address);
            writer.WriteString(varName);

            //lets serialize the new value
            writer.WriteByte(isRegister ? (byte)1 : (byte)0);

            if (isRegister)
            {
                writer.WriteByte((byte)register);
            }
            else
            {
                MemoryStream mem = new MemoryStream();
                new BinaryFormatter().Serialize(mem, newValue);
                writer.WriteInteger((int)mem.Length);
                writer.WriteBytes(mem.ToArray());
            }

            return(writer.ToByteArray());
        }
예제 #5
0
        /// <summary>
        /// Generate a random encryption/decryption algorithm
        /// </summary>
        /// <param name="Instructions">The max number of instructions to generate, higher=slower</param>
        /// <param name="EncryptCode"></param>
        /// <param name="DecryptCode"></param>
        /// <param name="TestAlgorithm">Test the algorithm for strongness, it will take longer generating the algorithm</param>
        public static void GenerateCryptoCode(int Seed, int Instructions, ref byte[] EncryptCode, ref byte[] DecryptCode, bool TestAlgorithm = true)
        {
            lock (Locky)
            {
                FastRandom rnd = new FastRandom(Seed);
                do
                {
                    using (PayloadWriter EncPw = new PayloadWriter())
                        using (PayloadWriter DecPw = new PayloadWriter())
                        {
                            //generate a random instruction and when generated set the opposide for Decryption
                            for (int i = 0; i < Instructions; i++)
                            {
                                InstructionInfo EncInstruction = null;
                                InstructionInfo DecInstruction = null;
                                GetNextRandomInstruction(rnd, ref EncInstruction, ref DecInstruction);

                                EncPw.WriteBytes(WriteInstruction(EncInstruction));
                                DecPw.WriteBytes(WriteInstruction(DecInstruction));
                            }

                            EncryptCode = EncPw.ToByteArray();
                            DecryptCode = DecPw.ToByteArray();
                        }
                } while (TestAlgorithm && IsAlgorithmWeak(EncryptCode, DecryptCode, Seed));
            }
        }
예제 #6
0
 public static byte[] Serialize(Header header)
 {
     using (MemoryStream ms = new MemoryStream())
         using (PayloadWriter pw = new PayloadWriter())
         {
             Serializer.Serialize(ms, header);
             pw.WriteUShort((ushort)ms.Length);
             pw.WriteBytes(ms.ToArray());
             return(pw.ToByteArray());
         }
 }
예제 #7
0
 private static byte[] WriteInstruction(InstructionInfo Instruction)
 {
     using (PayloadWriter pw = new PayloadWriter())
         using (MemoryStream ms = new MemoryStream())
         {
             Serializer.Serialize(ms, Instruction);
             pw.WriteByte((byte)ms.Length);
             pw.WriteBytes(ms.ToArray());
             return(pw.ToByteArray());
         }
 }
예제 #8
0
        public void WriteDnsFile(string FilePath)
        {
            if (!File.Exists(FilePath))
            {
                File.Create(FilePath).Close();
            }

            PayloadWriter pw = new PayloadWriter();

            for (int i = 0; i < DnsNames.Count; i++)
            {
                pw.WriteString(DnsNames.Values[i].DnsName);
                pw.WriteUInteger(DnsNames.Values[i].DnsId);
            }
            File.WriteAllBytes(FilePath, pw.ToByteArray());
        }
예제 #9
0
        public override byte[] ToByteArray()
        {
            PayloadWriter writer = new PayloadWriter();

            writer.WriteByte((byte)OpcodeList.MOV_EAX_DWORD_PTR);
            writer.WriteInteger(ModifiyValue.Address);
            writer.WriteByte(varName == null ? (byte)0 : (byte)1);
            writer.WriteInteger(Index);

            if (varName != null)
            {
                writer.WriteString(varName);
            }
            writer.WriteByte((byte)register);
            return(writer.ToByteArray());
        }
예제 #10
0
        public override byte[] ToByteArray()
        {
            byte[]        tmp    = new byte[0];
            PayloadWriter writer = new PayloadWriter();

            writer.WriteByte((byte)OpcodeList.PUSH_VALUE);

            if (Value.GetType() == typeof(int))
            {
                tmp = BitConverter.GetBytes((int)Value);
                writer.WriteByte(0);
            }
            else if (Value.GetType() == typeof(uint))
            {
                tmp = BitConverter.GetBytes((uint)Value);
                writer.WriteByte(1);
            }
            else if (Value.GetType() == typeof(byte))
            {
                tmp = BitConverter.GetBytes((byte)Value);
                writer.WriteByte(2);
            }
            else if (Value.GetType() == typeof(short))
            {
                tmp = BitConverter.GetBytes((short)Value);
                writer.WriteByte(3);
            }
            else if (Value.GetType() == typeof(ushort))
            {
                tmp = BitConverter.GetBytes((ushort)Value);
                writer.WriteByte(4);
            }
            else if (Value.GetType() == typeof(ulong))
            {
                tmp = BitConverter.GetBytes((ulong)Value);
                writer.WriteByte(5);
            }
            else if (Value.GetType() == typeof(long))
            {
                tmp = BitConverter.GetBytes((long)Value);
                writer.WriteByte(6);
            }
            writer.WriteBytes(tmp);
            return(writer.ToByteArray());
        }
예제 #11
0
        /// <summary>
        /// Generates a request packet.
        /// </summary>
        /// <returns></returns>
        public DiffieHellman GenerateRequest()
        {
            // Generate the parameters.
            prime = BigInteger.genPseudoPrime(bits, 30, random);
            mine  = BigInteger.genPseudoPrime(bits, 30, random);
            g     = BigInteger.genPseudoPrime(bits, 30, random);

            // Gemerate the string.
            PayloadWriter pw = new PayloadWriter();

            pw.WriteBigInteger(prime);
            pw.WriteBigInteger(g);

            // Generate the send BigInt.
            using (BigInteger send = g.modPow(mine, prime))
            {
                pw.WriteBigInteger(send);
            }
            representation = pw.ToByteArray();
            return(this);
        }
예제 #12
0
        public byte[] Encrypt(byte[] Data, int Offset, int Length)
        {
            lock (AES)
            {
                byte[] NewIV = new byte[16];
                Frandom.NextBytes(NewIV);
                this.IV = NewIV;

                //mask the IV to make it harder to grab the IV while packet sniffing / MITM
                IvConfuser.Obfuscate(ref NewIV, 0);

                using (ICryptoTransform Encryptor = AES.CreateEncryptor())
                {
                    using (PayloadWriter pw = new PayloadWriter(new System.IO.MemoryStream()))
                    {
                        pw.WriteBytes(NewIV);
                        pw.WriteBytes(Encryptor.TransformFinalBlock(Data, Offset, Length));
                        return(pw.ToByteArray());
                    }
                }
            }
        }
예제 #13
0
        /// <summary>
        /// Generate a response packet.
        /// </summary>
        /// <param name="request">The string representation of the request.</param>
        /// <returns></returns>
        public DiffieHellman GenerateResponse(PayloadReader pr)
        {
            // Generate the would-be fields.
            using (BigInteger prime = pr.ReadBigInteger())
                using (BigInteger g = pr.ReadBigInteger())
                    using (BigInteger mine = BigInteger.genPseudoPrime(bits, 30, random))
                    {
                        // Generate the key.
                        using (BigInteger given = pr.ReadBigInteger())
                            using (BigInteger key = given.modPow(mine, prime))
                            {
                                this.key = key.getBytes();
                            }
                        // Generate the response.
                        using (BigInteger send = g.modPow(mine, prime))
                        {
                            PayloadWriter pw = new PayloadWriter();
                            pw.WriteBigInteger(send);
                            this.representation = pw.ToByteArray();
                        }
                    }

            return(this);
        }
예제 #14
0
        private void _Invoke(ref object RetObject, params object[] args)
        {
            if (args.Length < ArgumentTypes.Length) //check if a argument is using "params x[] args"
            {
                throw new Exception("missing arguments");
            }

            List <int>    usedDelegates = new List <int>();
            PayloadWriter pw            = new PayloadWriter();

            pw.WriteInteger(sharedClass.SharedId);
            pw.WriteInteger(MethodId);
            pw.WriteByte(isDelegate ? (byte)1 : (byte)0);

            if (isDelegate)
            {
                pw.WriteInteger(this.DelegateId);
                pw.WriteInteger(this.sharedClass.SharedId);
            }

            SmartSerializer serializer = new SmartSerializer();

            for (int i = 0; i < args.Length; i++)
            {
                object obj = ArgumentTypes[i].IsByRef ? null : args[i];

                if (DelegateIndex.ContainsKey(i))
                {
                    obj = null;
                }

                byte[] SerializedObj = serializer.Serialize(obj);
                pw.WriteInteger(SerializedObj.Length);
                pw.WriteBytes(SerializedObj);
            }

            for (int i = 0; i < DelegateIndex.Count; i++)
            {
                Delegate del = args[DelegateIndex.Keys[i]] as Delegate;

                if (del != null)
                {
                    if (del.Method == null)
                    {
                        throw new Exception("Target delegate is NULL");
                    }

                    int id = rnd.Next();
                    while (Delegates.ContainsKey(id))
                    {
                        id = rnd.Next();
                    }

                    pw.WriteByte(1);
                    SharedDelegate sharedDel = new SharedDelegate(del.Method, sharedClass, del.GetType(), id, del, this.MethodId);
                    sharedDel.sharedMethod.Unchecked      = DelegateIndex.Values[i].isUnchecked;
                    sharedDel.sharedMethod.usePacketQueue = DelegateIndex.Values[i].UsePacketQueue;
                    sharedDel.sharedMethod.useUdp         = DelegateIndex.Values[i].UseUDP;
                    sharedDel.sharedMethod.NoWaitingTime  = DelegateIndex.Values[i].NoWaitingTime;
                    pw.WriteObject(sharedDel);

                    if (!isDelegate)
                    {
                        Delegates.Add(id, sharedDel);
                    }
                    continue;
                }
                pw.WriteByte(0);
            }

            if (Unchecked || useUdp)
            {
                //just execute the method and don't wait for response
                sharedClass.connection.Connection.SendMessage(new MsgExecuteMethod(0, pw.ToByteArray(), false));
            }
            else
            {
                SyncObject syncObject = null;
                Random     rnd        = new Random();
                int        RequestId  = rnd.Next();
                lock (sharedClass.connection.MethodRequests)
                {
                    while (sharedClass.connection.MethodRequests.ContainsKey(RequestId))
                    {
                        RequestId = rnd.Next();
                    }
                    syncObject = new SyncObject(sharedClass.connection.Connection.Connection);
                    sharedClass.connection.MethodRequests.Add(RequestId, syncObject);
                    sharedClass.connection.Connection.SendMessage(new MsgExecuteMethod(RequestId, pw.ToByteArray(), true));
                }
                RetObject = syncObject.Wait <ReturnResult>(null, 0);
            }

            /*if (callback != null)
             * {
             *  sharedClass.connection.BeginSendRequest(pw, callback, true, this.usePacketQueue);
             * }
             * else
             * {
             *  if (Unchecked || useUdp)
             *  {
             *      //just don't wait till we received something back since it's a VOID anyway
             *      sharedClass.connection.BeginSendRequest(pw, (object obj) => { }, false, this.usePacketQueue);
             *  }
             *  else
             *  {
             *      RetObject = sharedClass.connection.SendRequest(pw, this.usePacketQueue);
             *  }
             * }*/
            serializer = null;
        }
예제 #15
0
        public ClientProperties(string HostIp, ushort Port, Type BaseChannel, object[] BaseChannelArgs, byte[] PrivateKey, Stream[] KeyFiles = null,
                                ProxySettings proxySettings = null, int ConnectingTimeout = 30000, string Username = "", string Password = "",
                                bool AllowChannels          = true, bool AllowPeers = true)
        {
            this.HostIp            = HostIp;
            this.Port              = Port;
            this.BaseChannel       = BaseChannel;
            this.BaseChannelArgs   = BaseChannelArgs;
            this.proxySettings     = proxySettings;
            this.ConnectingTimeout = ConnectingTimeout;
            this.PrivateKey        = PrivateKey;
            this.Username          = Username;
            this.KeyFiles          = KeyFiles;
            this.AllowChannels     = AllowChannels;
            this.AllowPeers        = AllowPeers;

            if (Password != null && Password.Length > 0)
            {
                byte[]        basePass = ASCIIEncoding.ASCII.GetBytes(Convert.ToBase64String(ASCIIEncoding.Unicode.GetBytes(Password)));
                PayloadWriter keyPW    = new PayloadWriter();
                keyPW.WriteUInteger(new MurmurHash2Unsafe().Hash(BitConverter.GetBytes(new SuperFastHashUInt16Hack().Hash(basePass))));
                keyPW.WriteUInteger(new MurmurHash2Unsafe().Hash(BitConverter.GetBytes(new MurmurHash2Simple().Hash(basePass))));
                keyPW.WriteUInteger(new SuperFastHashInlineBitConverter().Hash(BitConverter.GetBytes(new SuperFastHashUInt16Hack().Hash(basePass))));
                keyPW.WriteBytes(SHA512Managed.Create().ComputeHash(keyPW.ToByteArray()));
                byte[] key = keyPW.ToByteArray();

                unsafe
                {
                    fixed(byte *x = basePass)
                    {
                        for (int i = 0; i < basePass.Length; i++)
                        {
                            x[i] += (byte)((x[i] ^ key[i % key.Length]) % 0xFF);
                            x[i] ^= (byte)((x[(i + 1) % basePass.Length] / 2) * key[i % key.Length]);
                            x[i] ^= (byte)Password[i % Password.Length];
                        }
                    }
                }

                //generate random bytes at the end based on the hash
                PayloadWriter Out = new PayloadWriter();
                Out.WriteBytes(basePass);

                byte[] GenKey = new byte[32];
                for (int i = 0; i < GenKey.Length; i++)
                {
                    GenKey[i] = (byte)((basePass[i % basePass.Length] + key[i % key.Length]) % 0xFF);

                    if ((GenKey[((i * 2) / 3) % GenKey.Length] ^ (byte)(key[i % key.Length] ^ GenKey[i])) > 0)
                    {
                        GenKey[((i * 2) / 3) % GenKey.Length] ^= (byte)(key[i % key.Length] ^ GenKey[i]);
                    }
                    else
                    {
                        GenKey[(i * 3) % GenKey.Length] ^= (byte)((basePass[i % basePass.Length] << 8) % 0xFF);
                    }
                }
                Out.WriteBytes(GenKey);

                this.Password = BitConverter.ToString(Out.ToByteArray()).Replace("-", "");
            }
            Password = ""; //remove from memory hopefully
        }
예제 #16
0
        private void _Invoke(ref object RetObject, params object[] args)
        {
            if (args.Length < ArgumentTypes.Length) //check if a argument is using "params x[] args"
                throw new Exception("missing arguments");

            List<int> usedDelegates = new List<int>();
            PayloadWriter pw = new PayloadWriter();
            pw.WriteInteger(sharedClass.SharedId);
            pw.WriteInteger(MethodId);
            pw.WriteByte(isDelegate ? (byte)1 : (byte)0);

            if (isDelegate)
            {
                pw.WriteInteger(this.DelegateId);
                pw.WriteInteger(this.sharedClass.SharedId);
            }

            SmartSerializer serializer = new SmartSerializer();
            for (int i = 0; i < args.Length; i++)
            {
                object obj = ArgumentTypes[i].IsByRef ? null : args[i];

                if (DelegateIndex.ContainsKey(i))
                    obj = null;

                byte[] SerializedObj = serializer.Serialize(obj);
                pw.WriteInteger(SerializedObj.Length);
                pw.WriteBytes(SerializedObj);
            }

            for (int i = 0; i < DelegateIndex.Count; i++)
            {
                Delegate del = args[DelegateIndex.Keys[i]] as Delegate;

                if (del != null)
                {
                    if (del.Method == null)
                        throw new Exception("Target delegate is NULL");

                    int id = rnd.Next();
                    while(Delegates.ContainsKey(id))
                        id = rnd.Next();

                    pw.WriteByte(1);
                    SharedDelegate sharedDel = new SharedDelegate(del.Method, sharedClass, del.GetType(), id, del, this.MethodId);
                    sharedDel.sharedMethod.Unchecked = DelegateIndex.Values[i].isUnchecked;
                    sharedDel.sharedMethod.usePacketQueue = DelegateIndex.Values[i].UsePacketQueue;
                    sharedDel.sharedMethod.useUdp = DelegateIndex.Values[i].UseUDP;
                    sharedDel.sharedMethod.NoWaitingTime = DelegateIndex.Values[i].NoWaitingTime;
                    pw.WriteObject(sharedDel);

                    if (!isDelegate)
                    {
                        Delegates.Add(id, sharedDel);
                    }
                    continue;
                }
                pw.WriteByte(0);
            }

            if (Unchecked || useUdp)
            {
                //just execute the method and don't wait for response
                sharedClass.connection.Connection.SendMessage(new MsgExecuteMethod(0, pw.ToByteArray(), false));
            }
            else
            {
                SyncObject syncObject = null;
                Random rnd = new Random();
                int RequestId = rnd.Next();
                lock (sharedClass.connection.MethodRequests)
                {
                    while(sharedClass.connection.MethodRequests.ContainsKey(RequestId))
                        RequestId = rnd.Next();
                    syncObject = new SyncObject(sharedClass.connection.Connection.Connection);
                    sharedClass.connection.MethodRequests.Add(RequestId, syncObject);
                    sharedClass.connection.Connection.SendMessage(new MsgExecuteMethod(RequestId, pw.ToByteArray(), true));
                }
                RetObject = syncObject.Wait<ReturnResult>(null, 0);
            }

            /*if (callback != null)
            {
                sharedClass.connection.BeginSendRequest(pw, callback, true, this.usePacketQueue);
            }
            else
            {
                if (Unchecked || useUdp)
                {
                    //just don't wait till we received something back since it's a VOID anyway
                    sharedClass.connection.BeginSendRequest(pw, (object obj) => { }, false, this.usePacketQueue);
                }
                else
                {
                    RetObject = sharedClass.connection.SendRequest(pw, this.usePacketQueue);
                }
            }*/
            serializer = null;
        }
예제 #17
0
        private void BuildFile()
        {
            this.Enabled = false;
            string uploadKey = tbUploadKey.txtbox.Text;
            string time      = nudInterval.Value.ToString();
            string mutex     = Random();

            if (string.IsNullOrEmpty(uploadKey) || string.IsNullOrEmpty(time))
            {
                this.Enabled = true;
                return;
            }
            tbBuildLog.Text += "> Upload Key: " + uploadKey + Environment.NewLine;
            tbBuildLog.Text += "> Log Interval: " + time + Environment.NewLine;
            tbBuildLog.Text += "> Mutex: " + mutex + Environment.NewLine;


            bool   installFile = cbInstallFile.Checked;
            string processName = tbProcessName.txtbox.Text;
            string folder      = tbFolder.txtbox.Text;
            string directory   = cbDirectory.Text;

            bool   hkcu    = cbHKCU.Checked;
            bool   hklm    = cbHKLM.Checked;
            string hkcuKey = tbHKCU.txtbox.Text;
            string hklmKey = tbHKLM.txtbox.Text;

            bool meltFile        = cbMeltFile.Checked;
            bool antis           = cbAntis.Checked;
            bool sendScreenshots = cbSendScreenshots.Checked;
            bool hideFile        = cbHideFile.Checked;
            bool pinlogger       = cbPinlogger.Checked;

            bool stealers = cbStealers.Checked;

            string title       = tbTitle.txtbox.Text;
            string description = tbDescription.txtbox.Text;
            string product     = tbProduct.txtbox.Text;
            string copyright   = tbCopyright.txtbox.Text;
            string version     = tbVersion.txtbox.Text;
            string guid        = tbGUID.txtbox.Text;

            string iconPath   = tbIconPath.txtbox.Text;
            bool   changeIcon = !string.IsNullOrEmpty(tbIconPath.txtbox.Text);

            byte[] iconFile = null;
            if (changeIcon)
            {
                iconFile = File.ReadAllBytes(iconPath);
            }

            using (PayloadWriter pw = new PayloadWriter())
            {
                pw.WriteByte(0x03);
                pw.WriteString(uploadKey);
                pw.WriteString(time);
                pw.WriteString(mutex);
                pw.WriteBool(installFile);
                if (installFile)
                {
                    pw.WriteString(processName);
                    pw.WriteString(folder);
                    pw.WriteString(directory);
                }
                pw.WriteBool(hkcu);
                if (hkcu)
                {
                    pw.WriteString(hkcuKey);
                }
                pw.WriteBool(hklm);
                if (hklm)
                {
                    pw.WriteString(hklmKey);
                }


                pw.WriteBool(meltFile);
                pw.WriteBool(antis);
                pw.WriteBool(sendScreenshots);
                pw.WriteBool(hideFile);
                pw.WriteBool(pinlogger);

                pw.WriteBool(stealers);


                pw.WriteString(title);
                pw.WriteString(description);
                pw.WriteString(product);
                pw.WriteString(copyright);
                pw.WriteString(version);
                pw.WriteString(guid);

                pw.WriteBool(changeIcon);

                if (changeIcon)
                {
                    pw.WriteInteger(iconFile.Length);
                    pw.WriteBytes(iconFile);
                }

                byte[] packet = pw.ToByteArray();
                tbBuildLog.Text += "> Sending packet size: " + packet.Length + Environment.NewLine;
                GlobalVariables.SendData(packet);
            }
        }
예제 #18
0
        private void _Invoke(ref object RetObject, params object[] args)
        {
            if (args.Length < ArgumentTypes.Length) //check if a argument is using "params x[] args"
            {
                throw new Exception("missing arguments");
            }

            List <int>    usedDelegates = new List <int>();
            PayloadWriter pw            = new PayloadWriter();

            SmartSerializer serializer = new SmartSerializer();

            for (int i = 0; i < args.Length; i++)
            {
                object obj = ArgumentTypes[i].IsByRef ? null : args[i];

                if (DelegateIndex.ContainsKey(i))
                {
                    obj = null;
                }

                byte[] SerializedObj = serializer.Serialize(obj);
                pw.WriteInteger(SerializedObj.Length);
                pw.WriteBytes(SerializedObj);
            }

            for (int i = 0; i < DelegateIndex.Count; i++)
            {
                Delegate del = args[DelegateIndex.Keys[i]] as Delegate;

                if (del != null)
                {
                    if (del.Method == null)
                    {
                        throw new Exception("Target delegate is NULL");
                    }

                    int id = rnd.Next();
                    while (Delegates.ContainsKey(id))
                    {
                        id = rnd.Next();
                    }

                    pw.WriteBool(true);
                    SharedDelegate sharedDel = new SharedDelegate(del.Method, sharedClass, del.GetType(), id, del, this.MethodId);
                    sharedDel.sharedMethod.Unchecked      = this.Unchecked;      //DelegateIndex.Values[i].isUnchecked;
                    sharedDel.sharedMethod.usePacketQueue = this.usePacketQueue; //DelegateIndex.Values[i].UsePacketQueue;
                    sharedDel.sharedMethod.useUdp         = this.useUdp;         //DelegateIndex.Values[i].UseUDP;
                    pw.WriteObject(sharedDel);

                    if (!isDelegate)
                    {
                        Delegates.Add(id, sharedDel);
                    }
                    continue;
                }
                pw.WriteBool(false);
            }

            try
            {
                if (Unchecked || useUdp)
                {
                    //just execute the method and don't wait for response
                    sharedClass.Client.Send(new MsgExecuteMethod(0, pw.ToByteArray(), false, sharedClass.SharedId, MethodId, this.DelegateId, this.sharedClass.SharedId));
                }
                else
                {
                    SyncObject syncObject = null;
                    Random     rnd        = new Random();
                    int        RequestId  = rnd.Next();
                    lock (sharedClass.Client.Requests)
                    {
                        while (sharedClass.Client.Requests.ContainsKey(RequestId))
                        {
                            RequestId = rnd.Next();
                        }
                        syncObject = new SyncObject(sharedClass.Client);
                        sharedClass.Client.Requests.Add(RequestId, syncObject);
                        sharedClass.Client.Send(new MsgExecuteMethod(RequestId, pw.ToByteArray(), true, sharedClass.SharedId, MethodId, this.DelegateId, this.sharedClass.SharedId));
                    }
                    RetObject = syncObject.Wait <ReturnResult>(null, TimeOutLength);

                    if (syncObject.TimedOut)
                    {
                        //copying the object in memory, maybe a strange way todo it but it works
                        RetObject = new ReturnResult(serializer.Deserialize(serializer.Serialize(this.TimeOutValue)), false);
                    }
                }
            }
            catch
            {
                //client most likely disconnected and was unable to send the message
                RetObject = null;
            }

            /*if (callback != null)
             * {
             *  sharedClass.connection.BeginSendRequest(pw, callback, true, this.usePacketQueue);
             * }
             * else
             * {
             *  if (Unchecked || useUdp)
             *  {
             *      //just don't wait till we received something back since it's a VOID anyway
             *      sharedClass.connection.BeginSendRequest(pw, (object obj) => { }, false, this.usePacketQueue);
             *  }
             *  else
             *  {
             *      RetObject = sharedClass.connection.SendRequest(pw, this.usePacketQueue);
             *  }
             * }*/
            serializer = null;
        }