1 #ifndef _XETCPIPLINK_HPP 2 #define _XETCPIPLINK_HPP 3 /*------------------------------------------------------------------------- 4 * drawElements Quality Program Test Executor 5 * ------------------------------------------ 6 * 7 * Copyright 2014 The Android Open Source Project 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 *//*! 22 * \file 23 * \brief Tcp/Ip communication link. 24 *//*--------------------------------------------------------------------*/ 25 26 #include "xeDefs.hpp" 27 #include "xeCommLink.hpp" 28 #include "deSocket.hpp" 29 #include "deRingBuffer.hpp" 30 #include "deBlockBuffer.hpp" 31 #include "xsProtocol.hpp" 32 #include "deThread.hpp" 33 #include "deTimer.h" 34 35 #include <vector> 36 37 namespace xe 38 { 39 40 class TcpIpLinkState 41 { 42 public: 43 TcpIpLinkState(CommLinkState initialState, const char *initialErr); 44 ~TcpIpLinkState(void); 45 46 CommLinkState getState(void) const; 47 CommLinkState getState(std::string &error) const; 48 49 void setCallbacks(CommLink::StateChangedFunc stateChangedCallback, CommLink::LogDataFunc testLogDataCallback, 50 CommLink::LogDataFunc infoLogDataCallback, void *userPtr); 51 52 void setState(CommLinkState state, const char *error = ""); 53 void onTestLogData(const uint8_t *bytes, size_t numBytes) const; 54 void onInfoLogData(const uint8_t *bytes, size_t numBytes) const; 55 56 void onKeepaliveReceived(void); 57 uint64_t getLastKeepaliveRecevied(void) const; 58 59 private: 60 mutable de::Mutex m_lock; 61 volatile CommLinkState m_state; 62 std::string m_error; 63 64 volatile uint64_t m_lastKeepaliveReceived; 65 66 volatile CommLink::StateChangedFunc m_stateChangedCallback; 67 volatile CommLink::LogDataFunc m_testLogDataCallback; 68 volatile CommLink::LogDataFunc m_infoLogDataCallback; 69 void *volatile m_userPtr; 70 }; 71 72 class TcpIpSendThread : public de::Thread 73 { 74 public: 75 TcpIpSendThread(de::Socket &socket, TcpIpLinkState &state); 76 ~TcpIpSendThread(void); 77 78 void start(void); 79 void run(void); 80 void stop(void); 81 isRunning(void) const82 bool isRunning(void) const 83 { 84 return m_isRunning; 85 } 86 getBuffer(void)87 de::BlockBuffer<uint8_t> &getBuffer(void) 88 { 89 return m_buffer; 90 } 91 92 private: 93 de::Socket &m_socket; 94 TcpIpLinkState &m_state; 95 96 de::BlockBuffer<uint8_t> m_buffer; 97 98 bool m_isRunning; 99 }; 100 101 class TcpIpRecvThread : public de::Thread 102 { 103 public: 104 TcpIpRecvThread(de::Socket &socket, TcpIpLinkState &state); 105 ~TcpIpRecvThread(void); 106 107 void start(void); 108 void run(void); 109 void stop(void); 110 isRunning(void) const111 bool isRunning(void) const 112 { 113 return m_isRunning; 114 } 115 116 private: 117 void handleMessage(xs::MessageType messageType, const uint8_t *data, size_t dataSize); 118 119 de::Socket &m_socket; 120 TcpIpLinkState &m_state; 121 122 std::vector<uint8_t> m_curMsgBuf; 123 size_t m_curMsgPos; 124 125 bool m_isRunning; 126 }; 127 128 class TcpIpLink : public CommLink 129 { 130 public: 131 TcpIpLink(void); 132 ~TcpIpLink(void); 133 134 // TcpIpLink -specific API 135 void connect(const de::SocketAddress &address); 136 void disconnect(void); 137 138 // CommLink API 139 void reset(void); 140 141 CommLinkState getState(void) const; 142 CommLinkState getState(std::string &error) const; 143 144 void setCallbacks(StateChangedFunc stateChangedCallback, LogDataFunc testLogDataCallback, 145 LogDataFunc infoLogDataCallback, void *userPtr); 146 147 void startTestProcess(const char *name, const char *params, const char *workingDir, const char *caseList); 148 void stopTestProcess(void); 149 150 private: 151 void closeConnection(void); 152 153 static void keepaliveTimerCallback(void *ptr); 154 155 de::Socket m_socket; 156 TcpIpLinkState m_state; 157 158 TcpIpSendThread m_sendThread; 159 TcpIpRecvThread m_recvThread; 160 161 deTimer *m_keepaliveTimer; 162 }; 163 164 } // namespace xe 165 166 #endif // _XETCPIPLINK_HPP 167