xref: /aosp_15_r20/external/deqp/external/vulkancts/vkscserver/vksClient.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1*35238bceSAndroid Build Coastguard Worker #ifndef _VKSCLIENT_HPP
2*35238bceSAndroid Build Coastguard Worker #define _VKSCLIENT_HPP
3*35238bceSAndroid Build Coastguard Worker 
4*35238bceSAndroid Build Coastguard Worker /*-------------------------------------------------------------------------
5*35238bceSAndroid Build Coastguard Worker  * Vulkan CTS Framework
6*35238bceSAndroid Build Coastguard Worker  * --------------------
7*35238bceSAndroid Build Coastguard Worker  *
8*35238bceSAndroid Build Coastguard Worker  * Copyright (c) 2021 The Khronos Group Inc.
9*35238bceSAndroid Build Coastguard Worker  *
10*35238bceSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
11*35238bceSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
12*35238bceSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
13*35238bceSAndroid Build Coastguard Worker  *
14*35238bceSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
15*35238bceSAndroid Build Coastguard Worker  *
16*35238bceSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
17*35238bceSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
18*35238bceSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19*35238bceSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
20*35238bceSAndroid Build Coastguard Worker  * limitations under the License.
21*35238bceSAndroid Build Coastguard Worker  *
22*35238bceSAndroid Build Coastguard Worker  *-------------------------------------------------------------------------*/
23*35238bceSAndroid Build Coastguard Worker 
24*35238bceSAndroid Build Coastguard Worker #include "vksNetwork.hpp"
25*35238bceSAndroid Build Coastguard Worker #include "vksProtocol.hpp"
26*35238bceSAndroid Build Coastguard Worker 
27*35238bceSAndroid Build Coastguard Worker #include <mutex>
28*35238bceSAndroid Build Coastguard Worker 
29*35238bceSAndroid Build Coastguard Worker #include "deSocket.hpp"
30*35238bceSAndroid Build Coastguard Worker 
31*35238bceSAndroid Build Coastguard Worker namespace vksc_server
32*35238bceSAndroid Build Coastguard Worker {
33*35238bceSAndroid Build Coastguard Worker 
34*35238bceSAndroid Build Coastguard Worker class Server
35*35238bceSAndroid Build Coastguard Worker {
36*35238bceSAndroid Build Coastguard Worker     de::SocketAddress addr;
37*35238bceSAndroid Build Coastguard Worker     de::Socket socket;
38*35238bceSAndroid Build Coastguard Worker     vector<u8> recvb;
39*35238bceSAndroid Build Coastguard Worker     std::mutex mutex;
40*35238bceSAndroid Build Coastguard Worker 
41*35238bceSAndroid Build Coastguard Worker public:
Server(const string & address)42*35238bceSAndroid Build Coastguard Worker     Server(const string &address)
43*35238bceSAndroid Build Coastguard Worker     {
44*35238bceSAndroid Build Coastguard Worker         string host;
45*35238bceSAndroid Build Coastguard Worker         int port;
46*35238bceSAndroid Build Coastguard Worker         StringToAddress(address, host, port);
47*35238bceSAndroid Build Coastguard Worker         addr.setHost(host.c_str());
48*35238bceSAndroid Build Coastguard Worker         addr.setPort(port);
49*35238bceSAndroid Build Coastguard Worker         socket.connect(addr);
50*35238bceSAndroid Build Coastguard Worker     }
51*35238bceSAndroid Build Coastguard Worker 
52*35238bceSAndroid Build Coastguard Worker     template <typename REQUEST, typename RESPONSE>
SendRequest(REQUEST & request,RESPONSE & response)53*35238bceSAndroid Build Coastguard Worker     void SendRequest(REQUEST &request, RESPONSE &response)
54*35238bceSAndroid Build Coastguard Worker     {
55*35238bceSAndroid Build Coastguard Worker         std::lock_guard<std::mutex> lock(mutex);
56*35238bceSAndroid Build Coastguard Worker         SendPayloadWithHeader(&socket, REQUEST::Type(), Serialize(request));
57*35238bceSAndroid Build Coastguard Worker 
58*35238bceSAndroid Build Coastguard Worker         vector<u8> packet = RecvPacket(&socket, recvb, RESPONSE::Type());
59*35238bceSAndroid Build Coastguard Worker         response          = Deserialize<RESPONSE>(packet);
60*35238bceSAndroid Build Coastguard Worker     }
61*35238bceSAndroid Build Coastguard Worker 
62*35238bceSAndroid Build Coastguard Worker     template <typename REQUEST>
SendRequest(REQUEST & request)63*35238bceSAndroid Build Coastguard Worker     void SendRequest(REQUEST &request)
64*35238bceSAndroid Build Coastguard Worker     {
65*35238bceSAndroid Build Coastguard Worker         std::lock_guard<std::mutex> lock(mutex);
66*35238bceSAndroid Build Coastguard Worker         SendPayloadWithHeader(&socket, REQUEST::Type(), Serialize(request));
67*35238bceSAndroid Build Coastguard Worker     }
68*35238bceSAndroid Build Coastguard Worker };
69*35238bceSAndroid Build Coastguard Worker 
StandardOutputServerSingleton()70*35238bceSAndroid Build Coastguard Worker inline std::unique_ptr<Server> &StandardOutputServerSingleton()
71*35238bceSAndroid Build Coastguard Worker {
72*35238bceSAndroid Build Coastguard Worker     static std::unique_ptr<Server> server;
73*35238bceSAndroid Build Coastguard Worker     return server;
74*35238bceSAndroid Build Coastguard Worker }
75*35238bceSAndroid Build Coastguard Worker 
OpenRemoteStandardOutput(const string & address)76*35238bceSAndroid Build Coastguard Worker inline void OpenRemoteStandardOutput(const string &address)
77*35238bceSAndroid Build Coastguard Worker {
78*35238bceSAndroid Build Coastguard Worker     StandardOutputServerSingleton() = std::unique_ptr<Server>(new Server(address));
79*35238bceSAndroid Build Coastguard Worker }
80*35238bceSAndroid Build Coastguard Worker 
RemoteWrite(int type,const char * message)81*35238bceSAndroid Build Coastguard Worker inline bool RemoteWrite(int type, const char *message)
82*35238bceSAndroid Build Coastguard Worker {
83*35238bceSAndroid Build Coastguard Worker     auto &&ss = StandardOutputServerSingleton();
84*35238bceSAndroid Build Coastguard Worker     if (ss)
85*35238bceSAndroid Build Coastguard Worker     {
86*35238bceSAndroid Build Coastguard Worker         LogRequest request;
87*35238bceSAndroid Build Coastguard Worker         request.type    = type;
88*35238bceSAndroid Build Coastguard Worker         request.message = message;
89*35238bceSAndroid Build Coastguard Worker         ss->SendRequest(request);
90*35238bceSAndroid Build Coastguard Worker         return false;
91*35238bceSAndroid Build Coastguard Worker     }
92*35238bceSAndroid Build Coastguard Worker     return true;
93*35238bceSAndroid Build Coastguard Worker }
94*35238bceSAndroid Build Coastguard Worker 
RemoteWriteFtm(int type,const char * format,va_list args)95*35238bceSAndroid Build Coastguard Worker inline bool RemoteWriteFtm(int type, const char *format, va_list args)
96*35238bceSAndroid Build Coastguard Worker {
97*35238bceSAndroid Build Coastguard Worker     auto &&ss = StandardOutputServerSingleton();
98*35238bceSAndroid Build Coastguard Worker     if (ss)
99*35238bceSAndroid Build Coastguard Worker     {
100*35238bceSAndroid Build Coastguard Worker         char message[4086];
101*35238bceSAndroid Build Coastguard Worker         vsprintf(message, format, args);
102*35238bceSAndroid Build Coastguard Worker         LogRequest request;
103*35238bceSAndroid Build Coastguard Worker         request.type    = type;
104*35238bceSAndroid Build Coastguard Worker         request.message = message;
105*35238bceSAndroid Build Coastguard Worker         ss->SendRequest(request);
106*35238bceSAndroid Build Coastguard Worker         return false;
107*35238bceSAndroid Build Coastguard Worker     }
108*35238bceSAndroid Build Coastguard Worker     return true;
109*35238bceSAndroid Build Coastguard Worker }
110*35238bceSAndroid Build Coastguard Worker 
111*35238bceSAndroid Build Coastguard Worker } // namespace vksc_server
112*35238bceSAndroid Build Coastguard Worker 
113*35238bceSAndroid Build Coastguard Worker #endif // _VKSCLIENT_HPP
114