Skip to content

Commit 5a0ab43

Browse files
committed
Fix client unable to connect; changed structure
1 parent a5750fe commit 5a0ab43

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed
Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace slick {
3333
template<typename T>
3434
class SlickQueue {
3535
struct slot {
36-
std::atomic_uint_fast64_t data_index{ 0 };
36+
std::atomic_uint_fast64_t data_index{ std::numeric_limits<uint64_t>::max() };
3737
uint32_t size = 1;
3838
};
3939

@@ -71,10 +71,10 @@ class SlickQueue {
7171
allocate_shm_data(shm_name, false);
7272
}
7373

74-
if (own_) {
75-
// invalidate first slot
76-
control_[0].data_index.store(-1, std::memory_order_relaxed);
77-
}
74+
// if (own_) {
75+
// // invalidate first slot
76+
// control_[0].data_index.store(-1, std::memory_order_relaxed);
77+
// }
7878
}
7979

8080
SlickQueue(const char* const shm_name)
@@ -148,12 +148,12 @@ class SlickQueue {
148148
std::pair<T*, uint32_t> read(uint64_t& read_index) noexcept {
149149
auto& slot = control_[read_index & mask_];
150150
auto index = slot.data_index.load(std::memory_order_relaxed);
151-
if (reserved_->load(std::memory_order_relaxed) < index) {
151+
if (index != std::numeric_limits<uint64_t>::max() && reserved_->load(std::memory_order_relaxed) < index) {
152152
// queue has been reset
153153
read_index = 0;
154154
}
155155

156-
if (index == -1 || index < read_index) {
156+
if (index == std::numeric_limits<uint64_t>::max() || index < read_index) {
157157
// data not ready yet
158158
return std::make_pair(nullptr, 0);
159159
}
@@ -188,8 +188,12 @@ class SlickQueue {
188188
}
189189

190190
void reset() noexcept {
191-
// invalidate first slot
192-
control_[0].data_index.store(-1, std::memory_order_release);
191+
if (use_shm_) {
192+
control_ = new ((uint8_t*)lpvMem_ + 64) slot[buffered_size_];
193+
} else {
194+
delete [] control_;
195+
control_ = new slot[buffered_size_];
196+
}
193197
reserved_->store(0, std::memory_order_release);
194198
}
195199

@@ -199,11 +203,20 @@ class SlickQueue {
199203
void allocate_shm_data(const char* const shm_name, bool open_only) {
200204
DWORD BF_SZ = 64 + sizeof(slot) * buffered_size_ + sizeof(T) * buffered_size_;
201205
hMapFile_ = NULL;
206+
207+
#ifndef UNICODE
208+
std::string shmName = shm_name;
209+
#else
210+
int size_needed = MultiByteToWideChar(CP_UTF8, 0, shm_name, strlen(shm_name), NULL, 0);
211+
std::wstring shmName(size_needed, 0);
212+
MultiByteToWideChar(CP_UTF8, 0, shm_name, strlen(shm_name), &shmName[0], size_needed);
213+
#endif
214+
202215
if (open_only) {
203216
#ifndef UNICODE
204-
hMapFile_ = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, (LPCSTR)shm_name);
217+
hMapFile_ = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, (LPCSTR)shmName.c_str());
205218
#else
206-
hMapFile_ = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, (LPCWSTR)shm_name);
219+
hMapFile_ = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, (LPCWSTR)shmName.c_str());
207220
#endif
208221
own_ = false;
209222
auto err = GetLastError();
@@ -219,38 +232,36 @@ class SlickQueue {
219232
size_ = *reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem_) + sizeof(std::atomic_uint_fast64_t));
220233
mask_ = size_ - 1;
221234
buffered_size_ = size_ + 1024;
222-
UnmapViewOfFile(lpvMem_);
223-
lpvMem_ = nullptr;
224235
}
225236
else {
226237
hMapFile_ = CreateFileMapping(
227238
INVALID_HANDLE_VALUE, // use paging file
228239
NULL, // default security
229240
PAGE_READWRITE, // read/write access
230241
0, // maximum object size (high-order DWORD)
231-
BF_SZ, // maximum object size (low-order DWORD)
242+
BF_SZ, // maximum object size (low-order DWORD)
232243
#ifndef UNICODE
233-
(LPCSTR)shm_name // name of mapping object
244+
(LPCSTR)shmName.c_str() // name of mapping object
234245
#else
235-
(LPCWSTR)shm_name // name of mapping object
246+
(LPCWSTR)shmName.c_str() // name of mapping object
236247
#endif
237248
);
238249

239250
own_ = false;
240251
auto err = GetLastError();
241252
if (hMapFile_ == NULL) {
242-
throw std::runtime_error("Failed to create shm. err=" + std::to_string(err));
253+
throw std::runtime_error("Failed to create shm. err=" + std::to_string(err));
243254
}
244255

245256
if (err != ERROR_ALREADY_EXISTS) {
246257
own_ = true;
247258
}
248-
}
249259

250-
lpvMem_ = MapViewOfFile(hMapFile_, FILE_MAP_ALL_ACCESS, 0, 0, BF_SZ);
251-
if (!lpvMem_) {
252-
auto err = GetLastError();
253-
throw std::runtime_error("Failed to map shm. err=" + std::to_string(err));
260+
lpvMem_ = MapViewOfFile(hMapFile_, FILE_MAP_ALL_ACCESS, 0, 0, BF_SZ);
261+
if (!lpvMem_) {
262+
auto err = GetLastError();
263+
throw std::runtime_error("Failed to map shm. err=" + std::to_string(err));
264+
}
254265
}
255266

256267
if (own_) {

0 commit comments

Comments
 (0)