Example #1
0
bool TMsgSocket::ReliableSendBaseMsgReply(unsigned short tagNum, WONMsg::BaseMessage& msg, long timeout,
									 bool async, bool copyData, const CompletionContainer<const SendBaseMsgResult&>& completion)
{
	SendBaseMsgData* sendBaseMsgData = new SendBaseMsgData;
	if (!sendBaseMsgData)
	{
		completion.Complete(SendBaseMsgResult(this, &msg, false, false));
		return false;
	}

	sendBaseMsgData->msg = &msg;
	sendBaseMsgData->completion = completion;

	try {
		msg.Pack();
	}
	catch (...)
	{
		delete sendBaseMsgData;
		completion.Complete(SendBaseMsgResult(this, &msg, false, false));
		return false;
	}

	return ReliableSendRawMsgReplyEx(tagNum, msg.GetDataLen(), msg.GetDataPtr(), timeout, async, copyData, DoneSendBaseMsg, sendBaseMsgData);
}
Example #2
0
bool TMsgSocket::SendRawMsg(unsigned long length, const void* msg, long timeout, bool async, bool copyData, const CompletionContainer<const SendRawMsgResult&>& completion)
{
	SendMsgData* sendMsgData = new SendMsgData;
	if (!sendMsgData)
	{
		completion.Complete(SendRawMsgResult(this, (unsigned char*)msg, length, false, false));
		return false;
	}
	unsigned char* buffer = (unsigned char*)msg;
	unsigned long newLength = length;
	if (actualSocket->GetType() == stream)
	{
		buffer = new unsigned char[length + lengthSize];
		if (!buffer)
		{
			delete sendMsgData;
			completion.Complete(SendRawMsgResult(this, (unsigned char*)msg, length, false, false));
			return false;
		}
		unsigned long sendLength = length + lengthSize;	// Will only work on a little-endian(?) machine
		makeLittleEndian(sendLength);
		memcpy(buffer, &sendLength, lengthSize);
		memcpy(buffer+lengthSize, msg, length);
		newLength += lengthSize;
	}
	sendMsgData->socketClosed = false;
	sendMsgData->thisSocket = this;
	sendMsgData->success = false;
	sendMsgData->msg = msg;
	sendMsgData->autoDelete = async;
	sendMsgData->length = length;
	sendMsgData->completion = completion;
	
	void (*DoneSendMsgFunc)(const Socket::TransmitResult&, SendMsgData*) = DoneSendMsg;

	actualSocket->SendEx(newLength, buffer, timeout, async, async, DoneSendMsgFunc, sendMsgData);
	
	if (actualSocket->GetType() == stream)
		delete buffer;

	if (async)
		return false;
	
	bool result = sendMsgData->success;

	delete sendMsgData;

	return result;
}
Example #3
0
	void Done()
	{
		completion.Complete(TMsgSocket::RecvBaseMsgResult(thisSocket, msg, socketClosed));

		if (autoDelete)
			delete this;
	}
//////////////////////////////////////////////////////////////////////////////
// PublisherShutdown
//
// Method called to shutdown the Publisher client.  Sends message to Observation
// Server to remove this publisher from it's table of publishers.  All publications
// published by this publisher will also be removed.
//
// Returns success or failure on compleation.
//////////////////////////////////////////////////////////////////////////////
Error
ObservationPublisherClient::
PublisherShutdown( const CompletionContainer<const ServerStatus&>& theCompletion, 
				   const ServerStatus* theServerStatusP) // Completion for this operation - Function pointer will be called or event will be signaled when this operation completes
{
	Error aReturn = Error_Success;

	ClientShutdown();

	if (mClientId != 0) 
	{
		MMsgObsRemovePublisher aMsg;

		aMsg.SetPublisherId(mClientId);
		
		aMsg.Pack();

		aReturn = SendMMsgToServer(aMsg, new CompletionContainer<const ServerStatus&>(theCompletion), theServerStatusP);
		mClientId = 0;
		mSocketMgrP->SetClientId(mClientId);
		//delete mSocketMgrP;
		//mSocketMgrP = NULL;
	}
	else // Complete with error
	{
		aReturn = Error_GeneralFailure;
		theCompletion.Complete(StatusObs_UnknownPublisher);
	}

	return aReturn;
}
Example #5
0
	void Done()
	{
		completion.Complete(TMsgSocket::SendRawMsgResult(thisSocket, (unsigned char*)msg, length, success, socketClosed));
		
		if (autoDelete)
			delete this;
	}
void TCPAcceptCompletion::Complete(const WSSocket::AcceptResult& result)
{
	usersCompletion.Complete(result);
	if (!result.acceptedSocket)
		delete acceptSocket;
	Completion<const WSSocket::AcceptResult&>::Complete(result);
}
Example #7
0
	void Done()
	{
		completion.Complete(err);
		if (autoDel)
			delete this;
		else
			doneEvent.Set();
	}
