TokTok / c-toxcore

The future of online communications.
https://tox.chat
GNU General Public License v3.0
2.21k stars 280 forks source link

unbootstrap #2733

Open og1en opened 3 months ago

og1en commented 3 months ago

I need turn off network but not destroy tox structure. I need turn on/off network many times in my program, not only one. How turn off automatic reconecting network ?

Green-Sky commented 3 months ago

You cant. You will have to destroy the tox object to be offline, and when you want to come online again you have to recreate it.

You can try using TCP only, as this might reduce traffic. Also NewGroupChats support disconnecting without destroying its state.

nurupo commented 3 months ago

If you want to go offline without destroying the Tox instance, simply stop calling tox_iterate(), which does all the Tox logic. Your Tox instance would stop all the processing and responding to network requests, so to others it would seem as if you suddenly lost the Internet connection and you will eventually time out and be shown as offline on their end.

If you want to go offline immediately however, without others still seeing you as being online until some timeout amount of time passes on their end, the Tox instance would need to send packets to others telling that it's shutting down, which is done only when the Tox instance is destroyed, there is no option to send those without destroying it. What you can do in this case to still access the Tox struct is re-create it back after destroying it but with network settings that prevent it from connecting to anything. So before you destroy the Tox instance you would call tox_get_savedata() to save its state, then destroy the Tox instance, then create a new Tox instance off the saved state, with UDP and LAN discovery options disabled and a bogus proxy that Tox would fail to connect to specified, avoiding calling any bootstrap/add_tcp_relay function calls as that would be pointless without a network.

fab1an2 commented 3 months ago

Tox struct is re-create it back after destroying it but with network settings that prevent it from connecting to anything.

Can You explain? what options?

It seems that such a feature is indeed missing and would be worth adding. Go offline mode. Just not using the main loop is not elegant or fast. Besides, you still have open ports and may be charged for Internet usage. Especially painful when roaming.

Green-Sky commented 3 months ago

Just not using the main loop is not elegant or fast. Besides, you still have open ports and may be charged for Internet usage.

The "don't call iterate()" option seems to be the easiest one. Open ports should not get you charged anything by themself, since the ports are local. But eg your friends who keep pinging you until you timeout might cause you traffic. Not calling iterate() or any send*() function should be good for the long run though.

iphydf commented 3 months ago

Why do you need the Tox structure to not be destroyed?

zoff99 commented 3 months ago

@og1en can you please give more details? are you using a tox client? if so which one? or are you developing your own program using the toxcore library?

if so, as mentioned, when you stop calling tox_iterate() in your code then tox will not do any networking activity anymore (connections will timeout).

automatic reconnecting and bootstrapping is only done in tox clients.

iphydf commented 3 months ago

@fab1an2 I see you :-1: my question. Was it a bad question? I'm trying to understand how to solve the problem.

fab1an2 commented 3 months ago

I'm trying to understand how to solve the problem. If you actually want to help add the missing function.

I don't understand people who have unnecessary discussions and in the end nothing comes out of it anyway.

iphydf commented 3 months ago

The requirements I see so far are:

  1. Keep the Tox structure alive.
  2. Close all network ports (and thus sockets).
  3. Notify other clients quickly about your going offline.
  4. (maybe?) Quickly reconnect to the network when going back online.

The 4th requirement means you will need to reopen those ports/sockets and rebuild the network connections, which will take around 10 seconds (regular bootstrap). Overall, you can achieve everything except 1) by doing tox_get_savedata (which stores some DHT endpoints so it can quickly bootstrap back into the network), then tox_kill (closes sockets and tells clients about going offline), and then tox_new with the saved DHT nodes.

So the question remains: why do you want the struct to remain alive?

An alternative question might be, since you're looking for a feature to be added to toxcore: do you want a function in toxcore that implements the method I described above?

og1en commented 3 months ago

I'm not undestand what 4. point will be. For me important is only 1 and 2 point.

