diff options
author | MikalaiR <nikolay.romanovich@narod.ru> | 2016-03-27 17:24:15 +0000 |
---|---|---|
committer | MikalaiR <nikolay.romanovich@narod.ru> | 2016-03-27 17:24:15 +0000 |
commit | deb10796ae4447c072fff33e2bb8bca2eeeb8ae6 (patch) | |
tree | 32bb075b7de205f87c71a4ebc0841e34ce424b66 /protocols | |
parent | 6e9ace645b7d24ed0fe60f4b7714d3bb11739dfc (diff) |
Telegram: netlib network pt. 1
git-svn-id: http://svn.miranda-ng.org/main/trunk@16554 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols')
-rw-r--r-- | protocols/Telegram/src/stdafx.h | 18 | ||||
-rw-r--r-- | protocols/Telegram/src/t_network.cpp | 121 | ||||
-rw-r--r-- | protocols/Telegram/src/t_network.h | 15 | ||||
-rw-r--r-- | protocols/Telegram/src/t_proto.cpp | 1 | ||||
-rw-r--r-- | protocols/Telegram/src/t_timers.cpp | 12 |
5 files changed, 142 insertions, 25 deletions
diff --git a/protocols/Telegram/src/stdafx.h b/protocols/Telegram/src/stdafx.h index 2781bcf4f9..d83f501535 100644 --- a/protocols/Telegram/src/stdafx.h +++ b/protocols/Telegram/src/stdafx.h @@ -45,22 +45,7 @@ extern "C" #include "tgl\tgl-timers.h"
#include "tgl\tgl-binlog.h"
#include "tgl\config.h"
-
- struct event;
- struct event_base;
- event_base __declspec(dllimport) *event_base_new(void);
- void *event_get_callback_arg(const void *ev);
- struct event *event_new(void *, intptr_t, short, void(*)(intptr_t, short, void *), void *);
- int event_del(void *);
- int event_add(void *ev, const struct timeval *timeout);
-#define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg))
- void event_free(void *);
-
-
- void read_auth_file(struct tgl_state *TLS);
- void read_state_file(struct tgl_state *TLS);
- void write_auth_file(struct tgl_state *TLS);
- void write_state_file(struct tgl_state *TLS);
+ #include "tgl\mtproto-client.h"
}
@@ -76,6 +61,7 @@ struct MirTLS : public tgl_state, public MZeroedObject #include "t_callback.h"
extern char g_szMirVer[];
+extern HANDLE hQueue;
#define MODULE "Telegram"
diff --git a/protocols/Telegram/src/t_network.cpp b/protocols/Telegram/src/t_network.cpp index 3bc7b1089d..3804a25cf4 100644 --- a/protocols/Telegram/src/t_network.cpp +++ b/protocols/Telegram/src/t_network.cpp @@ -1,6 +1,125 @@ #include "stdafx.h"
+#define PING_TIMEOUT 10
+
+static void start_ping_timer(connection *c);
+
+void __stdcall ping_alarm(PVOID arg, BOOLEAN TimerOrWaitFired)
+{
+ struct connection *c = (connection*)arg;
+ if (tglt_get_double_time() - c->last_receive_time > 6 * PING_TIMEOUT)
+ {
+ c->TLS->callback.logprintf("fail connection: reason: ping timeout");
+ }
+ else if (tglt_get_double_time() - c->last_receive_time > 3 * PING_TIMEOUT)
+ {
+ tgl_do_send_ping(c->TLS, c);
+ start_ping_timer(c);
+ }
+ else
+ {
+ start_ping_timer(c);
+ }
+}
+
+static void start_ping_timer(connection *c)
+{
+ CreateTimerQueueTimer(&c->ping_timer, hQueue, ping_alarm, c, PING_TIMEOUT * 1000, 0, 0);
+}
+
+static void stop_ping_timer(struct connection *c)
+{
+ DeleteTimerQueueTimer(hQueue, c->ping_timer, 0);
+ c->ping_timer = 0;
+}
+
+
+int mtgln_write_out(struct connection *c, const void *_data, int len) +{ + return Netlib_Send(c->hNetlibConn, (char*)_data, len, 0); +}
+
+int mtgln_read_in(struct connection *c, void *_data, int len) +{ + return Netlib_Recv(c->hNetlibConn, (char*)_data, len, 0); +}
+
+int mtgln_read_in_lookup(struct connection *c, void *_data, int len) +{ + return Netlib_Recv(c->hNetlibConn, (char*)_data, len, MSG_PEEK); +}
+
+connection* mtgln_create_connection(struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods)
+{
+ connection *conn = new connection;
+ conn->TLS = TLS;
+ conn->ip = mir_strdup(host);
+ conn->port = port;
+ conn->dc = dc; + conn->session = session; + conn->methods = methods;
+
+ NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
+ nloc.szHost = conn->ip;
+ nloc.wPort = port;
+
+ if (!(conn->hNetlibConn = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, (WPARAM)((MirTLS*)TLS)->m_proto->m_hNetlibUser, (LPARAM)&nloc)))
+ {
+ delete conn;
+ return 0;
+ }
+
+ char b = 0xfe;
+ mtgln_write_out(conn, &b, 1);
+
+ conn->last_receive_time = tglt_get_double_time();
+ start_ping_timer(conn);
+
+ return conn;
+}
+
+static void incr_out_packet_num(struct connection *c) { +} + +static struct tgl_dc *get_dc(struct connection *c) { + return c->dc; +} + +static struct tgl_session *get_session(struct connection *c) { + return c->session; +}
+
+static void tgln_free(struct connection *c)
+{
+ Netlib_CloseHandle(c->hNetlibConn);
+ mir_free(c->ip);
+ delete c;
+}
+
+void mtgln_flush_out(struct connection *c) { +}
+
+struct tgl_net_methods mtgl_conn_methods = { + mtgln_write_out, + mtgln_read_in, + mtgln_read_in_lookup, + mtgln_flush_out, + incr_out_packet_num, + tgln_free, + get_dc, + get_session, + mtgln_create_connection +};
+
void CTelegramProto::InitNetwork()
{
- tgl_set_net_methods(TLS, &tgl_conn_methods);
+ NETLIBUSER nlu = { 0 };
+ nlu.cbSize = sizeof(nlu);
+ nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE;
+ nlu.ptszDescriptiveName = L"TGL";
+ nlu.szSettingsModule = m_szModuleName;
+ m_hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);
+
+
+ tgl_set_net_methods(TLS, &mtgl_conn_methods);
}
\ No newline at end of file diff --git a/protocols/Telegram/src/t_network.h b/protocols/Telegram/src/t_network.h index e69de29bb2..eaf90ed31d 100644 --- a/protocols/Telegram/src/t_network.h +++ b/protocols/Telegram/src/t_network.h @@ -0,0 +1,15 @@ +struct connection
+{
+ char *ip; + int port; + int flags; + enum conn_state state;
+ struct mtproto_methods *methods; + struct tgl_state *TLS; + struct tgl_session *session; + struct tgl_dc *dc;
+ void *extra;
+ double last_receive_time;
+ HANDLE hNetlibConn;
+ HANDLE ping_timer;
+};
\ No newline at end of file diff --git a/protocols/Telegram/src/t_proto.cpp b/protocols/Telegram/src/t_proto.cpp index a54ded0c61..2be4a7fcdb 100644 --- a/protocols/Telegram/src/t_proto.cpp +++ b/protocols/Telegram/src/t_proto.cpp @@ -22,7 +22,6 @@ CTelegramProto::CTelegramProto(const char* protoName, const TCHAR* userName) : P TLS = new MirTLS(this);
tgl_set_verbosity(TLS, 10);
- tgl_set_ev_base(TLS, event_base_new());
InitNetwork();
diff --git a/protocols/Telegram/src/t_timers.cpp b/protocols/Telegram/src/t_timers.cpp index c897247fab..296242856d 100644 --- a/protocols/Telegram/src/t_timers.cpp +++ b/protocols/Telegram/src/t_timers.cpp @@ -11,10 +11,7 @@ struct tgl_timer HANDLE hTimer;
};
-VOID CALLBACK WaitOrTimerCallback(
- _In_ PVOID lpParameter,
- _In_ BOOLEAN TimerOrWaitFired
- )
+VOID CALLBACK WaitOrTimerCallback(_In_ PVOID lpParameter, _In_ BOOLEAN TimerOrWaitFired)
{
tgl_timer *p = (tgl_timer*)lpParameter;
p->cb(p->TLS, p->arg);
@@ -24,7 +21,7 @@ VOID CALLBACK WaitOrTimerCallback( struct tgl_timer *mtgl_timer_alloc (struct tgl_state *TLS, void (*cb)(struct tgl_state *TLS, void *arg), void *arg)
{
- tgl_timer *p = (tgl_timer *)calloc(sizeof (tgl_timer), 1);
+ tgl_timer *p = (tgl_timer *)mir_calloc(sizeof (tgl_timer));
p->TLS = TLS;
p->cb = cb;
p->arg = arg;
@@ -38,7 +35,8 @@ void mtgl_timer_insert (struct tgl_timer *t, double p) t->hTimer = hNewTimer;
}
-void mtgl_timer_delete (struct tgl_timer *t) {
+void mtgl_timer_delete (struct tgl_timer *t)
+{
DeleteTimerQueueTimer(hQueue, t->hTimer, 0);
t->hTimer = 0;
}
@@ -46,7 +44,7 @@ void mtgl_timer_delete (struct tgl_timer *t) { void mtgl_timer_free (struct tgl_timer *t)
{
if (t->hTimer) mtgl_timer_delete(t);
- free(t);
+ mir_free(t);
}
|