示例#1
0
int main ()
{
    int s = SocketOpen ("vcan0", 1000, 1000);

    struct FrameBag bag;
    int yes = 0;
    gettimeofday (&t1, NULL);
    gettimeofday (&t2, NULL);
    while ( t2.tv_sec - t1.tv_sec < 1 )
    {
        if ( SocketRead (s, &bag, 1, 700) > 0
             && bag.Frame.can_id == 0x111
             && bag.Frame.can_dlc == 5
             && bag.Frame.data[0] == 1
             && bag.Frame.data[1] == 2
             && bag.Frame.data[2] == 3
             && bag.Frame.data[3] == 4
             && bag.Frame.data[4] == 5 )
        {
            yes = 1;
            break;
        }

        gettimeofday (&t2, NULL);
    }

    SocketClose (s);
    if (yes)
        printf ("Тесты пройдены.\n");
    else
        printf ("\n ~~~ !!!!!- ^ТЕСТ ЗАВАЛЕН^ -!!!! ~~~ \n\n");

    return yes ? 0 : -1;
}
示例#2
0
void CSockWrap::Reopen(bool bForceClose)
{

    if (ISSOCKHANDLE(m_hSocket) && bForceClose) SocketClose(m_hSocket);
    if (!ISSOCKHANDLE(m_hSocket) )
    {
        m_hSocket=SocketOpen(m_tcpudp);
    }

}
示例#3
0
文件: s_fsock.c 项目: aharrison24/HAL
STATIC Bool SocketReopen(File * f, Str hostname, const char * port)
{
    SocketFile * s  = SocketFileCast(f);
    if (s) {
        File * f2;
        shutdown(s->sock, SHUT_RDWR);
        closesocket(s->sock);
        f2 = SocketOpen(hostname, port);
        if (f2) {
            File save = *f;
            SocketFile * s2 = SocketFileCast(f2);
            *s = *s2;
            *f = save;
            MEM_Free(s2);
            return True;
        }
    }
    return False;
}
示例#4
0
int
ConnectionEstablish( char *server_name, int port )
{
  int len;
  char rx_buffer[1024]; /* Temporary... */

  wmnotify_infos.sock_fd = SocketOpen( wmnotify_infos.server_name, wmnotify_infos.port );
  if( wmnotify_infos.sock_fd < 0 ) {
    goto error;
  }

#if HAVE_SSL
  if( wmnotify_infos.use_ssl == true ) {
    int status;
    status = InitSSL( wmnotify_infos.sock_fd );
    if( status != EXIT_SUCCESS ) {
      goto error;
    }
  }
#endif

  /* Testing connection. */
  len = WmnotifyGetResponse( rx_buffer, 1024 );
  if( len < 0 ) {
    goto error;
  }

  if( wmnotify_infos.debug ) {
    rx_buffer[len] = 0;
    printf(" Connect response:\n%s\n", rx_buffer );
  }

  return EXIT_SUCCESS;

 error:
  return EXIT_FAILURE;
}
示例#5
0
int CommServiceThreadClient( FThread *ptr )
{
	CommService *service = (CommService *)ptr->t_Data;
	
	DEBUG("CommunicationServiceSend Start\n");
	
	struct mq_attr attr;
	char buffer[ MAX_SIZE + 1 ];
/*
	// initialize the queue attributes 
	attr.mq_flags		= 0;
	attr.mq_maxmsg		= MAX_MSG;
	attr.mq_msgsize		= MAX_SIZE;
	attr.mq_curmsgs		= 0;

	DEBUG("QUEUE Before Created SERVER\n");
	// create the message queue 
	mode_t mode  = FLAGS;
	
	
	mode_t omask;
	omask = umask( 0 );
	service->s_inMqfd = mq_open( QUEUE_NAME, (O_CREAT | O_RDONLY), mode, &attr );
	umask( omask );
	
	DEBUG("QUEUE Created SERVER\n");
	*/
	pthread_mutex_lock( &InitMutex ); 
	pthread_cond_signal( &InitCond );    
	pthread_mutex_unlock( &InitMutex );  
	
	//
	// atm we only read FC connections
	//
	
	struct PropertiesLibrary *plib = NULL;
	char *servers = NULL;
	Props *prop = NULL;
	
	if( ( plib = (struct PropertiesLibrary *)LibraryOpen( SLIB, "properties.library", 0 ) ) != NULL )
	{
		char coresPath[ 1024 ];
		sprintf( coresPath, "%s/cfg/cfg.ini", getenv( "FRIEND_HOME" ) );
		
		prop = plib->Open( coresPath  );
		if( prop != NULL)
		{
			DEBUG("[Userlibrary] reading login\n");
			servers = plib->ReadString( prop, "Cores:servers", "" );
			DEBUG("[Userlibrary] servers %s\n", servers );
		}
		//DEBUG("PROPERTIES LIBRARY OPENED, poitner to props %p!   %s  %s  %s  %s  %d\n", prop, login, pass, host, dbname, port );
	}
		
	if( servers != NULL )
	{
		char *token;
		
		DEBUG("Server list found %s\n", servers );
   
		// get the first token 
		token = strtok( servers, SERVER_SPLIT_SIGN );
		// walk through other tokens 
		while( token != NULL ) 
		{
			CommFCConnection *newcon;
			
			char *address = NULL;
			char *name = NULL;
			
			char *pos = strchr( token, SERVER_NAME_SPLIT_SING );
			if( pos != NULL )
			{
				*pos = 0;
				address = token;
				name = ++pos;
				
			}else{
				address = token;
				name = token;
			}
			
			DEBUG("New connection found address : %s name : %s\n", address, name );
				
			//TODO
				
			if( ( newcon = CommFCConnectionNew( address, name ) ) != NULL )
			{
				DEBUG("Outgoing connection mem allocated\n");
				
				newcon->cfcc_Socket = SocketOpen( FRIEND_COMMUNICATION_PORT, SOCKET_TYPE_CLIENT );
				
				DEBUG("Outgoing connection created\n");
				
				if( SocketConnect( newcon->cfcc_Socket, address ) == 0 )
				{
					DEBUG("Connection setup with server : %s\n", token );
					if( service->s_FCConnections == NULL )
					{
						service->s_FCConnections = newcon;
					}
					else
					{	// we already have connections, we must add them to the end of list
						CommFCConnection *lc = service->s_FCConnections;
						while( lc->node.mln_Succ != NULL )
						{
							lc = (CommFCConnection *)lc->node.mln_Succ;
						}
						lc->node.mln_Succ = (MinNode *) newcon;
					}
					
					//
					// sockets connected, we create now special message where we sent ID of our host
					//
					
					DEBUG("Generate Data Form\n");
					DataForm * df = DataFormNew( NULL );
					DEBUG("DataForm Created\n");
					FriendCoreManager *fcm = (FriendCoreManager *) service->s_FCM;
					DataFormAdd( &df, (BYTE *)fcm->fcm_ID, FRIEND_CORE_MANAGER_ID_SIZE );
					//INFO("Message created name byte %c%c%c%c\n", fcm->fcm_ID[32], fcm->fcm_ID[33], fcm->fcm_ID[34], fcm->fcm_ID[35]	);
					
					SocketWrite( newcon->cfcc_Socket, (char *)df, df->df_Size );
					
					DEBUG("Message sent\n");
					DataFormDelete( df );
					
				}
				else
				{
					close( newcon->cfcc_Socket->fd );
					FFree( newcon->cfcc_Socket );
					FFree( newcon );
					ERROR("Cannot setup socket connection!\n");
				}
			}
				
			DEBUG( " %s\n", token );
			
			token = strtok( NULL, "," );
		}
		
		DEBUG("All tokens passed\n");
		
	}
	else
	{	// servers == NULL
	}
	
	if( plib != NULL && prop != NULL )
	{
		plib->Close( prop );
	}
	
	if( plib != NULL )
	{
		LibraryClose( (struct Library *)plib );
	}
	DEBUG("CommunicationServiceClient start\n");
	
	//
	// we should ask for information from connection
	//
	
	// messages get and pass to destination
	
	//int queueFd = mq_ msgqToFd( service->s_inMqfd );
	
	struct timeval tv;
	fd_set writeToServ;
	fd_set readFromServ;
	
	ULONG idMax = 0;
	if( service->s_sendPipe[ 0 ] > idMax ) idMax = service->s_sendPipe[ 0 ];
	if( service->s_sendPipe[ 1 ] > idMax ) idMax = service->s_sendPipe[ 1 ];
	if( service->s_recvPipe[ 0 ] > idMax ) idMax = service->s_recvPipe[ 0 ];
	if( service->s_recvPipe[ 1 ] > idMax ) idMax = service->s_recvPipe[ 1 ];
	
	{
		CommFCConnection *lc = service->s_FCConnections;
		while( lc != NULL )
		{
			if( lc->cfcc_Socket > idMax )
			{
				idMax = lc->cfcc_Socket->fd;
			}
			lc = (CommFCConnection *)lc->node.mln_Succ;
		}
	}
	
	DEBUG("IDMAX SET TO %ld\n", idMax );
	/*
	if( service->s_inMqfd != -1 )
	{*/
		while( service->s_Cam.cam_Quit != 1 )
		{
			FD_ZERO( &writeToServ );
			FD_ZERO( &readFromServ );
			FD_SET( service->s_sendPipe[ 0 ] , &writeToServ );
			//FD_SET( lc->cfcc_Socket , &readFromServ );
			
			tv.tv_sec = 0;
			tv.tv_usec = 10000000;

			//ret = 0;

			int ret = select( idMax+1, &writeToServ, NULL, NULL, &tv );
			
			// handle message
			
			if( ret > 0 )
			{
				int rets = read( service->s_sendPipe[ 0 ], buffer, MAX_SIZE );
				//ERROR("DATAREADED! %d\n", rets );
				
				buffer[ rets ] = '\0';
				//TODO
				// we should read from QUEUE, check destination server and send message
				//
/*
				CommFCConnection *lc = service->s_FCConnections;
				while( lc != NULL )
				{
					//TODO test message , should be removed
					SocketWrite( lc->cfcc_Socket, buffer, rets );//"hello", 5 );
					DEBUG("CommunicationServiceClient Sending message hello\n");
					int sockReadSize = 0;
					
					sockReadSize = SocketRead( lc->cfcc_Socket, buffer, MAX_SIZE, 0 );
					int writeSize = write( service->s_recvPipe[ 1 ], buffer, sockReadSize );
					
					DEBUG("Message received '%s'\n", buffer );
					lc = (CommFCConnection *)lc->node.mln_Succ;
				}*/
				
				}else{
				
				}
			//usleep( 10000000 );
			//DEBUG("CommunicationServiceClient Thread at work %d\n", ret );
		
			ssize_t bytes_read;

			// receive the message 
			//bytes_read = mq_receive( service->s_inMqfd, buffer, MAX_SIZE, NULL );
			//CHECK(bytes_read >= 0);
			/*
			*/
		}
		/*
	}else{
		ERROR("Cannot create QUEUE!\n");
	}*/
	
	DEBUG("CommunicationService close\n");
	
	CommFCConnection *lc = service->s_FCConnections;
	CommFCConnection *rlc = service->s_FCConnections;
	while( lc != NULL )
	{
		rlc = lc;
		lc = (CommFCConnection *) lc->node.mln_Succ;
		
		DEBUG("Closing output connection\n");
		
		SocketClose( rlc->cfcc_Socket );

		CommFCConnectionDelete( rlc );
	}
	
		// closing queue
		
	//mq_close( service->s_inMqfd );
		
	//mq_unlink( QUEUE_NAME );
	
	return 0;
}