디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

저번 프붕이 통신 프로토콜 질문에 대한 실제 예시 코드 ㅋㅋ

나르시갤로그로 이동합니다. 2025.09.27 19:15:01
조회 49 추천 0 댓글 0

다음 코드를 수정할 것이다.

    case NIM_BEEP:
      if (ic->cb_beep)
        ic->cb_beep (ic, ic->cb_user_data);

      nimf_ic_send_msg (ic, NIM_BEEP_REPLY, NULL, 0, NULL);
      break;
static void nimf_nic_call_beep (NimfServiceIc* ic)
{
  NimfNic* nic = (NimfNic*) ic;

  nimf_nic_ref (nic);
  if (nimf_conn_send (nic->conn, nic->icid, NIM_BEEP, NULL, 0, NULL))
    nimf_result_wait2 (nic->conn->result, nic->icid, NIM_BEEP_REPLY);
  nimf_nic_unref (nic);
}

프로토콜을 BEEP에서 NOTIFY로 변경했걸랑 ㅋㅋㅋ

다음처럼 수정하면 된다.

    case NIM_NOTIFY:
      if (ic->callbacks->notify)
        ic->callbacks->notify ((CimIcHandle) ic,
                               *(CimNotificationType*) message->data,
                               ic->cb_user_data);
      nimf_ic_send_msg (ic, NIM_NOTIFY_REPLY, NULL, 0, NULL);
      break;
static void nimf_nic_call_notify (NimfServiceIc* ic, CimNotificationType type)
{
  NimfNic* nic = (NimfNic*) ic;

  nimf_nic_ref (nic);
  if (nimf_conn_send (nic->conn, nic->icid, NIM_NOTIFY, &type,
                      sizeof (CimNotificationType), nullptr))
    nimf_result_wait2 (nic->conn->result, nic->icid, NIM_NOTIFY_REPLY);
  nimf_nic_unref (nic);
}

헐... 데이터 패킷은 어디에 있나요??

요기에서 만들어주지용. ㅋㅋ

NimfMsg *nimf_msg_new_full (uint16_t  type,
                            uint16_t  icid,
                            void*     data,
                            uint16_t  data_len,
                            CFreeFunc data_free_func)
{
  NimfMsg *message;

  message                  = c_malloc (sizeof (NimfMsg));
  message->header.icid     = icid;
  message->header.type     = type;
  message->header.data_len = data_len;
  message->header.padding  = 0;
  message->data            = data;
  message->data_free_func  = data_free_func;
  message->ref_count = 1;

  return message;
}

헐.. 그러면 send, recv는 어디에?? ㅋㅋ

두둥

bool nimf_send_message (int       sock_fd,
                        uint16_t  icid,
                        uint16_t  type,
                        void*     data,
                        uint16_t  data_len,
                        CFreeFunc data_free_func)
{
  CPollFd pfd;
  pfd.fd      = sock_fd;
  pfd.events  = POLLOUT;
  pfd.revents = 0;

  errno = 0;
  int status = c_poll (&pfd, 1, 100);

  if (status == -1)
  {
    c_log_critical ("%s", strerror (errno));
    return false;
  }
  else if (status == 0)
  {
    c_log_critical ("Time limit expires");
    return false;
  }

  NimfMsg* message;
  ssize_t  n_sent;
  message = nimf_msg_new_full (type, icid, data, data_len, data_free_func);

  struct iovec vector[2];
  vector[0].iov_base = (void*) nimf_msg_get_header (message);
  vector[0].iov_len  = nimf_msg_get_header_size ();
  vector[1].iov_base = message->data;
  vector[1].iov_len  = message->header.data_len;

  struct msghdr msg = { 0 };
  msg.msg_iov = vector;

  if (message->header.data_len > 0)
    msg.msg_iovlen = 2;
  else
    msg.msg_iovlen = 1;

  errno = 0;
  do {
    n_sent = sendmsg (sock_fd, &msg, 0);
  } while (n_sent == -1 && errno == EINTR);

  if (n_sent != vector[0].iov_len + vector[1].iov_len)
  {
    if (n_sent == -1)
    {
      c_log_critical ("n_sent %zd differs from %d, %s. %s",
        n_sent, vector[0].iov_len + vector[1].iov_len,
        nimf_msg_type_to_name (type), strerror (errno));
    }
    else
    {
      c_log_critical ("n_sent %zd differs from %d, %s",
        n_sent, vector[0].iov_len + vector[1].iov_len,
        nimf_msg_type_to_name (type));
    }

    nimf_msg_unref (message);

    return false;
  }

#ifdef DEBUG
  CLoop* loop = c_loop_get_default ();
  if (loop)
  {
    c_log_debug ("depth: %d, fd: %d send: %s", loop->depth, sock_fd,
                 nimf_msg_type_to_name (message->header.type));
  }
  else
  {
    c_log_debug ("client; fd: %d send: %s", sock_fd,
                 nimf_msg_type_to_name (message->header.type));
  }
#endif

  nimf_msg_unref (message);

  return true;
}

