Skip to content

Commit 950c4ea

Browse files
dawid-trendota-redpwielders
authored andcommitted
[ONEM-31782] wait for Websocket in Open() (#1378)
* [ONEM-31782] wait for Websocket in Open() * [ONEM-31782] simplify code * [ONEM-31782] unify SLEEPSLOT_POLLING_TIME
1 parent be42eba commit 950c4ea

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

Source/core/Portability.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ int clock_gettime(int, struct timespec*);
484484

485485
#define ALLOCA alloca
486486

487+
#define SLEEPSLOT_POLLING_TIME 100
487488
extern void EXTERNAL SleepMs(const unsigned int a_Time);
488489
extern void EXTERNAL SleepUs(const unsigned int a_Time);
489490
inline void EXTERNAL SleepS(unsigned int a_Time)

Source/core/SerialPort.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
namespace WPEFramework {
5353
namespace Core {
5454

55-
static constexpr uint32_t SLEEPSLOT_TIME = 100;
56-
5755
//////////////////////////////////////////////////////////////////////
5856
// SerialPort::SerialMonitor
5957
//////////////////////////////////////////////////////////////////////
@@ -646,11 +644,11 @@ namespace Core {
646644
// Make sure we aren't in the monitor thread waiting for close completion.
647645
ASSERT(Core::Thread::ThreadId() != ResourceMonitor::Instance().Id());
648646

649-
uint32_t sleepSlot = (waiting > SLEEPSLOT_TIME ? SLEEPSLOT_TIME : waiting);
647+
uint32_t sleepSlot = (waiting > SLEEPSLOT_POLLING_TIME ? SLEEPSLOT_POLLING_TIME : waiting);
650648

651649
_adminLock.Unlock();
652650

653-
// Right, lets sleep in slices of <= SLEEPSLOT_TIME ms
651+
// Right, lets sleep in slices of <= SLEEPSLOT_POLLING_TIME ms
654652
SleepMs(sleepSlot);
655653

656654
_adminLock.Lock();

Source/core/SocketPort.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ namespace WPEFramework {
262262
//////////////////////////////////////////////////////////////////////
263263

264264
static constexpr uint32_t MAX_LISTEN_QUEUE = 64;
265-
static constexpr uint32_t SLEEPSLOT_TIME = 100;
266265

267266
inline void DestroySocket(SOCKET& socket)
268267
{
@@ -896,7 +895,7 @@ namespace WPEFramework {
896895
// Make sure we aren't in the monitor thread waiting for close completion.
897896
ASSERT(Core::Thread::ThreadId() != ResourceMonitor::Instance().Id());
898897

899-
uint32_t sleepSlot = (waiting > SLEEPSLOT_TIME ? SLEEPSLOT_TIME : waiting);
898+
uint32_t sleepSlot = (waiting > SLEEPSLOT_POLLING_TIME ? SLEEPSLOT_POLLING_TIME : waiting);
900899

901900
m_syncAdmin.Unlock();
902901

@@ -931,7 +930,7 @@ namespace WPEFramework {
931930
// Make sure we aren't in the monitor thread waiting for close completion.
932931
ASSERT(Core::Thread::ThreadId() != ResourceMonitor::Instance().Id());
933932

934-
uint32_t sleepSlot = (waiting > SLEEPSLOT_TIME ? SLEEPSLOT_TIME : waiting);
933+
uint32_t sleepSlot = (waiting > SLEEPSLOT_POLLING_TIME ? SLEEPSLOT_POLLING_TIME : waiting);
935934

936935
// Right, lets sleep in slices of 100 ms
937936
SleepMs(sleepSlot);
@@ -959,11 +958,11 @@ namespace WPEFramework {
959958
// Make sure we aren't in the monitor thread waiting for close completion.
960959
ASSERT(Core::Thread::ThreadId() != ResourceMonitor::Instance().Id());
961960

962-
uint32_t sleepSlot = (waiting > SLEEPSLOT_TIME ? SLEEPSLOT_TIME : waiting);
961+
uint32_t sleepSlot = (waiting > SLEEPSLOT_POLLING_TIME ? SLEEPSLOT_POLLING_TIME : waiting);
963962

964963
m_syncAdmin.Unlock();
965964

966-
// Right, lets sleep in slices of <= SLEEPSLOT_TIME ms
965+
// Right, lets sleep in slices of <= SLEEPSLOT_POLLING_TIME ms
967966
SleepMs(sleepSlot);
968967

969968
m_syncAdmin.Lock();

Source/websocket/WebSocketLink.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,34 @@ POP_WARNING()
10221022
{
10231023
return (_channel.AbortUpgrade(status, reason));
10241024
}
1025+
uint32_t WaitForLink(const uint32_t time) const
1026+
{
1027+
// Make sure the state does not change in the mean time.
1028+
Lock();
1029+
1030+
uint32_t waiting = (time == Core::infinite ? Core::infinite : time); // Expect time in MS.
1031+
1032+
// Right, a wait till connection is closed is requested..
1033+
while ((waiting > 0) && (IsWebSocket() == false)) {
1034+
uint32_t sleepSlot = (waiting > SLEEPSLOT_POLLING_TIME ? SLEEPSLOT_POLLING_TIME : waiting);
1035+
1036+
Unlock();
1037+
// Right, lets sleep in slices of 100 ms
1038+
SleepMs(sleepSlot);
1039+
Lock();
1040+
1041+
waiting -= (waiting == Core::infinite ? 0 : sleepSlot);
1042+
}
1043+
1044+
uint32_t result = (((time == 0) || (IsWebSocket() == true)) ? Core::ERROR_NONE : Core::ERROR_TIMEDOUT);
1045+
Unlock();
1046+
return (result);
1047+
}
10251048
uint32_t Open(const uint32_t waitTime)
10261049
{
1027-
return (_channel.Open(waitTime));
1050+
_channel.Open(0);
1051+
1052+
return WaitForLink(waitTime);
10281053
}
10291054
uint32_t Close(const uint32_t waitTime)
10301055
{

0 commit comments

Comments
 (0)