디시인사이드 갤러리

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

갤러리 본문 영역

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

나르시갤로그로 이동합니다. 2025.09.27 19:15:01
조회 47 추천 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 - -
2893117 트럼프 "美 밖에서 만든 영화에 100% 관세...아기 사탕 훔치듯 도둑 발명도둑잡기(118.216) 09.30 33 0
2893116 다음달에는 진짜 250 못채울 수 있겠구만 [4] ㅆㅇㅆ(124.216) 09.30 82 0
2893115 오늘도 3만원 벌었다 [2] ㅆㅇㅆ(124.216) 09.30 68 0
2893114 빛의 속도에 가까워 질수록 [2] 루도그담당(211.184) 09.30 56 0
2893113 카카오 숏폼 사태 cpo가 저지른 짓이라는데 프갤러(27.168) 09.30 39 0
2893112 바보같은 중소 개발자 280:1 조금똑똑한 공무원 43:1 ㅇㅇ(175.197) 09.30 81 0
2893110 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 09.30 31 0
2893109 서울가는거보다 부산 공무원이 더 좋다 ㅇㅇ(175.197) 09.30 59 0
2893108 칼국수집 지나가는데 [1] 발명도둑잡기(211.246) 09.30 40 0
2893107 ai 는 이제 google gemma ,lg ai exaone만사용할련다 타이밍뒷.통수한방(1.213) 09.30 29 0
2893105 내가 느끼는게 차단한 병신이 관심받을라고 댓글다는거 애잔함 ㅆㅇㅆ(124.216) 09.30 35 0
2893104 좁아지는 공무원 임용문.. 부산 행정9급 경쟁률 43대 1 ㅇㅇ(175.197) 09.30 61 0
2893103 생각해보니 카카오톡 2개월 한거치고 버그 적은편이네 프갤러(121.190) 09.30 37 0
2893101 아 피곤하다 [2] ㅆㅇㅆ(124.216) 09.30 44 0
2893098 Rust 어려워 못하겠으면 Ada 하셈 [9] 나르시갤로그로 이동합니다. 09.30 234 0
2893096 스웨디시 12번 정도 가본 후기 ㅇㅇ(118.235) 09.30 77 0
2893094 대학때 수학 공부한건 아직도 후회중 [1] 무관갤로그로 이동합니다. 09.30 84 0
2893093 예전 며느리들의 기분 느끼는중 ㅇ ㅅㅇ; [2] 프갤러(60.196) 09.30 74 0
2893092 저도 전공자입니다만 [2] 루도그담당(118.235) 09.30 86 0
2893091 컴공전공은 전공했다는 사실이 중요한게 아님 [4] ㅇㅇ갤로그로 이동합니다. 09.30 158 1
2893090 array는 결국 포인터로 구현되는거아님? [3] 프갤러(223.39) 09.30 93 0
2893089 님들 학교 다닐때 30대있었음? [5] ㅇㅇ(117.53) 09.30 88 0
2893087 ❤✨☀⭐⚡☘⛩나로님 시작합니당⛩☘⚡⭐☀✨❤ Naro갤로그로 이동합니다. 09.30 27 0
2893086 모바일 신분증은 가짜 블록체인임? [3] 0di갤로그로 이동합니다. 09.30 70 2
2893085 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 09.30 48 0
2893084 regis ter qualifier쓰지말래 [1] 프갤러(223.38) 09.30 50 0
2893082 USB 이런 오류 본 적 있는 사람 있음? [2] 프갤러(209.127) 09.30 58 0
2893081 뉴프로에 재밋는글 많더라 [1] 헬마스터갤로그로 이동합니다. 09.30 65 0
2893080 현시대는 토발즈도 1찍 짱깨편인 무서운 시대이다ㅋㅋ번식 더 해줘라 이기 타이밍뒷.통수한방(1.213) 09.30 45 0
2893079 [행정안전부] 딥페이크 범죄 대응을 위한 AI 탐지 모델 경진대회 (~1 프갤러(14.32) 09.30 64 0
2893078 벽보 또 붙여봐..ㅇㅅㅇ [1] 헤르 미온느갤로그로 이동합니다. 09.30 58 0
2893077 태연 ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 09.30 38 0
2893076 하루 한 번 헤르미온느 찬양 헤르 미온느갤로그로 이동합니다. 09.30 67 0
2893075 와.... 프갤러(210.217) 09.30 67 1
2893074 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 09.30 66 0
2893073 님들 저 졸업작품 주제 추천좀요 [7] 공기역학갤로그로 이동합니다. 09.30 118 0
2893064 공부할 것 [3] PyTorch갤로그로 이동합니다. 09.30 133 0
2893063 오늘 빌드하고 올릴랬는데 조금 다듬고 올려야겠다 [1] ㅆㅇㅆ찡갤로그로 이동합니다. 09.30 74 0
2893062 프갤 마갤보다도 글리젠 안되는거 실하냐? [2] 헬마스터갤로그로 이동합니다. 09.29 96 1
2893061 에셋이 걱정인 게임 제작 희망자는 보시오 프갤러(110.8) 09.29 68 0
2893060 Kdmapper는 kmdf 지원 안하네 루도그담당(58.239) 09.29 55 0
2893059 강아지 이 사진들 어떠니 메쿠이롱갤로그로 이동합니다. 09.29 93 0
2893057 오늘의 코딩일기 [3] PyTorch갤로그로 이동합니다. 09.29 148 0
2893048 짱깨들은 17억인구로 만든게 qwen 이 끝임?? 타이밍뒷.통수한방(1.213) 09.29 82 0
2893037 토발즈도 짱개편인 세상에 살고있다ㅋㅋ번식 더 해줘 ㅋㅋㅋ 타이밍뒷.통수한방(1.213) 09.29 47 0
2893036 와.. 근방 50m 내에 접근만해도 썩은내 날거 같음 ♥냥덩이♥갤로그로 이동합니다. 09.29 63 0
2893032 유명재한테 사기당했다ㅋㅋ [1] 프갤러(211.45) 09.29 60 0
2893029 현시대는 토발즈도 1찍인 무서운 세상이다 이기이기 [1] 타이밍뒷.통수한방(1.213) 09.29 94 0
2893014 29살 지잡대 신입(10개월 일함) 중소 si 개발 취업 프갤러(118.220) 09.29 212 0
2893011 회사가 어려워서 연봉동결이네 [3] ㅇㅇ(175.197) 09.29 123 0
뉴스 악뮤 수현, 아이유 패션 지적 “예쁜 옷만 입지…” 울상 디시트렌드 10.04
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2