(wip) use select instead of a thread by server. Currently read and write seems to work properly, we lost some code (like more, next, ...) that never should be in server part
This commit is contained in:
parent
28c1ad088b
commit
81593a493b
8 changed files with 215 additions and 429 deletions
77
server/socket.py
Normal file
77
server/socket.py
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Nemubot is a smart and modulable IM bot.
|
||||
# Copyright (C) 2012-2014 nemunaire
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import ssl
|
||||
import socket
|
||||
|
||||
from server import AbstractServer
|
||||
|
||||
class SocketServer(AbstractServer):
|
||||
|
||||
def __init__(self, host, port=6667, password=None, ssl=False):
|
||||
AbstractServer.__init__(self)
|
||||
self.host = host
|
||||
self.port = int(port)
|
||||
self.password = password
|
||||
self.ssl = ssl
|
||||
|
||||
self.socket = None
|
||||
self.readbuffer = b''
|
||||
|
||||
def fileno(self):
|
||||
return self.socket.fileno() if self.socket else None
|
||||
|
||||
def _open(self):
|
||||
# Create the socket
|
||||
self.socket = socket.socket()
|
||||
|
||||
# Wrap the socket for SSL
|
||||
if self.ssl:
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
|
||||
self.socket = ctx.wrap_socket(self.socket)
|
||||
|
||||
try:
|
||||
self.socket.connect((self.host, self.port)) #Connect to server
|
||||
self.logger.info("Connected to %s:%d", self.host, self.port)
|
||||
except socket.error as e:
|
||||
self.socket = None
|
||||
self.logger.critical("Unable to connect to %s:%d: %s",
|
||||
self.host, self.port, os.strerror(e.errno))
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _close(self):
|
||||
if self.socket is not None:
|
||||
self.socket.shutdown(SHUT_RDWR)
|
||||
self.socket.close()
|
||||
self.socket = None
|
||||
|
||||
def _write(self, cnt):
|
||||
self.socket.send(cnt)
|
||||
|
||||
def format(self, txt):
|
||||
return txt.encode() + b'\r\n'
|
||||
|
||||
def read(self):
|
||||
raw = self.socket.recv(1024)
|
||||
temp = (self.readbuffer + raw).split(b'\r\n')
|
||||
self.readbuffer = temp.pop()
|
||||
|
||||
for line in temp:
|
||||
yield line
|
||||
Loading…
Add table
Add a link
Reference in a new issue