디시인사이드 갤러리

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

갤러리 본문 영역

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

나르시갤로그로 이동합니다. 2025.09.27 19:15:01
조회 43 추천 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
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 우리나라를 대표해서 UN 연설자로 내보내고 싶은 스타는? 운영자 25/09/29 - -
AD 프로게이머가 될테야!! 운영자 25/10/01 - -
2892478 류도그가 병신인건 LLM을 써서 만들건 코딩을 타이핑하건 ㅆㅇㅆ(124.216) 09.27 51 0
2892476 오늘 하루종일 분석만 했다 [1] 루도그담당(58.239) 09.27 80 0
2892475 슬슬 류도그 또 나왔구만 ㅆㅇㅆ(124.216) 09.27 49 0
2892474 사기꾼은 ㅆㅇㅆ지 프갤러(210.217) 09.27 77 3
2892473 애초에 모든 학문의 근간은 암기서 시작함 ㅆㅇㅆ(124.216) 09.27 45 0
2892472 52Hz ♥냥덩이♥갤로그로 이동합니다. 09.27 42 0
2892471 애새끼좀 더 번식해라 이기이기 ㅋㅋㅋㅋ 타이밍뒷.통수한방(1.213) 09.27 41 0
2892470 개발 암기 잘할수록 유리함 애초에 대부분 프로그램 패턴이 정해져있어서 ㅆㅇㅆ(124.216) 09.27 62 0
2892469 파워 a.s q.n.a 넥도리아(220.74) 09.27 46 0
2892468 git 커밋 로그 영어로 써 줘 ㅋㅋ [1] 나르시갤로그로 이동합니다. 09.27 71 0
2892466 시발거 존나 뻥뻥거리네 프갤러(49.165) 09.27 64 0
2892465 코드에 문제가 있는지 확인해. [2] 나르시갤로그로 이동합니다. 09.27 78 0
2892464 지금 개발판 사기꾼 바닥임 [6] 프갤러(220.93) 09.27 121 0
2892460 아 씨발 개발자 포기할려니까 할만한게 딱히 없음 [9] ㅇㅇ갤로그로 이동합니다. 09.27 109 0
2892459 민생쿠폰10만원받았는데 식비로도 10만원 넘게필요하지않음?? 타이밍뒷.통수한방(1.213) 09.27 39 0
2892458 XIM 통신 구현에서 공용체 union 사용 예시 나르시갤로그로 이동합니다. 09.27 40 0
저번 프붕이 통신 프로토콜 질문에 대한 실제 예시 코드 ㅋㅋ 나르시갤로그로 이동합니다. 09.27 43 0
2892455 공채 합격률이 상채 합격률보다 더 높은가? ㅇㅇ갤로그로 이동합니다. 09.27 37 0
2892453 프붕이들 깜놀랄 프로토콜 설계 ㅋㅋ [5] 나르시갤로그로 이동합니다. 09.27 74 0
2892452 고수분들은 코테 준비 어케 했음? 코테>>약간 후기도 있음 프갤러(114.205) 09.27 206 0
2892443 다음과 같은 에러가 났다. 어떻게 해결하는 것이 좋겠는가? [1] 나르시갤로그로 이동합니다. 09.27 66 0
2892442 다음 코드의 형변환에 문제 없지? 나르시갤로그로 이동합니다. 09.27 55 0
2892439 디자인 = 설계, 오늘 제미니 제안 사항 나르시갤로그로 이동합니다. 09.27 69 0
2892437 쓰레기통에 재활용하기에는 방의물건이 200-300 가치다 책포함. [2] 넥도리아(220.74) 09.27 38 0
2892434 내 자신의 디자인적 능력이 너무 덜떨어져있다 [4] ㅆㅇㅆ(124.216) 09.27 59 0
2892433 ■램16기가면 윈도우는 쓰지 말아야함? OS추천좀 [4] ㅇㅇ갤로그로 이동합니다. 09.27 84 0
2892431 주말이나 휴가때 즐기는 취미나 휴식활동 있음?? [3] ㅇㅇ(223.39) 09.27 56 0
2892430 알고리즘 강의 들을만한 사람 없음? 프갤러(114.205) 09.27 37 0
2892429 오늘도 코딩하다보면 정신나갈것같군. ㅆㅇㅆ(124.216) 09.27 58 0
2892424 책샀어요. 옆에는 기억이 안나는 SSD 넥도리아(220.74) 09.27 66 0
2892423 이미 메신저 앱 겁나 많아요. 위챗이 카톡의 최대 경쟁자입니다 [5] 나르시갤로그로 이동합니다. 09.27 72 0
2892422 몇%정도 점유율 뺏겨야 나머지 사람들도 움직이려나 프갤러(45.64) 09.27 40 0
2892419 지금 메신저앱 나오면 빈집털기 ㅆㄱㄴ인데 ㅇㅇ갤로그로 이동합니다. 09.27 46 0
2892418 카톡한데 서버 이중화 왜 안 했냐고 열라 까던 조센징 정부 [2] 나르시갤로그로 이동합니다. 09.27 118 0
2892417 분석계획실행피드백 [2] 공기역학갤로그로 이동합니다. 09.27 70 0
2892414 우체국은 서버 이중화도 안 한거냐?? 대박인걸 나르시갤로그로 이동합니다. 09.27 102 0
2892413 우체국 화재) 한국 IT가 이정도로 개판이다. [3] 나르시갤로그로 이동합니다. 09.27 134 0
2892412 카카오뱅크 욕했던 조센징들 보셈 나르시갤로그로 이동합니다. 09.27 63 0
2892410 뜌.. 뜌땨잇!! [2] ♥냥덩이♥갤로그로 이동합니다. 09.27 68 1
2892409 역시 착착 진행중이었구만 ㄷㄷ ㅇㅇ갤로그로 이동합니다. 09.27 64 0
2892407 인지과학조져라 손발이시립디다갤로그로 이동합니다. 09.27 44 0
2892406 노인들이 주장하는 노력과 운이라는 말 존나 좇같음 타이밍뒷.통수한방(1.213) 09.27 45 0
2892405 카톡 점유율 보아할 때 카톡은 착한 편 [1] 나르시갤로그로 이동합니다. 09.27 54 0
2892403 구글씨발 애미뒤진새끼들 광고존나덕지덕지줘패고싶다 [1] 밀우갤로그로 이동합니다. 09.27 44 0
2892401 나르시 뭐임? 간첩임? 카톡 욕하는 이유- [1] 프갤러(121.172) 09.27 47 0
2892400 라면이 이렇게 먹으면 은근 영양적으로 잘맞고 맛있기까지 하다니까;; [1] ㅇㅇ(223.39) 09.27 61 0
2892398 카톡 욕하는 놈들 구글 유튜브는 왜 욕 안 해? [1] 나르시갤로그로 이동합니다. 09.27 46 0
2892397 카톡 욕하는 놈들 지옥에나 떨어져라 나르시갤로그로 이동합니다. 09.27 44 0
2892396 [애니뉴스] 현재 만들고 있는 웹소설 사이트- 프갤러(121.172) 09.27 49 0
2892395 카톡 좆같죠? 우리꺼 써보세요 라는 전략으론 안되고 [1] 프갤러(110.8) 09.27 55 0
뉴스 트와이스, 북미·유럽 월드투어 추가 개최 디시트렌드 10.02
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2