123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- /*
- * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Intel Corporation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- #include <SocketTransporter.h>
- #include <IAERequest.h>
- #include <IAEResponse.h>
- #include <ISerializer.h>
- #include <ICommunicationSocket.h>
- #include <IAEMessage.h>
- #include <string.h>
- #include <stdlib.h>
- SocketTransporter::SocketTransporter(ISocketFactory* socketFactory, ISerializer* serializer)
- :mSocketFactory(socketFactory), mSerializer(serializer)
- {
- }
- SocketTransporter::~SocketTransporter()
- {
- if (mSocketFactory != NULL)
- {
- delete mSocketFactory;
- mSocketFactory = NULL;
- }
- if (mSerializer != NULL)
- {
- delete mSerializer;
- mSerializer = NULL;
- }
- }
- AEMessage* SocketTransporter::receiveMessage(ICommunicationSocket* sock) {
- AEMessage * message = new AEMessage();
- char* msgSize = NULL;
- msgSize = sock->readRaw(sizeof(AEMessage::size));
- if (msgSize != NULL)
- {
- memcpy((char*)&message->size, msgSize, sizeof(message->size));
- message->data = sock->readRaw(message->size);
- delete [] msgSize;
- }
- return message;
- }
- uae_oal_status_t SocketTransporter::sendMessage(AEMessage *message, ICommunicationSocket* sock) {
- if ((sock->writeRaw((char*)&message->size, sizeof(message->size))) == -1)
- return UAE_OAL_ERROR_UNEXPECTED;
- if ((sock->writeRaw(message->data, message->size)) == -1)
- return UAE_OAL_ERROR_UNEXPECTED;
- return UAE_OAL_SUCCESS;
- }
- uae_oal_status_t SocketTransporter::transact(IAERequest* request, IAEResponse* response, uint32_t timeout)
- {
- if (request == NULL || response == NULL)
- return UAE_OAL_ERROR_INVALID;
- ICommunicationSocket* communicationSocket = mSocketFactory->NewCommunicationSocket();
- if (communicationSocket == NULL)
- return UAE_OAL_ERROR_AESM_UNAVAILABLE;
- uae_oal_status_t ret = UAE_OAL_ERROR_UNEXPECTED;
- //set the timeout value
- if (timeout > 0)
- communicationSocket->setTimeout(timeout);
- AEMessage * reqMsg = request->serialize();
-
- if (reqMsg != NULL)
- {
- //send the request
- ret = sendMessage(reqMsg, communicationSocket);
- if (ret == UAE_OAL_SUCCESS)
- {
- //read the response
- AEMessage * resMsg = receiveMessage(communicationSocket);
- if (communicationSocket->wasTimeoutDetected() == false)
- {
- if (resMsg != NULL && resMsg->data != NULL)
- response->inflateWithMessage(resMsg);
- else
- ret = UAE_OAL_ERROR_UNEXPECTED;
- }
- delete resMsg;
- }
- }
- if (communicationSocket->wasTimeoutDetected() == true)
- ret = UAE_OAL_ERROR_TIMEOUT;
- //free stuff
- delete reqMsg;
- delete communicationSocket;
- return ret;
- }
- IAERequest* SocketTransporter::receiveRequest(ICommunicationSocket* sock) {
- AEMessage * msg = receiveMessage(sock);
- IAERequest* request = mSerializer->inflateRequest(msg);
- delete msg;
- return request;
- }
- void SocketTransporter::sendResponse(IAEResponse* response, ICommunicationSocket* sock) {
- if (response == NULL)
- return;
- AEMessage * message = response->serialize();
- if (sendMessage(message, sock) != UAE_OAL_SUCCESS)
- {
- //server can't do anything, ignore status
- }
- delete message;
- return;
- }
|