Example #8
0
	void Done(const TMsgSocket::RecvBaseMsgResult& result)
	{
		completion.Complete(result);

		if (autoDel)
			delete this;
		else
			doneEvent.Set();
	}
Example #9
0
	void Done()
	{
		if (length)
			*length = msgSize;
		completion.Complete(success ? TMsgSocket::RecvRawMsgResult(thisSocket, result, msgSize, socketClosed) : TMsgSocket::RecvRawMsgResult(thisSocket, 0, 0, socketClosed));

		if (autoDelete)
			delete this;
	}
Example #10
0
	void Done(const RecvRawMsgResult& result)
	{
		completion.Complete(ReliableRecvRawMsgResult(*tagNum, result));

		if (autoDel)
			delete this;
		else
			doneEvent.Set();
	}
	void Done()
	{
		if (serverList)
			delete[] serverList;
		completion.Complete(PrizecentralDBResult(err));
		WONComplete(hCompletion, (void*)err);

		if (autoDel)
			delete this;
		else
			doneEvent.Set();
	}
Example #12
0
bool TMsgSocket::ReliableSendRawMsgReply(unsigned short tagNum, unsigned long length, const void* msg, long timeout,
									bool async, bool copyData, const CompletionContainer<const SendRawMsgResult&>& completion)
{
	unsigned char* newMsg = 0;
	ReliableSendRawMsgData* reliableSendRawMsgData;

	if (msg && length && tagNum)
	{
		reliableSendRawMsgData = new ReliableSendRawMsgData;
		if (reliableSendRawMsgData)
		{
			newMsg = new unsigned char[length + 3];
			if (!newMsg)
				delete reliableSendRawMsgData;
		}
	}

	if (!newMsg)
	{
		completion.Complete(SendRawMsgResult(this, (unsigned char*)msg, length, false, false));
		return false;
	}
	
	*(newMsg) = HeaderWithTag;	// 13
	*(unsigned short*)(newMsg+1) = getLittleEndian(tagNum);
	memcpy(newMsg+3, msg, length);

	AutoCrit autoCrit(tagInfoCrit);

	TagNumMap::iterator itor = tagNumMap.find(tagNum);
	if (itor != tagNumMap.end())
	{
		TagInfo* tagInfo = (*itor).second;
		delete tagInfo->msg;
		tagInfo->msg = newMsg;
		tagInfo->msgLength = length + 3;
		reliableSendRawMsgData->newMsg = 0;
	}
	else
	{
		reliableSendRawMsgData->newMsg = newMsg;
		// couldn't find tag in line.  Might have already expired.  Send it anyway
	}

	reliableSendRawMsgData->completion = completion;
	reliableSendRawMsgData->origMsg = (unsigned char*)msg;

	return SendRawMsgEx(length+3, newMsg, timeout, async, false, DoneReliableSendRawMsg, reliableSendRawMsgData);
}
Error WONAPI::PrizecentralRegisterUser(Identity* ident, const IPSocket::Address* contestServers, unsigned int numAddrs,
								 const SMsgDBRegisterUser& theRegisterUserMsg,
                                 long timeout, bool async, const CompletionContainer<const PrizecentralDBResult&>& completion )
{
	Error err = Error_InvalidParams;
	if (numAddrs)
	{
		err = Error_OutOfMemory;
		PrizecentralData* aPrizecentralData = new PrizecentralData;
		if (aPrizecentralData)
		{
			auto_ptr<PrizecentralData> autoDelPrizecentralData(aPrizecentralData);
			aPrizecentralData->serverList = new IPSocket::Address[numAddrs];
			if (aPrizecentralData->serverList)
			{
				for (int i = 0; i < numAddrs; i++)
					aPrizecentralData->serverList[i] = contestServers[i];
				aPrizecentralData->authSocket.SetIdentity(ident);
				aPrizecentralData->autoDel = async;
				aPrizecentralData->hCompletion = 0;
				aPrizecentralData->completion = completion;
				aPrizecentralData->err = Error_Timeout;
				aPrizecentralData->timeout = timeout;
				aPrizecentralData->curServer = 0;
				aPrizecentralData->numServers = numAddrs;
				aPrizecentralData->req = theRegisterUserMsg;

				autoDelPrizecentralData.release();
				doRegisterUser(aPrizecentralData);

				err = Error_Pending;
				if (!async)
				{
					WSSocket::PumpUntil(aPrizecentralData->doneEvent, timeout);
					//aPrizecentralData->doneEvent.WaitFor();
					err = aPrizecentralData->err;
					delete aPrizecentralData;
				}
				return err;
			}
		}
	}
	completion.Complete(PrizecentralDBResult(err));
	return err;
}
Example #14
0
Error WONAPI::CreateProfile(Identity* identity, const IPSocket::Address* profileServers, unsigned int numAddrs,
                    const char* emailAddress, long timeout, bool async, const CompletionContainer<Error>& completion) 
{
	ProfileData* profileData = new ProfileData;
	if (profileData)
	{
		TCPSocket* tcpSocket = new TCPSocket(profileServers[0]);
		if (tcpSocket)
		{
			profileData->profileSocket = new AuthSocket(identity, tcpSocket, true, 2, false, false);
			if (profileData->profileSocket)
			{
				profileData->emailAddress = emailAddress;
				profileData->error = Error_Timeout;
				profileData->timeout = timeout;
				profileData->autoDelete = async;
//				profileData->dirResultCompletion = completion;

				for (unsigned int i = 0; i < numAddrs; i++)
					profileData->profileServers.push_back(profileServers[i]);
				profileData->curServer = profileData->profileServers.begin();

				SendCreateProfileRequest(profileData, emailAddress);

				Error err = Error_Pending;

				if (!async)
				{
					WSSocket::PumpUntil(profileData->doneEvent, timeout);
					err = profileData->error;
					delete profileData;
				}
				return err;
			}
			delete tcpSocket;
		}
		delete profileData;
	}
	completion.Complete(Error_OutOfMemory);
	return Error_OutOfMemory;
}
Example #15
0
unsigned long TMsgSocket::RecvFrom(unsigned long count, void* buffer, Address* recvFromAddr, long timeout, bool async, const CompletionContainer<const TransmitResult&>& completion)
{
	completion.Complete(TransmitResult(this, buffer, count, 0, false));
	return 0;
}
Example #16
0
	void Done(Error err)
	{
		error = err;

		if (!serverModTime)		// if the server didn't give us a modified time
			serverModTime = localModTime;

		if (file)
		{
			fclose(file);
			file = 0;			// close file early, so it's closed before Complete() is called

			time_t fileTime = serverModTime;
			
#if defined(macintosh) && (macintosh == 1)
			fileTime -= 126230400;	// # of seconds Mac time_t differs in stat and utime
#endif

			struct utimbuf times = { fileTime, fileTime };
			utime(saveAsFile.c_str(), &times );
			
#if defined(macintosh) && (macintosh == 1)
			// Mac hack to fix created time 
			OSErr err;
			ParamBlockRec pb;
			Str255 s;
			
			unsigned long nameLen = saveAsFile.size();
			if (nameLen < 255)
			{
				s[0] = nameLen;
				strncpy((char*)s+1, saveAsFile.c_str(), s[0]);
				pb.fileParam.ioNamePtr = s;
				pb.fileParam.ioVRefNum = 0;
				pb.fileParam.ioFVersNum = 0;
				pb.fileParam.ioFDirIndex = 0;
				err = PBGetFInfoSync(&pb);
				if (err == noErr)
				{
					unsigned long cr = pb.fileParam.ioFlCrDat;
					unsigned long md = pb.fileParam.ioFlMdDat;
					if (cr > md)
					{
						pb.fileParam.ioFlCrDat = md;
						err = PBSetFInfoSync(&pb);
					}
				}
			}
#endif

			
		}

		if (modTime)
			*modTime = serverModTime;

		completion.Complete(err);

		if (autoDel)
			delete this;
		else
			doneEvent.Set();
	}
