Skip to content

Commit eadab5f

Browse files
andrewgithubandrewgithub
andrewgithub
authored and
andrewgithub
committed
增加websocket的客户端和服务器端代码
1 parent 4a87371 commit eadab5f

File tree

2 files changed

+147
-131
lines changed

2 files changed

+147
-131
lines changed

utils/json_proxy.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,37 @@
55
#ifndef DLMS_JSON_PROXY_H
66
#define DLMS_JSON_PROXY_H
77
#include <utility>
8+
#include <iostream>
9+
#include <fstream>
810

911
#include "json.hpp"
12+
#include "status.h"
1013

1114
using json = nlohmann::json;
1215

1316
class JsonProxy {
1417
public:
15-
explicit JsonProxy(json json) : jsoObject(std::move(json)) {}
16-
explicit JsonProxy(json& json) : jsoObject(json) {}
17-
explicit JsonProxy(json&& json) : jsoObject(json) {}
18+
explicit JsonProxy(json json) : jsonObj(std::move(json)) {}
19+
explicit JsonProxy(json& json) : jsonObj(json) {}
20+
explicit JsonProxy(json&& json) : jsonObj(json) {}
1821

19-
JsonProxy(std::string& path, std::string& filename) {}
22+
JsonProxy(std::string& path, std::string& filename) {
23+
std::string file = path + filename;
24+
std::ofstream(file) << jsonObj;
25+
}
2026

2127
explicit JsonProxy(std::string& filename) {}
2228

2329

2430
json& GetJsonObj() {
25-
return jsoObject;
31+
return jsonObj;
2632
}
2733

2834

2935

3036

3137
private:
32-
json jsoObject;
38+
json jsonObj;
3339
};
3440

3541

utils/status.h

Lines changed: 135 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,165 @@
11
//
2-
// Created by wangyz38535 on 2022/6/29.
2+
// Created by wangyzhou on 2022/6/29.
33
//
44

55
#ifndef DLMS_STATUS_H
66
#define DLMS_STATUS_H
77
#include <string>
88

9-
109
class Status {
11-
public:
12-
// Create a success status.
13-
Status() noexcept: state_(nullptr) {}
10+
public:
11+
// Create a success status.
12+
Status() noexcept: state_(nullptr) {}
1413

15-
~Status() { delete[] state_; }
14+
~Status() { delete[] state_; }
1615

17-
Status(const Status &rhs);
16+
Status(const Status &rhs);
1817

19-
Status &operator=(const Status &rhs);
18+
Status &operator=(const Status &rhs);
2019

21-
Status(Status &&rhs) noexcept: state_(rhs.state_) { rhs.state_ = nullptr; }
20+
Status(Status &&rhs) noexcept: state_(rhs.state_) { rhs.state_ = nullptr; }
2221

23-
Status &operator=(Status &&rhs) noexcept;
22+
Status &operator=(Status &&rhs) noexcept;
2423

25-
// Return a success status.
26-
static Status OK() { return Status(); }
24+
// Return a success status.
25+
static Status OK() { return Status(); }
2726

28-
// Return error status of an appropriate type.
29-
static Status NotFound(const std::string &msg, const std::string &msg2 = std::string()) {
30-
return Status(kNotFound, msg, msg2);
31-
}
27+
// Return error status of an appropriate type.
28+
static Status NotFound(const std::string &msg, const std::string &msg2 = std::string()) {
29+
return Status(kNotFound, msg, msg2);
30+
}
31+
// something is broken, IE.json format invlaide
32+
static Status Corruption(const std::string &msg, const std::string &msg2 = std::string()) {
33+
return Status(kCorruption, msg, msg2);
34+
}
3235

33-
static Status Corruption(const std::string &msg, const std::string &msg2 = std::string()) {
34-
return Status(kCorruption, msg, msg2);
35-
}
36+
static Status NotSupported(const std::string &msg, const std::string &msg2 = std::string()) {
37+
return Status(kNotSupported, msg, msg2);
38+
}
3639

37-
static Status NotSupported(const std::string &msg, const std::string &msg2 = std::string()) {
38-
return Status(kNotSupported, msg, msg2);
39-
}
40+
static Status InvalidArgument(const std::string &msg, const std::string &msg2 = std::string()) {
41+
return Status(kInvalidArgument, msg, msg2);
42+
}
4043

41-
static Status InvalidArgument(const std::string &msg, const std::string &msg2 = std::string()) {
42-
return Status(kInvalidArgument, msg, msg2);
43-
}
44+
static Status IOError(const std::string &msg, const std::string &msg2 = std::string()) {
45+
return Status(kIOError, msg, msg2);
46+
}
4447

45-
static Status IOError(const std::string &msg, const std::string &msg2 = std::string()) {
46-
return Status(kIOError, msg, msg2);
47-
}
48+
static Status OutOfMemory(const std::string &msg, const std::string &msg2 = std::string()) {
49+
return Status(kOutOfMemory, msg, msg2);
50+
}
4851

49-
// Returns true iff the status indicates success.
50-
bool ok() const { return (state_ == nullptr); }
51-
52-
// Returns true iff the status indicates a NotFound error.
53-
bool IsNotFound() const { return code() == kNotFound; }
54-
55-
// Returns true iff the status indicates a Corruption error.
56-
bool IsCorruption() const { return code() == kCorruption; }
57-
58-
// Returns true iff the status indicates an IOError.
59-
bool IsIOError() const { return code() == kIOError; }
60-
61-
// Returns true iff the status indicates a NotSupportedError.
62-
bool IsNotSupportedError() const { return code() == kNotSupported; }
63-
64-
// Returns true iff the status indicates an InvalidArgument.
65-
bool IsInvalidArgument() const { return code() == kInvalidArgument; }
66-
67-
// Return a string representation of this status suitable for printing.
68-
// Returns the string "OK" for success.
69-
std::string ToString() const {
70-
if (state_ == nullptr) {
71-
return "OK";
72-
} else {
73-
char tmp[30];
74-
const char *type;
75-
switch (code()) {
76-
case kOk:
77-
type = "OK";
78-
break;
79-
case kNotFound:
80-
type = "NotFound: ";
81-
break;
82-
case kCorruption:
83-
type = "Corruption: ";
84-
break;
85-
case kNotSupported:
86-
type = "Not implemented: ";
87-
break;
88-
case kInvalidArgument:
89-
type = "Invalid argument: ";
90-
break;
91-
case kIOError:
92-
type = "IO error: ";
93-
break;
94-
default:
95-
std::snprintf(tmp, sizeof(tmp),
96-
"Unknown code(%d): ", static_cast<int>(code()));
97-
type = tmp;
98-
break;
99-
}
100-
std::string result(type);
101-
uint32_t length;
102-
std::memcpy(&length, state_, sizeof(length));
103-
result.append(state_ + 5, length);
104-
return result;
52+
// Returns true iff the status indicates success.
53+
bool ok() const { return (state_ == nullptr); }
54+
55+
// Returns true iff the status indicates a NotFound error.
56+
bool IsNotFound() const { return code() == kNotFound; }
57+
58+
// Returns true iff the status indicates a Corruption error.
59+
bool IsCorruption() const { return code() == kCorruption; }
60+
61+
// Returns true iff the status indicates an IOError.
62+
bool IsIOError() const { return code() == kIOError; }
63+
64+
// Returns true iff the status indicates a NotSupportedError.
65+
bool IsNotSupportedError() const { return code() == kNotSupported; }
66+
67+
// Returns true iff the status indicates an InvalidArgument.
68+
bool IsInvalidArgument() const { return code() == kInvalidArgument; }
69+
70+
// Returns true iff the status indicates an InvalidArgument.
71+
bool IsOutOfMemory() const { return code() == kOutOfMemory; }
72+
73+
// Return a string representation of this status suitable for printing.
74+
// Returns the string "OK" for success.
75+
std::string ToString() const {
76+
if (state_ == nullptr) {
77+
return "OK";
78+
} else {
79+
char tmp[30];
80+
const char *type;
81+
switch (code()) {
82+
case kOk:
83+
type = "OK";
84+
break;
85+
case kNotFound:
86+
type = "NotFound: ";
87+
break;
88+
case kCorruption:
89+
type = "Corruption: ";
90+
break;
91+
case kNotSupported:
92+
type = "Not implemented: ";
93+
break;
94+
case kInvalidArgument:
95+
type = "Invalid argument: ";
96+
break;
97+
case kIOError:
98+
type = "IO error: ";
99+
break;
100+
case kOutOfMemory:
101+
type = "Out of memory: ";
102+
break;
103+
default:
104+
std::snprintf(tmp, sizeof(tmp),
105+
"Unknown code(%d): ", static_cast<int>(code()));
106+
type = tmp;
107+
break;
105108
}
109+
std::string result(type);
110+
uint32_t length;
111+
std::memcpy(&length, state_, sizeof(length));
112+
result.append(state_ + 5, length);
113+
return result;
106114
}
115+
}
107116

108-
private:
109-
enum Code {
110-
kOk = 0,
111-
kNotFound = 1,
112-
kCorruption = 2,
113-
kNotSupported = 3,
114-
kInvalidArgument = 4,
115-
kIOError = 5
116-
};
117-
118-
Code code() const {
119-
return (state_ == nullptr) ? kOk : static_cast<Code>(state_[4]);
120-
}
117+
private:
118+
enum Code {
119+
kOk = 0,
120+
kNotFound = 1,
121+
kCorruption = 2,
122+
kNotSupported = 3,
123+
kInvalidArgument = 4,
124+
kIOError = 5,
125+
kOutOfMemory = 6,
126+
};
127+
128+
Code code() const {
129+
return (state_ == nullptr) ? kOk : static_cast<Code>(state_[4]);
130+
}
121131

122-
Status(Code code, const std::string &msg, const std::string &msg2) {
123-
assert(code != kOk);
124-
const auto len1 = static_cast<uint32_t>(msg.size());
125-
const auto len2 = static_cast<uint32_t>(msg2.size());
126-
const uint32_t size = len1 + (len2 ? (2 + len2) : 0);
127-
char *result = new char[size + 5];
128-
std::memcpy(result, &size, sizeof(size));
129-
result[4] = static_cast<char>(code);
130-
std::memcpy(result + 5, msg.data(), len1);
131-
if (len2) {
132-
result[5 + len1] = ':';
133-
result[6 + len1] = ' ';
134-
std::memcpy(result + 7 + len1, msg2.data(), len2);
135-
}
136-
state_ = result;
132+
Status(Code code, const std::string &msg, const std::string &msg2) {
133+
assert(code != kOk);
134+
const auto len1 = static_cast<uint32_t>(msg.size());
135+
const auto len2 = static_cast<uint32_t>(msg2.size());
136+
const uint32_t size = len1 + (len2 ? (2 + len2) : 0);
137+
char *result = new char[size + 5];
138+
std::memcpy(result, &size, sizeof(size));
139+
result[4] = static_cast<char>(code);
140+
std::memcpy(result + 5, msg.data(), len1);
141+
if (len2) {
142+
result[5 + len1] = ':';
143+
result[6 + len1] = ' ';
144+
std::memcpy(result + 7 + len1, msg2.data(), len2);
137145
}
146+
state_ = result;
147+
}
138148

139-
static const char *CopyState(const char *s) {
140-
uint32_t size;
141-
std::memcpy(&size, state, sizeof(size));
142-
char *result = new char[size + 5];
143-
std::memcpy(result, state, size + 5);
144-
return result;
145-
}
149+
static const char *CopyState(const char *s) {
150+
uint32_t size;
151+
std::memcpy(&size, state, sizeof(size));
152+
char *result = new char[size + 5];
153+
std::memcpy(result, state, size + 5);
154+
return result;
155+
}
146156

147-
// OK status has a null state_. Otherwise, state_ is a new[] array
148-
// of the following form:
149-
// state_[0..3] == length of message
150-
// state_[4] == code
151-
// state_[5..] == message
152-
const char *state_;
157+
// OK status has a null state_. Otherwise, state_ is a new[] array
158+
// of the following form:
159+
// state_[0..3] == length of message
160+
// state_[4] == code
161+
// state_[5..] == message
162+
const char *state_;
153163
};
154164

155165
inline Status::Status(const Status &rhs) {

0 commit comments

Comments
 (0)