디시인사이드 갤러리

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

갤러리 본문 영역

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

나르시갤로그로 이동합니다. 2025.09.27 19:15:01
조회 53 추천 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 - -
2894094 충격적임.. ♥덩냥이♥갤로그로 이동합니다. 10.04 52 0
2894093 대학원 수료한게 석사쪽 의뢰가 간간히 오는게 장점 ㅆㅇㅆ찡갤로그로 이동합니다. 10.04 66 0
2894092 오늘 코퍼스 언어학 관련 말뭉치해석 파싱 파이프라인 만듬 [3] ㅆㅇㅆ찡갤로그로 이동합니다. 10.04 70 0
2894090 웨이랜드 좆같은거 맞음 ㅇㅇ [5] 박민준갤로그로 이동합니다. 10.04 69 0
2894089 내란범 뇌물범만 골라서 빠는 2찍 일베들만 하겠냐 [3] 프갤러(110.8) 10.04 69 2
2894088 2찍은 민주당 정권이 하는 모든것에 반대하지 않는다 ㅇㅇ(121.168) 10.04 59 0
2894087 토스도 판교어쓰냐? [2] 밀우갤로그로 이동합니다. 10.04 94 0
2894084 내일 하루 쉬고 또 매일 일해야 하는구나 ㅠㅠ 나르시갤로그로 이동합니다. 10.04 53 0
2894082 weston 업무용 데탑에 대해 레딧에 나르시갤로그로 이동합니다. 10.04 44 0
2894081 속보) 이진숙 방통위원장 불법체포 기각 [4] ♥덩냥이♥갤로그로 이동합니다. 10.04 74 1
2894080 일단 사람 수준은 갖추고 정치 논하자 [2] 프갤러(61.74) 10.04 61 0
2894078 중졸 이하들이 위선이 어쩌고 저쩌고 [6] 프갤러(27.175) 10.04 68 0
2894077 민주당 정권이 위선을 독점하는게 아님 ㅇㅇ(121.168) 10.04 54 0
2894076 코딩을 너무 쉽게 생각해서 문제다. [1] 프갤러(59.16) 10.04 75 0
2894075 술은 무조건 낮술임 [8] ♥덩냥이♥갤로그로 이동합니다. 10.04 68 0
2894074 연휴 모하지 하는 달붕이들은 위한 유흥 가성비 대충 정리 ㅇㅇ(118.235) 10.04 47 0
2894073 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥덩냥이♥갤로그로 이동합니다. 10.04 44 0
2894070 2찍이 실력 타령하는게 제일 이해 안돼 [3] 프갤러(223.63) 10.04 58 0
2894068 취미로 플밍하는새끼들 특징 [1] 프갤러(211.235) 10.04 92 0
2894067 판교어는 중견까지만 잘 쓰는듯. [2] 프갤러(211.177) 10.04 166 1
2894066 갈비 먹습니다. 넥도리아(220.74) 10.04 45 0
2894061 내가 맥주를 안먹는 이유 프갤러(210.217) 10.04 57 1
2894060 점심 간식 저녁 발명도둑잡기(118.216) 10.04 48 0
2894057 인스타 개인정보가 국외이전으로 피싱당해서 [2] 넥도리아(220.74) 10.04 64 0
2894055 이게 내 포트폴리온데 어디까지 취업가능함? [3] 프갤러(210.217) 10.04 121 2
2894053 추석 유흥 50% 할인 리스트?ㅋㅋㅋㅋㅋㅋㅋ ㅇㅇ(118.235) 10.04 69 0
2894051 와 경기 안좋아서 못나갈꺼 아니까 연봉을 [1] ㅇㅇ(211.234) 10.04 62 0
2894048 이거보면 윤석열이 얼마나 말아먹었는지 보이지 [2] 프갤러(210.217) 10.04 95 3
2894044 김종국 신부가 궁금하다 발명도둑잡기(118.216) 10.04 54 0
2894043 개발자는 두가지 분류가 있다. [1] 프갤러(210.217) 10.04 126 0
2894041 Distant Thunder – Andrew Wyeth 발명도둑잡기(118.216) 10.04 39 0
2894039 난 1찍들이 외치는 실력이라는거 안믿는다 ㅇㅇ(121.168) 10.04 60 0
2894037 추석연휴 10일이나 쉬니까 코딩좀 해야지 [1] 박민준갤로그로 이동합니다. 10.04 63 0
2894036 ■요즘 Si수준 신입 대세포폴이 뭐에요? [1] ㅇㅇ갤로그로 이동합니다. 10.04 80 0
2894034 [단독] 초봉 6300만원 신의 직장 날벼락…취준생 망연자실 무슨일이? 발명도둑잡기(118.216) 10.04 86 0
2894033 우드스톡, 인류역사상 최대의 축제[손호철의 미국사 뒤집어보기] 발명도둑잡기(118.216) 10.04 48 0
2894032 게이샤의 비극 소비하며 수동적 일 여성상 강화 발명도둑잡기(118.216) 10.04 33 0
2894031 it업계도 1v1 다이다이까서 결과가 나오면 좋은데 [1] 프갤러(210.217) 10.04 56 1
2894029 진짜 대한민국에 좆밥새끼들 너무 넘쳐나 ㅋㅋ 프갤러(210.217) 10.04 55 1
2894028 ㅆㅇㅆ는 수재다 [1] 발명도둑잡기(118.216) 10.04 69 0
2894027 나도 바이브코딩으로 크몽 해볼까 [2] 발명도둑잡기(118.216) 10.04 80 0
2894026 gcc rust [1] 발명도둑잡기(118.216) 10.04 48 0
2894023 ㅆㅇㅆ는 뛰어봣자 벼룩임 [1] 프갤러(210.217) 10.04 266 4
2894019 사실 나에게 휴일이란 [1] 발명도둑잡기(118.216) 10.04 48 0
2894018 338688 맞대응) 프듀48 주작과 S엔터 ㅇㅇ(175.223) 10.04 63 0
2894015 차단한 병신 왜 자꾸 헛소리하냐 ㅆㅇㅆ찡갤로그로 이동합니다. 10.04 39 0
2894010 빕스갔다 왔더니 ㅆㅇㅆ새끼 또 개소리하네 ㅋㅋ 프갤러(210.217) 10.04 82 4
2894006 너네 PWA로 설정해두면 휴대폰으로 앱처럼 할 수있단거 알았냐 [2] ㅆㅇㅆ(124.216) 10.04 44 0
2894004 근데 실제 산업에서 AI 쓰는건 에어플로우같은거로 하나? ㅆㅇㅆ(124.216) 10.04 42 0
2894001 프로그래머 실력 고만고만하다. 그렇게 생각하던 시절이 제게도 있었죠 [3] 프갤러(221.146) 10.04 99 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2