void psync_set_event_callback(pevent_callback_t callback){
  pthread_mutex_lock(&statusmutex);
  eventthreadrunning=1;
  pthread_mutex_unlock(&statusmutex);
  psync_list_init(&eventlist);
  psync_run_thread1("event", event_thread, callback);
}
void overlay_main_loop(VOID)
{
  BOOL   fConnected = FALSE;
  HANDLE hPipe = INVALID_HANDLE_VALUE;
  HANDLE ghSemaphore;
  DWORD dwWaitResult;

  // The main loop creates an instance of the named pipe and
  // then waits for a client to connect to it. When the client
  // connects, a thread is created to handle communications
  // with that client, and this loop is free to wait for the
  // next client connect request. It is an infinite loop.

  ghSemaphore = CreateSemaphore(
    NULL,           // default security attributes
    MAX_SEM_COUNT,  // initial count
    MAX_SEM_COUNT,  // maximum count
    NULL);          // unnamed semaphore

  if (ghSemaphore == NULL)
  {
    printf("CreateSemaphore error: %d\n", GetLastError());
    return 1;
  }

  for (;;)
  {
    //debug(D_NOTICE, "\nPipe Server: Main thread awaiting client connection on %s\n", PORT);

    dwWaitResult = WaitForSingleObject(
      ghSemaphore,   // handle to semaphore
      INFINITE);           // zero-second time-out interval


    hPipe = CreateNamedPipe(
      PORT,                     // pipe name
      PIPE_ACCESS_DUPLEX,       // read/write access
      PIPE_TYPE_MESSAGE |       // message type pipe
      PIPE_READMODE_MESSAGE |   // message-read mode
      PIPE_WAIT,                // blocking mode
      PIPE_UNLIMITED_INSTANCES, // max. instances
      POVERLAY_BUFSIZE,         // output buffer size
      POVERLAY_BUFSIZE,         // input buffer size
      0,                        // client time-out
      NULL);                    // default security attribute

    if (hPipe == INVALID_HANDLE_VALUE)
    {
      //debug(D_NOTICE, "CreateNamedPipe failed, GLE=%d.\n", GetLastError());
      return;
    }

    fConnected = ConnectNamedPipe(hPipe, NULL) ?
    TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

    if (fConnected)
    {
      //debug(D_NOTICE, "Client connected, creating a processing thread.\n");

      // Create a thread for this client.
      psync_run_thread1(
        "Pipe request handle routine",
        instance_thread,    // thread proc
        (LPVOID)hPipe     // thread parameter
        );
    }
    else
      CloseHandle(hPipe);

    if (!ReleaseSemaphore(
      ghSemaphore,  // handle to semaphore
      1,            // increase count by one
      NULL))       // not interested in previous count
    {
      debug(D_WARNING,"ReleaseSemaphore error: %d\n", GetLastError());
    }

  }

  CloseHandle(ghSemaphore);
  return;
}
Exemple #3
0
static void psync_p2p_thread(){
  ssize_t ret;
  char buff[2048];
/*  struct sockaddr_in6 addr; */
  struct sockaddr_in addr4;
  psync_socket_t tcpsock, socks[2], *inconn;
  socklen_t sl;
  int sret;
  psync_wait_statuses_array(requiredstatuses, ARRAY_SIZE(requiredstatuses));
  tcpsock=INVALID_SOCKET;
/*  udpsock=psync_create_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  if (unlikely_log(udpsock==INVALID_SOCKET)){*/
    udpsock=psync_create_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (unlikely_log(udpsock==INVALID_SOCKET))
      goto ex;
    setsockopt(udpsock, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
    memset(&addr4, 0, sizeof(addr4));
    addr4.sin_family=AF_INET;
    addr4.sin_port  =htons(PSYNC_P2P_PORT);
    addr4.sin_addr.s_addr=INADDR_ANY;
    if (unlikely_log(bind(udpsock, (struct sockaddr *)&addr4, sizeof(addr4))==SOCKET_ERROR))
      goto ex;
/*  }
  else{
    setsockopt(udpsock, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
    memset(&addr, 0, sizeof(addr));
    addr.sin6_family=AF_INET6;
    addr.sin6_port  =htons(PSYNC_P2P_PORT);
    addr.sin6_addr  =in6addr_any;
    if (unlikely_log(bind(udpsock, (struct sockaddr *)&addr, sizeof(addr))==SOCKET_ERROR))
      goto ex;
  }
  tcpsock=psync_create_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  if (unlikely_log(tcpsock==INVALID_SOCKET)){*/
    tcpsock=psync_create_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (unlikely_log(tcpsock==INVALID_SOCKET))
      goto ex;
    setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
    memset(&addr4, 0, sizeof(addr4));
    addr4.sin_family=AF_INET;
    addr4.sin_port  =htons(0);
    addr4.sin_addr.s_addr=INADDR_ANY;
    if (unlikely_log(bind(tcpsock, (struct sockaddr *)&addr4, sizeof(addr4))==SOCKET_ERROR))
      goto ex;
    sl=sizeof(addr4);
    if (unlikely_log(getsockname(tcpsock, (struct sockaddr *)&addr4, &sl)==SOCKET_ERROR))
      goto ex;
    tcpport=ntohs(addr4.sin_port);
/*  }
  else{
    setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
    memset(&addr, 0, sizeof(addr));
    addr.sin6_family=AF_INET6;
    addr.sin6_port  =htons(0);
    addr.sin6_addr  =in6addr_any;
    if (unlikely_log(bind(tcpsock, (struct sockaddr *)&addr, sizeof(addr))==SOCKET_ERROR))
      goto ex;
    sl=sizeof(addr);
    if (unlikely_log(getsockname(tcpsock, (struct sockaddr *)&addr, &sl)==SOCKET_ERROR))
      goto ex;
    tcpport=ntohs(addr.sin6_port);
  }*/
  if (unlikely_log(listen(tcpsock, 2)))
    goto ex;
  socks[0]=udpsock;
  socks[1]=tcpsock;
  while (psync_do_run){
    if (unlikely(!psync_setting_get_bool(_PS(p2psync)))){
      pthread_mutex_lock(&p2pmutex);
      if (!psync_setting_get_bool(_PS(p2psync))){
        running=0;
        psync_close_socket(tcpsock);
        psync_close_socket(udpsock);
        pthread_mutex_unlock(&p2pmutex);
        return;
      }
      pthread_mutex_unlock(&p2pmutex);
    }
    psync_wait_statuses_array(requiredstatuses, ARRAY_SIZE(requiredstatuses));
    sret=psync_select_in(socks, 2, -1);
    if (unlikely_log(sret==-1)){
      psync_milisleep(1);
      continue;
    }
    if (sret==0){
      paddrlen=sizeof(paddr);
      ret=recvfrom(udpsock, buff, sizeof(buff), 0, (struct sockaddr *)&paddr, &paddrlen);
      if (likely_log(ret!=SOCKET_ERROR))
        psync_p2p_process_packet(buff, ret);
      else
        psync_milisleep(1);
    }
    else if (sret==1){
      inconn=psync_new(psync_socket_t);
      *inconn=accept(tcpsock, NULL, NULL);
      if (unlikely_log(*inconn==INVALID_SOCKET))
        psync_free(inconn);
      else
        psync_run_thread1("p2p tcp", psync_p2p_tcphandler, inconn);
    }
  }
ex:
  pthread_mutex_lock(&p2pmutex);
  running=0;
  psync_close_socket(tcpsock);
  psync_close_socket(udpsock);
  pthread_mutex_unlock(&p2pmutex);
}
void psync_set_status_callback(pstatus_change_callback_t callback){
  pthread_mutex_lock(&statusmutex);
  statusthreadrunning=1;
  pthread_mutex_unlock(&statusmutex);
  psync_run_thread1("status change", status_change_thread, callback);
}
Exemple #5
0
void psync_syncer_new(psync_syncid_t syncid){
  psync_syncid_t *psid=psync_new(psync_syncid_t);
  *psid=syncid;
  psync_run_thread1("syncer", psync_do_sync_thread, psid);
}