FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_networkType.inl
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17namespace fge::net
18{
19
21
22template<class T>
24 g_typeCopy(source._getter()),
25 g_typeSource(std::move(source))
26{}
27
28template<class T>
29void const* NetworkType<T>::getSource() const
30{
31 return &this->g_typeSource;
32}
33template<class T>
35{
36 if (pck >> this->g_typeCopy)
37 {
38 this->g_typeSource._setter(this->g_typeCopy);
39 this->setLastUpdateTime();
40 this->clearWaitingUpdateFlag();
41 this->_onApplied.call();
42 return true;
43 }
44 return false;
45}
46template<class T>
48{
49 auto it = this->_g_tableId.find(id);
50 if (it != this->_g_tableId.end())
51 {
52 pck << this->g_typeSource._getter();
53 it->second._config &= ~fge::net::PerClientConfigs::CLIENTCONFIG_MODIFIED_FLAG;
54 }
55}
56template<class T>
58{
59 pck << this->g_typeSource._getter();
60}
61template<class T>
63{
64 return (this->g_typeSource._getter() != this->g_typeCopy) || this->_g_force;
65}
66template<class T>
68{
69 this->_g_force = true;
70}
71template<class T>
73{
74 this->_g_force = false;
75 this->g_typeCopy = this->g_typeSource._getter();
76}
77
79
80template<class T>
82 g_typeSource(source)
83{
84 source->setType<T>();
85}
86
87template<class T>
89{
90 return this->g_typeSource;
91}
92
93template<class T>
95{
96 pck >> this->g_typeSource->template setType<T>();
97
98 this->setLastUpdateTime();
99 this->clearWaitingUpdateFlag();
100 this->_onApplied.call();
101 return true;
102}
103template<class T>
105{
106 auto it = this->_g_tableId.find(id);
107 if (it != this->_g_tableId.end())
108 {
109 pck << this->g_typeSource->template setType<T>();
110
111 it->second._config &= ~fge::net::PerClientConfigs::CLIENTCONFIG_MODIFIED_FLAG;
112 }
113}
114template<class T>
116{
117 pck << this->g_typeSource->template setType<T>();
118}
119
120template<class T>
122{
123 return this->g_typeSource->isModified();
124}
125template<class T>
127{
128 this->g_typeSource->setModifiedFlag(true);
129}
130template<class T>
132{
133 this->g_typeSource->setModifiedFlag(false);
134}
135
137template<class T>
139 g_typeSource(source),
140 g_vname(vname)
141{
142 fge::Property& property = source->getProperty(vname);
143 property.setType<T>();
144}
145
146template<class T>
148{
149 return this->g_typeSource;
150}
151
152template<class T>
154{
155 fge::Property& property = this->g_typeSource->getProperty(this->g_vname);
156
157 pck >> property.setType<T>();
158
159 this->setLastUpdateTime();
160 this->clearWaitingUpdateFlag();
161 this->_onApplied.call();
162 return false;
163}
164template<class T>
166{
167 auto it = this->_g_tableId.find(id);
168 if (it != this->_g_tableId.end())
169 {
170 fge::Property& property = this->g_typeSource->getProperty(this->g_vname);
171
172 pck << property.setType<T>();
173
174 it->second._config &= ~fge::net::PerClientConfigs::CLIENTCONFIG_MODIFIED_FLAG;
175 }
176}
177template<class T>
179{
180 fge::Property& property = this->g_typeSource->getProperty(this->g_vname);
181
182 pck << property.setType<T>();
183}
184
185template<class T>
187{
188 return this->g_typeSource->getProperty(this->g_vname).isModified();
189}
190template<class T>
192{
193 this->g_typeSource->getProperty(this->g_vname).setModifiedFlag(true);
194}
195template<class T>
197{
198 this->g_typeSource->getProperty(this->g_vname).setModifiedFlag(false);
199}
200
201template<class T>
202std::string const& NetworkTypePropertyList<T>::getValueName() const
203{
204 return this->g_vname;
205}
206
208template<class T>
210 g_typeSource(source),
211 g_trigger(false)
212{}
213
214template<class T>
216{
217 return this->g_typeSource;
218}
219
220template<class T>
222{
223 if (pck >> *this->g_typeSource)
224 {
225 this->setLastUpdateTime();
226 this->clearWaitingUpdateFlag();
227 this->_onApplied.call();
228 return true;
229 }
230 return false;
231}
232template<class T>
234{
235 fge::net::NetworkPerClientModificationTable::iterator it = this->_g_tableId.find(id);
236 if (it != this->_g_tableId.end())
237 {
238 pck << *this->g_typeSource;
239 it->second._config &= ~fge::net::PerClientConfigs::CLIENTCONFIG_MODIFIED_FLAG;
240 }
241}
242template<class T>
244{
245 pck << *this->g_typeSource;
246}
247
248template<class T>
250{
251 return this->g_trigger;
252}
253template<class T>
255{
256 this->g_trigger = true;
257}
258template<class T>
260{
261 this->g_trigger = false;
262}
263
264template<class T>
266{
267 this->g_trigger = true;
268}
269
270//RecordedVector
271
272template<class T>
273typename RecordedVector<T>::const_reference RecordedVector<T>::at(SizeType index) const
274{
275 return this->g_container.at(index);
276}
277template<class T>
278typename RecordedVector<T>::const_reference RecordedVector<T>::operator[](SizeType index) const
279{
280 return this->g_container[index];
281}
282template<class T>
283typename RecordedVector<T>::const_reference RecordedVector<T>::front() const
284{
285 return this->g_container.front();
286}
287template<class T>
288typename RecordedVector<T>::const_reference RecordedVector<T>::back() const
289{
290 return this->g_container.back();
291}
292template<class T>
293T const* RecordedVector<T>::data() const
294{
295 return this->g_container.data();
296}
297
298template<class T>
299typename RecordedVector<T>::const_iterator RecordedVector<T>::begin() const
300{
301 return this->g_container.begin();
302}
303template<class T>
304typename RecordedVector<T>::const_iterator RecordedVector<T>::end() const
305{
306 return this->g_container.end();
307}
308template<class T>
309typename RecordedVector<T>::const_iterator RecordedVector<T>::cbegin() const
310{
311 return this->g_container.cbegin();
312}
313template<class T>
314typename RecordedVector<T>::const_iterator RecordedVector<T>::cend() const
315{
316 return this->g_container.cend();
317}
318template<class T>
319typename RecordedVector<T>::const_reverse_iterator RecordedVector<T>::rbegin() const
320{
321 return this->g_container.rbegin();
322}
323template<class T>
324typename RecordedVector<T>::const_reverse_iterator RecordedVector<T>::rend() const
325{
326 return this->g_container.rend();
327}
328template<class T>
329typename RecordedVector<T>::const_reverse_iterator RecordedVector<T>::crbegin() const
330{
331 return this->g_container.crbegin();
332}
333template<class T>
334typename RecordedVector<T>::const_reverse_iterator RecordedVector<T>::crend() const
335{
336 return this->g_container.crend();
337}
338
339template<class T>
340SizeType RecordedVector<T>::size() const
341{
342 return static_cast<SizeType>(this->g_container.size());
343}
344template<class T>
345bool RecordedVector<T>::empty() const
346{
347 return this->g_container.empty();
348}
349
350template<class T>
351void RecordedVector<T>::reserve(SizeType n)
352{
353 this->g_container.reserve(n);
354}
355
356template<class T>
357void RecordedVector<T>::clear()
358{
359 this->g_container.clear();
360 this->clearEvents();
361 this->pushEvent({RecordedEventTypes::REMOVE_ALL, 0});
362}
363template<class T>
364typename RecordedVector<T>::iterator RecordedVector<T>::insert(const_iterator pos, T const& value)
365{
366 SizeType index = pos - this->g_container.begin();
367 this->pushEvent({RecordedEventTypes::ADD, index});
368 return this->g_container.insert(pos, value);
369}
370template<class T>
371typename RecordedVector<T>::iterator RecordedVector<T>::insert(const_iterator pos, T&& value)
372{
373 SizeType index = pos - this->g_container.begin();
374 this->pushEvent({RecordedEventTypes::ADD, index});
375 return this->g_container.insert(pos, std::move(value));
376}
377template<class T>
378template<class... TArgs>
379typename RecordedVector<T>::iterator RecordedVector<T>::emplace(const_iterator pos, TArgs&&... value)
380{
381 SizeType index = pos - this->g_container.begin();
382 this->pushEvent({RecordedEventTypes::ADD, index});
383 return this->g_container.emplace(pos, std::forward<TArgs>(value)...);
384}
385template<class T>
386template<class TArg>
387void RecordedVector<T>::push_back(TArg&& arg)
388{
389 this->pushEvent({RecordedEventTypes::ADD, this->g_container.size()});
390 this->g_container.push_back(std::forward<TArg>(arg));
391}
392template<class T>
393template<class... TArgs>
394typename RecordedVector<T>::reference RecordedVector<T>::emplace_back(TArgs&&... arg)
395{
396 this->pushEvent({RecordedEventTypes::ADD, this->g_container.size()});
397 return this->g_container.emplace_back(std::forward<TArgs>(arg)...);
398}
399template<class T>
400typename RecordedVector<T>::const_iterator RecordedVector<T>::erase(const_iterator pos)
401{
402 SizeType index = pos - this->g_container.begin();
403 this->pushEvent({RecordedEventTypes::REMOVE, index});
404 return this->g_container.erase(pos);
405}
406template<class T>
407void RecordedVector<T>::pop_back()
408{
409 this->pushEvent({RecordedEventTypes::REMOVE, this->g_container.size() - 1});
410 this->g_container.pop_back();
411}
412
413template<class T>
414typename RecordedVector<T>::reference RecordedVector<T>::modify(SizeType index)
415{
416 this->pushEvent({RecordedEventTypes::MODIFY, index});
417 return this->g_container[index];
418}
419template<class T>
420typename RecordedVector<T>::reference RecordedVector<T>::modify(const_iterator pos)
421{
422 SizeType index = pos - this->g_container.begin();
423 this->pushEvent({RecordedEventTypes::MODIFY, index});
424 return this->g_container[index];
425}
426
427template<class T>
428void RecordedVector<T>::clearEvents()
429{
430 this->g_events.clear();
431}
432template<class T>
433SizeType RecordedVector<T>::eventsSize() const
434{
435 return static_cast<SizeType>(this->g_events.size());
436}
437template<class T>
438typename RecordedVector<T>::EventQueue const& RecordedVector<T>::getEventQueue() const
439{
440 return this->g_events;
441}
442
443template<class T>
444bool RecordedVector<T>::isRegisteringEvents() const
445{
446 return this->g_registerEvents;
447}
448template<class T>
449void RecordedVector<T>::registerEvents(bool enable)
450{
451 this->clearEvents();
452 this->g_registerEvents = enable;
453}
454
455template<class T>
456void RecordedVector<T>::pushEvent(RecordedEvent event)
457{
458 if (this->g_registerEvents)
459 {
460 this->g_events.emplace_back(event);
461 }
462}
463
464template<class T>
465Packet& operator<<(Packet& pck, RecordedVector<T> const& vec)
466{
467 return pck << vec.g_container;
468}
469template<class T>
470Packet const& operator>>(Packet const& pck, RecordedVector<T>& vec)
471{
472 return pck >> vec.g_container;
473}
474inline Packet& operator<<(Packet& pck, RecordedEvent const& event)
475{
476 return pck << event._type << event._index;
477}
478inline Packet const& operator>>(Packet const& pck, RecordedEvent& event)
479{
480 return pck >> event._type >> event._index;
481}
482
484
485template<class T>
487 g_typeSource(source)
488{}
489template<class T>
491{
492 for (auto& it: this->_g_tableId)
493 {
494 this->destroyClientCustomData(it.second._customData);
495 }
496}
497
498template<class T>
500{
501 return this->g_typeSource;
502}
503
504template<class T>
506{
507 PackTypes packType;
508 pck >> packType;
509
510 if (packType == PackTypes::FULL)
511 {
512 pck >> *this->g_typeSource;
513 }
514 else
515 {
516 SizeType eventCount{0};
517 pck >> eventCount;
518 for (SizeType i = 0; i < eventCount; ++i)
519 {
520 RecordedEvent event{};
521 pck >> event;
522 switch (event._type)
523 {
524 case RecordedEventTypes::ADD:
525 if (event._index >= this->g_typeSource->size())
526 {
527 T value;
528 pck >> value;
529 this->g_typeSource->emplace_back(std::move(value));
530 }
531 else
532 {
533 T value;
534 pck >> value;
535 this->g_typeSource->emplace(this->g_typeSource->begin() + event._index, std::move(value));
536 }
537 break;
538 case RecordedEventTypes::REMOVE:
539 if (event._index < this->g_typeSource->size())
540 {
541 this->g_typeSource->erase(this->g_typeSource->begin() + event._index);
542 }
543 else
544 {
545 return false;
546 }
547 break;
548 case RecordedEventTypes::REMOVE_ALL:
549 this->g_typeSource->clear();
550 break;
551 case RecordedEventTypes::MODIFY:
552 if (event._index < this->g_typeSource->size())
553 {
554 pck >> this->g_typeSource->modify(event._index);
555 }
556 else
557 {
558 return false;
559 }
560 break;
561 }
562 }
563 }
564
565 this->setLastUpdateTime();
566 this->clearWaitingUpdateFlag();
567 this->_onApplied.call();
568 return true;
569}
570template<class T>
572{
573 auto itId = this->_g_tableId.find(id);
574 if (itId != this->_g_tableId.end())
575 {
576 auto* events = static_cast<typename RecordedVector<T>::EventQueue*>(itId->second._customData);
577
578 if (events->empty())
579 { //Events queue is empty but the modified flag is set, that means we have to send the full vector
580 pck << PackTypes::FULL;
581 pck << *this->g_typeSource;
582 }
583 else
584 {
585 pck << PackTypes::PARTIAL;
586 pck << static_cast<SizeType>(events->size());
587 for (auto itEvent = events->begin(); itEvent != events->end(); ++itEvent)
588 {
589 pck << *itEvent;
590 if (itEvent->_type == RecordedEventTypes::ADD || itEvent->_type == RecordedEventTypes::MODIFY)
591 {
592 //We have to pack the data but the index do not represent the current state of the vector.
593 //So we have to reverse read the events (history) to get the correct index.
594 auto finalIndex = itEvent->_index;
595 for (auto itReverse = events->rbegin(); &(*itReverse) != &(*itEvent); ++itReverse)
596 {
597 if (itReverse->_type == RecordedEventTypes::ADD && itReverse->_index <= finalIndex)
598 {
599 ++finalIndex;
600 }
601 else if (itReverse->_type == RecordedEventTypes::REMOVE && itReverse->_index < finalIndex)
602 {
603 --finalIndex;
604 }
605 }
606 pck << this->g_typeSource->at(finalIndex);
607 }
608 }
609 events->clear();
610 }
611
612 itId->second._config &= ~fge::net::PerClientConfigs::CLIENTCONFIG_MODIFIED_FLAG;
613 }
614}
615template<class T>
617{
618 pck << PackTypes::FULL << *this->g_typeSource;
619}
620
621template<class T>
623{
624 auto it = this->_g_tableId.find(id);
625 if (it != this->_g_tableId.end())
626 {
627 it->second._config |= fge::net::PerClientConfigs::CLIENTCONFIG_MODIFIED_FLAG;
628 auto* events = static_cast<typename RecordedVector<T>::EventQueue*>(it->second._customData);
629 events->clear();
630 }
631}
632template<class T>
634{
635 auto it = this->_g_tableId.find(id);
636 if (it != this->_g_tableId.end())
637 {
638 it->second._config &= ~fge::net::PerClientConfigs::CLIENTCONFIG_MODIFIED_FLAG;
639 auto* events = static_cast<typename RecordedVector<T>::EventQueue*>(it->second._customData);
640 events->clear();
641 }
642}
643
644template<class T>
646{
647 if (this->_g_force)
648 { //When events is cleared, we send the full vector
649 this->g_typeSource->clearEvents();
650 }
651 return this->g_typeSource->eventsSize() != 0 || this->_g_force;
652}
653template<class T>
655{
656 this->_g_force = true;
657}
658template<class T>
660{
661 this->_g_force = false;
662 this->g_typeSource->clearEvents();
663}
664
665template<class T>
667{
668 ptr = new typename RecordedVector<T>::EventQueue();
669}
670template<class T>
671void NetworkTypeVector<T>::destroyClientCustomData(void*& ptr) const
672{
673 delete static_cast<typename RecordedVector<T>::EventQueue*>(ptr);
674}
675template<class T>
676void NetworkTypeVector<T>::applyClientCustomData(void*& ptr) const
677{
678 auto* events = static_cast<typename RecordedVector<T>::EventQueue*>(ptr);
679 bool clearFirst = this->g_typeSource->getEventQueue().front()._type == RecordedEventTypes::REMOVE_ALL;
680 if (clearFirst)
681 {
682 events->clear();
683 }
684 for (auto const& event: this->g_typeSource->getEventQueue())
685 {
686 events->push_back(event);
687 }
688}
689
690
691} // namespace fge::net
A class that map a string to a Property.
Definition C_propertyList.hpp:35
A class that can store any type of data.
Definition C_property.hpp:54
The network type for a trivial type but triggered manually.
Definition C_networkType.hpp:425
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:215
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:254
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:233
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:259
NetworkTypeManual(T *source)
NetworkTypeManual.
Definition C_networkType.inl:209
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:221
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:249
The network type for a property inside a list.
Definition C_networkType.hpp:394
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:147
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:186
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:196
NetworkTypePropertyList(fge::PropertyList *source, std::string const &vname)
NetworkTypePropertyList.
Definition C_networkType.inl:138
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:153
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:191
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:165
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:131
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:126
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:94
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:104
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:88
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:121
NetworkTypeProperty(fge::Property *source)
NetworkTypeProperty.
Definition C_networkType.inl:81
The network type for a vector.
Definition C_networkType.hpp:549
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:654
void forceUncheckClient(fge::net::Identity const &id) override
Reset the modification flag for the specified client identity.
Definition C_networkType.inl:633
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:659
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:499
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:505
NetworkTypeVector(RecordedVector< T > *source)
NetworkTypeVector.
Definition C_networkType.inl:486
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:571
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:645
void forceCheckClient(fge::net::Identity const &id) override
Force the modification flag to be set for the specified client identity.
Definition C_networkType.inl:622
NetworkType(fge::DataAccessor< T > source)
NetworkType.
Definition C_networkType.inl:23
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:67
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:47
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:72
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:34
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:29
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:62
Definition C_packet.hpp:70
Definition C_networkType.hpp:473
Definition C_dataAccessor.hpp:27
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31
Definition C_networkType.hpp:463