NimfMsg* nimf_recv_message (int sockfd)
{
  CPollFd pfd;
  pfd.fd      = sockfd;
  pfd.events  = POLLIN;
  pfd.revents = 0;

  errno = 0;
  int status = c_poll (&pfd, 1, 100);

  if (status == -1)
  {
    c_log_critical ("%s", strerror (errno));
    return nullptr;
  }
  else if (status == 0)
  {
    c_log_critical ("Time limit expires");
    return nullptr;
  }

  NimfMsg* message = nimf_msg_new ();
  ssize_t n_read;

  errno = 0;
  do {
    n_read = recv (sockfd, (uint8_t*) &message->header,
                   nimf_msg_get_header_size (), 0);
  } while (n_read == -1 && errno == EINTR);

  if (n_read < nimf_msg_get_header_size ())
  {
    if (n_read == -1)
    {
      c_log_critical ("header received %zd less than %d. %s",
                      n_read, nimf_msg_get_header_size (), strerror (errno));
    }
    else
    {
      c_log_critical ("header received %zd less than %d",
                      n_read, nimf_msg_get_header_size ());
    }

    nimf_msg_unref (message);

    return NULL;
  }

  if (message->header.data_len > 0)
  {
    nimf_msg_set_body (message, c_malloc (message->header.data_len),
                       message->header.data_len, free);
    do {
      n_read = recv (sockfd, (char *) message->data,
                     message->header.data_len, 0);
    } while (n_read == -1 && errno == EINTR);

    if (n_read < message->header.data_len)
    {
      c_log_critical ("body received %zd less than %d", n_read,
                      message->header.data_len);
      if (n_read == -1)
        c_log_critical ("%s", strerror (errno));

      nimf_msg_unref (message);

      return NULL;
    }
  }

#ifdef DEBUG
  CLoop* loop = c_loop_get_default ();
  if (loop)
  {
    c_log_debug ("depth: %d, fd: %d recv: %s", loop->depth, sockfd,
                 nimf_msg_type_to_name (message->header.type));
  }
  else
  {
    c_log_debug ("client; fd: %d recv: %s", sockfd,
                 nimf_msg_type_to_name (message->header.type));
  }
#endif

  return message;
}

전에 모 프붕이가 질문했던 거에 대한 대답이 모두 들어있지용.

파싱요?? 그게 뭥미? FSM이요?? 그게 뭥미?


