示例#1
0
int main(int argc,char **argv){
	signal(SIGPIPE,SIG_IGN);	
	//create ioworker and logicprocessor
	logicprocessor_t logic = create_logic(on_new_connection,on_packet,on_disconnected);
	_worker = ioworker_new(logic);
	//run net worker thread
	ioworker_startrun(_worker);
	//run logic process thread
	logic_startrun(logic);

	//start listen
	engine_t p = kn_new_engine();
	kn_sockaddr local;
	kn_addr_init_in(&local,argv[1],atoi(argv[2]));
	
	handle_t l = kn_new_sock(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(0 == kn_sock_listen(l,&local)){
		kn_engine_associate(p,l,on_accept);
		kn_engine_run(p);
	}
	return 0;
}
示例#2
0
void GLogicEngineCPP::InitLogicLibrary()
{
	logic_library_copy_path = "";
	logic_library_handle = NULL;

#ifdef WIN32

	char * dll_path, tmp_path[MAX_PATH + 100], tmp_name[MAX_PATH + 100];
	dll_path = (char*)global_serverconfig->scripting_config.Cpp.lib_path.c_str();
	if (GetTempPathA(MAX_PATH, tmp_path))
	{
		INT32 retries = 100;
		while (--retries > 0)
		{
			sprintf(tmp_name, "%s%08d.dll", tmp_path, truerng.Rnd(100 * 1000000));
			FILE * plik = fopen(tmp_name, "rb");
			if (plik == NULL)
			{
				break;
			}
			fclose(plik);
			*tmp_name = 0;
		}
		if (*tmp_name && CopyFileA(dll_path, tmp_name, true))
		{
			dll_path = tmp_name;
			logic_library_copy_path = dll_path;
		}
	}

	logic_library_handle = (void *)LoadLibraryA(dll_path);
	if (logic_library_handle)
	{
		*(FARPROC *)&create_logic = GetProcAddress((HINSTANCE)logic_library_handle, "create_logic");
		*(FARPROC *)&destroy_logic = GetProcAddress((HINSTANCE)logic_library_handle, "destroy_logic");
	}
	if (!logic_library_handle && logic_library_copy_path != "")
	{
		DeleteFileA(logic_library_copy_path.c_str());
		logic_library_copy_path = "";
	}

#else

	char * dll_path, tmp_name[110];
	dll_path = (char*)global_serverconfig->scripting_config.Cpp.lib_path.c_str();
	strcpy(tmp_name, "/tmp/logicXXXXXX");
	int dst_fd = mkstemp(tmp_name);
	if (dst_fd != -1)
	{
		FILE * src, * dst;
		src = fopen(dll_path, "rb");
		if (src)
		{
			dst = fdopen(dst_fd, "wb");
			if (dst)
			{
				char buffer[64000];
				for (;;)
				{
					int count = fread(buffer, 1, sizeof(buffer), src);
					if (count == 0)
					{
						break;
					}
					fwrite(buffer, 1, count, dst);
				}

				dll_path = tmp_name;
				logic_library_copy_path = dll_path;
			}
			fclose(src);
		}
		close(dst_fd);
	}

	logic_library_handle = dlopen(dll_path, RTLD_LAZY);
	if (logic_library_handle)
	{
		*(void **)&create_logic = (void *)dlsym(logic_library_handle, "create_logic");
		*(void **)&destroy_logic = (void *)dlsym(logic_library_handle, "destroy_logic");
	}
	else
	{
		char temp_string[1024];
		SNPRINTF(temp_string, sizeof(temp_string), "dlerror(): %s", dlerror());
		temp_string[sizeof(temp_string) - 1] = 0;
		Log(temp_string);
	}

	if (!logic_library_handle && logic_library_copy_path != "")
	{
		unlink(logic_library_copy_path.c_str());
		logic_library_copy_path = "";
	}

#endif

	if (!logic_library_handle)
	{
		string err_str = str(boost::format("Error loading logic library: %1%") % global_serverconfig->scripting_config.Cpp.lib_path);
		Log(err_str.c_str());
	}

	logic_base = NULL;
	if (create_logic)
	{
		logic_base = create_logic(this);
	}
}