Skip to content

Commit 37b716b

Browse files
committed
Fix html5 connect issue
Fixes #57
1 parent a9264f9 commit 37b716b

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

websocket/src/websocket.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ static void CreateConnectionWslay(WebsocketConnection* conn)
395395
static WebsocketConnection* CreateConnection(const char* url)
396396
{
397397
WebsocketConnection* conn = new WebsocketConnection;
398+
memset(conn, 0, sizeof(WebsocketConnection));
398399
conn->m_BufferCapacity = g_Websocket.m_BufferSize;
399400
conn->m_Buffer = (char*)malloc(conn->m_BufferCapacity);
400401
conn->m_Buffer[0] = 0;
@@ -417,6 +418,7 @@ static WebsocketConnection* CreateConnection(const char* url)
417418
conn->m_HasHandshakeData = 0;
418419
conn->m_HandshakeResponse = 0;
419420
conn->m_ConnectionThread = 0;
421+
conn->m_State = STATE_DISCONNECTED;
420422

421423
#if defined(HAVE_WSLAY)
422424
CreateConnectionWslay(conn);
@@ -794,31 +796,31 @@ Result PushMessage(WebsocketConnection* conn, MessageType type, int length, cons
794796
{
795797
DM_MUTEX_SCOPED_LOCK(conn->m_Mutex);
796798
// we should only push messages when in the connected or disconnecting state
797-
assert((STATE_CONNECTED == conn->m_State) || (STATE_DISCONNECTING == conn->m_State));
798-
799-
if (conn->m_Messages.Full())
800-
conn->m_Messages.OffsetCapacity(4);
801-
802-
Message msg;
803-
msg.m_Type = (uint32_t)type;
804-
msg.m_Length = length;
805-
msg.m_Code = code;
806-
conn->m_Messages.Push(msg);
807-
808-
if ((conn->m_BufferSize + length) >= conn->m_BufferCapacity)
799+
if ((STATE_CONNECTED == conn->m_State) || (STATE_DISCONNECTING == conn->m_State))
809800
{
810-
conn->m_BufferCapacity = conn->m_BufferSize + length + 1;
811-
conn->m_Buffer = (char*)realloc(conn->m_Buffer, conn->m_BufferCapacity);
812-
}
813-
// append to the end of the buffer
814-
memcpy(conn->m_Buffer + conn->m_BufferSize, buffer, length);
801+
if (conn->m_Messages.Full())
802+
conn->m_Messages.OffsetCapacity(4);
815803

816-
conn->m_BufferSize += length;
817-
conn->m_Buffer[conn->m_BufferCapacity-1] = 0;
804+
Message msg;
805+
msg.m_Type = (uint32_t)type;
806+
msg.m_Length = length;
807+
msg.m_Code = code;
808+
conn->m_Messages.Push(msg);
818809

819-
// Instead of printing from the incoming buffer, we print from our own, to make sure it looks ok
820-
DebugPrint(2, __FUNCTION__, conn->m_Buffer+conn->m_BufferSize-length, length);
810+
if ((conn->m_BufferSize + length) >= conn->m_BufferCapacity)
811+
{
812+
conn->m_BufferCapacity = conn->m_BufferSize + length + 1;
813+
conn->m_Buffer = (char*)realloc(conn->m_Buffer, conn->m_BufferCapacity);
814+
}
815+
// append to the end of the buffer
816+
memcpy(conn->m_Buffer + conn->m_BufferSize, buffer, length);
821817

818+
conn->m_BufferSize += length;
819+
conn->m_Buffer[conn->m_BufferCapacity-1] = 0;
820+
821+
// Instead of printing from the incoming buffer, we print from our own, to make sure it looks ok
822+
DebugPrint(2, __FUNCTION__, conn->m_Buffer+conn->m_BufferSize-length, length);
823+
}
822824
return dmWebsocket::RESULT_OK;
823825
}
824826

0 commit comments

Comments
 (0)