SharpTox.Core.Tox.Tox C# (CSharp) Method

Tox() public method

Initializes a new instance of Tox.
public Tox ( ToxOptions options, ToxData data, ToxEncryptionKey key = null ) : System
options ToxOptions The options to initialize this instance of Tox with.
data ToxData A byte array containing Tox save data.
key ToxEncryptionKey The key to decrypt the given encrypted Tox profile data. If the data is not encrypted, this should be null.
return System
        public Tox(ToxOptions options, ToxData data, ToxEncryptionKey key = null)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            var optionsStruct = options.Struct;

            if (key == null || !data.IsEncrypted)
            {
                var error = ToxErrorNew.Ok;
                optionsStruct.SetData(data.Bytes, ToxSaveDataType.ToxSave);

                _tox = ToxFunctions.New(ref optionsStruct, ref error);

                if (_tox == null || _tox.IsInvalid || error != ToxErrorNew.Ok)
                    throw new Exception("Could not create a new instance of tox, error: " + error.ToString());
            }
            else
            {
                var error = ToxErrorNew.Ok;
                var decryptError = ToxErrorDecryption.Ok;
                byte[] decryptedData = ToxEncryption.DecryptData(data.Bytes, key, out decryptError);
                optionsStruct.SetData(decryptedData, ToxSaveDataType.ToxSave);

                _tox = ToxFunctions.New(ref optionsStruct, ref error);

                if (_tox == null || _tox.IsInvalid || error != ToxErrorNew.Ok || decryptError != ToxErrorDecryption.Ok)
                    throw new Exception(string.Format("Could not create a new instance of tox, error: {0}, decrypt error: {1}" + error.ToString(), decryptError.ToString()));
            }

            optionsStruct.Free();
            Options = options;
        }

Same methods

Tox::Tox ( ToxOptions options, ToxKey secretKey = null ) : System