Пример #1
0
//udp连接
void ntp_session::udp_connect(std::string hostname,unsigned short port)
{
    boost::system::error_code ec;
    udp::resolver::query query(hostname,boost::lexical_cast<std::string, unsigned short>(port));
    udp::resolver::iterator iter = uresolver_.resolve(query,ec);
    if(iter!=udp::resolver::iterator())
    {
        uendpoint_ = (*iter).endpoint();
        usocket().open(udp::v4());
        const udp::endpoint local_endpoint = udp::endpoint(udp::v4(),port);
        usocket().bind(local_endpoint);
        usocket().open(udp::v4());
    }
}
Пример #2
0
void pping_server::shutdown()
{
    if (!initialized) return;

    close_lstn_thread = true;

    USOCKET s = usocket(AF_INET, SOCK_STREAM, 0, NULL);
    if (s == U_INVALID_SOCKET)
        throw USER_ENV_EXCEPTION("Failed to create socket", false);

    if (uconnect_tcp(s, port, "localhost", NULL) == U_SOCKET_ERROR)
        throw USER_ENV_EXCEPTION("Failed to create TCP connection", false);

    char c = PPING_KEEP_ALIVE_MSG;
    usend(s, &c, sizeof(c), NULL);

    if (uThreadJoin(server_lstn_thread_handle, NULL) != 0)
        throw USER_ENV_EXCEPTION("Error waiting for pping server_lstn thread to shutdown", false);

    if (uclose_socket(s, NULL) != 0)
        throw USER_ENV_EXCEPTION("Failed to close socket", false);

    if (uclose_socket(sock, NULL) != 0)
        throw USER_ENV_EXCEPTION("Failed to close socket", false);

    if (uCloseThreadHandle(server_lstn_thread_handle, NULL) != 0)
        throw USER_EXCEPTION2(SE4063, "pping server_lstn_thread");
}
Пример #3
0
void ntp_session::start_read_head(int msgLen)
{
    udp::endpoint sender_endpoint;
    usocket().async_receive_from(boost::asio::buffer(receive_msg_ptr_->w_ptr(),receive_msg_ptr_->space()),sender_endpoint,
                                                boost::bind(&device_session::handle_udp_read,this,
                                                 boost::asio::placeholders::error,
                                                 boost::asio::placeholders::bytes_transferred)
                                              );

}
Пример #4
0
void pping_server::startup()
{
    sock = usocket(AF_INET, SOCK_STREAM, 0, __sys_call_error);
    if (sock == U_INVALID_SOCKET) throw USER_ENV_EXCEPTION("Failed to create socket", false);

    if (uNotInheritDescriptor(UHANDLE(sock), __sys_call_error) != 0) throw USER_EXCEPTION(SE4080);

    if (ubind_tcp(sock, port, "localhost", __sys_call_error) == U_SOCKET_ERROR) throw USER_ENV_EXCEPTION2("Failed to bind socket", usocket_error_translator(), false);

    if (ulisten(sock, PPING_LSTNR_QUEUE_LEN, __sys_call_error) == U_SOCKET_ERROR) throw USER_ENV_EXCEPTION("Failed to listen socket", false);

    uResVal res = uCreateThread(pping_server_lstn_thread_proc_st, this, &server_lstn_thread_handle, PPING_STACK_SIZE, NULL, __sys_call_error);
    if (res != 0) throw USER_ENV_EXCEPTION("Failed to create pping server thread", false);
    initialized = true;
}
Пример #5
0
void ntp_session::start_write(unsigned char* commStr,int commLen)
{
    if(commLen<=0)
        return;
    usocket().async_send_to(boost::asio::buffer(commStr,commLen),uendpoint_,
                        #ifdef USE_STRAND
                            strand_.wrap(
                            #endif
                                boost::bind(&net_session::handle_write,shared_from_this(),
                                            boost::asio::placeholders::error,
                                            boost::asio::placeholders::bytes_transferred)
                            #ifdef USE_STRAND
                                )
                        #endif
                            );
}
Пример #6
0
void device_session::start_read_body(int msgLen)
{
        udp::endpoint sender_endpoint;
        usocket().async_receive_from(boost::asio::buffer(receive_msg_ptr_->w_ptr(),
                                                         msgLen),
                                     sender_endpoint,
                             #ifdef USE_STRAND
                                     strand_.wrap(
                                 #endif
                                         boost::bind(&device_session::handle_read_body,this,
                                                     boost::asio::placeholders::error,
                                                     boost::asio::placeholders::bytes_transferred)
                                 #ifdef USE_STRAND
                                         )
                             #endif
                                     );

}
Пример #7
0
void pping_client::startup(SednaUserException& e, bool is_soft)
{
    sock = usocket(AF_INET, SOCK_STREAM, 0, __sys_call_error);

    if (sock == U_INVALID_SOCKET) throw USER_ENV_EXCEPTION("Failed to create socket", false);
    if (uconnect_tcp(sock, port, host, __sys_call_error) == U_SOCKET_ERROR)
    {
        if (uclose_socket(sock, NULL) == U_SOCKET_ERROR)
            throw USER_ENV_EXCEPTION("Failed to close socket", false);
        throw_exception(e, is_soft);
    }

    if (UUnnamedSemaphoreCreate(&sem, 0, NULL, __sys_call_error) != 0)
        throw USER_ENV_EXCEPTION("Failed to create semaphore", false);


    uResVal res = uCreateThread(pping_client_thread_proc, this, &client_thread_handle, PPING_STACK_SIZE, NULL, __sys_call_error);
    if (res != 0) throw USER_ENV_EXCEPTION("Failed to create pping client thread", false);

    initialized = true;
}
Пример #8
0
int main(int argc, char** argv) {
#if (defined WIN32 || defined _WIN32)
  WSADATA wsadata;
  int rc = WSAStartup(MAKEWORD(2,0), &wsadata); 
  if (rc) return 1;
#endif

  upoll_t* upq = upoll_create(32);
  intptr_t sd1 = usocket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  printf("server: %li\n", sd1);

  printf("bind: %d\n", ubind(sd1, "127.0.0.1", "1976"));
  printf("listen: %d\n", ulisten(sd1, 128));

  upoll_event_t ev1, ev2;
  upoll_event_t events[8];

  ev1.events = UPOLLIN;
  ev1.data.fd = sd1;

  upoll_ctl(upq, UPOLL_CTL_ADD, sd1, &ev1);

  int r = 0, x = 0;
  while (1) {
    int i, e;
    e = upoll_wait(upq, events, 8, -1);
    printf("events: %i\n", e);
    if (x++ == 50) return 0;
    for (i = 0; i < e; i++) {
      char buf[4096];
      if (events[i].events & UPOLLERR) {
        printf("ERROR on %li\n", events[i].data.fd);
        upoll_ctl(upq, UPOLL_CTL_DEL, events[i].data.fd, NULL);
        uclose(events[i].data.fd);
      }
      else if (events[i].events & UPOLLIN) {
        printf("have POLLIN on %li\n", events[i].data.fd);
        if (events[i].data.fd == sd1) {
          int sd2 = uaccept(sd1);
          printf("uaccept: %i\n", sd2);
          ev2.events = UPOLLIN;
          ev2.data.fd = sd2;
          upoll_ctl(upq, UPOLL_CTL_ADD, sd2, &ev2);
        }
        else {
          int cfd = events[i].data.fd;
          memset(buf, 0, 4096);
          printf("client input...\n");
          r = uread(cfd, buf, 4096);
          printf("read %i bytes\n", r);
          if (r == 0) {
            printf("EOF DETECTED\n");
            upoll_ctl(upq, UPOLL_CTL_DEL, events[i].data.fd, NULL);
            uclose(events[i].data.fd);
          }
          else {
            ev2.events = UPOLLOUT;
            upoll_ctl(upq, UPOLL_CTL_MOD, cfd, &ev2);
          }
        }
      }
      else if (events[i].events & UPOLLOUT) {
        printf("client writable...\n");
        int cfd = events[i].data.fd;
        int w = uwrite(cfd, buf, r);
        ev2.events = UPOLLIN;
        printf("wrote %i bytes, mod for UPOLLIN again\n", w);
        upoll_ctl(upq, UPOLL_CTL_MOD, cfd, &ev2);
      }
    }
  }

#if (defined WIN32 || defined _WIN32)
  WSACleanup();
#endif

  return 0;
}