// New returns a new Tox instance. func New(o *Options) (*Tox, error) { var ctox *C.Tox if o != nil { // Let's map o from Options to C.Tox_Options cIPv6Enabled := (C.uint8_t)(0) if o.IPv6Enabled { cIPv6Enabled = (C.uint8_t)(1) } cUDPDisabled := (C.uint8_t)(0) if o.UDPDisabled { cUDPDisabled = (C.uint8_t)(1) } cProxyEnabled := (C.uint8_t)(0) if o.ProxyEnabled { cProxyEnabled = (C.uint8_t)(1) } cProxyPort := (C.uint16_t)(o.ProxyPort) // Max ProxyAddress length is 255 if len(o.ProxyAddress) > 255 { return nil, ErrArgs } // Ugly hack cProxyAddress := [256]C.char{} for i, c := range o.ProxyAddress { cProxyAddress[i] = C.char(c) } // NULL terminated C.char array required. cProxyAddress[len(o.ProxyAddress)] = 0 co := &C.Tox_Options{ ipv6enabled: cIPv6Enabled, udp_disabled: cUDPDisabled, proxy_enabled: cProxyEnabled, proxy_address: cProxyAddress, proxy_port: cProxyPort} ctox = C.tox_new(co) } else { ctox = C.tox_new(nil) } if ctox == nil { return nil, ErrInit } t := &Tox{tox: ctox} return t, nil }
// Create or restore a Tox instance. This will bring the instance into a valid // state. If the startup options parameter is null, then the default options // are used. If the data parameter is not null, then this function will attempt // to restore the instance from that state. The user data parameter can be used // to pass custom information to callback functions. func New(options *ToxOptions, data []byte, userData unsafe.Pointer) (tox *Tox, throw error) { // Create some uninitialized variables for interfacing with C. var c_options *C.struct_Tox_Options var c_data *C.uint8_t var c_length C.size_t var c_tox *C.Tox var c_error C.TOX_ERR_NEW // Check if any custom startup options were provided. if options != nil { // Convert the startup options to their C-side equivilant. c_options = options.cOptions() defer C.free(unsafe.Pointer(c_options.proxy_host)) } // Check if any previously saved data was provided. if data != nil { // Assign the pointer to the location of that data. c_data = (*C.uint8_t)(&data[0]) } // Get the length of the previously saved data. c_length = C.size_t(len(data)) // Create a Tox instance. c_tox = C.tox_new(c_options, c_data, c_length, &c_error) // Check if an error occurred. if c_error != C.TOX_ERR_NEW_OK { // Assign the error to the return value. switch c_error { case C.TOX_ERR_NEW_NULL: throw = ToxErrNewNull case C.TOX_ERR_NEW_MALLOC: throw = ToxErrNewMalloc case C.TOX_ERR_NEW_PORT_ALLOC: throw = ToxErrNewPortAlloc case C.TOX_ERR_NEW_PROXY_BAD_TYPE: throw = ToxErrNewProxyBadType case C.TOX_ERR_NEW_PROXY_BAD_HOST: throw = ToxErrNewProxyBadHost case C.TOX_ERR_NEW_PROXY_BAD_PORT: throw = ToxErrNewProxyBadPort case C.TOX_ERR_NEW_PROXY_NOT_FOUND: throw = ToxErrNewProxyNotFound case C.TOX_ERR_NEW_LOAD_ENCRYPTED: throw = ToxErrNewLoadEncrypted case C.TOX_ERR_NEW_LOAD_BAD_FORMAT: throw = ToxErrNewLoadBadFormat default: throw = ToxErrUnknown } } else { // Assign the Tox instance to the return value. tox = &Tox{handle: c_tox, UserData: userData} } return }