MCPcopy Create free account
hub / github.com/Ishabdullah/Codey-v2 / _handle_client

Method _handle_client

core/daemon.py:294–334  ·  view source on GitHub ↗

Handle incoming client connection.

(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter)

Source from the content-addressed store, hash-verified

292 return {"status": "ok", "message": "Shutting down"}
293
294 async def _handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
295 """Handle incoming client connection."""
296 try:
297 # Read request (JSON message)
298 data = await reader.read(65536)
299 if not data:
300 return
301
302 request = json.loads(data.decode('utf-8'))
303 cmd = request.get("cmd", "unknown")
304
305 info(f"Received command: {cmd}")
306
307 # Route to handler
308 handler = self._handlers.get(cmd)
309 if handler:
310 response = await handler(request.get("data", {}))
311 else:
312 response = {"status": "error", "message": f"Unknown command: {cmd}"}
313
314 # Send response
315 writer.write(json.dumps(response).encode('utf-8'))
316 await writer.drain()
317
318 except json.JSONDecodeError as e:
319 error(f"Invalid JSON from client: {e}")
320 writer.write(json.dumps({"status": "error", "message": "Invalid JSON"}).encode('utf-8'))
321 await writer.drain()
322 except Exception as e:
323 error(f"Error handling client: {e}")
324 try:
325 writer.write(json.dumps({"status": "error", "message": str(e)}).encode('utf-8'))
326 await writer.drain()
327 except:
328 pass
329 finally:
330 try:
331 writer.close()
332 await writer.wait_closed()
333 except:
334 pass
335
336 async def start(self):
337 """Start the socket server."""

Callers

nothing calls this directly

Calls 6

infoFunction · 0.90
errorFunction · 0.90
readMethod · 0.80
getMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected