1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2002 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a base class of an asynchronous interface for the debugger. |
|
8 """ |
|
9 |
|
10 |
|
11 class AsyncIO(object): |
|
12 """ |
|
13 Class implementing asynchronous reading and writing. |
|
14 """ |
|
15 def __init__(self): |
|
16 """ |
|
17 Constructor |
|
18 """ |
|
19 # There is no connection yet. |
|
20 self.disconnect() |
|
21 |
|
22 def disconnect(self): |
|
23 """ |
|
24 Public method to disconnect any current connection. |
|
25 """ |
|
26 self.readfd = None |
|
27 self.writefd = None |
|
28 |
|
29 def setDescriptors(self, rfd, wfd): |
|
30 """ |
|
31 Public method called to set the descriptors for the connection. |
|
32 |
|
33 @param rfd file descriptor of the input file (int) |
|
34 @param wfd file descriptor of the output file (int) |
|
35 """ |
|
36 self.rbuf = '' |
|
37 self.readfd = rfd |
|
38 |
|
39 self.wbuf = '' |
|
40 self.writefd = wfd |
|
41 |
|
42 def readReady(self, fd): |
|
43 """ |
|
44 Public method called when there is data ready to be read. |
|
45 |
|
46 @param fd file descriptor of the file that has data to be read (int) |
|
47 """ |
|
48 try: |
|
49 got = self.readfd.readline_p() |
|
50 except Exception: |
|
51 return |
|
52 |
|
53 if len(got) == 0: |
|
54 self.sessionClose() |
|
55 return |
|
56 |
|
57 self.rbuf = self.rbuf + got |
|
58 |
|
59 # Call handleLine for the line if it is complete. |
|
60 eol = self.rbuf.find('\n') |
|
61 |
|
62 while eol >= 0: |
|
63 s = self.rbuf[:eol + 1] |
|
64 self.rbuf = self.rbuf[eol + 1:] |
|
65 self.handleLine(s) |
|
66 eol = self.rbuf.find('\n') |
|
67 |
|
68 def writeReady(self, fd): |
|
69 """ |
|
70 Public method called when we are ready to write data. |
|
71 |
|
72 @param fd file descriptor of the file that has data to be written (int) |
|
73 """ |
|
74 self.writefd.write(self.wbuf) |
|
75 self.writefd.flush() |
|
76 self.wbuf = '' |
|
77 |
|
78 def write(self, s): |
|
79 """ |
|
80 Public method to write a string. |
|
81 |
|
82 @param s the data to be written (string) |
|
83 """ |
|
84 self.wbuf = self.wbuf + s |
|
85 |
|
86 # |
|
87 # eflag: FileType = Python2 |
|
88 # eflag: noqa = M601, M702 |
|