1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "LocalSocket.h"
17
18 #include "PreviewerEngineLog.h"
19
20 using namespace std;
LocalSocket()21 LocalSocket::LocalSocket() : pipeHandle(nullptr) {}
22
~LocalSocket()23 LocalSocket::~LocalSocket() {}
24
ConnectToServer(string name,LocalSocket::OpenMode openMode,TransMode transMode)25 bool LocalSocket::ConnectToServer(string name, LocalSocket::OpenMode openMode, TransMode transMode)
26 {
27 wstring tempName = wstring(name.begin(), name.end());
28
29 DWORD openModeWin = GetWinOpenMode(openMode);
30 pipeHandle = CreateFileW(tempName.c_str(), openModeWin, 0, nullptr, OPEN_EXISTING, 0, NULL);
31 if (pipeHandle == INVALID_HANDLE_VALUE) {
32 ELOG("LocalSocket::ConnectToServer CreateFileW failed: %d", GetLastError());
33 return false;
34 }
35
36 DWORD tranMode = GetWinTransMode(transMode);
37 if (!SetNamedPipeHandleState(pipeHandle, &tranMode, nullptr, nullptr)) {
38 ELOG("LocalSocket::ConnectToServer SetNamedPipeHandleState failed: %d", GetLastError());
39 return false;
40 }
41
42 return true;
43 }
44
GetTracePipeName(string baseName) const45 string LocalSocket::GetTracePipeName(string baseName) const
46 {
47 return string("\\\\.\\pipe\\") + baseName;
48 }
49
GetCommandPipeName(string baseName) const50 string LocalSocket::GetCommandPipeName(string baseName) const
51 {
52 return string("\\\\.\\pipe\\") + baseName + "_commandPipe";
53 }
54
GetImagePipeName(string baseName) const55 string LocalSocket::GetImagePipeName(string baseName) const
56 {
57 return string("\\\\.\\pipe\\") + baseName + "_imagePipe";
58 }
59
DisconnectFromServer()60 void LocalSocket::DisconnectFromServer()
61 {
62 CloseHandle(pipeHandle);
63 }
64
ReadData(char * data,size_t length) const65 int64_t LocalSocket::ReadData(char* data, size_t length) const
66 {
67 if (length > UINT32_MAX) {
68 ELOG("LocalSocket::ReadData length must < %d", UINT32_MAX);
69 return -1;
70 }
71
72 DWORD readSize = 0;
73 if (!PeekNamedPipe(pipeHandle, nullptr, 0, nullptr, &readSize, nullptr)) {
74 return 0;
75 }
76
77 if (readSize == 0) {
78 return 0;
79 }
80
81 if (!ReadFile(pipeHandle, data, static_cast<DWORD>(length), &readSize, NULL)) {
82 DWORD error = GetLastError();
83 ELOG("LocalSocket::ReadData ReadFile failed: %d", error);
84 return 0 - static_cast<int64_t>(error);
85 }
86 return readSize;
87 }
88
WriteData(const void * data,size_t length) const89 size_t LocalSocket::WriteData(const void* data, size_t length) const
90 {
91 if (length > UINT32_MAX) {
92 ELOG("LocalSocket::WriteData length must < %d", UINT32_MAX);
93 return 0;
94 }
95
96 DWORD writeSize = 0;
97 if (!WriteFile(pipeHandle, data, static_cast<DWORD>(length), &writeSize, nullptr)) {
98 DWORD error = GetLastError();
99 ELOG("LocalSocket::WriteData WriteFile failed: %d", error);
100 return 0 - static_cast<size_t>(error);
101 }
102 return writeSize;
103 }
104
operator <<(const string data) const105 const LocalSocket& LocalSocket::operator<<(const string data) const
106 {
107 WriteData(data.c_str(), data.length() + 1);
108 return *this;
109 }
110
operator >>(string & data) const111 const LocalSocket& LocalSocket::operator>>(string& data) const
112 {
113 char c = '\255';
114 while (c != '\0' && ReadData(&c, 1) > 0) {
115 data.push_back(c);
116 }
117 return *this;
118 }
119
GetWinOpenMode(LocalSocket::OpenMode mode) const120 DWORD LocalSocket::GetWinOpenMode(LocalSocket::OpenMode mode) const
121 {
122 DWORD openModeWin = GENERIC_READ;
123 switch (mode) {
124 case READ_ONLY:
125 openModeWin = GENERIC_READ;
126 break;
127 case WRITE_ONLY:
128 openModeWin = GENERIC_WRITE;
129 break;
130 case READ_WRITE:
131 openModeWin = GENERIC_READ | GENERIC_WRITE;
132 }
133 return openModeWin;
134 }
135
GetWinTransMode(LocalSocket::TransMode mode) const136 DWORD LocalSocket::GetWinTransMode(LocalSocket::TransMode mode) const
137 {
138 DWORD transMode = PIPE_READMODE_BYTE;
139 switch (mode) {
140 case TRANS_BYTE:
141 transMode = PIPE_READMODE_BYTE;
142 break;
143 case TRANS_MESSAGE:
144 transMode = PIPE_READMODE_MESSAGE;
145 break;
146 }
147 return transMode;
148 }
149