From 61ff7e1d88312918a7f96ae074c2a418590c09b1 Mon Sep 17 00:00:00 2001 From: David Roth Date: Wed, 8 Jan 2020 13:57:35 +1100 Subject: [PATCH] Pushing up initial TCPwork --- WindowsSendCode/CommonTypes.h | 2 + WindowsSendCode/Debug/PixelShader.h | 42 +++---- WindowsSendCode/Debug/VertexShader.h | 38 +++---- WindowsSendCode/DesktopDuplication.cpp | 148 +++++++++++++++++++++++-- 4 files changed, 179 insertions(+), 51 deletions(-) diff --git a/WindowsSendCode/CommonTypes.h b/WindowsSendCode/CommonTypes.h index ee6fd9e..2983732 100644 --- a/WindowsSendCode/CommonTypes.h +++ b/WindowsSendCode/CommonTypes.h @@ -11,6 +11,7 @@ #pragma comment(lib, "Ws2_32.lib") // DITY BOI #include +#include #include @@ -138,4 +139,5 @@ typedef struct _VERTEX DirectX::XMFLOAT2 TexCoord; } VERTEX; + #endif diff --git a/WindowsSendCode/Debug/PixelShader.h b/WindowsSendCode/Debug/PixelShader.h index e3cb05b..2fb3fa0 100644 --- a/WindowsSendCode/Debug/PixelShader.h +++ b/WindowsSendCode/Debug/PixelShader.h @@ -64,10 +64,10 @@ ret const BYTE g_PS[] = { - 68, 88, 66, 67, 165, 210, - 114, 199, 188, 79, 71, 215, - 213, 121, 245, 47, 45, 7, - 17, 194, 1, 0, 0, 0, + 68, 88, 66, 67, 125, 180, + 16, 193, 38, 16, 77, 45, + 11, 191, 199, 57, 35, 204, + 24, 24, 1, 0, 0, 0, 56, 58, 0, 0, 7, 0, 0, 0, 60, 0, 0, 0, 32, 2, 0, 0, 140, 2, @@ -601,11 +601,11 @@ const BYTE g_PS[] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 46, - 49, 1, 218, 217, 17, 94, - 1, 0, 0, 0, 242, 102, - 178, 93, 184, 78, 194, 78, - 175, 229, 99, 234, 16, 217, - 98, 231, 0, 0, 0, 0, + 49, 1, 28, 51, 19, 94, + 1, 0, 0, 0, 148, 181, + 6, 170, 44, 239, 134, 78, + 151, 47, 239, 107, 68, 202, + 25, 168, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -776,9 +776,9 @@ const BYTE g_PS[] = 3, 0, 162, 202, 1, 0, 38, 247, 2, 0, 65, 36, 1, 0, 39, 82, 3, 0, - 67, 34, 2, 0, 217, 42, + 177, 210, 1, 0, 217, 42, 2, 0, 21, 230, 0, 0, - 65, 79, 0, 0, 73, 20, + 187, 121, 0, 0, 73, 20, 1, 0, 153, 189, 3, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -1114,7 +1114,7 @@ const BYTE g_PS[] = 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 27, 226, 48, 1, 128, 0, 0, 0, - 38, 66, 134, 173, 197, 195, + 116, 42, 222, 119, 147, 196, 213, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -1405,14 +1405,14 @@ const BYTE g_PS[] = 5, 16, 0, 0, 23, 0, 1, 0, 4, 16, 0, 0, 14, 0, 23, 21, 0, 16, - 0, 0, 3, 2, 112, 0, + 0, 0, 3, 2, 176, 2, 0, 0, 242, 241, 10, 0, 24, 21, 7, 16, 0, 0, 1, 0, 1, 0, 10, 0, 24, 21, 8, 16, 0, 0, 1, 0, 0, 2, 14, 0, 23, 21, 0, 0, 0, 0, - 10, 2, 112, 0, 0, 0, + 10, 2, 176, 2, 0, 0, 242, 241, 10, 0, 24, 21, 10, 16, 0, 0, 1, 0, 1, 0, 10, 0, 24, 21, @@ -1980,19 +1980,19 @@ const BYTE g_PS[] = 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 32, 0, 0, 96, - 0, 0, 0, 0, 0, 0, + 0, 0, 25, 168, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 9, 0, 20, 2, 0, 0, 0, 0, 0, 0, 124, 0, 0, 0, 1, 0, - 0, 0, 8, 105, 162, 0, + 0, 0, 24, 22, 215, 2, 0, 0, 0, 0, 0, 0, 0, 0, 80, 83, 0, 110, 111, 110, 101, 0, 45, 186, 46, 241, 1, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 32, 0, 0, 96, - 0, 0, 0, 0, 0, 0, + 0, 0, 25, 168, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 7, 0, 0, 0, 0, 0, 1, 0, @@ -2052,10 +2052,10 @@ const BYTE g_PS[] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 46, 49, 1, - 218, 217, 17, 94, 1, 0, - 0, 0, 242, 102, 178, 93, - 184, 78, 194, 78, 175, 229, - 99, 234, 16, 217, 98, 231, + 28, 51, 19, 94, 1, 0, + 0, 0, 148, 181, 6, 170, + 44, 239, 134, 78, 151, 47, + 239, 107, 68, 202, 25, 168, 120, 0, 0, 0, 47, 76, 105, 110, 107, 73, 110, 102, 111, 0, 47, 110, 97, 109, diff --git a/WindowsSendCode/Debug/VertexShader.h b/WindowsSendCode/Debug/VertexShader.h index 1b27f73..8869252 100644 --- a/WindowsSendCode/Debug/VertexShader.h +++ b/WindowsSendCode/Debug/VertexShader.h @@ -63,10 +63,10 @@ ret const BYTE g_VS[] = { - 68, 88, 66, 67, 222, 196, - 209, 20, 126, 248, 195, 193, - 172, 106, 106, 176, 198, 80, - 143, 194, 1, 0, 0, 0, + 68, 88, 66, 67, 254, 1, + 116, 196, 35, 169, 233, 235, + 147, 29, 112, 164, 193, 78, + 23, 248, 1, 0, 0, 0, 144, 58, 0, 0, 7, 0, 0, 0, 60, 0, 0, 0, 164, 2, 0, 0, 20, 3, @@ -623,10 +623,10 @@ const BYTE g_VS[] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 46, 49, 1, - 218, 217, 17, 94, 1, 0, - 0, 0, 244, 210, 224, 193, - 46, 116, 241, 78, 146, 231, - 164, 180, 197, 133, 56, 223, + 29, 51, 19, 94, 1, 0, + 0, 0, 201, 42, 70, 62, + 184, 65, 166, 64, 128, 220, + 243, 120, 153, 125, 192, 173, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, @@ -1135,8 +1135,8 @@ const BYTE g_VS[] = 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 27, 226, 48, 1, - 128, 0, 0, 0, 114, 248, - 181, 173, 197, 195, 213, 1, + 128, 0, 0, 0, 191, 30, + 12, 120, 147, 196, 213, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -1905,7 +1905,7 @@ const BYTE g_VS[] = 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 15, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 26, 9, 47, 241, 0, 0, 0, 0, @@ -2002,19 +2002,19 @@ const BYTE g_VS[] = 1, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 0, 32, 0, 0, 96, 0, 0, - 0, 0, 0, 0, 0, 0, + 192, 173, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 9, 0, 68, 2, 0, 0, 0, 0, 0, 0, 148, 0, 0, 0, 1, 0, 0, 0, - 112, 137, 44, 3, 0, 0, + 192, 12, 110, 3, 0, 0, 0, 0, 0, 0, 0, 0, 86, 83, 0, 110, 111, 110, 101, 0, 45, 186, 46, 241, 1, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 0, 32, 0, 0, 96, 0, 0, - 0, 0, 0, 0, 0, 0, + 192, 173, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 7, 0, 0, 0, 0, 0, 1, 0, 255, 255, @@ -2073,11 +2073,11 @@ const BYTE g_VS[] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 148, 46, 49, 1, 218, 217, - 17, 94, 1, 0, 0, 0, - 244, 210, 224, 193, 46, 116, - 241, 78, 146, 231, 164, 180, - 197, 133, 56, 223, 121, 0, + 148, 46, 49, 1, 29, 51, + 19, 94, 1, 0, 0, 0, + 201, 42, 70, 62, 184, 65, + 166, 64, 128, 220, 243, 120, + 153, 125, 192, 173, 121, 0, 0, 0, 47, 76, 105, 110, 107, 73, 110, 102, 111, 0, 47, 110, 97, 109, 101, 115, diff --git a/WindowsSendCode/DesktopDuplication.cpp b/WindowsSendCode/DesktopDuplication.cpp index f5d61a6..96941d9 100644 --- a/WindowsSendCode/DesktopDuplication.cpp +++ b/WindowsSendCode/DesktopDuplication.cpp @@ -723,8 +723,15 @@ void DisplayMsg(_In_ LPCWSTR Str, _In_ LPCWSTR Title, HRESULT hr) void InitWinsock() { + char str[8192]; WSADATA wsaData; - WSAStartup(MAKEWORD(2, 2), &wsaData); + int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (iResult != 0) + { + sprintf(str, "WSAStartup failed with error: %d\n", iResult); + printfN(str); + } + } @@ -737,16 +744,16 @@ char *grab_chunk() } -int socket_server_main(void **datahere) +int socket_server_main(void **datahere, int port) { SOCKET socketS; - InitWinsock(); + struct sockaddr_in local; struct sockaddr_in from; int fromlen = sizeof(from); local.sin_family = AF_INET; - local.sin_port = htons(6969); + local.sin_port = htons(port); local.sin_addr.s_addr = INADDR_ANY; socketS = socket(AF_INET, SOCK_DGRAM, 0); @@ -773,25 +780,46 @@ int socket_server_main(void **datahere) unsigned int width = 640; unsigned int height = 480; unsigned int bytedepth = 4; + + + int monitor_count_x = 3; + int monitor_count_y = 3; + unsigned int* monitorXOff = (unsigned int *)malloc(sizeof (unsigned int) * monitor_count_x); + unsigned int* monitorYOff = (unsigned int *)malloc(sizeof (unsigned int) * monitor_count_y); + monitorXOff[0] = 0; + monitorXOff[1] = 640; + monitorXOff[2] = 1280; + monitorYOff[0] = 0; + monitorYOff[1] = 480; + monitorYOff[2] = 480; + + + int monitorXindex = 0; + int monitorYindex = 0; + int sendSteps = 24; int sendSize = (width * height * bytedepth)/sendSteps; int sendSleep = 1; char* resizedFrame = (char*)malloc(width*height*bytedepth+1); + sprintf(str, "Waiting for connection...\n"); + printfN(str); while (true) { + sprintf(str, "innaloooopp\n"); + //printfN(str); char buffer[BUFSIZE]; ZeroMemory(buffer, sizeof(buffer)); - sprintf(str, "Waiting for connection...\n"); - printfN(str); if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR) { Data = (FRAME_DATA*)*datahere; // TODO: FIX - sprintf(str, "Received message from %s: %s;;; wil send them data at %p\n", inet_ntoa(from.sin_addr), buffer, Data->cpu_frame); - printfN(str); + sprintf(str, "Received message from %s: %s;;; wil send them data at %p\n", inet_ntoa(from.sin_addr), buffer, Data->cpu_frame); // TODO: Events like admin prompt, and sleep change Data pointer, check for somehow + //printfN(str); // First create our 640*480 rect for (unsigned int i = 0; i < height; i++) - memcpy(resizedFrame + (width * i * bytedepth), (const char*)Data->cpu_frame + ((serverwidth * bytedepth) * i), width * bytedepth); + memcpy(resizedFrame + (width * i * bytedepth), + ((const char*)Data->cpu_frame + ((serverwidth * bytedepth) * (i+monitorYOff[monitorYindex]))) + (bytedepth * monitorXOff[monitorXindex]), + width * bytedepth); int sentb; for (int i = 0; i < sendSteps; i++) @@ -817,9 +845,21 @@ int socket_server_main(void **datahere) //sendto(socketS, (const char*)buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen); sprintf(str, "Sent! Sent bytes: %d\n", sentb); - printfN(str); + //printfN(str); + } + else + { + wchar_t* s = NULL; + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, WSAGetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPWSTR)&s, 0, NULL); + sprintf(str, "\nSOCKET ERROR: %S\n", s); + printfN(str); + LocalFree(s); } + //Sleep(50); } @@ -828,10 +868,96 @@ int socket_server_main(void **datahere) return 0; } + + +#define DEFAULT_BUFLEN 2048 +int socket_setup_thread(int port) +{ + + char str[8192]; + + struct setupData + { + int width; + int height; + int port; + int id; + int maxerrors; + }; + struct setupData setupInfo; + setupInfo.width = 640; + setupInfo.height = 480; + setupInfo.port = 6970; + setupInfo.id = 1; + setupInfo.maxerrors = 8; + + SOCKET s, new_socket; + struct sockaddr_in server, client; + int c; + char* message; + + //Create a socket + if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) + { + printf("Could not create socket : %d \n", WSAGetLastError()); + } + + printf("Socket created.\n"); + + //Prepare the sockaddr_in structure + server.sin_family = AF_INET; + server.sin_addr.s_addr = INADDR_ANY; + server.sin_port = htons(port); + + //Bind + if (bind(s, (struct sockaddr*) &server, sizeof(server)) == SOCKET_ERROR) + { + sprintf(str, "Bind failed with error code : %d\n", WSAGetLastError()); + printfN(str); + } + + sprintf(str, "Bind done!\n"); + printfN(str); + + + + while (true) + { + //Listen to incoming connections + listen(s, 3); + + //Accept and incoming connection + sprintf(str, "Waiting for incoming connections...\n\n"); + printfN(str); + + c = sizeof(struct sockaddr_in); + new_socket = accept(s, (struct sockaddr*) & client, &c); + if (new_socket == INVALID_SOCKET) + { + sprintf(str, "Accept failed with error code : %d\n", WSAGetLastError()); + printfN(str); + } + + sprintf(str, "Connection accepted\n"); + printfN(str); + + send(new_socket, (const char*)&setupInfo, sizeof(struct setupData), 0); + closesocket(new_socket); + } + + return 0; +} + + void testThread(int id, void **data) { char str[1024]; sprintf_s(str, "Firing up socket thread: %d \n", id); printfN(str); - socket_server_main(data); + + // First of all, fireup winsock + InitWinsock(); + + //socket_setup_thread(6969); + socket_server_main(data, 6969); }