路泰科技体检小程序UI设计新版本
qx
2025-08-06 fe97f78b9a343ee9fa45a3531d03d73dcd1df31b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef IPC_H
#define IPC_H
 
#include <string>
#include <stdlib.h>
 
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#endif
 
class IPC {
public:
  IPC(std::string path) {
    mStopped = false;
    #ifdef _WIN32
      while (true) {
        mPipe = CreateFile(
          path.data(), // pipe name
          GENERIC_READ | GENERIC_WRITE, // read and write access
          0, // no sharing
          NULL, // default security attributes
          OPEN_EXISTING, // opens existing pipe
          FILE_FLAG_OVERLAPPED, // attributes
          NULL // no template file
        );
 
        if (mPipe != INVALID_HANDLE_VALUE) {
          break;
        }
 
        if (GetLastError() != ERROR_PIPE_BUSY) {
          throw std::runtime_error("Could not open pipe");
        }
 
        // Wait for pipe to become available if it is busy
        if (!WaitNamedPipe(path.data(), 30000)) {
          throw std::runtime_error("Error waiting for pipe");
        }
      }
 
      mReader = CreateEvent(NULL, true, false, NULL);
      mWriter = CreateEvent(NULL, true, false, NULL);
    #else
      struct sockaddr_un addr;
      memset(&addr, 0, sizeof(addr));
      addr.sun_family = AF_UNIX;
      strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);
 
      mSock = socket(AF_UNIX, SOCK_STREAM, 0);
      if (connect(mSock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un))) {
        throw std::runtime_error("Error connecting to socket");
      }
    #endif
  }
 
  ~IPC() {
    mStopped = true;
    #ifdef _WIN32
      CancelIo(mPipe);
      CloseHandle(mPipe);
      CloseHandle(mReader);
      CloseHandle(mWriter);
    #else
      shutdown(mSock, SHUT_RDWR);
    #endif
  }
 
  void write(std::string buf) {
    #ifdef _WIN32
      OVERLAPPED overlapped;
      overlapped.hEvent = mWriter;
      bool success = WriteFile(
        mPipe, // pipe handle
        buf.data(), // message
        buf.size(), // message length
        NULL, // bytes written
        &overlapped // overlapped
      );
 
      if (mStopped) {
        return;
      }
 
      if (!success) {
        if (GetLastError() != ERROR_IO_PENDING) {
          throw std::runtime_error("Write error");
        }
      }
 
      DWORD written;
      success = GetOverlappedResult(mPipe, &overlapped, &written, true);
      if (!success) {
        throw std::runtime_error("GetOverlappedResult failed");
      }
 
      if (written != buf.size()) {
        throw std::runtime_error("Wrong number of bytes written");
      }
    #else
      int r = 0;
      for (unsigned int i = 0; i != buf.size(); i += r) {
        r = ::write(mSock, &buf[i], buf.size() - i);
        if (r == -1) {
          if (errno == EAGAIN) {
            r = 0;
          } else if (mStopped) {
            return;
          } else {
            throw std::runtime_error("Write error");
          }
        }
      }
    #endif
  }
 
  int read(char *buf, size_t len) {
    #ifdef _WIN32
      OVERLAPPED overlapped;
      overlapped.hEvent = mReader;
      bool success = ReadFile(
        mPipe, // pipe handle
        buf, // buffer to receive reply
        len, // size of buffer
        NULL, // number of bytes read
        &overlapped // overlapped
      );
 
      if (!success && !mStopped) {
        if (GetLastError() != ERROR_IO_PENDING) {
          throw std::runtime_error("Read error");
        }
      }
 
      DWORD read = 0;
      success = GetOverlappedResult(mPipe, &overlapped, &read, true);
      if (!success && !mStopped) {
        throw std::runtime_error("GetOverlappedResult failed");
      }
 
      return read;
    #else
      int r = ::read(mSock, buf, len);
      if (r == 0 && !mStopped) {
        throw std::runtime_error("Socket ended unexpectedly");
      }
 
      if (r < 0) {
        if (mStopped) {
          return 0;
        }
 
        throw std::runtime_error(strerror(errno));
      }
 
      return r;
    #endif
  }
 
private:
  bool mStopped;
  #ifdef _WIN32
    HANDLE mPipe;
    HANDLE mReader;
    HANDLE mWriter;
  #else
    int mSock;
  #endif
};
 
#endif