116 lines
3.7 KiB
Python
Raw Normal View History

2014-01-20 14:12:37 -05:00
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.
2014-01-02 10:26:00 -05:00
#
2014-01-20 14:12:37 -05:00
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
2014-01-02 10:26:00 -05:00
#
2014-01-20 14:12:37 -05:00
# http://www.apache.org/licenses/LICENSE-2.0
2014-01-02 10:26:00 -05:00
#
2014-01-20 14:12:37 -05:00
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
2014-01-13 15:39:04 -05:00
__all__ = [
"read_socket_input",
"write_socket_output"
2014-01-20 14:12:37 -05:00
]
2014-01-13 15:39:04 -05:00
2014-01-20 14:12:37 -05:00
import errno
import logging
import socket
2014-01-02 10:26:00 -05:00
from connection import Connection
LOG = logging.getLogger(__name__)
"""helper methods that provide boilerplate socket I/O and Connection
processing.
"""
2014-01-20 14:12:37 -05:00
def read_socket_input(connection, socket_obj):
2014-01-02 10:26:00 -05:00
"""Read from the network layer and processes all data read. Can
support both blocking and non-blocking sockets.
Returns the number of input bytes processed, or EOS if input processing
is done. Any exceptions raised by the socket are re-raised.
"""
count = connection.needs_input
if count <= 0:
return count # 0 or EOS
try:
sock_data = socket_obj.recv(count)
2014-01-20 14:12:37 -05:00
except socket.timeout as e:
2014-01-02 10:26:00 -05:00
LOG.debug("Socket timeout exception %s", str(e))
raise # caller must handle
2014-01-20 14:12:37 -05:00
except socket.error as e:
2014-01-02 10:26:00 -05:00
LOG.debug("Socket error exception %s", str(e))
err = e.args[0]
# ignore non-fatal errors
if (err != errno.EAGAIN and
2014-01-20 14:12:37 -05:00
err != errno.EWOULDBLOCK and
err != errno.EINTR):
2014-01-02 10:26:00 -05:00
# otherwise, unrecoverable:
connection.close_input()
raise # caller must handle
2014-01-20 14:12:37 -05:00
except Exception as e: # beats me... assume fatal
2014-01-02 10:26:00 -05:00
LOG.debug("unknown socket exception %s", str(e))
connection.close_input()
raise # caller must handle
if sock_data:
2014-01-20 14:12:37 -05:00
count = connection.process_input(sock_data)
2014-01-02 10:26:00 -05:00
else:
LOG.debug("Socket closed")
count = Connection.EOS
connection.close_input()
return count
2014-01-20 14:12:37 -05:00
def write_socket_output(connection, socket_obj):
2014-01-02 10:26:00 -05:00
"""Write data to the network layer. Can support both blocking and
non-blocking sockets.
Returns the number of output bytes sent, or EOS if output processing
is done. Any exceptions raised by the socket are re-raised.
2014-01-02 10:26:00 -05:00
"""
count = connection.has_output
if count <= 0:
2014-01-20 14:12:37 -05:00
return count # 0 or EOS
2014-01-02 10:26:00 -05:00
data = connection.output_data()
if not data:
# error - has_output > 0, but no data?
return Connection.EOS
2014-01-02 10:26:00 -05:00
try:
count = socket_obj.send(data)
2014-01-20 14:12:37 -05:00
except socket.timeout as e:
2014-01-02 10:26:00 -05:00
LOG.debug("Socket timeout exception %s", str(e))
2014-01-20 14:12:37 -05:00
raise # caller must handle
except socket.error as e:
2014-01-02 10:26:00 -05:00
LOG.debug("Socket error exception %s", str(e))
err = e.args[0]
# ignore non-fatal errors
if (err != errno.EAGAIN and
2014-01-20 14:12:37 -05:00
err != errno.EWOULDBLOCK and
err != errno.EINTR):
2014-01-02 10:26:00 -05:00
# otherwise, unrecoverable
connection.close_output()
raise
2014-01-20 14:12:37 -05:00
except Exception as e: # beats me... assume fatal
2014-01-02 10:26:00 -05:00
LOG.debug("unknown socket exception %s", str(e))
connection.close_output()
raise
if count > 0:
connection.output_written(count)
elif data:
LOG.debug("Socket closed")
count = Connection.EOS
connection.close_output()
return count