33# pylint: disable=C0209
44
55import logging as _logging
6+ import os as _os
67import socket as _socket
78import ssl as _ssl
89
10+ import ser2tcp .connection_socket as _connection_socket
911import ser2tcp .connection_ssl as _connection_ssl
1012import ser2tcp .connection_tcp as _connection_tcp
1113import ser2tcp .connection_telnet as _connection_telnet
@@ -22,6 +24,7 @@ class Server():
2224 'TCP' : _connection_tcp .ConnectionTcp ,
2325 'TELNET' : _connection_telnet .ConnectionTelnet ,
2426 'SSL' : _connection_ssl .ConnectionSsl ,
27+ 'SOCKET' : _connection_socket .ConnectionSocket ,
2528 }
2629
2730 def __init__ (self , config , ser , log = None ):
@@ -34,19 +37,32 @@ def __init__(self, config, ser, log=None):
3437 self ._buffer_limit = self ._config .get ('buffer_limit' )
3538 self ._ssl_context = None
3639 self ._socket = None
37- self ._log .info (
38- " Server: %s %d %s" ,
39- self ._config ['address' ],
40- self ._config ['port' ],
41- self ._protocol )
4240 if self ._protocol not in self .CONNECTIONS :
4341 raise ConfigError ('Unknown protocol %s' % self ._protocol )
44- if self ._protocol == 'SSL' :
45- self ._ssl_context = self ._create_ssl_context ()
46- self ._socket = _socket .socket (
47- _socket .AF_INET , _socket .SOCK_STREAM , _socket .IPPROTO_TCP )
48- self ._socket .setsockopt (_socket .SOL_SOCKET , _socket .SO_REUSEADDR , 1 )
49- self ._socket .bind ((config ['address' ], config ['port' ]))
42+ if self ._protocol == 'SOCKET' :
43+ self ._log .info (
44+ " Server: %s %s" ,
45+ self ._config ['address' ],
46+ self ._protocol )
47+ self ._socket = _socket .socket (
48+ _socket .AF_UNIX , _socket .SOCK_STREAM )
49+ sock_path = config ['address' ]
50+ if _os .path .exists (sock_path ):
51+ _os .unlink (sock_path )
52+ self ._socket .bind (sock_path )
53+ else :
54+ self ._log .info (
55+ " Server: %s %d %s" ,
56+ self ._config ['address' ],
57+ self ._config ['port' ],
58+ self ._protocol )
59+ if self ._protocol == 'SSL' :
60+ self ._ssl_context = self ._create_ssl_context ()
61+ self ._socket = _socket .socket (
62+ _socket .AF_INET , _socket .SOCK_STREAM , _socket .IPPROTO_TCP )
63+ self ._socket .setsockopt (
64+ _socket .SOL_SOCKET , _socket .SO_REUSEADDR , 1 )
65+ self ._socket .bind ((config ['address' ], config ['port' ]))
5066 self ._socket .listen (1 )
5167
5268 def __del__ (self ):
@@ -70,6 +86,8 @@ def _create_ssl_context(self):
7086 def _client_connect (self ):
7187 """connect to client, will accept waiting connection"""
7288 sock , addr = self ._socket .accept ()
89+ if self ._protocol == 'SOCKET' :
90+ addr = (self ._config ['address' ],)
7391 kwargs = {
7492 'connection' : (sock , addr ),
7593 'ser' : self ._serial ,
@@ -82,7 +100,8 @@ def _client_connect(self):
82100 try :
83101 connection = self .CONNECTIONS [self ._protocol ](** kwargs )
84102 except _connection_ssl .SslHandshakeError as err :
85- self ._log .info ("Client rejected: %s:%d (%s)" , * addr , err )
103+ self ._log .info (
104+ "Client rejected: %s:%d (%s)" , addr [0 ], addr [1 ], err )
86105 if not self ._connections :
87106 self ._serial .disconnect ()
88107 return
@@ -102,6 +121,10 @@ def close(self):
102121 self .close_connections ()
103122 self ._socket .close ()
104123 self ._socket = None
124+ if self ._protocol == 'SOCKET' :
125+ sock_path = self ._config ['address' ]
126+ if _os .path .exists (sock_path ):
127+ _os .unlink (sock_path )
105128
106129 def has_connections (self ):
107130 """True if server has some connections"""
@@ -138,9 +161,9 @@ def process_read(self, read_sockets):
138161 data = b''
139162 try :
140163 data = con .socket ().recv (4096 )
141- self ._log .debug ("(%s:%d ): %s" , * con .get_address (), data )
164+ self ._log .debug ("(%s): %s" , con .address_str (), data )
142165 except (ConnectionResetError , _ssl .SSLError ) as err :
143- self ._log .info ("(%s:%d ): %s" , * con .get_address (), err )
166+ self ._log .info ("(%s): %s" , con .address_str (), err )
144167 if not data :
145168 self ._remove_connection (con )
146169 continue
@@ -153,15 +176,15 @@ def process_write(self, write_sockets):
153176 result = con .flush ()
154177 if result is None :
155178 self ._log .info (
156- "(%s:%d ): write error" , * con .get_address ())
179+ "(%s): write error" , con .address_str ())
157180 self ._remove_connection (con )
158181
159182 def process_stale (self ):
160183 """Remove stale connections (send timeout expired)"""
161184 for con in list (self ._connections ):
162185 if con .is_stale ():
163186 self ._log .info (
164- "(%s:%d ): send timeout" , * con .get_address ())
187+ "(%s): send timeout" , con .address_str ())
165188 self ._remove_connection (con )
166189
167190 def send (self , data ):
0 commit comments