Commit 2fba7809 authored by Christian Bergmiller's avatar Christian Bergmiller

[ADD] async modules wip

parent 271034d1
This diff is collapsed.
This diff is collapsed.
import asyncio
import logging
from opcua.common.connection import SecureConnection
from opcua.ua.async_ua_binary import header_from_binary
from opcua import ua
logger = logging.getLogger('opcua.uaprotocol')
class AsyncSecureConnection(SecureConnection):
"""
Async version of SecureConnection
"""
async def receive_from_socket(self, protocol):
"""
Convert binary stream to OPC UA TCP message (see OPC UA
specs Part 6, 7.1: Hello, Acknowledge or ErrorMessage), or a Message
object, or None (if intermediate chunk is received)
"""
logger.debug("Waiting for header")
header = await header_from_binary(protocol)
logger.info("received header: %s", header)
body = await protocol.read(header.body_size)
if len(body) != header.body_size:
# ToDo: should never happen since UASocketProtocol.read() waits until `size` bytes are received. Remove?
raise ua.UaError("{0} bytes expected, {1} available".format(header.body_size, len(body)))
return self.receive_from_header_and_body(header, ua.utils.Buffer(body))
import struct
from opcua import ua
from opcua.ua.ua_binary import Primitives
async def header_from_binary(data):
hdr = ua.Header()
hdr.MessageType, hdr.ChunkType, hdr.packet_size = struct.unpack("<3scI", await data.read(8))
hdr.body_size = hdr.packet_size - 8
if hdr.MessageType in (ua.MessageType.SecureOpen, ua.MessageType.SecureClose, ua.MessageType.SecureMessage):
hdr.body_size -= 4
hdr.ChannelId = Primitives.UInt32.unpack(ua.utils.Buffer(await data.read(4)))
return hdr
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment