예제 #1
0
/****************************************************************************
*功能:释放UDP通信器
****************************************************************************/
int ReleaseUDPCommunicator(Communicator* p)
{
	WaitForSingleObject(Connections::hUDPInitMutex,INFINITE);
	SCPrint(("%d:enter releasecomm\r\n",GetCurrentThreadId()));
	if(p->m_nCommtype==COMM_UDP)
	{
		p->m_nCommtype=NOT_CONNECT;
		if(p->udpConn->reference<=0)
		{
			p->udpConn->reference=0;
			ReleaseMutex(Connections::hUDPInitMutex);
			return SCERR_INVALID_COMM;
		}
		p->udpConn->reference--;
		if(p->udpConn->reference==0)
		{
			CloseUDPSocket(p->udpConn->s);
			Connections::udpCommManagerVector[p->csIndex].udpCommList.remove(p->udpConn);
			delete p->udpConn;
			SCPrint(("%d:delete udpconn:%08X\r\n",GetCurrentThreadId(),p->udpConn));
		}	
		
		SCPrint(("%d:delete communicatror:%08X,reference:%d\r\n",GetCurrentThreadId(),p,p->udpConn->reference));
		memset(p,0,sizeof(Communicator));
		delete p;
		ReleaseMutex(Connections::hUDPInitMutex);	
		return 0;
	}
	SCPrint(("leave releasecomm\r\n"));
	ReleaseMutex(Connections::hUDPInitMutex);
	return SCERR_INVALID_COMM;
}
예제 #2
0
int UdpSocketWindows::SetParameter(string ip, short port, unsigned long timeOut)
{
	CloseUDPSocket();

	this->ip = ip;
	this->port = port;
	this->timeOut = timeOut;

	return OpenUDPSocket();
}
예제 #3
0
int UdpSocketWindows::OpenUDPSocket(void)
{
	if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
		return -1;
	}

	hSocket = socket(AF_INET, SOCK_DGRAM, 0);
	if(hSocket == INVALID_SOCKET) {
		return -1;
	}

	SOCKADDR_IN tcp_sock_addr;

	memset(&tcp_sock_addr, 0, sizeof(tcp_sock_addr));
	tcp_sock_addr.sin_family = AF_INET;
	tcp_sock_addr.sin_addr.s_addr = inet_addr(ip.c_str());
	tcp_sock_addr.sin_port = htons((short)port);

	if(connect(hSocket, (SOCKADDR *)&tcp_sock_addr, sizeof(tcp_sock_addr)) == SOCKET_ERROR) {
		CloseUDPSocket();
		return -1;
	}

	int opt_timeout = timeOut;

	if(setsockopt(hSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&opt_timeout, sizeof(opt_timeout)) == SOCKET_ERROR) {
		CloseUDPSocket();
		return -1;
	}

	if(setsockopt(hSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&opt_timeout, sizeof(opt_timeout)) == SOCKET_ERROR) {
		CloseUDPSocket();
		return -1;
	}
	
	return 0;
}
예제 #4
0
int main(int argc, char *argv[])
{
    int done = FALSE;
    opterr = 0;

    //fprintf(stderr, "ctrack started.");
    c_control = C_RUN;

    srand((int)time(NULL));

	ErrorInit();    
    ConfigInit(); // initializes verbose; must be called before any output

   	ReadConfig(TRUE); // default loc.
    TorrentDBCreate();

    unsigned int p;
    while (!done) {
        switch (getopt(argc, argv, "+vqVhrdt:u:p:c:"))  { // + means "don't shuffle args"
            case '?': // invalid option
                PrintSyntax(1);
            case 'h': // help option
                PrintSyntax(0);
            case 'V':
            	printf("ctrack " VERSION "\n");
            	return 0;
                break;
            case 'v':
                SetConfigBool(CFG_VERBOSE, TRUE);
                log("Verbose mode");
                break;
            case 'q':
                SetConfigBool(CFG_VERBOSE, FALSE);
                log("Quiet mode");
                break;
            case 't':
                if (b_assign(atoi(optarg),1,65535,(int *)&p))
                    SetConfigUInt(CFG_HTTP_PORT, p);
                break;
            case 'u':
                if (b_assign(atoi(optarg),1,65535,(int *)&p))
                    SetConfigUInt(CFG_UDP_PORT, p);
                break;
            case 'p':
                if (b_assign(atoi(optarg),1,65535,(int *)&p))
                    SetConfigUInt(CFG_SERVER_PORT, p);
                break;
            case 'd':
                SetConfigBool(CFG_DAEMON, TRUE);
                break;
            case 'c': //config file
	            SetConfigString(CFG_CONFIG_FILE, optarg);
                ReadConfig(FALSE);
            	break;
            case -1:  //end of options
                done = TRUE;
                break;
            default:
                PrintSyntax(1);
                break;    
        }//switch
    }//while
    int i;
    for (i = optind; i < argc; i++) {
    	PTorrentInfo t;
    	char ibuffer[20];
    	if (strtohash(argv[i], ibuffer)) {
			NewTorrentInfo(&t, ibuffer);
		} else {
			errorm("Invalid hash: %s", argv[i]);
		}
    }//for

    //TODO:parse args
    //TODO:init torrent db

#ifdef DAEMON_SUPPORT
    if (GetConfigBool(CFG_DAEMON) == TRUE) {
    /* Our process ID and Session ID */
    pid_t pid, sid;

    /* Fork off the parent process */
    pid = fork();
    if (pid < 0) { exit(EXIT_FAILURE); }
    /* If we got a good PID, then we can exit the parent process. */
    if (pid > 0) { exit(EXIT_SUCCESS); }
    /* Change the file mode mask */
    umask(0);
        /* Create a new SID for the child process */
    sid = setsid();
    if (sid < 0) {
		errorm("setsid() failed: %s", str_error());
		exit(EXIT_FAILURE);
    }
    /* Change the current working directory */
    if ((chdir("/")) < 0) {
		errorm("chdir() failed: %s", str_error());
        exit(EXIT_FAILURE);
    }
    /* Close out the standard file descriptors */
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

	if (daemon(TRUE, FALSE) != 0) 
        errorf(E_SYSTEM, "daemon() failed: %S", str_error());
    } else {
		extern int asdaemon;
		asdaemon = TRUE;
    }
#endif

    //SetupSignalHandlers
	sigset_t set;
	int sig;

    while (c_control == C_RUN) {
    	sigfillset(&set);
    	pthread_sigmask(SIG_SETMASK, &set, NULL);

        if (GetConfigBool(CFG_HTTP_SERVER)) { // HTTP server
	        if (SetUpListener())
    	        HTTPThreadInit();
	    }

	    if (GetConfigBool(CFG_UDP_SERVER)) { // UDP server
	        UDPThreadInit();
	    }

		//TODO: sigwait
		while (c_control == C_RUN) {
		    sigwait(&set, &sig);
			logf("Caught signal #%d", sig);
			switch (sig) {
				case SIGINT:
	                c_control = C_TERMINATE;
    	            log("Exitting...");
        	        break;
				case SIGHUP:
	                c_control = C_RESTART;
	                log("Restarting...");
                   	ReadConfig(FALSE);
	                break;
				case SIGSEGV:
	                c_control = C_TERMINATE;
    	            log("Exitting...");
			}//switch
		}//while
        ThreadsDestroy();
    	CloseTCPSocket();
    	CloseUDPSocket();

        if (c_control == C_RESTART) { c_control = C_RUN; }
    }//while

  	log("exit ");
    return ((sig==SIGINT) ? EXIT_SUCCESS : EXIT_FAILURE);
}
예제 #5
0
UdpSocketWindows::~UdpSocketWindows(void)
{
	CloseUDPSocket();
}