__init__.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. #!/usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of DigiLib.
  5. #
  6. # DigiLib is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # DigiLib is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with DigiLib. If not, see <http://www.gnu.org/licenses/>.
  18. import atexit
  19. import logging
  20. import logging.handlers
  21. import os
  22. import queue
  23. import select
  24. import socket
  25. import sys
  26. import threading
  27. import time
  28. import traceback
  29. import blinker
  30. import curio
  31. import digilib.misc
  32. lclient = logging.getLogger(__name__+".client")
  33. lserver = logging.getLogger(__name__+".server")
  34. lschat = logging.getLogger(__name__+".server.chat")
  35. lcchat = logging.getLogger(__name__+".client.chat")
  36. _tasks = []
  37. class ConnHandlerBase(object):
  38. """
  39. ConnectionHandlerBase is the base class for all connection handlers.
  40. It provides basic methodes. Consider inheriting form ConnectionHandler
  41. instead, as it provides better functionality.
  42. """
  43. addr = None
  44. block_size = 1024
  45. server = None
  46. def __init__(self, socket, addr, server):
  47. super(ConnHandlerBase, self).__init__()
  48. self.socket = socket
  49. self.addr = addr
  50. self.server = server
  51. async def disconnect(self):
  52. """
  53. disconenct explicitely disconnects the client, shuts the socket down
  54. and closes it.
  55. """
  56. try:
  57. await self.send(bytes())
  58. except:
  59. lserver.debug("error during disconenct")
  60. try:
  61. await self.socket.shutdown(0)
  62. except:
  63. lserver.debug("error during socket shutdown")
  64. try:
  65. await self.socket.close()
  66. except:
  67. lserver.debug("error closing socket")
  68. async def handle(self, data):
  69. """
  70. This method is called for every message the server receives from the
  71. client. It should handle the data. Performing asynchronous blocking
  72. actions is ok, as the client loop does not wait for this method to
  73. finish. therefore, this method can be called multiple times at once,
  74. use curio locks if appropriate.
  75. """
  76. raise NotImplemented()
  77. async def recv(self):
  78. """
  79. This method waits for the client to send something and returns bytes!
  80. """
  81. data_received = await self.socket.recv(self.block_size)
  82. return data_received
  83. async def send(self, data, log_msg=None):
  84. """
  85. This method sends bytes to the client. Returns False if an exception
  86. was raised during sending, otherwise True.
  87. """
  88. if log_msg:
  89. lschat.info("server:"+str(log_msg))
  90. else:
  91. lschat.info("Server:"+str(data))
  92. try:
  93. await self.socket.send(data)
  94. return True
  95. except Exception as e:
  96. lserver.error(e, exc_info=True)
  97. return False
  98. class ConnHandler(object):
  99. """
  100. More advanced connection handler than ConnectionHandlerBase. For
  101. instance, sends() takes a string and encodes it, recv() decodes
  102. the client's and returns a string and welcome_client is called after
  103. the ConnHandler is initialized (Not after the inheriting class is
  104. initialized though!)
  105. """
  106. def __init__(self, socket, addr, server):
  107. super(ConnHandlerBase, self).__init__(socket,addr,server)
  108. await self.welcome_client()
  109. async def disconnect(self):
  110. """
  111. disconenct() explicitely disconnects the client, performes a proper
  112. the shutdow on the socket and closes it.
  113. """
  114. try:
  115. await self.send("")
  116. except:
  117. lserver.debug("error during disconenct")
  118. try:
  119. await self.socket.shutdown(0)
  120. except:
  121. lserver.debug("error during socket shutdown")
  122. try:
  123. await self.socket.close()
  124. except:
  125. lserver.debug("error closing socket")
  126. async def handle(self, data):
  127. return
  128. async def recv(self):
  129. """
  130. This method waits for the client to send something, decodes it and
  131. returns a string.
  132. """
  133. data_received = await self.socket.recv(self.block_size)
  134. data_decoded = data_received.decode("utf-8")
  135. return data_decoded
  136. async def send(self, data, log_msg=False):
  137. """
  138. This method takes a string, encodes it and sends it to the client.
  139. Returns False if an exception was raised during sending, otherwise True.
  140. """
  141. if log_msg:
  142. lschat.info("server:"+log_msg)
  143. else:
  144. lschat.info("Server:"+data)
  145. data_encoded = bytes(data, "utf-8")
  146. try:
  147. await self.socket.send(data_encoded)
  148. return True
  149. except Exception as e:
  150. lserver.error(e, exc_info=True)
  151. return False
  152. async def welcome_client(self):
  153. """
  154. This method can be used to send a welcome message to the client.
  155. """
  156. pass
  157. class ConnHandlerEcho(ConnHandler):
  158. """
  159. A Conn handler which sends everything it receives to every other client
  160. connected to the server
  161. """
  162. def __init__(self, socket, addr, server):
  163. super(ConnHandlerEcho, self).__init__(socket, addr, server)
  164. def handle(self, data):
  165. for h in self.server.connection_handler:
  166. if not h is self:
  167. h.send(data)
  168. class Server(object):
  169. """
  170. Server opens either an unix or an inet connection. for every client which connects a new ClientHandler class is created.
  171. """
  172. def __init__(self,
  173. host,
  174. port=None,
  175. af_family="AF_INET",
  176. log_ip=False,
  177. max_allowed_clients=5,
  178. handler_class=None,
  179. handler_kwargs={},
  180. ):
  181. super(Server, self).__init__()
  182. # set to true when the server shuts down, for instance after a
  183. # fatal exception
  184. self.exit_event = False
  185. # on which hostname or ip address the connection will be opened
  186. self.host = host
  187. # on which port the connection will be opened
  188. self.port = port
  189. # what AF_INET family to use, either AF_INET or AF_UNIX (file socket)
  190. self.af_family = af_family
  191. # whether ip addresses will be logged
  192. self.log_ip = log_ip
  193. # number of maximum client connection at a time
  194. self.max_allowed_clients = max_allowed_clients
  195. # this handler class will be used when creating a new handler
  196. self.handler_class = handler_class
  197. # the kwargs passed to the handler's __init__ function
  198. self.handler_kwargs = handler_kwargs
  199. # don't make the socket yet, we don't need right now. will be created
  200. # when the start method is called
  201. # self.socket = self.make_socket()
  202. self.socket = None
  203. # create a task group for handlers, so we can easily cancel/terminate
  204. # them all at once
  205. self.handle_tasks = curio.TaskGroup(name="tg_handle_clients")
  206. # list of all active connection handlers
  207. self.connection_handler = []
  208. # these aren't actually used, I will remove them soon
  209. # self.conn_to_addr = {}
  210. # self.addr_to_conn = {}
  211. # self.conn_to_handler ={}
  212. # self.handler_to_conn = {}
  213. # this was used for the select.select approach. we use the async approach now, let's remove it
  214. # self.read_sockets_expected = [self.socket]
  215. # register our cleanup method to be executed when the program exits.
  216. # the cleanup function unregisters itself, so it won't get executed twice when the user called it befor the program exites
  217. atexit.register(self.shutdown)
  218. def make_socket(self):
  219. """
  220. factory method for sockets.
  221. this method makes a normal socket and wraps it in a curi.io.Socket wrapper
  222. """
  223. lserver.debug("making a {} socket".format(self.af_family))
  224. if self.af_family == "AF_INET":
  225. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  226. elif self.af_family == "AF_UNIX":
  227. s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
  228. else:
  229. raise ValueError(
  230. "AF_FAMILY '{}' not supported!".format(
  231. self.af_family
  232. )
  233. )
  234. s = curio.io.Socket(s)
  235. return s
  236. def make_handler(self, conn, addr):
  237. """
  238. factory method for handlers.
  239. this method creates a handler object from self.handler_class and self.handler_kwargs
  240. """
  241. return self.handler_class(conn, addr, self, **self.handler_kwargs)
  242. # DEPRECATED
  243. # def register_conn(self, conn, addr):
  244. # # if self.log_ip:
  245. # # lserver.info("New connection from {} on port {}".format(*addr))
  246. # self.read_sockets_expected.append(conn)
  247. # if addr:
  248. # self.conn_to_addr[conn] = addr
  249. # self.addr_to_conn[addr] = conn
  250. #
  251. # def unregister_conn(self, conn):
  252. # self.read_sockets_expected.remove(conn)
  253. # addr = self.conn_to_addr.get(conn, False)
  254. # if addr:
  255. # del self.addr_to_conn[addr]
  256. # del self.conn_to_addr[conn]
  257. #
  258. # def register_handler(self, handler, conn):
  259. # self.connection_handler.append(handler)
  260. # self.conn_to_handler[conn] = handler
  261. # self.handler_to_conn[handler] = conn
  262. #
  263. # def unregister_handler(self, handler, conn):
  264. # self.connection_handler.remove(handler)
  265. # del self.conn_to_handler[conn]
  266. # del self.handler_to_conn[handler]
  267. def setup(self):
  268. """
  269. creates a scoket, opens the connection and starts listening for
  270. clients. this method does not block, it returns after starting to
  271. listen.
  272. """
  273. lserver.info("setting up server")
  274. self.socket = self.make_socket()
  275. if self.af_family == "AF_INET":
  276. self.socket.bind((self.host, self.port))
  277. elif self.af_family == "AF_UNIX":
  278. if os.path.exists(self.host):
  279. lserver.debug("file already exists")
  280. lserver.debug("attempting to remove it")
  281. os.remove(self.host)
  282. self.socket.bind(self.host)
  283. self.socket.listen(self.max_allowed_clients)
  284. def shutdown(self):
  285. """
  286. This method properly shuts down the sockets and closes them.
  287. it unregisters itself from atexit, so it doesn't get executed twice
  288. when it was manually called before to program exits.
  289. """
  290. def error_handler(func,*args,log_text="error",async_=False,**kwargs):
  291. try:
  292. if async_:
  293. coro = func(*args,**kwargs)
  294. lserver.debug("{} {}".format(func,coro))
  295. else:
  296. func(*args,**kwargs)
  297. except Exception as exc:
  298. lserver.debug("error occured during "+log_text,exc_info=exc)
  299. atexit.unregister(self.shutdown)
  300. lserver.info("shutting down server")
  301. error_handler(
  302. self.handle_tasks.cancel_remaining,log_text="handler cancel",async_=True)
  303. # check if there is actually a socket. if the shutdown method is
  304. # executed before the start method, there is no socket.
  305. if self.socket:
  306. error_handler(self.socket.shutdown,log_text="socket shutdown")
  307. error_handler(self.socket.close,log_text="socket close")
  308. def start(self):
  309. """
  310. this method starts the server. it is blocking.
  311. """
  312. self.setup()
  313. curio.run(self.run)
  314. async def run(self):
  315. """
  316. this method is the main loop of the Server. it waits for new client
  317. connections and creates a handle task for each of them. it does not
  318. receive or send anything.
  319. """
  320. lserver.debug("entering main loop")
  321. while ( not self.exit_event ):
  322. lserver.debug("waiting for client to connect")
  323. # wait for a client to connect
  324. conn,addr = await self.socket.accept()
  325. if self.log_ip:
  326. lserver.info(
  327. "new client connection, {}:{}!".format(*addr))
  328. else:
  329. lserver.info("a new client connected, let's handle it")
  330. handler = self.make_handler(conn, addr)
  331. # self.register_conn(conn, addr)
  332. # self.register_handler(handler, conn)
  333. await self.handle_tasks.spawn(self.handle_client(conn,handler))
  334. async def handle_client(self,socket,handler):
  335. """
  336. This method waits for the client to send something and calls the
  337. ClientHandler's handle method. there is a handle_client method running for each client connected.
  338. """
  339. while True:
  340. try:
  341. if self.log_ip:
  342. lserver.debug("waiting for {} to send something"
  343. .format(socket.getsockname()))
  344. else:
  345. lserver.debug("waiting for the client to send something")
  346. # wait for the client to send something
  347. data = await handler.recv()
  348. # if there is no data the client disconnected. this is a
  349. # tcp protocoll specification.
  350. if not data:
  351. if self.log_ip:
  352. lserver.info("the connection to {} was closed"
  353. .format(socket.getsockname()))
  354. else:
  355. lserver.info("the connection to the client was closed")
  356. # self.unregister_handler(handler, socket)
  357. # self.unregister_conn(socket)
  358. await handler.close()
  359. # break out of the loop. don't return because we need to
  360. # do cleanup
  361. break
  362. else:
  363. # don't strip the data of its whitespaces, since they may
  364. # be important.
  365. lschat.info("Client:"+data.rstrip())
  366. coro = handler.handle(data)
  367. task = await curio.spawn(coro)
  368. except Exception as e:
  369. lserver.error(e, exc_info=True)
  370. # let's sleep a bit, in case something is broken and the
  371. # loop throws an exception every time
  372. await curio.sleep(0.01)
  373. # if a task exits and hasn't been joined, curio prints a warning.
  374. # we don't want the warning, so let's join the current task for 0
  375. # seconds. instead of task.join() we use task.wait(). the only
  376. # difference is that wait doesn't throw a exception if the task was
  377. # stopped or crashed
  378. cur_task = await curio.current_task()
  379. await curio.ignore_after(0,cur_task.wait)
  380. class Client(threading.Thread):
  381. """docstring for Client"""
  382. is_connecting = False
  383. is_connected = False
  384. status = "uninitialized"
  385. def __init__(self,
  386. host,
  387. port=None,
  388. af_family="AF_INET",
  389. handle_data_func=None,
  390. error_handler=None,
  391. block_size=1024,
  392. ):
  393. self.super_class = super(Client, self)
  394. self.super_class.__init__()
  395. self.name = "Client"
  396. self.exit_event = False
  397. self.host = host
  398. self.port = port
  399. self.af_family = af_family
  400. self.block_size = block_size
  401. self.handle_data_func = handle_data_func
  402. self.is_connected = False
  403. self.error_handler = error_handler
  404. # self.socket = self.make_socket()
  405. self.socket = None
  406. self.status = "disconnected"
  407. def connect(self):
  408. self.status = "connecting"
  409. self.socket = self.make_socket()
  410. lclient.info(
  411. "connecting to socket '{}' of type {}".format(
  412. self.host,
  413. self.af_family
  414. )
  415. )
  416. try:
  417. if self.af_family == "AF_INET":
  418. self.socket.connect((self.host, self.port))
  419. elif self.af_family == "AF_UNIX":
  420. if os.path.exists(self.host):
  421. self.socket.connect(self.host)
  422. else:
  423. lclient.warn("File not found. Aborting.")
  424. return
  425. self.is_connected = True
  426. self.status = "connected"
  427. lclient.info("connected")
  428. return True
  429. except Exception as e:
  430. lclient.debug(e, exc_info=True)
  431. if type(e) is ConnectionRefusedError:
  432. lclient.info("failed to connect to socket '{}'".format(self.host))
  433. self.disconnect()
  434. return False
  435. def disconnect(self):
  436. lclient.info("disconnecting from socket '{}'".format(self.host))
  437. self.is_connected = False
  438. self.status = "disconnected"
  439. if self.socket:
  440. try:
  441. self.socket.shutdown(socket.SHUT_RDWR)
  442. except Exception as e:
  443. lclient.error(e)
  444. try:
  445. self.socket.close()
  446. except Exception as e:
  447. lclient.error("error occured while closing the socket, " +
  448. "maybe it is already closed",exc_info=e)
  449. del self.socket
  450. self.socket = None
  451. def handle_data(self, data_received):
  452. data_decoded = data_received.decode("utf-8")
  453. lcchat.info("Server: "+data_decoded)
  454. if self.handle_data_func:
  455. try:
  456. self.handle_data_func(data_decoded)
  457. except Exception as e:
  458. lclient.error(e, exc_info=True)
  459. def is_running(self):
  460. return (self in threading.enumerate())
  461. def make_socket(self):
  462. lclient.info("creating a {} socket".format(self.af_family))
  463. if self.af_family == "AF_INET":
  464. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  465. elif self.af_family == "AF_UNIX":
  466. s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
  467. else:
  468. raise ValueError(
  469. "AF_FAMILY '{}' not supported!".format(
  470. self.af_family
  471. )
  472. )
  473. return s
  474. def main_loop(self):
  475. lclient.debug("starting main loop")
  476. while ( not self.exit_event ):
  477. if not self.status in ["connected"]:
  478. time.sleep(0.1)
  479. continue
  480. # print(0)
  481. read_confirmed, write_confirmed, exc_confirmed \
  482. = select.select(
  483. [self.socket],
  484. [],
  485. [self.socket],
  486. 1
  487. )
  488. if self.socket in exc_confirmed:
  489. self.is_connected = False
  490. lclient.warning("socket is expected to corrupt, exiting")
  491. self.disconnect()
  492. # self.stop()
  493. break
  494. elif self.socket in read_confirmed:
  495. try:
  496. data_received = self.read_from_socket()
  497. if data_received == b'':
  498. lclient.info("connection is broken, closing socket exiting")
  499. self.disconnect()
  500. # self.stop()
  501. # break
  502. else:
  503. try:
  504. self.handle_data(data_received)
  505. except Exception as e:
  506. lserver.error(
  507. "Error while handling data",
  508. exc_info=e
  509. )
  510. except Exception as e:
  511. lclient.error(e, exc_info=True)
  512. if type(e) is OSError:
  513. self.is_connected = False
  514. lclient.warn("connection broken, exiting")
  515. self.disconnect()
  516. # self.stop()
  517. # break
  518. else:
  519. raise
  520. else:
  521. time.sleep(0.1)
  522. def read_from_socket(self):
  523. data_received = self.socket.recv(self.block_size)
  524. return data_received
  525. def run(self):
  526. # self.connect()
  527. if self.error_handler:
  528. self.error_handler(self.main_loop)
  529. else:
  530. self.main_loop()
  531. def send(self, msg):
  532. msg = msg.rstrip()
  533. msg_encoded = bytes(msg+"\r\n", "utf-8")
  534. try:
  535. lcchat.info("Client: "+msg)
  536. self.socket.send(msg_encoded)
  537. except Exception as e:
  538. self.is_connected = False
  539. lclient.error(e, exc_info=True)
  540. self.status = "shutdown"
  541. def setup(self):
  542. pass
  543. def stop(self,reason=None):
  544. self.disconnect()
  545. self.exit_event = True
  546. if reason:
  547. print(reason)
  548. class AsyncClient(object):
  549. """docstring for Client"""
  550. is_connecting = False
  551. is_connected = False
  552. status = "uninitialized"
  553. def __init__(self,
  554. host,
  555. port=None,
  556. af_family="AF_INET",
  557. handle_data_func=None,
  558. error_handler=None,
  559. block_size=1024,
  560. ):
  561. self.super_class = super(AsyncClient, self)
  562. self.super_class.__init__()
  563. self.name = "Client"
  564. self.exit_event = False
  565. self.host = host
  566. self.port = port
  567. self.af_family = af_family
  568. self.block_size = block_size
  569. self.handle_data_func = handle_data_func
  570. self.is_connected = False
  571. self.error_handler = error_handler
  572. self.socket = None
  573. self.status = "disconnected"
  574. def connect(self):
  575. self.status = "connecting"
  576. self.socket = self.make_socket()
  577. lclient.info("connecting to socket '{}' of type {}".format(
  578. self.host,self.af_family))
  579. try:
  580. if self.af_family == "AF_INET":
  581. self.socket.connect((self.host, self.port))
  582. elif self.af_family == "AF_UNIX":
  583. self.socket.connect(self.host)
  584. self.is_connected = True
  585. self.status = "connected"
  586. lclient.info("connected")
  587. return True
  588. except Exception as e:
  589. lclient.debug(e, exc_info=True)
  590. if type(e) is ConnectionRefusedError:
  591. lclient.info("failed to connect to socket '{}'".format(self.host))
  592. self.disconnect()
  593. return False
  594. def disconnect(self):
  595. lclient.info("disconnecting from socket '{}'".format(self.host))
  596. self.is_connected = False
  597. self.status = "disconnected"
  598. if self.socket:
  599. try:
  600. self.socket.shutdown(socket.SHUT_RDWR)
  601. except Exception as e:
  602. lclient.error(e)
  603. try:
  604. self.socket.close()
  605. except Exception as e:
  606. lclient.error("error occured while closing the socket, " +
  607. "maybe it is already closed",exc_info=e)
  608. del self.socket
  609. self.socket = None
  610. def handle_data(self, data_received):
  611. data_decoded = data_received.decode("utf-8")
  612. lcchat.info("Server: "+data_decoded)
  613. if self.handle_data_func:
  614. try:
  615. self.handle_data_func(data_decoded)
  616. except Exception as e:
  617. lclient.error(e, exc_info=True)
  618. def is_running(self):
  619. return (self in threading.enumerate())
  620. def make_socket(self):
  621. lclient.info("creating a {} socket".format(self.af_family))
  622. if self.af_family == "AF_INET":
  623. s = trio.socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  624. elif self.af_family == "AF_UNIX":
  625. s = trio.socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
  626. else:
  627. raise ValueError(
  628. "AF_FAMILY '{}' not supported!".format(
  629. self.af_family
  630. )
  631. )
  632. return s
  633. async def read_from_socket(self):
  634. data_received = await self.socket.recv(self.block_size)
  635. return data_received
  636. async def recv(self):
  637. data_received = await self.socket.recv(self.block_size)
  638. data_decoded = data_received.decode("utf-8")
  639. return data_decoded
  640. async def run(self):
  641. lclient.debug("starting main loop")
  642. while ( not self.exit_event ):
  643. if not self.status in ["connected"]:
  644. time.sleep(0.1)
  645. continue
  646. # if self.socket in exc_confirmed:
  647. # self.is_connected = False
  648. # lclient.warning("socket is expected to corrupt, exiting")
  649. # self.disconnect()
  650. # # self.stop()
  651. # break
  652. # elif self.socket in read_confirmed:
  653. try:
  654. data = await self.read_from_socket()
  655. if not data:
  656. lclient.info("connection closed")
  657. self.disconnect()
  658. else:
  659. self.handle_data(data)
  660. except Exception as e:
  661. lclient.error(e, exc_info=True)
  662. if type(e) is OSError:
  663. self.is_connected = False
  664. lclient.warn("connection broken, exiting")
  665. self.disconnect()
  666. else:
  667. raise
  668. async def start(self,connect=False):
  669. if connect:
  670. self.connect()
  671. if self.error_handler:
  672. self.error_handler(self.run)
  673. else:
  674. self.run()
  675. def send(self, msg):
  676. msg = msg.rstrip()
  677. msg_encoded = bytes(msg+"\r\n", "utf-8")
  678. try:
  679. lcchat.info("Client: "+msg)
  680. self.socket.send(msg_encoded)
  681. except Exception as e:
  682. self.is_connected = False
  683. lclient.error(e, exc_info=True)
  684. self.disconnect()
  685. # self.exit_event = True
  686. # self.status = "shutdown"
  687. def setup(self):
  688. pass
  689. def stop(self,reason=None):
  690. self.disconnect()
  691. self.exit_event = True
  692. if reason:
  693. print(reason)
  694. #