Exemple #1
0
CSoundManager::CSoundManager()
	: m_Context(nullptr), m_Device(nullptr), m_ALSourceBuffer(nullptr),
	m_CurrentTune(nullptr), m_CurrentEnvirons(nullptr),
	m_Worker(nullptr), m_DistressMutex(), m_PlayListItems(nullptr), m_SoundGroups(),
	m_Gain(.5f), m_MusicGain(.5f), m_AmbientGain(.5f), m_ActionGain(.5f), m_UIGain(.5f),
	m_Enabled(false), m_BufferSize(98304), m_BufferCount(50),
	m_SoundEnabled(true), m_MusicEnabled(true), m_MusicPaused(false),
	m_AmbientPaused(false), m_ActionPaused(false),
	m_RunningPlaylist(false), m_PlayingPlaylist(false), m_LoopingPlaylist(false),
	m_PlaylistGap(0), m_DistressErrCount(0), m_DistressTime(0)
{
	CFG_GET_VAL("sound.mastergain", m_Gain);
	CFG_GET_VAL("sound.musicgain", m_MusicGain);
	CFG_GET_VAL("sound.ambientgain", m_AmbientGain);
	CFG_GET_VAL("sound.actiongain", m_ActionGain);
	CFG_GET_VAL("sound.uigain", m_UIGain);

	AlcInit();

	if (m_Enabled)
	{
		SetMasterGain(m_Gain);
		InitListener();

		m_PlayListItems = new PlayList;
	}

	if (!CXeromyces::AddValidator(g_VFS, "sound_group", "audio/sound_group.rng"))
		LOGERROR("CSoundManager: failed to load grammar file 'audio/sound_group.rng'");

	RegisterFileReloadFunc(ReloadChangedFileCB, this);
}
Exemple #2
0
void InitSockets() {
	// Init socket.    
	// ---------------------------
	out.sun_family = AF_UNIX;
	strcpy(out.sun_path, sckInAddr);
	sckOut = socket(AF_UNIX, SOCK_DGRAM, 0);

	InitListener();
	// ---------------------------
}
/* Create socket according to local ip-address */
int CXtNetUdpAcceptor::Init( void *arg )
{
	#define cfg		((xt_acceptor_cfg_t*)arg)
	#define local_address		cfg->local_addr
	#define conn_pool			cfg->conn_pool

	int ret = FUN_RET_OK;

	m_event_info.fd = InitListener( local_address );
	if ( m_event_info.fd == INVALID_SOCKET )
	{
		return -4;
	}

	m_event_info.events = (EV_READ|EV_WRITE|EV_PERSIST); /* <==> AddEvents( EV_READ|EV_WRITE|EV_PERSIST ); */

	m_pConnPool = conn_pool;

	return FUN_RET_OK;
}
Exemple #4
0
int DoInitialize()
{
	cout << "Version:" << XVERSIONNO << endl;

	int iThreadPoolSize = 0;
	char *pThreadPoolSize = NULL;
#ifdef _WIN32
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#else
	signal(SIGCLD, SIG_IGN);
	signal(SIGPIPE, SIG_IGN);
	signal(SIGINT, exit);
	signal(SIGKILL, exit);
	signal(SIGTERM, exit);
	signal(SIGQUIT, exit);
#endif

	char szFileName[256 + 1] = { 0 };
#ifdef _WIN32
	DWORD dwResult = GetModuleFileName(NULL, szFileName, 256);
	if (dwResult <= 0 || dwResult > 256)
	{
		cout << "Windows GetModuleFileName Fail" << endl;
		return X_FAILURE;
	}
	g_xAppConfigInfo.m_strFileName = szFileName;
	int iTemp = g_xAppConfigInfo.m_strFileName.find_last_of("\\") + 1;
	g_xAppConfigInfo.m_strPath = g_xAppConfigInfo.m_strFileName.substr(0, iTemp);
	g_xAppConfigInfo.m_strFile = g_xAppConfigInfo.m_strFileName.substr(iTemp);
#else
	int iPathLen = readlink("/proc/self/exe", szFileName, 256);
	if (iPathLen <= 0 || iPathLen > 256)
	{
		cout << "Linux readlink Fail" << endl;
		return X_FAILURE;
	}
	g_xAppConfigInfo.m_strFileName = szFileName;
	int iTemp = g_xAppConfigInfo.m_strFileName.find_last_of("/") + 1;
	g_xAppConfigInfo.m_strPath = g_xAppConfigInfo.m_strFileName.substr(0, iTemp);
	g_xAppConfigInfo.m_strFile = g_xAppConfigInfo.m_strFileName.substr(iTemp);
#endif
	cout << "FileName:" + g_xAppConfigInfo.m_strFileName << endl;
	cout << "Path:" + g_xAppConfigInfo.m_strPath << endl;
	cout << "File:" + g_xAppConfigInfo.m_strFile << endl;

	int iResult = X_SUCCESS;
	//初始化日志
	iResult = XLogClass::Initialize(g_xAppConfigInfo.m_strPath, g_xAppConfigInfo.m_strFile, log4cpp::Priority::DEBUG);
	if (iResult != X_SUCCESS)
	{
		cout << "XLogClass::Initialize Fail" << endl;
		return X_FAILURE;
	}

	//读取配置
	iResult = XConfig::ReadConfig();
	if (iResult != X_SUCCESS)
	{
		XLogClass::error("XConfig::ReadConfig Fail");
		return X_FAILURE;
	}

	//设置配置中的日志级别
	XLogClass::SetPriority(g_xAppConfigInfo.m_xLoggerInfo.m_iLogPriority);

	//初始化线程池,要环境变量增加 UV_THREADPOOL_SIZE
	pThreadPoolSize = getenv("UV_THREADPOOL_SIZE");
	if (pThreadPoolSize != NULL)
		iThreadPoolSize = atoi(pThreadPoolSize);
	else
		iThreadPoolSize = 0;
	cout << "Before Set ThreadPoolSize:" << iThreadPoolSize << endl;
	if (g_xAppConfigInfo.m_xThreadPoolInfo.m_szSize > 0 && g_xAppConfigInfo.m_xThreadPoolInfo.m_szSize != (size_t)iThreadPoolSize)
	{
		string strSize = "UV_THREADPOOL_SIZE=" + convert<string>(g_xAppConfigInfo.m_xThreadPoolInfo.m_szSize);
		iResult = putenv((char *)strSize.c_str());
		if (iResult != X_SUCCESS)
		{
			XLogClass::error("ThreadPool Initialize Fail");
		}
	}

	pThreadPoolSize = getenv("UV_THREADPOOL_SIZE");
	if (pThreadPoolSize != NULL)
		iThreadPoolSize = atoi(pThreadPoolSize);
	else
		iThreadPoolSize = 0;
	cout << "After Set ThreadPoolSize:" << iThreadPoolSize << endl;

	InitOpensslLocks();
	cout << "InitOpensslLocks OK" << endl;

	//_CrtDumpMemoryLeaks();
	//初始化TcpClient连接
	iResult = InitTcpClientPool();
	if (iResult != X_SUCCESS)
	{
		XLogClass::error("InitTcpClientPool Fail");
	}

	//初始化数据库连接
	iResult = InitDataBase();
	if (iResult != X_SUCCESS)
	{
		XLogClass::error("InitDataBase Fail");
		return X_FAILURE;
	}

	iResult = InitService();
	if (iResult != X_SUCCESS)
	{
		XLogClass::error("InitService Fail");
		return X_FAILURE;
	}

	iResult = InitListener();
	if (iResult != X_SUCCESS)
	{
		XLogClass::error("InitListener Fail");
		return X_FAILURE;
	}

	//ReleaseOpensslLocks();
	return X_SUCCESS;
}
Exemple #5
0
/* InvokeXLISP - invoke the xlisp interpreter */
int InvokeXLISP(xlCallbacks *callbacks,int argc,char *argv[])
{
    xlSubrDef *sdp;
    xlXSubrDef *xsdp;
    
    /* initialize xlisp */
    if (!xlInit(callbacks,argc,argv,NULL)) {
        Error("initializing xlisp");
        return FALSE;
    }

    /* set our platform type */
    xlSetSoftwareType("WIN95");

    /* add all of our functions */
    for (sdp = subrtab; sdp->name != NULL; ++sdp)
        xlSubr(sdp->name,sdp->subr);
    for (xsdp = xsubrtab; xsdp->name != NULL; ++xsdp)
        xlXSubr(xsdp->name,xsdp->subr);

    /* initialize the widget classes */
    InitWidgets();

    /* initialize the window classes */
    InitWindows();

    /* initialize the canvas class */
    InitCanvas();
    
    /* initialize the menu classes */
    InitMenus();

    /* initialize the edit classes */
    InitEdit();

    /* initialize the toolbar routines */
    InitToolbars();

    /* initialize the accelerator routines */
    InitAccelerators();

    /* initialize the listener routines */
    InitListener();

    /* initialize the miscellaneous routines */
    InitMisc();

    /* setup the *command-line* variable */
    xlVal = xlMakeCString((char *)GetCommandLine());
    xlSetValue(xlEnter("*COMMAND-LINE*"),xlVal);

    /* load the lisp code */
    xlLoadFile("xlispw.lsp");
    
    /* display the banner */
    xlInfo("%s",xlBanner());
    
    /* display the initial prompt */
    xlCallFunctionByName(NULL,0,"LISTENER-PROMPT",0);

    /* flush terminal output */
    xlosFlushOutput();

    /* return successfully */
    return TRUE;
}
int main(int argc, char** argv)
{
    char* host;
    char* filename_to_request;
    unsigned char* req;
    char* filename_of_output;
    char* data;
    char* received;
    short server_port, listen_port;
    short num_connections;
    short temp;
    unsigned char temp_bytes[2];
    SocketHandle s;
    SocketHandle listener;
    FileReceiver* fr;
    int done = 0;
    int size = 0;
    int i;
    int fail = 0;
    int fail_start = -1;
    int fail_len = -1;
    FILE* f;
    
    if(argc < 7)
    {
        printf("Usage %s host server_port listen_port num_connections filename_to_request filename_of_output\n", argv[0]);
        exit(-1);
    }
    
    host = argv[1];
    server_port = atoi(argv[2]);
    listen_port = atoi(argv[3]);
    num_connections = atoi(argv[4]);
    filename_to_request = argv[5];
    filename_of_output = argv[6];
    
    size = (4 + strlen(filename_to_request) + 1);
    req = malloc(size);

    listener = InitListener("localhost", listen_port);
    
    
    printf("CLIENT NUM_CONN = %d PORT = %d\r\n", num_connections, listen_port);
    
    temp = htons(num_connections);
    req[1] = (temp & 0xFF00) >> 8;
    req[0] = (temp & 0x00FF);
    printf("CLIENT NUM_CONN HEX = 0x%X 0x%X\r\n", req[0], req[1]);
    
    send(s, &temp_bytes, 2, 0); 
    temp = htons(listen_port);
    req[3] = (temp & 0xFF00) >> 8;
    req[2] = (temp & 0x00FF);
    printf("CLIENT PORT HEX = 0x%X 0x%X\r\n", req[2], req[3]);

    printf("Filename %s\r\n", filename_to_request);
    strcpy((char*) (&req[4]), filename_to_request);    
    printf("REQUEST+4 '%s'\r\n", &(req[4]));
    
    s = InitClient(host, server_port);
    for(i =0; i < size; i++)
    {
        printf("0x%X ", req[i]);
    }
    printf("\n");
    send(s, req, size, 0);
    
    printf("Sent the stuff to the serrrrver.\r\n");
    
    fr = FileReceiverNew(filename_to_request, num_connections, listener);
    while(fr->connection_ended < num_connections && FileReceiverUpdate(fr))
    {
    }
    printf("Transmission ended: %d closed of %d expected\r\n", fr->connection_ended, num_connections);
    
    size = 0;
    for(i = 0; i < num_connections; i++)
    {
        /* First sizeof(int) bytes correspond to the file offset */
        if(fr->bytes_read[i] >= sizeof(int) && fr->file_offset[i] == -1)
        {
            fr->file_offset[i] = ntohl(*((int*) &(fr->chunk[i]->data[0])));
        }
    
        fr->bytes_read[i] -= sizeof(int);
        
        if(fr->bytes_read[i] >= 0)
        {            
            printf("%d: %d bytes of the file\r\n", i, fr->bytes_read[i]);
            /*size += fr->bytes_read[i];*/
        }
        else
        {
            printf("%d: closed prematurely\r\n", i);
        }
        
        fr->bytes_read[i] = fr->bytes_read[i] < 0 ? 0 : fr->bytes_read[i];
        size = (size < fr->file_offset[i] + fr->bytes_read[i]) ? fr->file_offset[i] + fr->bytes_read[i] : size;
        
    }
    printf("\r\n\r\nData buffer is %d bytes.\r\n\r\n", size);
    
    /* Nothing received. */
    if(size == 0)
    {
        printf("Server never sent the file in time.\r\nPlease try again, after checking the server is still operational.\r\n");
        
        close(s);
        exit(-1);        
    }
    
    /* Thing to fill */
    data = malloc(size);
    
    /* Checklist of stuff requring filling */
    received = malloc(size);
    memset(received, 0, size);
    
    for(i = 0; i < num_connections; i++)
    {
        if(fr->file_offset[i] == -1)
        {
            printf("%d: closed prematurely\r\n", i);
            continue;
        }
        printf("%d: file_offset = %d\r\n", i, fr->file_offset[i]);
        
        /* Copy the chunk. */
        memcpy(data + fr->file_offset[i], fr->chunk[i]->data + sizeof(int), fr->chunk[i]->len - sizeof(int));
        
        /* Check off those bytes as being received */
        memset(received + fr->file_offset[i], 1, fr->chunk[i]->len - sizeof(int));
    }
    
    /* Print any bytes that were detected as missing. */
    printf("\r\nChecking file integrity...\r\n");
    fail = 0;
    fail_start = -1;
    fail_len = -1;
    for(i = 0; i < size; i++)
    {
        if(!received[i])
        {
            if(!fail)
            {
                printf("Failed to receive the following byte offsets:\r\n");
            }
            if(fail_start == -1)
            {
                fail_start = i;
            }
            fail = 1;
            fail_len++;
        }
        else if(received[i] && fail_start >= 0)
        {
            printf("(%d .. %d) ", fail_start, fail_start + fail_len);
            fail_start = -1;
            fail_len = -1;
        }
    }
    if(fail_start >= 0)
    {
        printf("(%d .. %d) ", fail_start, fail_start + fail_len);
        fail_start = -1;
        fail_len = -1;
    }
    if(fail)
    {
        printf("\r\n\r\n");
        printf("Please try this request again.\r\n");
        
        close(s);
        exit(-1);
    }
    else
    {
        printf("Yes! No problems detected in file.\r\n(Inspect the file manually to be safe though.)\r\n\r\n");
    }
    
    printf("Saving...\r\n");
    /* Otherwise, no known failures. Write the data to disk. */
    f = fopen(filename_of_output, "wb");
    fwrite(data, size, 1, f);
    fclose(f);
    
    printf("Wrote results to %s\r\n", filename_of_output);
    
    close(s);

    return 0;
}
void TCPServer<SessionClass>::Run()
{
   while (true)
   {
      // State machine to handle the server
      switch (State)
      {
      case SRVR_NOT_STARTED:
         // Idle state waits for the server to be started
         if (ServerIsActive)
         {
            State = SRVR_BIND;
         }
         break;

      case SRVR_BIND:

         // Create, bind, and listen the listener socket
         ServerIsActive = InitListener();

         if (ServerIsActive)
         {
            // Poll each of the client sessions
            PollClients();
            State = SRVR_LISTENING;
         }
         else
         {
            // Disconnect all connected clients
            DisconnectAll();
            State = SRVR_NOT_STARTED;
         }

         break;

      case SRVR_LISTENING:

         if (!ServerIsActive)
         {
            // Disconnect all connected clients
            DisconnectAll();
            State = SRVR_NOT_STARTED;
         }
         else
         {
            // Accept incoming connections
            AcceptClient();

            // Poll each of the client sessions
            PollClients();
         }

         // If the number of connected clients reaches the max, stop listening
         if (NumActiveSessions() >= MaxSessions)
         {
            close(ListenerSocket);
            State = SRVR_FULL;
         }

         break;

      case SRVR_FULL:

         if (!ServerIsActive)
         {
            // Disconnect all connected clients
            DisconnectAll();
            State = SRVR_NOT_STARTED;
         }
         else
         {
            // Poll each of the client sessions
            PollClients();
         }

         // If the number of connected clients is less than the max, then bind and listen for connections again
         if (NumActiveSessions() < MaxSessions)
         {
            State = SRVR_BIND;
         }

         break;

      default:
         ServerIsActive = false;
         ServerAddress = 0;
         ServerPort = 0;
         State = SRVR_NOT_STARTED;
         break;

      }

      Delay(POLL_INTERVAL_MS);
   }
}