I pay for open ports, for something being sent over the network, etc.

I would like my program to be able to create messages but not send them. Just like thunderbird https://www.thunderbird.net/ . I can write a dozen emails and only when I have the network on, or when I realistically hit the exchange button only then do I send and receive data. And so I can quietly turn on the program, write data.

I do not need to inform that I change the status to busy. Although it is possible. It is just that for me it is not the most important thing. I simply want to disable the network plug from the wall for this particular program. Go offline but still be able to write messages and read them.

{
create_tox();
atexit(koniec_tox());

while(not_end)
  {
  <bootstrap(....)>
  <send-receive messages>
  <unbootstrap(....) or similar>
  }

 tox_kill(tox);
}
JFreegman commented 3 months ago

I would like my program to be able to create messages but not send them. Just like thunderbird https://www.thunderbird.net/ . I can write a dozen emails and only when I have the network on, or when I realistically hit the exchange button only then do I send and receive data. And so I can quietly turn on the program, write data.

You don't need to disconnect from the tox network to achieve this. You can simply write a message queue that holds your messages until your trigger condition, then batch sends them. Your trigger condition could be a manual button that you click, or it could be upon detection that the message's respective contact has come online.

iphydf commented 3 months ago

Tox does not store sent messages permanently. In particular, if you are not online or your friend is not online, you can't send messages through toxcore. If you send a message when the friend appears online, but they don't actually receive it (e.g. network issues), then toxcore will drop the message on the floor (you don't get a delivery receipt then).

It sounds like what you want is a storage to put messages in until you're online and able to send them all in a batch. This is something usually implemented in clients rather than in toxcore. If you want to close network sockets, then tox_kill will do that for you.

Given that Toxcore is not able to enqueue messages when offline, is there a reason to keep the struct alive even when offline?

nurupo commented 3 months ago

I pay for open ports, for something being sent over the network, etc.

Is it like VPN port forwarding, where the VPN service assigns you an open TCP port to use? If so, depending on how the service you use exactly works, you might not need to open any ports as Tox does NAT hole-punching, and in the case that doesn't work, you could use a TCP relay.

I would like my program to be able to create messages but not send them. Just like thunderbird https://www.thunderbird.net/ .

Just want to make sure you understand that Tox doesn't work like email. Tox doesn't support offline/asynchronous messaging. For your messages to be sent, both the sender and the receiver have to be online at the same time. Sender sends messages directly to the receiver, there is no intermediary that will hold sender's messages and relay them to the receiver once they are online. If the receiver or the sender (or both) are offline, no message can be sent or received.

I can write a dozen emails and only when I have the network on, or when I realistically hit the exchange button only then do I send and receive data.

Toxcore won't let you queue up messages to be sent while you or the receiver are offline, it's designed to send the messages right away. If you need such a message queue, that's something you have implement on top of Toxcore in your own code.

fab1an2 commented 3 months ago

As I thought. Instead of helping you multiply problems. Guest wrote that he wants to have like offline in firefox and thunderbird. Your good advice is something completely different than necessary. Eh, not worth writing. If someone doesn't want to help then he just writes nonsense.

fab1an2 commented 3 months ago

It sounds like what you want is a storage to put messages in until you're online and able to send them all in a batch. This is something usually implemented in clients rather than in toxcore.

After all, he writes the client. And toxcore does not include the ability to stop sending messages as he requested. It looks like you've never seen with your eyes how it works in an IRC client, either mail or otherwise. Imagine that people have been using computers and networks longer than you.

nurupo commented 3 months ago

@fab1an2 please let @og1en speak for themselves. No need to butt-in into someone else's issue discussion and be a passive-aggressive jerk about it. If you are incapable of corresponding in respectful, or at least non-condescending, way, you are not welcome to comment here. Take @og1en's comments as an example of how to behave. I'm marking your comments as off-topic as they don't seem to be adding anything useful to the discussion. Same with this comment I'm making.