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