Example #17
0
unsigned long TMsgSocket::SendTo(unsigned long count, const void* buffer, const Address& sendToAddr, long timeout, bool async, bool copyData, const CompletionContainer<const TransmitResult&>& completion)
{
	completion.Complete(TransmitResult(this, (void*)buffer, count, 0, false));
	return 0;
}
Example #18
0
Error WONAPI::ReportEvents(Identity* ident, const IPSocket::Address* eventSrvrs, unsigned int numSrvrs,
						   const WONEvent* evts, unsigned int numEvts, bool reliable, bool useUDP,
						   long timeout, long perUDPtimeout, bool async,
						   const CompletionContainer<Error>& completion)
{
	if (!eventSrvrs || !numSrvrs || !evts || !numEvts)
	{
		completion.Complete(Error_InvalidParams);
		return Error_InvalidParams;
	}
	ReportEventsData* reportEventsData = new ReportEventsData;
	if (!reportEventsData)
	{
		completion.Complete(Error_OutOfMemory);
		return Error_OutOfMemory;
	}

	reportEventsData->authSocket = 0;
	reportEventsData->completion = completion;
	reportEventsData->autoDel = async;
	reportEventsData->err = Error_Success;
	reportEventsData->useUDP = useUDP;
	reportEventsData->reliable = reliable;
	reportEventsData->numEvts = numEvts;
	reportEventsData->evtsLeft = numEvts;
	reportEventsData->timeout = timeout;
	reportEventsData->perUDPtimeout = perUDPtimeout;

	for (int curEvt = 0; curEvt < numEvts; curEvt++)
		reportEventsData->evts.push_back(evts[curEvt]);

	for (int i = 0; i < numSrvrs; i++)
		reportEventsData->addrs.push_back(eventSrvrs[i]);
	reportEventsData->addrItor = reportEventsData->addrs.begin();
	
	AuthSocket* authSocket;
	if (useUDP)
	{
		UDPSocket* udpSocket = new UDPSocket(*eventSrvrs);
		if (!udpSocket)
		{
			delete reportEventsData;
			completion.Complete(Error_OutOfMemory);
			return Error_OutOfMemory;
		}
		authSocket = new AuthSocket(ident, udpSocket, true, *eventSrvrs, 4, true, false, false);
		if (!authSocket)
		{
			delete udpSocket;
			delete reportEventsData;
			completion.Complete(Error_OutOfMemory);
			return Error_OutOfMemory;
		}
		reportEventsData->authSocket = authSocket;
	}
	else
	{
		TCPSocket* tcpSocket = new TCPSocket(*eventSrvrs);
		if (!tcpSocket)
		{
			delete reportEventsData;
			completion.Complete(Error_OutOfMemory);
			return Error_OutOfMemory;
		}
		authSocket = new AuthSocket(ident, tcpSocket, true, 4, true, false, false);
		if (!authSocket)
		{
			delete tcpSocket;
			delete reportEventsData;
			completion.Complete(Error_OutOfMemory);
			return Error_OutOfMemory;
		}
		reportEventsData->authSocket = authSocket;
	}
	authSocket->OpenEx(true, timeout, true, ReportEventOpenDone, reportEventsData);

	if (async)
		return Error_Pending;

	WSSocket::PumpUntil(reportEventsData->doneEvent, timeout);
	Error err = reportEventsData->err;
	delete reportEventsData;
	return err;
}
Example #19
0
Error HTTPGetLL(const IPSocket::Address& proxyAddr,
				const std::string& hostName, unsigned short httpPort,
				const std::string& getPath,
				const std::string& saveAsFile, bool allowResume, void* recvBuf, unsigned long* recvBufSize, bool useFile,
				bool* isNew, time_t* modTime, ProgressCallback callback, void* callbackPrivData,
				long timeout, bool async, const CompletionContainer<Error>& completion)
{
	if (getPath.empty() || 
		(useFile	?	(saveAsFile.empty())
					:	(!recvBuf || !recvBufSize || !*recvBufSize)))
	{
		completion.Complete(Error_InvalidParams);
		return Error_InvalidParams;
	}
	
	struct stat fileInfo;
	unsigned long existingSize = 0;
	bool doResume = false;
	time_t localModTime = 0;
	if (modTime)
		localModTime = *modTime;

	if (useFile && allowResume)
	{
		if (!stat(saveAsFile.c_str(), &fileInfo))
		{
		
#if defined(macintosh) && (macintosh == 1)
			fileInfo.st_mtime += 126230400;	// # of seconds Mac time_t differs in stat and utime
#endif

			if (!localModTime)
				localModTime = fileInfo.st_mtime;
			existingSize = fileInfo.st_size;
			if (existingSize)
				doResume = true;
		}
	}

	HTTPGetData* getData = new HTTPGetData;
	if (!getData)
	{
		completion.Complete(Error_OutOfMemory);
		return Error_OutOfMemory;
	}
	getData->existingSize = existingSize;
	getData->callback = callback;
	getData->callbackPrivData = callbackPrivData;
	getData->doResume = doResume;
	getData->modTime = modTime;
	getData->localModTime = localModTime;
	getData->serverModTime = 0;
	getData->getPath = getPath;
	getData->saveAsFile = saveAsFile;
	getData->recvBuf = recvBuf;
	getData->recvBufSize = recvBufSize;
	if (recvBufSize)
	{
		getData->maxRecvBufSize = *recvBufSize;
		*recvBufSize = 0;
	}
	else
		getData->maxRecvBufSize = 0;
	getData->useFile = useFile;
	getData->timeout = timeout;
	getData->autoDel = async;
	getData->proxyAddr = proxyAddr;
	getData->hostName = hostName;
	getData->httpPort = httpPort;
	getData->recvSize = 0;
	getData->isNew = isNew;
	if (isNew)
		*isNew = false;
	getData->completion = completion;
	
	getData->tcpSocket.OpenEx(proxyAddr, timeout, true, DoneHTTPOpen, getData);

	if (async)
		return Error_Pending;

	WSSocket::PumpUntil(getData->doneEvent, timeout);
	Error result = getData->error;
	delete getData;
	return result;
}