[ create a new paste ] login | about

Link: http://codepad.org/MvOYdUD7    [ raw code | fork ]

C, pasted on Oct 21:
#include <winsock2.h>


/*
	EPOpen()
	Must be called prior to all other functions in the module.
	Sets up the handler for ctrl-C termination.
	Opens the UDP socket for asynchronous messages from the stack to Lua.
	Sets the panic function to try and cleanly terminate when there is is script error.
	Registers the callback function that sends the asynchronous UDP notifications.
	Initializes the stack.
	Starts the stack update thread.
*/
static INT32 EPOpen( lua_State *L )
{
    UINT32   lThreadId;
    HANDLE   hThread;    
	unsigned long uset = 1;
	int		err;

	gpLuaState = L;	

	if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) == FALSE )
	{
		printf( "\nERROR: Could not set control handler"); 
	} 
	else
	{
		/* Return in error if Windows Sockets version 2.2 is not supported. */
		wVersionRequested = MAKEWORD( 2, 2 );
		if ( ( err = WSAStartup( wVersionRequested, &wsaData ) ) != 0 )
		{
			printf( "Error in WSAStartup = %d\n", err );
			return 0;
		}

		/* Confirm that the WinSock DLL supports 2.2.        */
		/* Note that if the DLL supports versions greater    */
		/* than 2.2 in addition to 2.2, it will still return */
		/* 2.2 in wVersion since that is the version we      */
		/* requested.                                        */
		if ( LOBYTE( wsaData.wVersion ) != 2 ||
			HIBYTE( wsaData.wVersion ) != 2 )
		{
			/* Tell the user that we could not find a usable */
			/* WinSock DLL.                                  */
			printf( "Error: Wrong WinSock version = 0x%x\n", wsaData.wVersion );
			WSACleanup( );
			return 0;
		}
		
		// Create a socket for sending data
		udpSockFD = socket(AF_INET, SOCK_DGRAM, 0);
		if (udpSockFD == INVALID_SOCKET) {
			printf("socket failed with error: %ld\n", WSAGetLastError());
			WSACleanup();
			return 0;
		}

		xmtSockaddr.sin_family = AF_INET;
		xmtSockaddr.sin_port = htons( PRODUCED_PORT );
		xmtSockaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );

		/* Display version of WinSock used just to indicate successful open. */
		if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
		{
			printf( "!! WinSock version = %d.%d\n",
				LOBYTE( wsaData.wVersion ),
				HIBYTE( wsaData.wVersion ) );
		}

		lua_atpanic(L, EPScriptShutdown);
		clientRegisterEventCallBack( fnCallback );
		clientStart();
		scannerStart();
		hThread = (HANDLE)CreateThread(NULL, 
								  (unsigned)0,
								  (LPTHREAD_START_ROUTINE)EIPTask, 
								  NULL, 
								  (unsigned)0, 
								  (unsigned long*)&lThreadId );

		if ( !hThread )
		{
			printf( "\nERROR: Could not start EIPTask thread"); 
		} 
	}

	return 0;
}

//*#---------------------------------------------------------------------------
// Function: fnCallback()
// 
// Description: Callback function that will be called when the scanner has some 
// notification or an error to post.
//*#---------------------------------------------------------------------------
void ET_IP_CALLBACK fnCallback( int nEvent, int nParam1, int nParam2 )
{   
    int iResult;
	char msg[80];

	// Format the event data in a text string to be sent for decoding by Lua
	sprintf(msg,"%d,%d,%d", nEvent, nParam1, nParam2);

	// Send the packet
	iResult = sendto( udpSockFD,
		(const char *)msg,
		strlen((const char *)msg),
		0,
		(struct sockaddr *)&xmtSockaddr,
		sizeof( struct sockaddr ) );

    if (iResult == SOCKET_ERROR) {
		sprintf(msg,"sendto failed with error: %d\n", WSAGetLastError());
        LuaLogError(gpLuaState, msg);
	}
}


Create a new paste based on this one


Comments: