SocketTransporter.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <SocketTransporter.h>
  32. #include <IAERequest.h>
  33. #include <IAEResponse.h>
  34. #include <ISerializer.h>
  35. #include <ICommunicationSocket.h>
  36. #include <IAEMessage.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. SocketTransporter::SocketTransporter(ISocketFactory* socketFactory, ISerializer* serializer)
  40. :mSocketFactory(socketFactory), mSerializer(serializer)
  41. {
  42. }
  43. SocketTransporter::~SocketTransporter()
  44. {
  45. if (mSocketFactory != NULL)
  46. {
  47. delete mSocketFactory;
  48. mSocketFactory = NULL;
  49. }
  50. if (mSerializer != NULL)
  51. {
  52. delete mSerializer;
  53. mSerializer = NULL;
  54. }
  55. }
  56. AEMessage* SocketTransporter::receiveMessage(ICommunicationSocket* sock) {
  57. AEMessage * message = new AEMessage();
  58. char* msgSize = NULL;
  59. msgSize = sock->readRaw(sizeof(AEMessage::size));
  60. if (msgSize != NULL)
  61. {
  62. memcpy((char*)&message->size, msgSize, sizeof(message->size));
  63. message->data = sock->readRaw(message->size);
  64. delete [] msgSize;
  65. }
  66. return message;
  67. }
  68. uae_oal_status_t SocketTransporter::sendMessage(AEMessage *message, ICommunicationSocket* sock) {
  69. if ((sock->writeRaw((char*)&message->size, sizeof(message->size))) == -1)
  70. return UAE_OAL_ERROR_UNEXPECTED;
  71. if ((sock->writeRaw(message->data, message->size)) == -1)
  72. return UAE_OAL_ERROR_UNEXPECTED;
  73. return UAE_OAL_SUCCESS;
  74. }
  75. uae_oal_status_t SocketTransporter::transact(IAERequest* request, IAEResponse* response, uint32_t timeout)
  76. {
  77. if (request == NULL || response == NULL)
  78. return UAE_OAL_ERROR_INVALID;
  79. ICommunicationSocket* communicationSocket = mSocketFactory->NewCommunicationSocket();
  80. if (communicationSocket == NULL)
  81. return UAE_OAL_ERROR_AESM_UNAVAILABLE;
  82. uae_oal_status_t ret = UAE_OAL_ERROR_UNEXPECTED;
  83. //set the timeout value
  84. if (timeout > 0)
  85. communicationSocket->setTimeout(timeout);
  86. AEMessage * reqMsg = request->serialize();
  87. if (reqMsg != NULL)
  88. {
  89. //send the request
  90. ret = sendMessage(reqMsg, communicationSocket);
  91. if (ret == UAE_OAL_SUCCESS)
  92. {
  93. //read the response
  94. AEMessage * resMsg = receiveMessage(communicationSocket);
  95. if (communicationSocket->wasTimeoutDetected() == false)
  96. {
  97. if (resMsg != NULL && resMsg->data != NULL)
  98. response->inflateWithMessage(resMsg);
  99. else
  100. ret = UAE_OAL_ERROR_UNEXPECTED;
  101. }
  102. delete resMsg;
  103. }
  104. }
  105. if (communicationSocket->wasTimeoutDetected() == true)
  106. ret = UAE_OAL_ERROR_TIMEOUT;
  107. //free stuff
  108. delete reqMsg;
  109. delete communicationSocket;
  110. return ret;
  111. }
  112. IAERequest* SocketTransporter::receiveRequest(ICommunicationSocket* sock) {
  113. AEMessage * msg = receiveMessage(sock);
  114. IAERequest* request = mSerializer->inflateRequest(msg);
  115. delete msg;
  116. return request;
  117. }
  118. uae_oal_status_t SocketTransporter::sendResponse(IAEResponse* response, ICommunicationSocket* sock) {
  119. AEMessage * message = response->serialize();
  120. uae_oal_status_t retVal = sendMessage(message, sock);
  121. delete message;
  122. return retVal;
  123. }