Move buffered input, output handling into lib/nntp/tiny/socket.py
This commit is contained in:
parent
9ea8306806
commit
f2a60a8b1a
2 changed files with 23 additions and 17 deletions
|
@ -9,7 +9,7 @@ import email.utils
|
|||
|
||||
from typing import Optional
|
||||
|
||||
from nntp.tiny.buffer import LineBuffer, OutputBuffer, BufferOverflow
|
||||
from nntp.tiny.socket import Connection
|
||||
from nntp.tiny.db import Database
|
||||
from nntp.tiny.response import Response, ResponseCode
|
||||
from nntp.tiny.newsgroup import Newsgroup
|
||||
|
@ -20,7 +20,7 @@ from nntp.tiny.message import (Message, MessageRange, MessagePart,
|
|||
class SessionMode(enum.Enum):
|
||||
READER = 1
|
||||
|
||||
class Session():
|
||||
class Session(Connection):
|
||||
NNTP_VERSION = 2
|
||||
NNTP_CAPABILITIES = [
|
||||
'VERSION %d' % (NNTP_VERSION),
|
||||
|
@ -37,9 +37,6 @@ class Session():
|
|||
def __init__(self, server, sock: socket.socket):
|
||||
self.server = server
|
||||
self.db: Database = server.connect_to_db()
|
||||
self.sock: socket.socket = sock
|
||||
self.buf: LineBuffer = LineBuffer()
|
||||
self.output: OutputBuffer = OutputBuffer(sock)
|
||||
self.mode: SessionMode = SessionMode.READER
|
||||
self.active: bool = True
|
||||
|
||||
|
@ -48,18 +45,7 @@ class Session():
|
|||
self.perms: Optional[UserPermission] = None
|
||||
self.article_id: Optional[int] = None
|
||||
|
||||
def print(self, text: str, end: str="\r\n"):
|
||||
return self.output.print(text, end)
|
||||
|
||||
def flush(self):
|
||||
return self.output.flush()
|
||||
|
||||
def readline(self):
|
||||
self.flush()
|
||||
return self.buf.readline(self.sock)
|
||||
|
||||
def end(self):
|
||||
return self.print('.')
|
||||
super().__init__(sock)
|
||||
|
||||
def respond(self, code: ResponseCode, message: str=None, body=None):
|
||||
response = Response(code, message, body)
|
||||
|
|
20
lib/nntp/tiny/socket.py
Normal file
20
lib/nntp/tiny/socket.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from nntp.tiny.buffer import OutputBuffer, LineBuffer
|
||||
|
||||
class Connection():
|
||||
def __init__(self, sock):
|
||||
self.sock = sock
|
||||
self.output = OutputBuffer(sock)
|
||||
self.buf = LineBuffer()
|
||||
|
||||
def print(self, text: str, end: str="\r\n"):
|
||||
return self.output.print(text, end)
|
||||
|
||||
def flush(self):
|
||||
return self.output.flush()
|
||||
|
||||
def readline(self):
|
||||
self.flush()
|
||||
return self.buf.readline(self.sock)
|
||||
|
||||
def end(self):
|
||||
return self.print('.')
|
Loading…
Add table
Reference in a new issue