Using osiSockAddress parameter by reference wherever possible.
This commit is contained in:
@@ -307,13 +307,15 @@
|
||||
<buildTargets>
|
||||
<target name="all" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>make</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>all</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
<runAllBuilders>false</runAllBuilders>
|
||||
</target>
|
||||
<target name="clean" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>make</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>clean</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
@@ -321,7 +323,6 @@
|
||||
</target>
|
||||
<target name="uninstall" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>make</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>uninstall</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
|
||||
@@ -117,7 +117,7 @@ namespace epics {
|
||||
_verified = true;
|
||||
}
|
||||
|
||||
virtual void setRecipient(const osiSockAddr* sendTo) {
|
||||
virtual void setRecipient(const osiSockAddr& sendTo) {
|
||||
// noop
|
||||
}
|
||||
|
||||
@@ -492,7 +492,7 @@ namespace epics {
|
||||
virtual ~BlockingTCPConnector();
|
||||
|
||||
virtual Transport* connect(TransportClient* client,
|
||||
ResponseHandler* responseHandler, osiSockAddr* address,
|
||||
ResponseHandler* responseHandler, osiSockAddr& address,
|
||||
short transportRevision, int16 priority);
|
||||
private:
|
||||
/**
|
||||
@@ -528,7 +528,7 @@ namespace epics {
|
||||
* @return the SOCKET
|
||||
* @throws IOException
|
||||
*/
|
||||
SOCKET tryConnect(osiSockAddr* address, int tries);
|
||||
SOCKET tryConnect(osiSockAddr& address, int tries);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -32,13 +32,13 @@ namespace epics {
|
||||
delete _namedLocker;
|
||||
}
|
||||
|
||||
SOCKET BlockingTCPConnector::tryConnect(osiSockAddr* address, int tries) {
|
||||
SOCKET BlockingTCPConnector::tryConnect(osiSockAddr& address, int tries) {
|
||||
for(int tryCount = 0; tryCount<tries; tryCount++) {
|
||||
// sleep for a while
|
||||
if(tryCount>0) epicsThreadSleep(0.1);
|
||||
|
||||
char strBuffer[64];
|
||||
ipAddrToA(&address->ia, strBuffer, sizeof(strBuffer));
|
||||
ipAddrToA(&address.ia, strBuffer, sizeof(strBuffer));
|
||||
|
||||
errlogSevPrintf(errlogInfo,
|
||||
"Opening socket to CA server %s, attempt %d.",
|
||||
@@ -53,7 +53,7 @@ namespace epics {
|
||||
strBuffer);
|
||||
}
|
||||
else {
|
||||
if(::connect(socket, &address->sa, sizeof(sockaddr))==0)
|
||||
if(::connect(socket, &address.sa, sizeof(sockaddr))==0)
|
||||
return socket;
|
||||
else {
|
||||
epicsSocketConvertErrnoToString(strBuffer,
|
||||
@@ -67,19 +67,19 @@ namespace epics {
|
||||
}
|
||||
|
||||
Transport* BlockingTCPConnector::connect(TransportClient* client,
|
||||
ResponseHandler* responseHandler, osiSockAddr* address,
|
||||
ResponseHandler* responseHandler, osiSockAddr& address,
|
||||
short transportRevision, int16 priority) {
|
||||
|
||||
SOCKET socket = INVALID_SOCKET;
|
||||
|
||||
char ipAddrStr[64];
|
||||
ipAddrToA(&address->ia, ipAddrStr, sizeof(ipAddrStr));
|
||||
ipAddrToA(&address.ia, ipAddrStr, sizeof(ipAddrStr));
|
||||
|
||||
// first try to check cache w/o named lock...
|
||||
BlockingClientTCPTransport
|
||||
* transport =
|
||||
(BlockingClientTCPTransport*)(_context->getTransportRegistry()->get(
|
||||
"TCP", address, priority));
|
||||
"TCP", &address, priority));
|
||||
if(transport!=NULL) {
|
||||
errlogSevPrintf(errlogInfo,
|
||||
"Reusing existing connection to CA server: %s",
|
||||
@@ -88,13 +88,13 @@ namespace epics {
|
||||
}
|
||||
|
||||
bool lockAcquired = _namedLocker->acquireSynchronizationObject(
|
||||
address, LOCK_TIMEOUT);
|
||||
&address, LOCK_TIMEOUT);
|
||||
if(lockAcquired) {
|
||||
try {
|
||||
// ... transport created during waiting in lock
|
||||
transport
|
||||
= (BlockingClientTCPTransport*)(_context->getTransportRegistry()->get(
|
||||
"TCP", address, priority));
|
||||
"TCP", &address, priority));
|
||||
if(transport!=NULL) {
|
||||
errlogSevPrintf(errlogInfo,
|
||||
"Reusing existing connection to CA server: %s",
|
||||
@@ -156,10 +156,10 @@ namespace epics {
|
||||
} catch(...) {
|
||||
// close socket, if open
|
||||
if(socket!=INVALID_SOCKET) epicsSocketDestroy(socket);
|
||||
_namedLocker->releaseSynchronizationObject(address);
|
||||
_namedLocker->releaseSynchronizationObject(&address);
|
||||
throw;
|
||||
}
|
||||
_namedLocker->releaseSynchronizationObject(address);
|
||||
_namedLocker->releaseSynchronizationObject(&address);
|
||||
}
|
||||
else {
|
||||
ostringstream temp;
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace epics {
|
||||
public TransportSendControl {
|
||||
public:
|
||||
BlockingUDPTransport(ResponseHandler* responseHandler,
|
||||
SOCKET channel, osiSockAddr* bindAddress,
|
||||
SOCKET channel, osiSockAddr& bindAddress,
|
||||
InetAddrVector* sendAddresses,
|
||||
short remoteTransportRevision);
|
||||
|
||||
@@ -107,8 +107,10 @@ namespace epics {
|
||||
// noop since all UDP requests are sent immediately
|
||||
}
|
||||
|
||||
virtual void setRecipient(const osiSockAddr* sendTo) {
|
||||
_sendTo = sendTo;
|
||||
virtual void setRecipient(const osiSockAddr& sendTo) {
|
||||
if(_sendTo!=NULL) delete _sendTo;
|
||||
_sendTo = new osiSockAddr;
|
||||
memcpy(_sendTo, &sendTo, sizeof(osiSockAddr));
|
||||
}
|
||||
|
||||
virtual void flushSerializeBuffer() {
|
||||
@@ -135,7 +137,9 @@ namespace epics {
|
||||
return _ignoredAddresses;
|
||||
}
|
||||
|
||||
bool send(ByteBuffer* buffer, const osiSockAddr* address = NULL);
|
||||
bool send(ByteBuffer* buffer, const osiSockAddr& address);
|
||||
|
||||
bool send(ByteBuffer* buffer);
|
||||
|
||||
/**
|
||||
* Get list of send addresses.
|
||||
@@ -149,7 +153,7 @@ namespace epics {
|
||||
* Get bind address.
|
||||
* @return bind address.
|
||||
*/
|
||||
osiSockAddr* getBindAddress() {
|
||||
const osiSockAddr* getBindAddress() const {
|
||||
return _bindAddress;
|
||||
}
|
||||
|
||||
@@ -177,7 +181,7 @@ namespace epics {
|
||||
private:
|
||||
static void threadRunner(void* param);
|
||||
|
||||
bool processBuffer(osiSockAddr* fromAddress,
|
||||
bool processBuffer(osiSockAddr& fromAddress,
|
||||
epics::pvData::ByteBuffer* receiveBuffer);
|
||||
|
||||
// Context only used for logging in this class
|
||||
@@ -207,7 +211,7 @@ namespace epics {
|
||||
*/
|
||||
InetAddrVector* _ignoredAddresses;
|
||||
|
||||
const osiSockAddr* _sendTo;
|
||||
osiSockAddr* _sendTo;
|
||||
|
||||
/**
|
||||
* Receive buffer.
|
||||
@@ -259,7 +263,7 @@ namespace epics {
|
||||
* NOTE: transport client is ignored for broadcast (UDP).
|
||||
*/
|
||||
virtual Transport* connect(TransportClient* client,
|
||||
ResponseHandler* responseHandler, osiSockAddr* bindAddress,
|
||||
ResponseHandler* responseHandler, osiSockAddr& bindAddress,
|
||||
short transportRevision, int16 priority);
|
||||
|
||||
private:
|
||||
|
||||
@@ -24,10 +24,10 @@ namespace epics {
|
||||
namespace pvAccess {
|
||||
|
||||
Transport* BlockingUDPConnector::connect(TransportClient* client,
|
||||
ResponseHandler* responseHandler, osiSockAddr* bindAddress,
|
||||
ResponseHandler* responseHandler, osiSockAddr& bindAddress,
|
||||
short transportRevision, int16 priority) {
|
||||
errlogSevPrintf(errlogInfo, "Creating datagram socket to: %s",
|
||||
inetAddressToString(bindAddress).c_str());
|
||||
inetAddressToString(&bindAddress).c_str());
|
||||
|
||||
SOCKET socket = epicsSocketCreate(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if(socket==INVALID_SOCKET) {
|
||||
@@ -45,7 +45,7 @@ namespace epics {
|
||||
* because of an early setsockopt call failing.
|
||||
*/
|
||||
|
||||
int retval = ::bind(socket, (sockaddr*)&(bindAddress->sa),
|
||||
int retval = ::bind(socket, (sockaddr*)&(bindAddress.sa),
|
||||
sizeof(sockaddr));
|
||||
if(retval<0) {
|
||||
errlogSevPrintf(errlogMajor, "Error binding socket: %s",
|
||||
|
||||
@@ -34,21 +34,28 @@ namespace epics {
|
||||
|
||||
BlockingUDPTransport::BlockingUDPTransport(
|
||||
ResponseHandler* responseHandler, SOCKET channel,
|
||||
osiSockAddr* bindAddress, InetAddrVector* sendAddresses,
|
||||
osiSockAddr& bindAddress, InetAddrVector* sendAddresses,
|
||||
short remoteTransportRevision) :
|
||||
_closed(false), _responseHandler(responseHandler),
|
||||
_channel(channel), _socketAddress(bindAddress),
|
||||
_bindAddress(bindAddress), _sendAddresses(sendAddresses),
|
||||
_channel(channel), _sendAddresses(sendAddresses),
|
||||
_ignoredAddresses(NULL), _sendTo(NULL), _receiveBuffer(
|
||||
new ByteBuffer(MAX_UDP_RECV)), _sendBuffer(
|
||||
new ByteBuffer(MAX_UDP_RECV)),
|
||||
_lastMessageStartPosition(0), _readBuffer(
|
||||
new char[MAX_UDP_RECV]), _mutex(new Mutex()),
|
||||
_threadId(NULL) {
|
||||
_socketAddress = new osiSockAddr;
|
||||
memcpy(_socketAddress, &bindAddress, sizeof(osiSockAddr));
|
||||
_bindAddress = _socketAddress;
|
||||
|
||||
}
|
||||
|
||||
BlockingUDPTransport::~BlockingUDPTransport() {
|
||||
close(true); // close the socket and stop the thread.
|
||||
if(_sendTo!=NULL) delete _sendTo;
|
||||
delete _socketAddress;
|
||||
// _bindAddress equals _socketAddress
|
||||
|
||||
delete _receiveBuffer;
|
||||
delete _sendBuffer;
|
||||
delete[] _readBuffer;
|
||||
@@ -89,7 +96,10 @@ namespace epics {
|
||||
sender->send(_sendBuffer, this);
|
||||
sender->unlock();
|
||||
endMessage();
|
||||
send(_sendBuffer, _sendTo);
|
||||
if(_sendTo==NULL)
|
||||
send(_sendBuffer);
|
||||
else
|
||||
send(_sendBuffer, *_sendTo);
|
||||
} catch(...) {
|
||||
sender->unlock();
|
||||
}
|
||||
@@ -172,7 +182,7 @@ namespace epics {
|
||||
|
||||
_receiveBuffer->flip();
|
||||
|
||||
processBuffer(&fromAddress, _receiveBuffer);
|
||||
processBuffer(fromAddress, _receiveBuffer);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -210,7 +220,7 @@ namespace epics {
|
||||
errlogSevPrintf(errlogInfo, "Thread '%s' exiting", threadName);
|
||||
}
|
||||
|
||||
bool BlockingUDPTransport::processBuffer(osiSockAddr* fromAddress,
|
||||
bool BlockingUDPTransport::processBuffer(osiSockAddr& fromAddress,
|
||||
ByteBuffer* receiveBuffer) {
|
||||
|
||||
// handle response(s)
|
||||
@@ -238,7 +248,7 @@ namespace epics {
|
||||
if(nextRequestPosition>receiveBuffer->getLimit()) return false;
|
||||
|
||||
// handle
|
||||
_responseHandler->handleResponse(fromAddress, this,
|
||||
_responseHandler->handleResponse(&fromAddress, this,
|
||||
(int8)(magicAndVersion&0xFF), command, payloadSize,
|
||||
_receiveBuffer);
|
||||
|
||||
@@ -251,32 +261,32 @@ namespace epics {
|
||||
}
|
||||
|
||||
bool BlockingUDPTransport::send(ByteBuffer* buffer,
|
||||
const osiSockAddr* address) {
|
||||
if(address==NULL&&_sendAddresses==NULL) return false;
|
||||
const osiSockAddr& address) {
|
||||
|
||||
if(address!=NULL) {
|
||||
buffer->flip();
|
||||
int retval =
|
||||
sendto(_channel, buffer->getArray(),
|
||||
buffer->getLimit(), 0, &(address->sa),
|
||||
sizeof(sockaddr));
|
||||
if(retval<0) {
|
||||
errlogSevPrintf(errlogMajor, "Socket sendto error: %s",
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
buffer->flip();
|
||||
int retval = sendto(_channel, buffer->getArray(),
|
||||
buffer->getLimit(), 0, &(address.sa), sizeof(sockaddr));
|
||||
if(retval<0) {
|
||||
errlogSevPrintf(errlogMajor, "Socket sendto error: %s",
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
for(size_t i = 0; i<_sendAddresses->size(); i++) {
|
||||
buffer->flip();
|
||||
int retval = sendto(_channel, buffer->getArray(),
|
||||
buffer->getLimit(), 0,
|
||||
&(_sendAddresses->at(i)->sa), sizeof(sockaddr));
|
||||
{
|
||||
if(retval<0) errlogSevPrintf(errlogMajor,
|
||||
"Socket sendto error: %s", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BlockingUDPTransport::send(ByteBuffer* buffer) {
|
||||
if(_sendAddresses==NULL) return false;
|
||||
|
||||
for(size_t i = 0; i<_sendAddresses->size(); i++) {
|
||||
buffer->flip();
|
||||
int retval = sendto(_channel, buffer->getArray(),
|
||||
buffer->getLimit(), 0, &(_sendAddresses->at(i)->sa),
|
||||
sizeof(sockaddr));
|
||||
{
|
||||
if(retval<0) errlogSevPrintf(errlogMajor,
|
||||
"Socket sendto error: %s", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -418,7 +418,7 @@ class MockTransportSendControl: public TransportSendControl
|
||||
public:
|
||||
void endMessage() {}
|
||||
void flush(bool lastMessageCompleted) {}
|
||||
void setRecipient(const osiSockAddr* sendTo) {}
|
||||
void setRecipient(const osiSockAddr& sendTo) {}
|
||||
void startMessage(int8 command, int32 ensureCapacity) {}
|
||||
void ensureBuffer(int32 size) {}
|
||||
void flushSerializeBuffer() {}
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace epics {
|
||||
|
||||
virtual void flush(bool lastMessageCompleted) =0;
|
||||
|
||||
virtual void setRecipient(const osiSockAddr* sendTo) =0;
|
||||
virtual void setRecipient(const osiSockAddr& sendTo) =0;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -326,7 +326,7 @@ namespace epics {
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
virtual Transport* connect(TransportClient* client,
|
||||
ResponseHandler* responseHandler, osiSockAddr* address,
|
||||
ResponseHandler* responseHandler, osiSockAddr& address,
|
||||
short transportRevision, int16 priority) =0;
|
||||
|
||||
};
|
||||
|
||||
@@ -147,7 +147,7 @@ namespace epics {
|
||||
|
||||
virtual void send(ByteBuffer* buffer, TransportSendControl* control) {
|
||||
control->startMessage(CMD_ECHO, 0);
|
||||
control->setRecipient(&_echoFrom);
|
||||
control->setRecipient(_echoFrom);
|
||||
}
|
||||
|
||||
virtual void lock() {
|
||||
|
||||
@@ -53,7 +53,7 @@ void testBeaconEmitter()
|
||||
bindAddr.ia.sin_family = AF_INET;
|
||||
bindAddr.ia.sin_port = htons(5066);
|
||||
bindAddr.ia.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
Transport* transport = connector.connect(NULL, &drh, &bindAddr, 1, 50);
|
||||
Transport* transport = connector.connect(NULL, &drh, bindAddr, 1, 50);
|
||||
|
||||
cout<<"Sending beacons"<<endl;
|
||||
BeaconEmitter beaconEmitter(transport, transport->getRemoteAddress());
|
||||
|
||||
@@ -112,7 +112,7 @@ void testBeaconHandler()
|
||||
bindAddr.ia.sin_family = AF_INET;
|
||||
bindAddr.ia.sin_port = htons(5067);
|
||||
bindAddr.ia.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
Transport* transport = connector.connect(NULL, &brh, &bindAddr, 1, 50);
|
||||
Transport* transport = connector.connect(NULL, &brh, bindAddr, 1, 50);
|
||||
(static_cast<BlockingUDPTransport*>(transport))->start();
|
||||
|
||||
while(1) sleep(1);
|
||||
|
||||
@@ -118,7 +118,7 @@ void testBlockingTCPSender() {
|
||||
return;
|
||||
}
|
||||
|
||||
Transport* transport = connector.connect(&dtc, &drh, &srvAddr,
|
||||
Transport* transport = connector.connect(&dtc, &drh, srvAddr,
|
||||
CA_MAGIC_AND_VERSION, CA_DEFAULT_PRIORITY);
|
||||
|
||||
cout<<"Sending 10 messages..."<<endl;
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
|
||||
#define SRV_IP "192.168.71.132"
|
||||
|
||||
using namespace epics::pvAccess;
|
||||
using namespace epics::pvData;
|
||||
|
||||
@@ -41,15 +43,13 @@ public:
|
||||
}
|
||||
|
||||
virtual void send(ByteBuffer* buffer, TransportSendControl* control) {
|
||||
// set recipient
|
||||
sendTo.ia.sin_family = AF_INET;
|
||||
sendTo.ia.sin_port = htons(65000);
|
||||
if(aToIPAddr("192.168.71.129", 65000, &sendTo.ia)<0) {
|
||||
// SRV_IP defined at the top of the this file
|
||||
if(aToIPAddr(SRV_IP, 65000, &sendTo.ia)<0) {
|
||||
cout<<"error in aToIPAddr(...)"<<endl;
|
||||
return;
|
||||
}
|
||||
|
||||
control->setRecipient(&sendTo);
|
||||
control->setRecipient(sendTo);
|
||||
|
||||
// send the packet
|
||||
count++;
|
||||
@@ -79,7 +79,7 @@ void testBlockingUDPSender() {
|
||||
bindAddr.ia.sin_port = htons(65001);
|
||||
bindAddr.ia.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
Transport* transport = connector.connect(NULL, &drh, &bindAddr, 1, 50);
|
||||
Transport* transport = connector.connect(NULL, &drh, bindAddr, 1, 50);
|
||||
|
||||
cout<<"Sending 10 packets..."<<endl;
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ void testBlockingUDPConnector() {
|
||||
bindAddr.ia.sin_port = htons(65000);
|
||||
bindAddr.ia.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
Transport* transport = connector.connect(NULL, &drh, &bindAddr, 1, 50);
|
||||
Transport* transport = connector.connect(NULL, &drh, bindAddr, 1, 50);
|
||||
|
||||
((BlockingUDPTransport*)transport)->start();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user