JD2022-TU1/main/extern/Camcam/LIBS/Mixer/SCENE_FlashCom.cpp

221 lines
4.9 KiB
C++

#include <windows.h>
#include <process.h>
#include "SCENE.h"
#include "account.h"
#ifdef ONLINE_MODE
extern CCamCamAccount Account;
#endif
unsigned int FLASHPORT = 10000;
typedef struct FlashCom_
{
SOCKET FlashSocket;
SOCKET Client;
} FlashCom;
unsigned __stdcall FLASHCOM_DEDICACED_THREAD(void *params);
unsigned __stdcall FLASHCOM_THREAD(void *params)
{
WORLD *pSCENE;
pSCENE = (WORLD *)params;
HANDLE FlashActiveXThreadHandle;
WORD sockVersion;
WSADATA wsaData;
int rVal;
sockVersion = MAKEWORD(1,1);
//start dll
WSAStartup(sockVersion, &wsaData);
pSCENE->pFC->Client = -1;
unsigned int attempt = 0;
while (1)
{
//create socket
//if (!attempt)
//{
pSCENE->pFC->FlashSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(pSCENE->pFC->FlashSocket == INVALID_SOCKET)
{
WSACleanup();
goto SOCKETFAIL;
}
//}
//fill in sockaddr_in struct
SOCKADDR_IN sin;
sin.sin_family = PF_INET;
sin.sin_port = htons(FLASHPORT);
sin.sin_addr.s_addr = INADDR_ANY;
//bind the socket
rVal = bind(pSCENE->pFC->FlashSocket, (LPSOCKADDR)&sin, sizeof(sin));
if(rVal == SOCKET_ERROR)
{
if (attempt == 3)
{
FLASHPORT = 0;
WSACleanup();
goto SOCKETFAIL;
}
attempt ++;
FLASHPORT ++;
continue;
}
//get socket to listen
rVal = listen(pSCENE->pFC->FlashSocket, 2);
if(rVal == SOCKET_ERROR)
{
WSACleanup();
goto SOCKETFAIL;
}
//wait for a client
pSCENE->pFC->Client = accept(pSCENE->pFC->FlashSocket, NULL, NULL);
if(pSCENE->pFC->Client == INVALID_SOCKET)
{
WSACleanup();
goto SOCKETFAIL;
}
int Receive;
u32 LoopOK = 1;
SimpleString<2048> messageReceiv;
while (LoopOK == 1)
{
char BUF[4000];
Receive = recv(pSCENE->pFC->Client,BUF,4000,0);
if (Receive == -1 || Receive == 0)
{
LoopOK = 0;
}
else
{
messageReceiv.clear();
messageReceiv.appendStr((const char*)BUF, Receive);
if (strcmp(messageReceiv.c_str(),"<policy-file-request/>") == 0)
{
SimpleString<2048> policyFile;
policyFile.sprintf("<?xml version=\"1.0\"?>\n<!DOCTYPE cross-domain-policy SYSTEM \" http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\n<cross-domain-policy>\n<allow-access-from domain=\"*\" to-ports=\"%u\" />\n</cross-domain-policy>", FLASHPORT);
send(pSCENE->pFC->Client, policyFile.c_str(), strlen(policyFile.c_str()) +1,0);
} else if (strcmp(messageReceiv.c_str(), "HELLO FLASH\n") == 0)
{
// Init the communication
pSCENE->FlashClient = pSCENE->pFC->Client;
//Flash comm, create a new thread to treat the flash communication
#define THREAD_STACK_SIZE (1024)
FlashActiveXThreadHandle = (HANDLE)_beginthreadex( NULL , THREAD_STACK_SIZE , FLASHCOM_DEDICACED_THREAD , (void*)&pSCENE->FlashClient, 0, NULL);
#ifdef ONLINE_MODE
Account.onFlashMsgReceived((const char*)messageReceiv.c_str());
#endif
LoopOK = 2;
} else if (strcmp(messageReceiv.c_str(), "WHO_ARE_YOU") == 0)
{
// OK, a second instance of youup try to communicate... Just send a response to say "hi"
char TEXT[] = "CCOK";
send(pSCENE->pFC->Client,TEXT,strlen(TEXT) + 1,0);
} else
{
s8* Base = BUF;
while (Base < BUF + Receive)
{
pSCENE->SceneScript_ADD(&pSCENE->SceneScript_Ecluse,Base);
while (*Base && (Base <= BUF + Receive)) {Base++;};
Base++;
}
#ifdef ONLINE_MODE
Account.onFlashMsgReceived((const char*)messageReceiv.c_str());
#endif
}
}
}
if (LoopOK == 0) // If loopOK != 0, we are in a new thread (flash)
closesocket(pSCENE->pFC->Client);
closesocket(pSCENE->pFC->FlashSocket);
pSCENE->pFC->Client = -1;
}
return 0;
SOCKETFAIL:
pSCENE->pFC->FlashSocket = -1;
return 0; //correct ?
} // timeGetGetTime()
unsigned __stdcall FLASHCOM_DEDICACED_THREAD(void *params)
{
SOCKET* Client = (SOCKET*) params;
SimpleString<2048> messageReceiv;
int Receive;
while (1)
{
char BUF[4000];
Receive = recv(*Client,BUF,4000,0);
if (Receive == -1 || Receive ==0)
break;
else
{
messageReceiv.clear();
messageReceiv.appendStr((const char*)BUF, Receive);
#ifdef ONLINE_MODE
Account.onFlashMsgReceived((const char*)messageReceiv.c_str());
#endif
}
Sleep(1);
}
closesocket(*Client);
*Client = -1;
return 0;
}
void WORLD::FlashCom_Send(char *Text, int client)
{
int usedClient = client > 0 ? client : pFC->Client;
if (usedClient != -1)
{
send(usedClient ,Text,strlen(Text) + 1,0);
}
}
void WORLD::FlashCom_Init()
{
pFC = (FlashCom *)CC_malloc(sizeof(FlashCom));
memset(pFC,0,sizeof(FlashCom));
#define THREAD_STACK_SIZE (1024)
FlashthreadHandle = (HANDLE)_beginthreadex( NULL , THREAD_STACK_SIZE , FLASHCOM_THREAD , this , 0 , &FlashthreadID );
//SetThreadPriority((HANDLE)FlashthreadID,THREAD_PRIORITY_HIGHEST);
}
void WORLD::FlashCom_OneFrame()
{
if(pFC->FlashSocket == -1) return;
}
void WORLD::FlashCom_Destroy()
{
CC_free(pFC);
}