Commit Graph

435 Commits

Author SHA1 Message Date
Lonami Exo
0a567fcd7c Make creating a new sender cleaner 2017-09-30 12:08:06 +02:00
Lonami Exo
5da300ca84 Make MtProtoSender not thread-safe
Rationale: a new connection should be spawned if one desires to
send and receive requests in parallel, which would otherwise cause
one of either threads to lock.
2017-09-30 11:50:11 +02:00
Lonami Exo
003e231239 Attempt at cleaning up TelegramBareClient.invoke() 2017-09-30 11:45:35 +02:00
Lonami Exo
61033b2f56 Allow disabling spawning a second thread 2017-09-30 11:28:15 +02:00
Lonami Exo
a3ae56ca9e Use a timeout when worker threads are polling 2017-09-30 11:21:07 +02:00
Lonami Exo
7cef5885fa Rename process_updates/polling to workers 2017-09-30 11:17:31 +02:00
Lonami Exo
72b7e99222 Ensure the worker threads have updates once they acquire the lock 2017-09-30 10:59:33 +02:00
Lonami Exo
9560bcc324 Remove "if background thread" check as it isn't exposed anymore 2017-09-30 10:27:46 +02:00
Lonami Exo
b87a798dd5 Spawn new worker threads to handle updates instead using ReadThread 2017-09-30 10:27:46 +02:00
Lonami Exo
479afddf50 Move core functionality to the TelegramBareClient
Rationale: the intended behaviour of the TelegramClient will now
be to focus on abstracting the users from manually importing
requests and types to work with Telegram's API. Thus, all the
core functionality has been moved to the TelegramBareClient,
which will now be responsible of spawning new threads or
connections and even handling updates.

This way there is a clear distinction between the two clients,
TelegramClient is the one meant to be exposed to the end user,
since it provides all the mentioned abstractions, while the
TelegramBareClient is the "basic" client needed to work with
the API in a comfortable way.