추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 공개연애가 득보다 실인 것 같은 스타는? 운영자 25/10/06 - -
AD 프로게이머가 될테야!! 운영자 25/10/01 - -
공지 프로그래밍 갤러리 이용 안내 [96] 운영자 20.09.28 47687 65
2894552 맨유 레전드들이 다 같이 한국 노래방을 간다면??? 루니 가창력 뭐야 발명도둑잡기(118.216) 21:08 0 0
2894551 코덱스 이새끼 진짜 개똑똑하네 시발 ㅇㅇ(106.102) 21:00 11 0
2894545 '김종국 신부 인스타 떴네요! 프갤러(121.169) 20:50 7 0
2894543 명절에 50만원은 그냥 쓰네 ㅅㅂ ㅇㅇ(175.223) 20:33 18 0
2894542 님들 하루 순수 커밋 코드 변화량 몇줄임 [3] 주아갤로그로 이동합니다. 20:19 35 0
2894541 와 개씨빌 모기있네 [2] ♥덩냥이♥갤로그로 이동합니다. 20:13 25 0
2894540 사람 장난 아니게 많음 ㅇㅅㅇ [3] 류류(175.223) 20:13 30 0
2894539 오사카 도착 ㅇㅅㅇ 류류(175.223) 19:58 12 0
2894538 저수준에서 코드딸 치는게 제일 쉬운거같은데 ㅇㅇ(106.241) 19:56 15 0
2894537 [냥덩칼럼] 집시와 유대인 그리고 ♥덩냥이♥갤로그로 이동합니다. 19:21 18 0
2894536 명절 잘 보내고 있냐 [3] 루도그담당(49.165) 19:00 39 0
2894535 프붕이 추석맞이 수원 화성 답사기 [2] ㅇㅇ(121.168) 18:58 26 1
2894534 GPT 코드 작성 존나 못하던데 [2] ㅇㅇ(106.241) 18:47 35 0
2894533 승리를 예감한자의 웃음 ㄷㅅㄷ ♥덩냥이♥갤로그로 이동합니다. 18:27 29 0
2894532 고성능 마우스로 도청하는 사이버 공격 발명도둑잡기(118.216) 18:25 16 0
2894531 ㅇㅅㅇ [1] 류류(118.235) 18:04 34 0
2894530 RPA 종사 하시는 선배님들 계신가용? 케인즈갤로그로 이동합니다. 17:54 13 0
2894529 모처럼 쉬는 날인데 비만 오네.. ㅇㅅㅇ [2] 헤르 미온느갤로그로 이동합니다. 17:46 24 0
2894528 프붕이 돈에서 해방되어버림.. [1] 프갤러(172.225) 17:39 35 0
2894527 지난 여름부터 속썩이던 컴터 쓰로틀링 걸리는 문제 해결 ㅇㅅㅇ [2] 헤르 미온느갤로그로 이동합니다. 17:34 29 0
2894526 악 30분 딜레이 [1] 류류(118.235) 17:32 15 0
2894525 개좆소 프붕이네 회사 오늘도 리뷰 관리하네.. [3] 프갤러(211.235) 16:43 66 1
2894524 100 Years of Fictional UI - Were They AL 발명도둑잡기(118.216) 16:33 13 0
2894523 통제불능 리재명 끌어내리는 탄핵이 답이당⭐+ ♥덩냥이♥갤로그로 이동합니다. 16:26 19 0
2894522 러스트를 써보자. ㅇㅇ(49.165) 15:52 26 0
2894521 ㅅㅂ 아이스크림 2개를 쳐먹어도 성이 안차네 ㅇㅇ(223.38) 15:51 18 0
2894520 도쿄행 매진되어서 오사카 가기로 ㅠㅠ 류류(39.7) 15:45 21 0
2894519 주주클럽 발명도둑잡기(118.216) 15:10 29 0
2894518 지하철역 벽면 부조..ㅇㅅㅇ [2] 헤르 미온느갤로그로 이동합니다. 14:56 30 0
2894517 ■요즘 Si정도 수준 신입 대세포폴이 뭐에요? [2] ㅇㅇ갤로그로 이동합니다. 14:37 41 0
2894516 배고파서 김치찜 만들고 스팸올렸어 ㅇㅅㅇ [1] ㅇㅇ(223.38) 14:35 31 0
2894513 햄지에 보스터.gif [4] ♥덩냥이♥갤로그로 이동합니다. 13:58 62 0
2894512 태연 ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 13:35 28 0
2894511 햄들 ㅠㅠ 웹사이트같은거 만드는건 어렵지않나요? [2] 프갤러(222.97) 13:34 46 0
2894510 하루 한 번 헤르미온느 찬양 헤르 미온느갤로그로 이동합니다. 13:33 39 0
2894507 나님 애널 글 하나 쓰고싶구낭 ♥덩냥이♥갤로그로 이동합니다. 13:13 39 0
2894505 개발 공부 프로그램 만들어보다 포기 시간낭비인가요? [1] 프갤러(211.234) 12:41 44 0
2894504 러스트에서 Memory Safety의 범위는 [8] 프갤러(211.114) 12:41 53 0
2894503 Rust 개븅신 맨날 자랑/경멸에 염병 떨더만 Ada 한테 따먹히고 나르시갤로그로 이동합니다. 11:58 29 0
2894502 Ada 언어 성장세로 보는 투자 판단 나르시갤로그로 이동합니다. 11:52 77 0
2894501 미국이 관세협상에서 한국에 집착하는 이유 발명도둑잡기(118.216) 11:46 37 0
2894500 [대한민국] 민주주의와 사각지대에 있는 세력 프갤러(112.172) 11:44 22 0
2894499 Rust 에서 Ada 로 추세가 전환됨 ㅋㅋㅋ 나르시갤로그로 이동합니다. 11:39 40 0
2894498 리재명 드럼통을 부탁해 민심 근황 ㄹㅇ ♥덩냥이♥갤로그로 이동합니다. 11:25 37 0
2894497 확실히 코딩배우고나서 장점이 인터넷서 ㅆㅇㅆ찡갤로그로 이동합니다. 11:24 53 0
2894496 Ada에서 메시지 전달, 동적 디스패치 등으로 OOP에 대한 명확한 접근 나르시갤로그로 이동합니다. 11:17 24 0
2894495 해외에서는 Rust 대신 Ada 로 고고싱~~ 이러한 분위기인데 ㅋ [2] 나르시갤로그로 이동합니다. 11:08 42 0
2894494 한국 해커 뉴스에 올라온 Ada vs Rust ㅋㅋ 나르시갤로그로 이동합니다. 11:05 43 0
2894493 aur.archlinux.org 가 사망했나봅니다 나르시갤로그로 이동합니다. 10:47 27 0
뉴스 '신인감독 김연경' 한때 IBK 유망주였던 세터 이진, 친정팀 상대로 선전포고! “잘하는 거 보여주고 나올 것” 디시트렌드 10.05
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2