From 409c1f17d767e7c48f6940feeb318eba3a20a0b1 Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Sun, 18 Sep 2011 10:06:37 +0200 Subject: [PATCH] byte order independent code, transportRevision is int8 --- pvAccessApp/ca/caConstants.h | 9 ++----- .../remote/blockingClientTCPTransport.cpp | 2 +- pvAccessApp/remote/blockingTCP.h | 8 +++--- pvAccessApp/remote/blockingTCPConnector.cpp | 2 +- pvAccessApp/remote/blockingTCPTransport.cpp | 26 +++++++++++-------- pvAccessApp/remote/blockingUDP.h | 2 +- pvAccessApp/remote/blockingUDPConnector.cpp | 2 +- pvAccessApp/remote/blockingUDPTransport.cpp | 12 +++++---- pvAccessApp/remote/channelSearchManager.cpp | 3 ++- pvAccessApp/remote/remote.h | 2 +- .../remoteClient/clientContextImpl.cpp | 2 +- pvAccessApp/remoteClient/clientContextImpl.h | 2 +- testApp/remote/testBlockingTCPClnt.cpp | 2 +- 13 files changed, 38 insertions(+), 36 deletions(-) diff --git a/pvAccessApp/ca/caConstants.h b/pvAccessApp/ca/caConstants.h index 8b19cdb..5ab07ef 100644 --- a/pvAccessApp/ca/caConstants.h +++ b/pvAccessApp/ca/caConstants.h @@ -31,13 +31,8 @@ namespace epics { /** Unknown CA protocol minor revision. */ const int8 CA_UNKNOWN_MINOR_PROTOCOL_REVISION = 0; - /** CA magic/major version signature (e.g. 0xCA50). */ - const int16 CA_MAGIC_AND_MAJOR_VERSION = CA_MAGIC<<8 - |CA_MAJOR_PROTOCOL_REVISION<<4; - - /** CA magic/version signature (e.g. 0xCA51). */ - const int16 CA_MAGIC_AND_VERSION = CA_MAGIC<<8 - |CA_MAJOR_PROTOCOL_REVISION<<4|CA_MINOR_PROTOCOL_REVISION; + /** CA version signature (e.g. 0x50). */ + const int8 CA_VERSION = ((unsigned int8)CA_MAJOR_PROTOCOL_REVISION<<4)|CA_MINOR_PROTOCOL_REVISION; /** CA protocol port base. */ const int32 CA_PORT_BASE = 5056; diff --git a/pvAccessApp/remote/blockingClientTCPTransport.cpp b/pvAccessApp/remote/blockingClientTCPTransport.cpp index 7455b21..2fe61e3 100644 --- a/pvAccessApp/remote/blockingClientTCPTransport.cpp +++ b/pvAccessApp/remote/blockingClientTCPTransport.cpp @@ -34,7 +34,7 @@ namespace epics { BlockingClientTCPTransport::BlockingClientTCPTransport( Context::shared_pointer const & context, SOCKET channel, auto_ptr& responseHandler, int receiveBufferSize, - TransportClient::shared_pointer client, short remoteTransportRevision, + TransportClient::shared_pointer client, int8 remoteTransportRevision, float beaconInterval, int16 priority) : BlockingTCPTransport(context, channel, responseHandler, receiveBufferSize, priority), _introspectionRegistry(false), diff --git a/pvAccessApp/remote/blockingTCP.h b/pvAccessApp/remote/blockingTCP.h index 2fc2dff..2b2fe1a 100644 --- a/pvAccessApp/remote/blockingTCP.h +++ b/pvAccessApp/remote/blockingTCP.h @@ -343,7 +343,7 @@ namespace epics { int _storedPosition; int _storedLimit; - short _magicAndVersion; + int8 _version; int8 _packetType; int8 _command; int _payloadSize; @@ -447,13 +447,13 @@ namespace epics { private: BlockingClientTCPTransport(Context::shared_pointer const & context, SOCKET channel, std::auto_ptr& responseHandler, int receiveBufferSize, - TransportClient::shared_pointer client, short remoteTransportRevision, + TransportClient::shared_pointer client, int8 remoteTransportRevision, float beaconInterval, int16 priority); public: static BlockingClientTCPTransport::shared_pointer create(Context::shared_pointer const & context, SOCKET channel, std::auto_ptr& responseHandler, int receiveBufferSize, - TransportClient::shared_pointer client, short remoteTransportRevision, + TransportClient::shared_pointer client, int8 remoteTransportRevision, float beaconInterval, int16 priority) { BlockingClientTCPTransport::shared_pointer thisPointer( @@ -592,7 +592,7 @@ namespace epics { virtual Transport::shared_pointer connect(TransportClient::shared_pointer const & client, std::auto_ptr& responseHandler, osiSockAddr& address, - short transportRevision, int16 priority); + int8 transportRevision, int16 priority); private: /** * Lock timeout diff --git a/pvAccessApp/remote/blockingTCPConnector.cpp b/pvAccessApp/remote/blockingTCPConnector.cpp index e4c9f0e..966e6c3 100644 --- a/pvAccessApp/remote/blockingTCPConnector.cpp +++ b/pvAccessApp/remote/blockingTCPConnector.cpp @@ -67,7 +67,7 @@ namespace epics { Transport::shared_pointer BlockingTCPConnector::connect(TransportClient::shared_pointer const & client, std::auto_ptr& responseHandler, osiSockAddr& address, - short transportRevision, int16 priority) { + int8 transportRevision, int16 priority) { SOCKET socket = INVALID_SOCKET; diff --git a/pvAccessApp/remote/blockingTCPTransport.cpp b/pvAccessApp/remote/blockingTCPTransport.cpp index 24ee6f3..bcb849c 100644 --- a/pvAccessApp/remote/blockingTCPTransport.cpp +++ b/pvAccessApp/remote/blockingTCPTransport.cpp @@ -107,7 +107,7 @@ namespace epics { _storedPayloadSize(0), _storedPosition(0), _storedLimit(0), - _magicAndVersion(0), + _version(0), _packetType(0), _command(0), _payloadSize(0), @@ -234,7 +234,8 @@ namespace epics { _sendPending = false; // prepare ACK marker - _sendBuffer->putShort(CA_MAGIC_AND_VERSION); + _sendBuffer->putByte(CA_MAGIC); + _sendBuffer->putByte(CA_VERSION); _sendBuffer->putByte(1); // control data _sendBuffer->putByte(1); // marker ACK _sendBuffer->putInt(0); @@ -327,7 +328,8 @@ namespace epics { _lastMessageStartPosition = -1; ensureBuffer(CA_MESSAGE_HEADER_SIZE+ensureCapacity); _lastMessageStartPosition = _sendBuffer->getPosition(); - _sendBuffer->putShort(CA_MAGIC_AND_VERSION); + _sendBuffer->putByte(CA_MAGIC); + _sendBuffer->putByte(CA_VERSION); _sendBuffer->putByte(_lastSegmentedMessageType); // data _sendBuffer->putByte(command); // command _sendBuffer->putInt(0); // temporary zero payload @@ -396,9 +398,10 @@ namespace epics { int position = _sendBuffer->getPosition(); int bytesLeft = _sendBuffer->getRemaining(); - if(position>=_nextMarkerPosition&&bytesLeft - >=CA_MESSAGE_HEADER_SIZE) { - _sendBuffer->putShort(CA_MAGIC_AND_VERSION); + if(position>=_nextMarkerPosition && + bytesLeft>=CA_MESSAGE_HEADER_SIZE) { + _sendBuffer->putByte(CA_MAGIC); + _sendBuffer->putByte(CA_VERSION); _sendBuffer->putByte(1); // control data _sendBuffer->putByte(0); // marker _sendBuffer->putInt((int)(_totalBytesSent+position @@ -549,9 +552,10 @@ namespace epics { // first byte is CA_MAGIC // second byte version - major/minor nibble - // check magic and version at once - _magicAndVersion = _socketBuffer->getShort(); - if((short)(_magicAndVersion&0xFFF0)!=CA_MAGIC_AND_MAJOR_VERSION) { + int8 magic = _socketBuffer->getByte(); + _version = _socketBuffer->getByte(); + if((magic != CA_MAGIC) || (((unsigned int8)_version) >> 4)!=CA_MAJOR_PROTOCOL_REVISION) + { // error... disconnect LOG( logLevelError, @@ -614,7 +618,7 @@ namespace epics { if(_stage==PROCESS_PAYLOAD) { // read header - int8 version = (int8)(_magicAndVersion&0xFF); + // last segment bit set (means in-between segment or last segment) bool notFirstSegment = (_packetType&0x20)!=0; @@ -631,7 +635,7 @@ namespace epics { // handle response Transport::shared_pointer thisPointer = shared_from_this(); _responseHandler->handleResponse(&_socketAddress, - thisPointer, version, _command, _payloadSize, + thisPointer, _version, _command, _payloadSize, _socketBuffer); } catch(...) { //noop // TODO print? diff --git a/pvAccessApp/remote/blockingUDP.h b/pvAccessApp/remote/blockingUDP.h index 50c38e5..eb19611 100644 --- a/pvAccessApp/remote/blockingUDP.h +++ b/pvAccessApp/remote/blockingUDP.h @@ -295,7 +295,7 @@ namespace epics { */ virtual Transport::shared_pointer connect(TransportClient::shared_pointer const & client, std::auto_ptr& responseHandler, osiSockAddr& bindAddress, - short transportRevision, int16 priority); + int8 transportRevision, int16 priority); private: diff --git a/pvAccessApp/remote/blockingUDPConnector.cpp b/pvAccessApp/remote/blockingUDPConnector.cpp index ce6b703..5787463 100644 --- a/pvAccessApp/remote/blockingUDPConnector.cpp +++ b/pvAccessApp/remote/blockingUDPConnector.cpp @@ -23,7 +23,7 @@ namespace epics { Transport::shared_pointer BlockingUDPConnector::connect(TransportClient::shared_pointer const & client, auto_ptr& responseHandler, osiSockAddr& bindAddress, - short transportRevision, int16 priority) { + int8 transportRevision, int16 priority) { LOG(logLevelDebug, "Creating datagram socket to: %s", inetAddressToString(bindAddress).c_str()); diff --git a/pvAccessApp/remote/blockingUDPTransport.cpp b/pvAccessApp/remote/blockingUDPTransport.cpp index 0943378..e18e2e4 100644 --- a/pvAccessApp/remote/blockingUDPTransport.cpp +++ b/pvAccessApp/remote/blockingUDPTransport.cpp @@ -144,7 +144,8 @@ namespace epics { void BlockingUDPTransport::startMessage(int8 command, int ensureCapacity) { _lastMessageStartPosition = _sendBuffer->getPosition(); - _sendBuffer->putShort(CA_MAGIC_AND_VERSION); + _sendBuffer->putByte(CA_MAGIC); + _sendBuffer->putByte(CA_VERSION); _sendBuffer->putByte(0); // data _sendBuffer->putByte(command); // command _sendBuffer->putInt(0); // temporary zero payload @@ -260,9 +261,10 @@ namespace epics { // first byte is CA_MAGIC // second byte version - major/minor nibble - // check magic and version at once - short magicAndVersion = receiveBuffer->getShort(); - if((short)(magicAndVersion&0xFFF0)!=CA_MAGIC_AND_MAJOR_VERSION) return false; + int8 magic = receiveBuffer->getByte(); + int8 version = receiveBuffer->getByte(); + if((magic != CA_MAGIC) || (((unsigned int8)version) >> 4)!=CA_MAJOR_PROTOCOL_REVISION) + return false; // only data for UDP receiveBuffer->getByte(); @@ -277,7 +279,7 @@ namespace epics { // handle _responseHandler->handleResponse(&fromAddress, thisTransport, - (int8)(magicAndVersion&0xFF), command, payloadSize, + version, command, payloadSize, _receiveBuffer); // set position (e.g. in case handler did not read all) diff --git a/pvAccessApp/remote/channelSearchManager.cpp b/pvAccessApp/remote/channelSearchManager.cpp index 15c70e9..7ac7c62 100644 --- a/pvAccessApp/remote/channelSearchManager.cpp +++ b/pvAccessApp/remote/channelSearchManager.cpp @@ -637,7 +637,8 @@ void ChannelSearchManager::initializeSendBuffer() // new buffer _sendBuffer->clear(); - _sendBuffer->putShort(CA_MAGIC_AND_VERSION); + _sendBuffer->putByte(CA_MAGIC); + _sendBuffer->putByte(CA_VERSION); _sendBuffer->putByte((int8)0); // data _sendBuffer->putByte((int8)3); // search _sendBuffer->putInt(sizeof(int32)/sizeof(int8) + 1); // "zero" payload diff --git a/pvAccessApp/remote/remote.h b/pvAccessApp/remote/remote.h index c7c6037..a1381d1 100644 --- a/pvAccessApp/remote/remote.h +++ b/pvAccessApp/remote/remote.h @@ -403,7 +403,7 @@ namespace epics { */ virtual Transport::shared_pointer connect(TransportClient::shared_pointer const & client, std::auto_ptr& responseHandler, osiSockAddr& address, - short transportRevision, int16 priority) = 0; + int8 transportRevision, int16 priority) = 0; }; diff --git a/pvAccessApp/remoteClient/clientContextImpl.cpp b/pvAccessApp/remoteClient/clientContextImpl.cpp index d78454c..4f799b1 100644 --- a/pvAccessApp/remoteClient/clientContextImpl.cpp +++ b/pvAccessApp/remoteClient/clientContextImpl.cpp @@ -4028,7 +4028,7 @@ TODO * @param priority process priority. * @return transport for given address */ - Transport::shared_pointer getTransport(TransportClient::shared_pointer const & client, osiSockAddr* serverAddress, int16 minorRevision, int16 priority) + Transport::shared_pointer getTransport(TransportClient::shared_pointer const & client, osiSockAddr* serverAddress, int8 minorRevision, int16 priority) { try { diff --git a/pvAccessApp/remoteClient/clientContextImpl.h b/pvAccessApp/remoteClient/clientContextImpl.h index 9cd99e8..237a045 100644 --- a/pvAccessApp/remoteClient/clientContextImpl.h +++ b/pvAccessApp/remoteClient/clientContextImpl.h @@ -72,7 +72,7 @@ namespace epics { virtual ResponseRequest::shared_pointer unregisterResponseRequest(pvAccessID ioid) = 0; - virtual Transport::shared_pointer getTransport(TransportClient::shared_pointer const & client, osiSockAddr* serverAddress, int16 minorRevision, int16 priority) = 0; + virtual Transport::shared_pointer getTransport(TransportClient::shared_pointer const & client, osiSockAddr* serverAddress, int8 minorRevision, int16 priority) = 0; virtual void beaconAnomalyNotify() = 0; diff --git a/testApp/remote/testBlockingTCPClnt.cpp b/testApp/remote/testBlockingTCPClnt.cpp index 8ce31b3..1d350bf 100644 --- a/testApp/remote/testBlockingTCPClnt.cpp +++ b/testApp/remote/testBlockingTCPClnt.cpp @@ -151,7 +151,7 @@ void testBlockingTCPSender() { try { Transport::shared_pointer transport(connector.connect(dtc, drh, srvAddr, - CA_MAGIC_AND_VERSION, CA_DEFAULT_PRIORITY)); + CA_VERSION, CA_DEFAULT_PRIORITY)); cout<<"Sending 10 messages..."<