There is still a need for an MtProtoSender, which still even
lower level, and knows as little as possible of what requests
are. This handles parsing the messages received from the
server so that their result can be understood.
2017-09-30 10:27:46 +02:00
Lonami Exo
b61deb5cfb Delete methods to create_new_connection and invoke_on_dc 2017-09-30 10:27:46 +02:00
Lonami Exo
0a693c705a Create a new connection when called from a different thread
This allows to invoke several requests in parallel while not
waiting for other requests to be written to the network.
2017-09-30 10:27:46 +02:00
Lonami Exo
ee08232473 Fix UpdateState.check_error popping the wrong side 2017-09-30 10:27:16 +02:00
Lonami Exo
6df38da793 Fix session failing to load if no auth_key present 2017-09-29 19:54:40 +02:00
Lonami Exo
56103845de Fix LogOutRequest consuming all retries (#270) 2017-09-29 13:58:15 +02:00
Lonami Exo
61e5c73658 Update to v0.14.2 2017-09-29 13:13:05 +02:00
Lonami Exo
afc4bd9cab Rename constructor/subclass_of_id to upper case, keep only static 2017-09-29 13:11:33 +02:00
Lonami Exo
b3f04fd359 Raise AssertionError instead TypeNotFoundError on authenticator 2017-09-29 13:07:21 +02:00
Lonami Exo
3c7dd25982 Revert "if * is None" check (8bff10d) on get_input_* due to 423efc4 2017-09-29 12:02:26 +02:00
Lonami Exo
dc5bbc1719 Update to v0.14.1 2017-09-28 12:02:47 +02:00
Lonami Exo
cc337328c6 Rename handmade Message class to TLMessage to avoid confusion 2017-09-28 11:59:24 +02:00
Lonami Exo
71c2a82b3d Completely remove BinaryWriter from the project 2017-09-28 11:49:45 +02:00
Lonami Exo
2fd03e5422 Stop using BinaryWriter on MtProtoPlainSender 2017-09-28 11:43:06 +02:00
Lonami Exo
fe30f63d5d Use autogen code on the authenticator instead hardcoding requests 2017-09-28 11:36:51 +02:00
Lonami Exo
fb0898b9cb Don't distinguish between str and bytes when serializing
This makes it easier to use some requests like ReqPqRequest which
needs a string of bytes, not a valid utf-8 string per se.
2017-09-28 09:55:29 +02:00
Lonami Exo
1518be0b95 Use .appendleft(x) for deque instead .insert(0, x) (fix #268) 2017-09-28 09:30:47 +02:00
Lonami Exo
c667a00281 Avoid using BinaryWriter where possible 2017-09-27 21:23:59 +02:00
Lonami Exo
8a605f21e6 Remove BinaryWriter dependency on MessageContainer 2017-09-27 21:06:01 +02:00
Lonami Exo
833aeb86eb Fix auth_key is unsigned 2017-09-27 21:04:52 +02:00
Lonami Exo
bd3dd371a2 Create a proper Message class (msg_id, seqno, body; only .to_bytes()) 2017-09-27 21:01:20 +02:00
Lonami Exo
b0839a028e Update to v0.14 2017-09-27 13:51:59 +02:00
Lonami Exo
795cb98909 Move MessageContainer.on_send inside its .to_bytes 2017-09-27 13:49:58 +02:00
Lonami Exo
6df9fc558e Create and use a new GzipPacked class, also when sending 2017-09-27 13:46:53 +02:00
Lonami Exo
7b736aa6ef Fix MessageContainer having the wrong constructor id 2017-09-27 13:46:02 +02:00
Lonami Exo
efca981312 Fix adding a request twice (or container) to ._pending_receive 2017-09-27 13:26:35 +02:00
Lonami Exo
0c11967232 Remove unused .shaes attribute from CdnDecrypter 2017-09-27 12:11:16 +02:00
Lonami Exo
657e8a70c9 Fix CdnDecrypter incorrectly accessing clients' timeout (closes #265) 2017-09-27 12:08:37 +02:00
Lonami Exo
98c2e1dd4f Replace 4 .append calls with a single one when serializing bytes 2017-09-26 14:41:11 +02:00
Lonami Exo
b83cd98ba0 Replace TLObject.on_send with the new .to_bytes()
This also replaces some int.to_bytes() calls with a faster
struct.pack (up to x4 faster). This approach is also around
x3 faster than creating a BinaryWriter just to serialize a
TLObject as bytes.
2017-09-26 14:36:02 +02:00
Lonami Exo
f233110732 Allow sending several requests at once through new MessageContainer 2017-09-25 20:52:27 +02:00
Lonami Exo
1d50bba8bc Add get_input_* methods for Media and such 2017-09-25 13:43:03 +02:00
Lonami Exo
edcd23f94c Making timeout when reporting an error shorter 2017-09-25 12:00:09 +02:00
Lonami Exo
493f5cec1f Fix-up target method typo 2017-09-25 11:57:42 +02:00
Lonami Exo
01c91bb895 Report errors in the background not to interfer with users (#262) 2017-09-25 11:56:44 +02:00
Lonami Exo
c0ee123156 Add a small sleep when trying to reconnect forever 2017-09-25 11:04:09 +02:00
Lonami Exo
0f10e1419f Update to v0.13.6 2017-09-23 11:44:04 +02:00
Lonami Exo
b04607e7ba Ignore requests invoked from ReadThread instead raising
Any unhandled exception on the ReadThread would cause it to stop,
and handling the exception to the main thread on the next invoke
or poll. Instead causing the thread to stop, simply ignore it.
2017-09-23 11:31:58 +02:00
Lonami Exo
80e9877256 Show the type of children TLObjects on .stringify() 2017-09-23 11:01:25 +02:00
Andrey Egorov
73fbfde7ef Process messages without pts 2017-09-22 23:14:31 +02:00
Lonami Exo
f39f9c33a0 Fix timeout on confirm_received.wait not being added (fix #257) 2017-09-22 19:20:37 +02:00
Lonami Exo
9ce4307304 Update to v0.13.5 2017-09-22 16:12:43 +02:00
Lonami Exo
b0f6b23e52 Fix creating new connections accessing invalid attributes 2017-09-22 16:11:07 +02:00
Lonami Exo
bfa3001f87 Fix MainThread would lock when reconnecting
This is because it was thinking that the ReadThread would be
ready to read the result, but actually, this thread is also
locked trying to reconnect at the same time
2017-09-22 16:02:10 +02:00
Lonami Exo
9dfb5d493c Fix BrokenPipeError was instance of OSError 2017-09-22 13:51:11 +02:00
Lonami Exo
4a8e5c865a Detect BrokenAuthKeyError on MtProtoSender._decode_msg 2017-09-22 13:32:00 +02:00
Lonami Exo
a992f42781 Attempt at fixing socket is None on .connect() 2017-09-22 13:14:56 +02:00
Lonami Exo
6d60e83adc Start background thread only if it was None 2017-09-22 13:13:41 +02:00
Lonami Exo
d8bf8bb2eb Fix reconnect always returning False -> infinite loop 2017-09-22 12:48:04 +02:00
Lonami Exo
4d5f16f2aa Fix background thread could not reconnect properly 2017-09-22 12:44:09 +02:00
Lonami Exo
4245ec5abc Make reconnect private 2017-09-22 12:31:41 +02:00
Lonami Exo
bc15b451b5 Use a safer reconnect behaviour (respect multithread too) 2017-09-22 12:20:38 +02:00
Lonami Exo
ffadcd029f Save the session much less often (doable because 151e162) 2017-09-21 19:12:46 +02:00
Lonami Exo
12c6d4d3ac Start the background thread only if the user is authorized 2017-09-21 15:36:20 +02:00
Lonami Exo
4ba12e717f Fix reconnect to new_dc wasn't changing connection.ip 2017-09-21 13:58:57 +02:00
Lonami Exo
7f83a6109f Fix authenticator was disconnecting when it shouldn't 2017-09-21 13:54:44 +02:00
Lonami Exo
2b2da843a1 Create a Connection only once and avoid no-op if was "connected" 2017-09-21 13:44:22 +02:00
Lonami Exo
4777b8dad4 Handle .connect() method more gracefully 2017-09-21 13:44:22 +02:00
Sergey
446174c7de Catching WinError 10038
While client.connect() there were OSError: [WinError 10038] an operation was attempted on something that is not a socket
2017-09-21 13:37:13 +02:00
Lonami Exo
c22224f516 Attempt at handling ProxyConnectionError on .connect() 2017-09-20 13:23:52 +02:00
Lonami Exo
b8d7b1c8af Phone number shouldn't actually start with '+' 2017-09-20 13:23:52 +02:00
jiang gau
9ae4f7f641 Fix wrong check on .send_message (closes #250) 2017-09-19 16:27:10 +02:00
Lonami Exo
1d3273a306 Fix UpdateState calling handlers with updates with lower pts 2017-09-19 13:17:40 +02:00
Lonami Exo
e12e82357d Update to v0.13.4 2017-09-18 13:32:16 +02:00
Lonami Exo
91f44613a8 Fix SendMessageRequest could return UpdateShortSentMessage 2017-09-18 11:59:55 +02:00
Lonami Exo
200d1d67be Make BufferError message more useful 2017-09-18 11:45:08 +02:00
Lonami Exo
0235fce99c Don't hold ._updates_lock while calling .handlers 2017-09-18 11:01:15 +02:00
Lonami Exo
143e046cf5 Attempt at passing errors to the main thread through .updates 2017-09-18 10:59:54 +02:00
Lonami Exo
e3ab98815d Fix reconnect on TypeNotFoundError failing 2017-09-18 10:21:53 +02:00
Lonami Exo
e0e3947d2f Fix some misleading errors/documentation 2017-09-18 09:37:20 +02:00
Lonami Exo
2a3e5e5ed2 Handle BufferError's more gracefully 2017-09-17 19:39:41 +02:00
Lonami Exo
78155a21bd Retry authentication for auth keys up to five times by default 2017-09-17 19:15:43 +02:00
Lonami Exo
e7a936222f Catch -404 BrokenAuthKeyError on .connect()'s .do_authentication 2017-09-17 18:38:03 +02:00
Lonami Exo
940dfe6816 Fix process_updates=True preventing new sessions from signing in 2017-09-17 18:13:58 +02:00
Lonami Exo
58fe39f060 Expose MtProtoSender.is_connected() and use it on .connect() 2017-09-17 16:39:29 +02:00
Lonami Exo
05a8723d36 Catch calling .receive() when .sender is None on recv_thread 2017-09-17 16:37:08 +02:00
Lonami Exo
12c12fdfaf Fix representing .stringify() for bytes with indent > 0 2017-09-17 16:35:35 +02:00
Lonami Exo
76b7420b63 Ignore TimeoutError when invoking a request 2017-09-17 16:32:51 +02:00
Lonami Exo
2f0f6d4591 Remove initial_query leftovers 2017-09-17 16:20:04 +02:00
Lonami Exo
aea10b103b Rename generated layer variable to uppercase readonly property 2017-09-17 16:17:55 +02:00
Lonami Exo
c84e54b647 Get rid of the initial_query= parameter on .connect() 2017-09-17 16:06:43 +02:00
Lonami Exo
2689b0750d Make DC options static
Some datacenters don't allow calling GetConfigRequest,
this way it can both be reused and such calls omitted.
2017-09-17 15:19:21 +02:00
Lonami Exo
59a4315c28 Avoid calling InitConnectionRequest when it's not needed 2017-09-17 14:26:25 +02:00
Lonami Exo
29471f3bba Force obfuscated2 mode if api_id < 20 2017-09-15 11:49:39 +02:00
Lonami Exo
279eb110be Update to v0.13.3 2017-09-14 11:51:41 +02:00
Lonami Exo
12a39873f1 Workaround msg_seqno too low/high (closes #95) 2017-09-14 11:50:38 +02:00
Lonami Exo
151e1623d3 Stop saving the session ID, sequence and time offset
After some discussion, storing the session ID is not needed at all.
It can always start off as a new random 8-byte-long number, also
restarting the sequence number at 0. This should prevent some seqno
too low errors.

Also, saving the time offset could cause more trouble than fixing
them, so that has also been removed.
2017-09-14 11:44:42 +02:00
Lonami Exo
73fe7258d1 Modify .send_message to return the sent message object 2017-09-13 12:00:27 +02:00
Lonami Exo
d12fb5b924 Add an optional reply_to parameter to .send_message/media 2017-09-13 11:51:23 +02:00
Lonami Exo
7043f4c9ba Add a convenience method to send voice notes 2017-09-13 11:30:20 +02:00
Lonami Exo
d79dccc923 Add a lock around TcpClient.close() to try avoiding None.close() 2017-09-13 09:44:58 +02:00