updating tox.py

This commit is contained in:
Андрей Владимирович 2016-02-18 00:40:40 +03:00
parent 4e7c6b309f
commit a6f764bbb5

View file

@ -2,51 +2,69 @@
from ctypes import * from ctypes import *
from settings import Settings from settings import Settings
from platform import system from platform import system
from toxcore_enums import *
import os import os
class ToxOptions(Structure): class ToxOptions(Structure):
_fields_ = [ _fields_ = [
("ipv6_enabled", c_bool), ('ipv6_enabled', c_bool),
("udp_enabled", c_bool), ('udp_enabled', c_bool),
("proxy_type", c_int), ('proxy_type', c_int),
("proxy_host", c_char_p), ('proxy_host', c_char_p),
("proxy_port", c_uint16), ('proxy_port', c_uint16),
("start_port", c_uint16), ('start_port', c_uint16),
("end_port", c_uint16), ('end_port', c_uint16),
("tcp_port", c_uint16), ('tcp_port', c_uint16),
("savedata_type", c_int), ('savedata_type', c_int),
("savedata_data", c_char_p), ('savedata_data', c_char_p),
("savedata_length", c_size_t) ('savedata_length', c_size_t)
] ]
class Tox(object): class Tox(object):
def __init__(self, name, path=None): def __init__(self, name, path=None):
# load toxcore
if system() == 'Linux':
temp = os.path.dirname(os.path.abspath(__file__)) + '/libs/'
os.chdir(temp)
self.libtoxcore = CDLL(temp + 'libtoxcore.so')
elif system() == 'Windows':
self.libtoxcore = CDLL('libs/libtox.dll')
self.libtoxcore.tox_options_new.restype = POINTER(ToxOptions)
# load saved data
if path is None: if path is None:
path = Settings.get_default_path() path = Settings.get_default_path()
full_path = path + name + '.tox' full_path = path + name + '.tox'
with open(full_path, 'rb') as fl: with open(full_path, 'rb') as fl:
data = fl.read() savedata = fl.read()
size = len(data) settings = Settings()
print size
if system() == 'Linux': # creating tox options struct
temp = os.path.dirname(os.path.abspath(__file__)) + '/libs/' tox_err = c_int()
os.chdir(temp) self.tox_options = self.libtoxcore.tox_options_new(addressof(tox_err))
print temp if tox_err == TOX_ERR_OPTIONS_NEW['TOX_ERR_OPTIONS_NEW_MALLOC']:
self.libtoxcore = CDLL(temp + 'libtoxcore.so') raise MemoryError('The function failed to allocate enough memory for the options struct.')
elif system() == 'Windows':
self.libtoxcore = CDLL('libs/libtox.dll') # filling tox options struct
print self.libtoxcore.__dict__ self.tox_options.contents.ipv6_enabled = settings['ipv6_enabled']
self.libtoxcore.tox_options_new.restype = POINTER(ToxOptions) self.tox_options.contents.udp_enabled = settings['udp_enabled']
# TODO: load from settings self.tox_options.contents.proxy_type = settings['proxy_type']
self.tox_options = self.libtoxcore.tox_options_new(0) self.tox_options.contents.proxy_host = settings['proxy_host']
self.tox_options.contents.savedata_length = size self.tox_options.contents.proxy_port = settings['proxy_port']
self.tox_options.contents.savedata_data = c_char_p(data) self.tox_options.contents.start_port = settings['start_port']
self.tox_options.contents.end_port = settings['end_port']
self.tox_options.contents.tcp_port = settings['tcp_port']
self.tox_options.contents.savedata_type = TOX_SAVEDATA_TYPE['TOX_SAVEDATA_TYPE_TOX_SAVE']
self.tox_options.contents.savedata_data = c_char_p(savedata)
self.tox_options.contents.savedata_length = len(savedata)
# creating tox object
self.libtoxcore.tox_new.restype = POINTER(c_void_p) self.libtoxcore.tox_new.restype = POINTER(c_void_p)
self.tox = self.libtoxcore.tox_new(self.tox_options, None) # Tox *tox self.tox_pointer = self.libtoxcore.tox_new(self.tox_options, None)
print self.tox.contents
if __name__ == "__main__": if __name__ == '__main__':
t = Tox('tox_save') t = Tox('tox_save')