Skip to content

Commit

Permalink
move lower-level r/w functionality to c_connection.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bleepbop committed Dec 1, 2023
1 parent f9fcc7e commit 599419d
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions app/contacts/contact_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ async def refresh(self):
session = self.sessions[index]

try:
session.connection.writer.write(str.encode(' '))
await session.connection.writer.drain()
await session.connection.send(str.encode(' '))
except socket.error:
self.log.debug('Error occurred when refreshing session %s. Removing from session pool.', session.id)
del self.sessions[index]
Expand All @@ -85,10 +84,9 @@ async def accept(self, reader, writer):
async def send(self, session_id: int, cmd: str, timeout: int = 60) -> Tuple[int, str, str, str]:
try:
conn = next(i.connection for i in self.sessions if i.id == int(session_id))
conn.writer.write(str.encode(' '))
await conn.send(str.encode(' '))
time.sleep(0.01)
conn.writer.write(str.encode('%s\n' % cmd))
await conn.writer.drain()
await conn.send(str.encode('%s\n' % cmd))
response = await self._attempt_connection(session_id, conn, timeout=timeout)
response = json.loads(response)
return response['status'], response['pwd'], response['response'], response.get('agent_reported_time', '')
Expand All @@ -108,7 +106,7 @@ async def _attempt_connection(self, session_id, connection, timeout):
time.sleep(0.1) # initial wait for fast operations.
while True:
try:
part = await connection.reader.read(buffer)
part = await connection.recv(buffer)
data += part
if len(part) < buffer:
break
Expand Down

0 comments on commit 599419d

Please sign in to comment.