aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/Net/OVR_PacketizedTCPSocket.cpp
blob: 6352480003b87a3697e449b4932b5aa404026940 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/************************************************************************************

Filename    :   OVR_PacketizedTCPSocket.cpp
Content     :   TCP with automated message framing.
Created     :   June 10, 2014
Authors     :   Kevin Jenkins

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#include "OVR_PacketizedTCPSocket.h"

namespace OVR { namespace Net {


//-----------------------------------------------------------------------------
// Constants

static const int LENGTH_FIELD_BYTES = 4;


//-----------------------------------------------------------------------------
// PacketizedTCPSocket

PacketizedTCPSocket::PacketizedTCPSocket()
{
	pRecvBuff = 0;
	pRecvBuffSize = 0;
	Transport = TransportType_PacketizedTCP;
}

PacketizedTCPSocket::PacketizedTCPSocket(SocketHandle _sock, bool isListenSocket) : PacketizedTCPSocketBase(_sock, isListenSocket)
{
	pRecvBuff = 0;
	pRecvBuffSize = 0;
	Transport = TransportType_PacketizedTCP;
}

PacketizedTCPSocket::~PacketizedTCPSocket()
{
	OVR_FREE(pRecvBuff);
}

int PacketizedTCPSocket::Send(const void* pData, int bytes)
{
    Lock::Locker locker(&sendLock);

	if (bytes <= 0)
	{
		return -1;
	}

	// Convert length to 4 endian-neutral bytes
	uint32_t lengthWord = bytes;
	uint8_t lengthBytes[LENGTH_FIELD_BYTES] = {
		(uint8_t)lengthWord,
		(uint8_t)(lengthWord >> 8),
		(uint8_t)(lengthWord >> 16),
		(uint8_t)(lengthWord >> 24)
	};

	int s = PacketizedTCPSocketBase::Send(lengthBytes, LENGTH_FIELD_BYTES);
	if (s > 0)
	{
		return PacketizedTCPSocketBase::Send(pData,bytes);
	}
	else
	{
		return s;
	}
}

int PacketizedTCPSocket::SendAndConcatenate(const void** pDataArray, int* dataLengthArray, int arrayCount)
{
    Lock::Locker locker(&sendLock);

    if (arrayCount == 0)
		return 0;

	int totalBytes = 0;
	for (int i = 0; i < arrayCount; i++)
		totalBytes += dataLengthArray[i];

	// Convert length to 4 endian-neutral bytes
	uint32_t lengthWord = totalBytes;
	uint8_t lengthBytes[LENGTH_FIELD_BYTES] = {
		(uint8_t)lengthWord,
		(uint8_t)(lengthWord >> 8),
		(uint8_t)(lengthWord >> 16),
		(uint8_t)(lengthWord >> 24)
	};

	int s = PacketizedTCPSocketBase::Send(lengthBytes, LENGTH_FIELD_BYTES);
	if (s > 0)
	{
		for (int i = 0; i < arrayCount; i++)
		{
			PacketizedTCPSocketBase::Send(pDataArray[i], dataLengthArray[i]);
		}
	}

	return s;
}

void PacketizedTCPSocket::OnRecv(SocketEvent_TCP* eventHandler, uint8_t* pData, int bytesRead)
{
	uint8_t* dataSource = NULL;
	int dataSourceSize = 0;

	recvBuffLock.DoLock();

	if (pRecvBuff == NULL)
	{
		dataSource = pData;
		dataSourceSize = bytesRead;
	}
	else
	{
		uint8_t* pRecvBuffNew = (uint8_t*)OVR_REALLOC(pRecvBuff, bytesRead + pRecvBuffSize);
		if (!pRecvBuffNew)
		{
			OVR_FREE(pRecvBuff);
			pRecvBuff = NULL;
			pRecvBuffSize = 0;
			recvBuffLock.Unlock();
			return;
		}
		else
		{
			pRecvBuff = pRecvBuffNew;

			memcpy(pRecvBuff + pRecvBuffSize, pData, bytesRead);

			dataSourceSize = pRecvBuffSize + bytesRead;
			dataSource = pRecvBuff;
		}
	}

	int bytesReadFromStream;
	while (bytesReadFromStream = BytesFromStream(dataSource, dataSourceSize),
		   LENGTH_FIELD_BYTES + bytesReadFromStream <= dataSourceSize)
	{
		dataSource += LENGTH_FIELD_BYTES;
		dataSourceSize -= LENGTH_FIELD_BYTES;

		TCPSocket::OnRecv(eventHandler, dataSource, bytesReadFromStream);

		dataSource += bytesReadFromStream;
		dataSourceSize -= bytesReadFromStream;
	}

	if (dataSourceSize > 0)
	{
        if (dataSource != NULL)
        {
            if (pRecvBuff == NULL)
            {
                pRecvBuff = (uint8_t*)OVR_ALLOC(dataSourceSize);
                if (!pRecvBuff)
                {
                    pRecvBuffSize = 0;
                    recvBuffLock.Unlock();
                    return;
                }
                else
                {
                    memcpy(pRecvBuff, dataSource, dataSourceSize);
                }
            }
            else
            {
                memmove(pRecvBuff, dataSource, dataSourceSize);
            }
        }
	}
	else
	{
		if (pRecvBuff != NULL)
			OVR_FREE(pRecvBuff);

		pRecvBuff = NULL;
	}
	pRecvBuffSize = dataSourceSize;

	recvBuffLock.Unlock();
}

int PacketizedTCPSocket::BytesFromStream(uint8_t* pData, int bytesRead)
{
	if (pData != 0 && bytesRead >= LENGTH_FIELD_BYTES)
	{
		return pData[0] | ((uint32_t)pData[1] << 8) | ((uint32_t)pData[2] << 16) | ((uint32_t)pData[3] << 24);
	}

	return 0;
}


}} // OVR::Net