driver-stratum.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #ifndef WIN32
  11. #include <arpa/inet.h>
  12. #else
  13. #include <winsock2.h>
  14. #endif
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <event2/buffer.h>
  19. #include <event2/bufferevent.h>
  20. #include <event2/event.h>
  21. #include <event2/listener.h>
  22. #include <jansson.h>
  23. #include "deviceapi.h"
  24. #include "driver-proxy.h"
  25. #include "miner.h"
  26. #include "util.h"
  27. #include "work2d.h"
  28. #define _ssm_client_octets work2d_xnonce1sz
  29. #define _ssm_client_xnonce2sz work2d_xnonce2sz
  30. static char *_ssm_notify;
  31. static int _ssm_notify_sz;
  32. static struct event *ev_notify;
  33. static notifier_t _ssm_update_notifier;
  34. struct stratumsrv_job {
  35. char *my_job_id;
  36. struct timeval tv_prepared;
  37. struct stratum_work swork;
  38. UT_hash_handle hh;
  39. };
  40. static struct stratumsrv_job *_ssm_jobs;
  41. static struct work _ssm_cur_job_work;
  42. static uint64_t _ssm_jobid;
  43. static struct event_base *_smm_evbase;
  44. static bool _smm_running;
  45. static struct evconnlistener *_smm_listener;
  46. struct stratumsrv_conn {
  47. struct bufferevent *bev;
  48. uint32_t xnonce1_le;
  49. struct timeval tv_hashes_done;
  50. bool hashes_done_ext;
  51. struct stratumsrv_conn *next;
  52. };
  53. static struct stratumsrv_conn *_ssm_connections;
  54. #define _ssm_gen_dummy_work work2d_gen_dummy_work
  55. static
  56. bool stratumsrv_update_notify_str(struct pool * const pool, bool clean)
  57. {
  58. cg_rlock(&pool->data_lock);
  59. struct stratumsrv_conn *conn;
  60. const struct stratum_work * const swork = &pool->swork;
  61. const int n2size = pool->swork.n2size;
  62. char my_job_id[33];
  63. int i;
  64. struct stratumsrv_job *ssj;
  65. ssize_t n2pad = n2size - _ssm_client_octets - _ssm_client_xnonce2sz;
  66. if (n2pad < 0)
  67. return false;
  68. size_t coinb1in_lenx = swork->nonce2_offset * 2;
  69. size_t n2padx = n2pad * 2;
  70. size_t coinb1_lenx = coinb1in_lenx + n2padx;
  71. size_t coinb2_len = bytes_len(&swork->coinbase) - swork->nonce2_offset - n2size;
  72. size_t coinb2_lenx = coinb2_len * 2;
  73. sprintf(my_job_id, "%"PRIx64"-%"PRIx64, (uint64_t)time(NULL), _ssm_jobid++);
  74. size_t bufsz = 166 + strlen(my_job_id) + coinb1_lenx + coinb2_lenx + (swork->merkles * 67);
  75. char * const buf = malloc(bufsz);
  76. char *p = buf;
  77. char prevhash[65], coinb1[coinb1_lenx + 1], coinb2[coinb2_lenx], version[9], nbits[9], ntime[9];
  78. uint32_t ntime_n;
  79. bin2hex(prevhash, &swork->header1[4], 32);
  80. bin2hex(coinb1, bytes_buf(&swork->coinbase), swork->nonce2_offset);
  81. memset(&coinb1[coinb1in_lenx], 'B', n2padx);
  82. coinb1[coinb1_lenx] = '\0';
  83. bin2hex(coinb2, &bytes_buf(&swork->coinbase)[swork->nonce2_offset + n2size], coinb2_len);
  84. p += sprintf(p, "{\"params\":[\"%s\",\"%s\",\"%s\",\"%s\",[", my_job_id, prevhash, coinb1, coinb2);
  85. for (i = 0; i < swork->merkles; ++i)
  86. {
  87. if (i)
  88. *p++ = ',';
  89. *p++ = '"';
  90. bin2hex(p, &bytes_buf(&swork->merkle_bin)[i * 32], 32);
  91. p += 64;
  92. *p++ = '"';
  93. }
  94. bin2hex(version, swork->header1, 4);
  95. bin2hex(nbits, swork->diffbits, 4);
  96. ntime_n = htobe32(swork->ntime + timer_elapsed(&swork->tv_received, NULL));
  97. bin2hex(ntime, &ntime_n, 4);
  98. p += sprintf(p, "],\"%s\",\"%s\",\"%s\",%s],\"method\":\"mining.notify\",\"id\":null}\n", version, nbits, ntime, clean ? "true" : "false");
  99. ssj = malloc(sizeof(*ssj));
  100. *ssj = (struct stratumsrv_job){
  101. .my_job_id = strdup(my_job_id),
  102. };
  103. timer_set_now(&ssj->tv_prepared);
  104. stratum_work_cpy(&ssj->swork, swork);
  105. cg_runlock(&pool->data_lock);
  106. ssj->swork.data_lock_p = NULL;
  107. HASH_ADD_KEYPTR(hh, _ssm_jobs, ssj->my_job_id, strlen(ssj->my_job_id), ssj);
  108. if (likely(_ssm_cur_job_work.pool))
  109. clean_work(&_ssm_cur_job_work);
  110. _ssm_gen_dummy_work(&_ssm_cur_job_work, &ssj->swork, &ssj->tv_prepared, NULL, 0);
  111. _ssm_notify_sz = p - buf;
  112. assert(_ssm_notify_sz <= bufsz);
  113. free(_ssm_notify);
  114. _ssm_notify = buf;
  115. LL_FOREACH(_ssm_connections, conn)
  116. {
  117. if (unlikely(!conn->xnonce1_le))
  118. continue;
  119. bufferevent_write(conn->bev, _ssm_notify, _ssm_notify_sz);
  120. }
  121. return true;
  122. }
  123. static
  124. void _ssj_free(struct stratumsrv_job * const ssj)
  125. {
  126. free(ssj->my_job_id);
  127. stratum_work_clean(&ssj->swork);
  128. free(ssj);
  129. }
  130. static
  131. void stratumsrv_job_pruner()
  132. {
  133. struct stratumsrv_job *ssj, *tmp_ssj;
  134. struct timeval tv_now;
  135. timer_set_now(&tv_now);
  136. HASH_ITER(hh, _ssm_jobs, ssj, tmp_ssj)
  137. {
  138. if (timer_elapsed(&ssj->tv_prepared, &tv_now) <= opt_expiry)
  139. break;
  140. HASH_DEL(_ssm_jobs, ssj);
  141. applog(LOG_DEBUG, "SSM: Pruning job_id %s", ssj->my_job_id);
  142. _ssj_free(ssj);
  143. }
  144. }
  145. static void stratumsrv_client_close(struct stratumsrv_conn *);
  146. static
  147. void stratumsrv_conn_close_completion_cb(struct bufferevent *bev, void *p)
  148. {
  149. struct evbuffer * const output = bufferevent_get_output(bev);
  150. if (evbuffer_get_length(output))
  151. // Still have more data to write...
  152. return;
  153. stratumsrv_client_close(p);
  154. }
  155. static void stratumsrv_event(struct bufferevent *, short, void *);
  156. static
  157. void stratumsrv_boot(struct stratumsrv_conn * const conn, const char * const msg)
  158. {
  159. struct bufferevent * const bev = conn->bev;
  160. char buf[58 + strlen(msg)];
  161. int bufsz = sprintf(buf, "{\"params\":[\"%s\"],\"method\":\"client.show_message\",\"id\":null}\n", msg);
  162. bufferevent_write(bev, buf, bufsz);
  163. bufferevent_setcb(bev, NULL, stratumsrv_conn_close_completion_cb, stratumsrv_event, conn);
  164. }
  165. static
  166. void stratumsrv_boot_all_subscribed(const char * const msg)
  167. {
  168. struct stratumsrv_conn *conn, *tmp_conn;
  169. free(_ssm_notify);
  170. _ssm_notify = NULL;
  171. // Boot all connections
  172. LL_FOREACH_SAFE(_ssm_connections, conn, tmp_conn)
  173. {
  174. if (!conn->xnonce1_le)
  175. continue;
  176. stratumsrv_boot(conn, msg);
  177. }
  178. }
  179. static
  180. void _stratumsrv_update_notify(evutil_socket_t fd, short what, __maybe_unused void *p)
  181. {
  182. struct pool *pool = current_pool();
  183. bool clean;
  184. if (fd == _ssm_update_notifier[0])
  185. {
  186. evtimer_del(ev_notify);
  187. notifier_read(_ssm_update_notifier);
  188. applog(LOG_DEBUG, "SSM: Update triggered by notifier");
  189. }
  190. clean = _ssm_cur_job_work.pool ? stale_work(&_ssm_cur_job_work, true) : true;
  191. if (clean)
  192. {
  193. struct stratumsrv_job *ssj, *tmp;
  194. applog(LOG_DEBUG, "SSM: Current replacing job stale, pruning all jobs");
  195. HASH_ITER(hh, _ssm_jobs, ssj, tmp)
  196. {
  197. HASH_DEL(_ssm_jobs, ssj);
  198. _ssj_free(ssj);
  199. }
  200. }
  201. else
  202. stratumsrv_job_pruner();
  203. if (!pool_has_usable_swork(pool))
  204. {
  205. applog(LOG_WARNING, "SSM: No usable 2D work upstream!");
  206. if (clean)
  207. stratumsrv_boot_all_subscribed("Current upstream pool does not have usable 2D work");
  208. goto out;
  209. }
  210. if (!stratumsrv_update_notify_str(pool, clean))
  211. {
  212. applog(LOG_WARNING, "SSM: Failed to subdivide upstream stratum notify!");
  213. if (clean)
  214. stratumsrv_boot_all_subscribed("Current upstream pool does not have active stratum");
  215. }
  216. out: ;
  217. struct timeval tv_scantime = {
  218. .tv_sec = opt_scantime,
  219. };
  220. evtimer_add(ev_notify, &tv_scantime);
  221. }
  222. static struct proxy_client *_stratumsrv_find_or_create_client(const char *);
  223. static
  224. struct proxy_client *(*stratumsrv_find_or_create_client)(const char *) = _stratumsrv_find_or_create_client;
  225. static
  226. struct proxy_client *_stratumsrv_find_or_create_client(const char *user)
  227. {
  228. struct proxy_client * const client = proxy_find_or_create_client(user);
  229. struct cgpu_info *cgpu;
  230. struct thr_info *thr;
  231. if (!client)
  232. return NULL;
  233. cgpu = client->cgpu;
  234. thr = cgpu->thr[0];
  235. memcpy(thr->work_restart_notifier, _ssm_update_notifier, sizeof(thr->work_restart_notifier));
  236. stratumsrv_find_or_create_client = proxy_find_or_create_client;
  237. return client;
  238. }
  239. static
  240. void _stratumsrv_failure(struct bufferevent * const bev, const char * const idstr, const int e, const char * const emsg)
  241. {
  242. if (!idstr)
  243. return;
  244. char buf[0x100];
  245. size_t bufsz = snprintf(buf, sizeof(buf), "{\"error\":[%d,\"%s\",null],\"id\":%s,\"result\":null}\n", e, emsg, idstr);
  246. bufferevent_write(bev, buf, bufsz);
  247. }
  248. #define return_stratumsrv_failure(e, emsg) do{ \
  249. _stratumsrv_failure(bev, idstr, e, emsg); \
  250. return; \
  251. }while(0)
  252. static
  253. void _stratumsrv_success(struct bufferevent * const bev, const char * const idstr)
  254. {
  255. if (!idstr)
  256. return;
  257. size_t bufsz = 36 + strlen(idstr);
  258. char buf[bufsz];
  259. bufsz = sprintf(buf, "{\"result\":true,\"id\":%s,\"error\":null}\n", idstr);
  260. bufferevent_write(bev, buf, bufsz);
  261. }
  262. static
  263. void stratumsrv_mining_subscribe(struct bufferevent *bev, json_t *params, const char *idstr, uint32_t *xnonce1_p)
  264. {
  265. char buf[90 + strlen(idstr) + (_ssm_client_octets * 2 * 2) + 0x10];
  266. char xnonce1x[(_ssm_client_octets * 2) + 1];
  267. int bufsz;
  268. if (!_ssm_notify)
  269. {
  270. evtimer_del(ev_notify);
  271. _stratumsrv_update_notify(-1, 0, NULL);
  272. if (!_ssm_notify)
  273. return_stratumsrv_failure(20, "No notify set (upstream not stratum?)");
  274. }
  275. if (!*xnonce1_p)
  276. {
  277. if (!reserve_work2d_(xnonce1_p))
  278. return_stratumsrv_failure(20, "Maximum clients already connected");
  279. }
  280. bin2hex(xnonce1x, xnonce1_p, _ssm_client_octets);
  281. bufsz = sprintf(buf, "{\"id\":%s,\"result\":[[[\"mining.set_difficulty\",\"x\"],[\"mining.notify\",\"%s\"]],\"%s\",%d],\"error\":null}\n", idstr, xnonce1x, xnonce1x, _ssm_client_xnonce2sz);
  282. bufferevent_write(bev, buf, bufsz);
  283. bufferevent_write(bev, "{\"params\":[0.9999847412109375],\"id\":null,\"method\":\"mining.set_difficulty\"}\n", 75);
  284. bufferevent_write(bev, _ssm_notify, _ssm_notify_sz);
  285. }
  286. static
  287. void stratumsrv_mining_authorize(struct bufferevent *bev, json_t *params, const char *idstr, uint32_t *xnonce1_p)
  288. {
  289. struct proxy_client * const client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  290. if (unlikely(!client))
  291. return_stratumsrv_failure(20, "Failed creating new cgpu");
  292. _stratumsrv_success(bev, idstr);
  293. }
  294. static
  295. void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const char *idstr, struct stratumsrv_conn * const conn)
  296. {
  297. uint32_t * const xnonce1_p = &conn->xnonce1_le;
  298. struct work _work, *work;
  299. struct stratumsrv_job *ssj;
  300. struct proxy_client *client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  301. struct cgpu_info *cgpu;
  302. struct thr_info *thr;
  303. const char * const job_id = __json_array_string(params, 1);
  304. const char * const extranonce2 = __json_array_string(params, 2);
  305. const char * const ntime = __json_array_string(params, 3);
  306. const char * const nonce = __json_array_string(params, 4);
  307. uint8_t xnonce2[work2d_xnonce2sz];
  308. uint32_t nonce_n;
  309. if (unlikely(!client))
  310. return_stratumsrv_failure(20, "Failed creating new cgpu");
  311. if (unlikely(!(job_id && extranonce2 && ntime && nonce)))
  312. return_stratumsrv_failure(20, "Couldn't understand parameters");
  313. if (unlikely(strlen(nonce) < 8))
  314. return_stratumsrv_failure(20, "nonce too short");
  315. if (unlikely(strlen(ntime) < 8))
  316. return_stratumsrv_failure(20, "ntime too short");
  317. if (unlikely(strlen(extranonce2) < _ssm_client_xnonce2sz * 2))
  318. return_stratumsrv_failure(20, "extranonce2 too short");
  319. cgpu = client->cgpu;
  320. thr = cgpu->thr[0];
  321. // Lookup job_id
  322. HASH_FIND_STR(_ssm_jobs, job_id, ssj);
  323. if (!ssj)
  324. return_stratumsrv_failure(21, "Job not found");
  325. // Generate dummy work
  326. work = &_work;
  327. hex2bin(xnonce2, extranonce2, work2d_xnonce2sz);
  328. work2d_gen_dummy_work(work, &ssj->swork, &ssj->tv_prepared, xnonce2, *xnonce1_p);
  329. // Submit nonce
  330. hex2bin(&work->data[68], ntime, 4);
  331. hex2bin((void*)&nonce_n, nonce, 4);
  332. nonce_n = le32toh(nonce_n);
  333. if (!submit_nonce(thr, work, nonce_n))
  334. _stratumsrv_failure(bev, idstr, 23, "H-not-zero");
  335. else
  336. if (stale_work(work, true))
  337. _stratumsrv_failure(bev, idstr, 21, "stale");
  338. else
  339. _stratumsrv_success(bev, idstr);
  340. clean_work(work);
  341. if (!conn->hashes_done_ext)
  342. {
  343. struct timeval tv_now, tv_delta;
  344. timer_set_now(&tv_now);
  345. timersub(&tv_now, &conn->tv_hashes_done, &tv_delta);
  346. conn->tv_hashes_done = tv_now;
  347. hashes_done(thr, 0x100000000, &tv_delta, NULL);
  348. }
  349. }
  350. static
  351. void stratumsrv_mining_hashes_done(struct bufferevent * const bev, json_t * const params, const char * const idstr, struct stratumsrv_conn * const conn)
  352. {
  353. double f;
  354. struct timeval tv_delta;
  355. struct cgpu_info *cgpu;
  356. struct thr_info *thr;
  357. struct proxy_client * const client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  358. json_t *jduration = json_array_get(params, 1);
  359. json_t *jhashcount = json_array_get(params, 2);
  360. if (!(json_is_number(jduration) && json_is_number(jhashcount)))
  361. return_stratumsrv_failure(20, "mining.hashes_done(String username, Number duration-in-seconds, Number hashcount)");
  362. cgpu = client->cgpu;
  363. thr = cgpu->thr[0];
  364. f = json_number_value(jduration);
  365. tv_delta.tv_sec = f;
  366. tv_delta.tv_usec = (f - tv_delta.tv_sec) * 1e6;
  367. f = json_number_value(jhashcount);
  368. hashes_done(thr, f, &tv_delta, NULL);
  369. conn->hashes_done_ext = true;
  370. }
  371. static
  372. bool stratumsrv_process_line(struct bufferevent * const bev, const char * const ln, void * const p)
  373. {
  374. struct stratumsrv_conn *conn = p;
  375. json_error_t jerr;
  376. json_t *json, *params, *j2;
  377. const char *method;
  378. char *idstr;
  379. json = JSON_LOADS(ln, &jerr);
  380. if (!json)
  381. {
  382. applog(LOG_ERR, "SSM: JSON parse error: %s", ln);
  383. return false;
  384. }
  385. method = bfg_json_obj_string(json, "method", NULL);
  386. if (!method)
  387. {
  388. applog(LOG_ERR, "SSM: JSON missing method: %s", ln);
  389. return false;
  390. }
  391. params = json_object_get(json, "params");
  392. if (!params)
  393. {
  394. applog(LOG_ERR, "SSM: JSON missing params: %s", ln);
  395. return false;
  396. }
  397. applog(LOG_DEBUG, "SSM: RECV: %s", ln);
  398. j2 = json_object_get(json, "id");
  399. idstr = (j2 && !json_is_null(j2)) ? json_dumps_ANY(j2, 0) : NULL;
  400. if (!strcasecmp(method, "mining.submit"))
  401. stratumsrv_mining_submit(bev, params, idstr, conn);
  402. else
  403. if (!strcasecmp(method, "mining.hashes_done"))
  404. stratumsrv_mining_hashes_done(bev, params, idstr, conn);
  405. else
  406. if (!strcasecmp(method, "mining.authorize"))
  407. stratumsrv_mining_authorize(bev, params, idstr, &conn->xnonce1_le);
  408. else
  409. if (!strcasecmp(method, "mining.subscribe"))
  410. stratumsrv_mining_subscribe(bev, params, idstr, &conn->xnonce1_le);
  411. else
  412. _stratumsrv_failure(bev, idstr, -3, "Method not supported");
  413. free(idstr);
  414. return true;
  415. }
  416. static
  417. void stratumsrv_client_close(struct stratumsrv_conn * const conn)
  418. {
  419. struct bufferevent * const bev = conn->bev;
  420. bufferevent_free(bev);
  421. LL_DELETE(_ssm_connections, conn);
  422. release_work2d_(conn->xnonce1_le);
  423. free(conn);
  424. }
  425. static
  426. void stratumsrv_read(struct bufferevent *bev, void *p)
  427. {
  428. struct evbuffer *input = bufferevent_get_input(bev);
  429. char *ln;
  430. bool rv;
  431. while ( (ln = evbuffer_readln(input, NULL, EVBUFFER_EOL_ANY)) )
  432. {
  433. rv = stratumsrv_process_line(bev, ln, p);
  434. free(ln);
  435. if (unlikely(!rv))
  436. {
  437. stratumsrv_client_close(p);
  438. break;
  439. }
  440. }
  441. }
  442. static
  443. void stratumsrv_event(struct bufferevent *bev, short events, void *p)
  444. {
  445. if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR))
  446. {
  447. if (events & BEV_EVENT_ERROR)
  448. applog(LOG_ERR, "Error from bufferevent");
  449. if (events & BEV_EVENT_EOF)
  450. applog(LOG_DEBUG, "EOF from bufferevent");
  451. stratumsrv_client_close(p);
  452. }
  453. }
  454. static
  455. void stratumlistener(struct evconnlistener *listener, evutil_socket_t sock, struct sockaddr *addr, int len, void *p)
  456. {
  457. struct stratumsrv_conn *conn;
  458. struct event_base *evbase = evconnlistener_get_base(listener);
  459. struct bufferevent *bev = bufferevent_socket_new(evbase, sock, BEV_OPT_CLOSE_ON_FREE);
  460. conn = malloc(sizeof(*conn));
  461. *conn = (struct stratumsrv_conn){
  462. .bev = bev,
  463. };
  464. LL_PREPEND(_ssm_connections, conn);
  465. bufferevent_setcb(bev, stratumsrv_read, NULL, stratumsrv_event, conn);
  466. bufferevent_enable(bev, EV_READ | EV_WRITE);
  467. }
  468. void stratumsrv_start();
  469. void stratumsrv_change_port()
  470. {
  471. struct event_base * const evbase = _smm_evbase;
  472. if (_smm_listener)
  473. evconnlistener_free(_smm_listener);
  474. if (!_smm_running)
  475. {
  476. stratumsrv_start();
  477. return;
  478. }
  479. struct sockaddr_in sin = {
  480. .sin_family = AF_INET,
  481. .sin_addr.s_addr = INADDR_ANY,
  482. .sin_port = htons(stratumsrv_port),
  483. };
  484. _smm_listener = evconnlistener_new_bind(evbase, stratumlistener, NULL, (
  485. LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE
  486. ), 0x10, (void*)&sin, sizeof(sin));
  487. }
  488. static
  489. void *stratumsrv_thread(__maybe_unused void *p)
  490. {
  491. pthread_detach(pthread_self());
  492. RenameThread("stratumsrv");
  493. work2d_init();
  494. struct event_base *evbase = event_base_new();
  495. _smm_evbase = evbase;
  496. {
  497. ev_notify = evtimer_new(evbase, _stratumsrv_update_notify, NULL);
  498. _stratumsrv_update_notify(-1, 0, NULL);
  499. }
  500. {
  501. notifier_init(_ssm_update_notifier);
  502. struct event *ev_update_notifier = event_new(evbase, _ssm_update_notifier[0], EV_READ | EV_PERSIST, _stratumsrv_update_notify, NULL);
  503. event_add(ev_update_notifier, NULL);
  504. }
  505. stratumsrv_change_port();
  506. event_base_dispatch(evbase);
  507. return NULL;
  508. }
  509. void stratumsrv_start()
  510. {
  511. _smm_running = true;
  512. pthread_t pth;
  513. if (unlikely(pthread_create(&pth, NULL, stratumsrv_thread, NULL)))
  514. quit(1, "stratumsrv thread create failed");
  515. }