Пример #1
0
p_scr *
g_connect(char *displayName)
{
  p_scr *s = 0;
  int i, j, i0=-1, len=0, number=0;

  /* split display into base name and screen number (separated by dot) */
  if (displayName) while (displayName[len]) len++;
  if (len) {
    for (i=len-1 ; i>=0 ; i--) if (displayName[i]=='.') break;
    if (i>=0) {
      int i0 = i;
      for (i++ ; i<len && displayName[i]<='9' && displayName[i]>='0' ; i++)
        number = 10*number + (displayName[i]-'0');
      if (i == len) len = i0;
      else number = 0;
    }
  }
  if (!len) displayName = 0;
  if (g_screens) {
    for (i=0 ; i<n_screens ; i++) {
      j = 0;
      if (g_screens[i].name) {
        for ( ; j<len ; j++)
          if (g_screens[i].s && g_screens[i].name[j]!=displayName[j]) break;
      }
      if (j==len && (len? (!g_screens[i].name[j]) : !g_screens[i].name)) {
        if (number == g_screens[i].number) break;
        else if (i0<0) i0 = i;
      }
    }
    if (i<n_screens) s = g_screens[i].s;
  }
  if (!s) {
    if (i0<0) s = p_connect(displayName);
    else s = p_multihead(g_screens[i0].s, number);
    if (!s) return s;
    g_test_pending(s);
    for (i=0 ; i<n_screens ; i++) if (!g_screens[i].s) break;
    if (i==n_screens && !(i & (i-1))) {
      int n = i? 2*i : 1;
      g_screens = p_realloc(g_screens, sizeof(g_scr)*n);
    }
    g_screens[i].number = number;
    g_screens[i].name = displayName? p_strncat(0, displayName, len) : 0;
    g_screens[i].s = s;
    if (i==n_screens) n_screens++;
  }

  return s;
}
Пример #2
0
int main(int argc, char **argv)
{
	signal(SIGINT, &h_mac_quit);
	signal(SIGUSR1, &h_mac_quit);
	signal(SIGSEGV, &h_mac_quit);

#define _TEST
#undef _TEST
	if (getuid() && geteuid())
		die_with_signal("not root");
#ifdef _TEST
	builtin_tcp_connect("192.168.1.102","80");	
#else
	struct pkt_info_t pass = init_and_connect("www.google.com", "80");
	p_connect(&pass);
	close(pass.fd);
#endif
	return 0;
}
Пример #3
0
/** 
 * Collects user input and performs the main program loop.
 * @param argc - Number of arguments; should be 3.
 * @param argv - Command line arguments; 1 should be the server; 2 should be the
 * 				port.
 * @return int - Returns 0 if no errors; nonzero otherwise.
 */
int 
main(int argc, char *argv[]) 
{	
	struct p_conn_params conn;	// Holds information for the connection.
	int errcode;	// Hold return error codes.
	int running = 1;	// 0 if program should exit.
	int unsent_msg = 0; // Indicates if last user message was not sent.
	pthread_t rdr_id;

	// Check proper usage.
	if (argc != 3) {
		return p_perror(P_EUSAGE, NULL);
	}

	// Initialize the connection.
	p_init_conn_params(&conn);
	conn.server = argv[1];
	conn.port = argv[2];	
	p_init_addrinfo(&conn.params, 0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
	
	// Start up the reader.
	g_rdr = 1;
	if (pthread_create(&rdr_id, NULL, &rdr_task, (void *) &conn)) {
		return p_perror(P_EREADER, NULL);
	}
	
	
	// Main program loop.
	pstatus("Client program. Enter *quit to quit.");
	
	do {
		// Reconnect if necessary.
		if (sync_get_active(&conn, &mx_active) != 1) {
			pstatus("Attempting to establish connection..");
			
			MXLOCK(&mx_socket);
			MXLOCK(&mx_active);
			
			errcode = p_connect(&conn);	// Try to connect.
			
			MXUNLOCK(&mx_active);
			MXUNLOCK(&mx_socket);

			if (errcode) {	// Connection failed.
				p_perror(errcode, "In main loop, after (re)connect block");
				clean_exit(errcode, &conn, &rdr_id);
			}
			
			pstatus("Connection opened.\n");
		}
		
		// Get a line of input from the user, unless the previous message was
		// unsent (e.g. because of disconnect).
		fflush(stdout);
		if (unsent_msg) {
			pstatus("Last message was unsent. Now sending:");
			printf("\n%s\n", INBUF);
			unsent_msg = 0;
		}
		else {
			printf(">>");
			running = get_input();
		}	
	
		// Continue and send if the user had not chosen to quit.
		if (running) {
			
			// Check connection.
			errcode = p_test_conn(conn.socket);
			if (errcode == 0) {
				pstatus("The server closed the connection.\n");
				sync_set_active(&conn, &mx_active, 0);
				p_close_socket(&conn.socket);
				unsent_msg = 1;
			}
		
			// Send the message if the connection is ok.
			if (sync_get_active(&conn, &mx_active)) { 
				MXLOCK(&mx_socket);

				if (p_send_msg(&conn.socket, INBUF, sizeof(char)*strlen(INBUF)) == 0) {
					p_perror(P_ESENDF, strerror(errno));
					running = 0;
				} 
			
				MXUNLOCK(&mx_socket);
			}
		}		
			
	} while(running);
	
	
	// Close the socket and join with the reader thread.
	clean_exit(0, &conn, &rdr_id);
}