From f02e916f6ceb0369a57b6dfff4e22628b1e796d5 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 18 Jan 2014 15:14:42 -0500 Subject: [PATCH] Initial commit --- limelight-common.sln | 22 ++++ limelight-common/ByteBuffer.cpp | 98 ++++++++++++++++ limelight-common/ByteBuffer.h | 22 ++++ limelight-common/Config.cpp | 106 ++++++++++++++++++ limelight-common/Handshake.cpp | 80 +++++++++++++ limelight-common/Limelight.h | 7 ++ limelight-common/Platform.h | 4 + limelight-common/PlatformSockets.cpp | 22 ++++ limelight-common/PlatformSockets.h | 15 +++ limelight-common/limelight-common.vcxproj | 79 +++++++++++++ .../limelight-common.vcxproj.filters | 20 ++++ 11 files changed, 475 insertions(+) create mode 100644 limelight-common.sln create mode 100644 limelight-common/ByteBuffer.cpp create mode 100644 limelight-common/ByteBuffer.h create mode 100644 limelight-common/Config.cpp create mode 100644 limelight-common/Handshake.cpp create mode 100644 limelight-common/Limelight.h create mode 100644 limelight-common/Platform.h create mode 100644 limelight-common/PlatformSockets.cpp create mode 100644 limelight-common/PlatformSockets.h create mode 100644 limelight-common/limelight-common.vcxproj create mode 100644 limelight-common/limelight-common.vcxproj.filters diff --git a/limelight-common.sln b/limelight-common.sln new file mode 100644 index 0000000..4c77b2b --- /dev/null +++ b/limelight-common.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "limelight-common", "limelight-common\limelight-common.vcxproj", "{15D85395-B083-4688-B578-58AB22C85BE5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {15D85395-B083-4688-B578-58AB22C85BE5}.Debug|Win32.ActiveCfg = Debug|Win32 + {15D85395-B083-4688-B578-58AB22C85BE5}.Debug|Win32.Build.0 = Debug|Win32 + {15D85395-B083-4688-B578-58AB22C85BE5}.Release|Win32.ActiveCfg = Release|Win32 + {15D85395-B083-4688-B578-58AB22C85BE5}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/limelight-common/ByteBuffer.cpp b/limelight-common/ByteBuffer.cpp new file mode 100644 index 0000000..424e414 --- /dev/null +++ b/limelight-common/ByteBuffer.cpp @@ -0,0 +1,98 @@ +#include "ByteBuffer.h" + +void BbInitializeWrappedBuffer(PBYTE_BUFFER buff, char* data, int offset, int length, int byteOrder) { + buff->buffer = data; + buff->offset = offset; + buff->length = length; + buff->position = 0; + buff->byteOrder = byteOrder; +} + +static int byteSwapInt(PBYTE_BUFFER buff, int i) { + if (buff->byteOrder == BYTE_ORDER_BIG) { + return htonl(i); + } + else { + return i; + } +} + +static int byteSwapShort(PBYTE_BUFFER buff, short s) { + if (buff->byteOrder == BYTE_ORDER_BIG) { + return htons(s); + } + else { + return s; + } +} + +int BbGet(PBYTE_BUFFER buff, char *c) { + if (buff->position + sizeof(c) >= buff->length) { + return 0; + } + + memcpy(c, &buff[buff->position], sizeof(*c)); + + return 1; +} + +int BbGetShort(PBYTE_BUFFER buff, short *s) { + if (buff->position + sizeof(s) >= buff->length) { + return 0; + } + + memcpy(s, &buff[buff->position], sizeof(*s)); + + *s = byteSwapShort(buff, *s); + + return 1; +} + +int BbGetInt(PBYTE_BUFFER buff, int *i) { + if (buff->position + sizeof(i) >= buff->length) { + return 0; + } + + memcpy(i, &buff[buff->position], sizeof(*i)); + + *i = byteSwapInt(buff, *i); + + return 1; +} + +int BbPutInt(PBYTE_BUFFER buff, int i) { + if (buff->position + sizeof(i) >= buff->length) { + return 0; + } + + i = byteSwapInt(buff, i); + + memcpy(&buff[buff->position], &i, sizeof(i)); + buff->position += sizeof(i); + + return 1; +} + +int BbPutShort(PBYTE_BUFFER buff, short s) { + if (buff->position + sizeof(s) >= buff->length) { + return 0; + } + + s = byteSwapShort(buff, s); + + memcpy(&buff[buff->position], &s, sizeof(s)); + buff->position += sizeof(s); + + return 1; +} + +int BbPut(PBYTE_BUFFER buff, char c) { + if (buff->position + sizeof(c) >= buff->length) { + return 0; + } + + memcpy(&buff[buff->position], &c, sizeof(c)); + buff->position += sizeof(c); + + return 1; +} \ No newline at end of file diff --git a/limelight-common/ByteBuffer.h b/limelight-common/ByteBuffer.h new file mode 100644 index 0000000..9b40de3 --- /dev/null +++ b/limelight-common/ByteBuffer.h @@ -0,0 +1,22 @@ +#include "Platform.h" + +#define BYTE_ORDER_LITTLE 1 +#define BYTE_ORDER_BIG 2 + +typedef struct _BYTE_BUFFER { + char* buffer; + int offset; + int length; + int position; + int byteOrder; +} BYTE_BUFFER, *PBYTE_BUFFER; + +void BbInitializeWrappedBuffer(PBYTE_BUFFER buff, char* data, int offset, int length, int byteOrder); + +int BbGet(PBYTE_BUFFER buff, char *c); +int BbGetShort(PBYTE_BUFFER buff, short *s); +int BbGetInt(PBYTE_BUFFER buff, int *i); + +int BbPutInt(PBYTE_BUFFER buff, int i); +int BbPutShort(PBYTE_BUFFER buff, short s); +int BbPut(PBYTE_BUFFER buff, char c); \ No newline at end of file diff --git a/limelight-common/Config.cpp b/limelight-common/Config.cpp new file mode 100644 index 0000000..a01a830 --- /dev/null +++ b/limelight-common/Config.cpp @@ -0,0 +1,106 @@ +#include "Limelight.h" + +#include "ByteBuffer.h" + +const int UNKNOWN_CONFIG [] = { + 70151, + 68291329, + 1280, + 68291584, + 1280, + 68291840, + 15360, + 68292096, + 25600, + 68292352, + 2048, + 68292608, + 1024, + 68289024, + 262144, + 17957632, + 302055424, + 134217729, + 16777490, + 70153, + 68293120, + 768000, + 17961216, + 303235072, + 335609857, + 838861842, + 352321536, + 1006634002, + 369098752, + 335545362, + 385875968, + 1042, + 402653184, + 134218770, + 419430400, + 167773202, + 436207616, + 855638290, + 266779, + 7000, + 266780, + 2000, + 266781, + 50, + 266782, + 3000, + 266783, + 2, + 266794, + 5000, + 266795, + 500, + 266784, + 75, + 266785, + 25, + 266786, + 10, + 266787, + 60, + 266788, + 30, + 266789, + 3, + 266790, + 1000, + 266791, + 5000, + 266792, + 5000, + 266793, + 5000, + 70190, + 68301063, + 10240, + 68301312, + 6400, + 68301568, + 768000, + 68299776, + 768, + 68300032, + 2560, + 68300544, + 0, + 34746368, + (int) 0xFE000000 +}; + +const int CONFIG_SIZE = sizeof(UNKNOWN_CONFIG) +(8 * 4) + 3; + +char* allocateConfigDataForStreamConfig(PSTREAM_CONFIGURATION streamConfig) { + BYTE_BUFFER bb; + char* config = (char *)malloc(CONFIG_SIZE); + if (config == NULL) { + return NULL; + } + + BbInitializeWrappedBuffer(&bb, config, 0, CONFIG_SIZE, BYTE_ORDER_LITTLE); + +} diff --git a/limelight-common/Handshake.cpp b/limelight-common/Handshake.cpp new file mode 100644 index 0000000..78c028f --- /dev/null +++ b/limelight-common/Handshake.cpp @@ -0,0 +1,80 @@ + +#include "PlatformSockets.h" + +const char HELLO [] = { + 0x07, 0x00, 0x00, 0x00, + 0x61, 0x6e, 0x64, 0x72, + 0x6f, 0x69, 0x64, 0x03, + 0x01, 0x00, 0x00 +}; + +const char PACKET2 [] = { + 0x01, 0x03, 0x02, 0x00, + 0x08, 0x00 +}; + +const char PACKET3 [] = { + 0x04, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 +}; + +const char PACKET4 [] = { + 0x01, 0x01, 0x00, 0x0 +}; + +static int waitAndDiscardResponse(SOCKET sock) { + char temp[256]; + return recv(sock, temp, sizeof(temp), 0); +} + +int performHandshake(IP_ADDRESS host) { + SOCKET s; + int err; + + s = connectTcpSocket(host, 47991); + if (s == INVALID_SOCKET) { + return LastSocketError(); + } + + err = send(s, HELLO, sizeof(HELLO), 0); + if (err == SOCKET_ERROR) { + goto CleanupError; + } + + err = waitAndDiscardResponse(s); + if (err == SOCKET_ERROR) { + goto CleanupError; + } + + err = send(s, PACKET2, sizeof(PACKET2), 0); + if (err == SOCKET_ERROR) { + goto CleanupError; + } + + err = waitAndDiscardResponse(s); + if (err == SOCKET_ERROR) { + goto CleanupError; + } + + err = send(s, PACKET3, sizeof(PACKET3), 0); + if (err == SOCKET_ERROR) { + goto CleanupError; + } + + err = waitAndDiscardResponse(s); + if (err == SOCKET_ERROR) { + goto CleanupError; + } + + err = send(s, PACKET4, sizeof(PACKET4), 0); + if (err == SOCKET_ERROR) { + goto CleanupError; + } + + closesocket(s); + return 0; + +CleanupError: + closesocket(s); + return LastSocketError(); +} \ No newline at end of file diff --git a/limelight-common/Limelight.h b/limelight-common/Limelight.h new file mode 100644 index 0000000..b95a4b1 --- /dev/null +++ b/limelight-common/Limelight.h @@ -0,0 +1,7 @@ +#include "Platform.h" + +typedef struct _STREAM_CONFIGURATION { + int width; + int height; + int fps; +} STREAM_CONFIGURATION, *PSTREAM_CONFIGURATION; \ No newline at end of file diff --git a/limelight-common/Platform.h b/limelight-common/Platform.h new file mode 100644 index 0000000..d7454b9 --- /dev/null +++ b/limelight-common/Platform.h @@ -0,0 +1,4 @@ +#ifdef _WIN32 +#include +#else +#endif \ No newline at end of file diff --git a/limelight-common/PlatformSockets.cpp b/limelight-common/PlatformSockets.cpp new file mode 100644 index 0000000..d1205f4 --- /dev/null +++ b/limelight-common/PlatformSockets.cpp @@ -0,0 +1,22 @@ +#include "PlatformSockets.h" + +SOCKET connectTcpSocket(IP_ADDRESS dstaddr, unsigned short port) { + SOCKET s; + struct sockaddr_in addr; + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (s == INVALID_SOCKET) { + return INVALID_SOCKET; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + memcpy(&addr.sin_addr, &dstaddr, sizeof(dstaddr)); + if (connect(s, (struct sockaddr*) &addr, sizeof(addr)) == SOCKET_ERROR) { + closesocket(s); + return INVALID_SOCKET; + } + + return s; +} \ No newline at end of file diff --git a/limelight-common/PlatformSockets.h b/limelight-common/PlatformSockets.h new file mode 100644 index 0000000..10c7c0a --- /dev/null +++ b/limelight-common/PlatformSockets.h @@ -0,0 +1,15 @@ + +#ifdef _WIN32 +#include +#define LastSocketError() WSAGetLastError() +#else +#define SOCKET int +#define LastSocketError() errno +#define INVALID_SOCKET -1 +#define SOCKET_ERROR -1 +#define closesocket(x) close(x) +#endif + +#define IP_ADDRESS unsigned int + +SOCKET connectTcpSocket(IP_ADDRESS dstaddr, unsigned short port); \ No newline at end of file diff --git a/limelight-common/limelight-common.vcxproj b/limelight-common/limelight-common.vcxproj new file mode 100644 index 0000000..c69be09 --- /dev/null +++ b/limelight-common/limelight-common.vcxproj @@ -0,0 +1,79 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {15D85395-B083-4688-B578-58AB22C85BE5} + Win32Proj + limelightcommon + + + + StaticLibrary + true + v120 + Unicode + + + StaticLibrary + false + v120 + true + Unicode + + + + + + + + + + + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/limelight-common/limelight-common.vcxproj.filters b/limelight-common/limelight-common.vcxproj.filters new file mode 100644 index 0000000..34c1b07 --- /dev/null +++ b/limelight-common/limelight-common.vcxproj.filters @@ -0,0 +1,20 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + \ No newline at end of file