디시인사이드 갤러리

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

갤러리 본문 영역

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

나르시갤로그로 이동합니다. 2025.09.27 19:15:01
조회 34 추천 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 - -
공지 프로그래밍 갤러리 이용 안내 [96] 운영자 20.09.28 47502 65
2892851 오늘의 작사 실마리: 홀로 서서 쳐다보는 불꽃놀이 [1] 발명도둑잡기(118.216) 03:15 7 0
2892850 챗지피티에게 살인을 고백한 사람 발명도둑잡기(118.216) 02:47 9 0
2892849 스레드가 뭔지도 모르는 새끼가 왜 비동기로 개념싸움 거는거야 ㅇㅇ(106.241) 02:39 11 0
2892848 '좆'의 어원 발명도둑잡기(118.216) 02:22 14 0
2892845 '씹'의 어원 발명도둑잡기(118.216) 02:14 9 0
2892844 c언어 [4] 프갤러(218.238) 02:03 28 0
2892843 밥버러지... 고기도 익고... 명절한상 1주 시작. 넥도리아(220.74) 01:36 12 0
2892842 파이썬 질문인데 sha256 체크섬이 일치하지 않는다고 뜨는데.. 프갤러(125.249) 01:32 13 0
2892841 ㅅㅂ... 알콜중독인가 잠이안와서 갈비탕 한그릇에 맥주깠다 ㅇㅇ(223.38) 01:29 12 0
2892840 밥 언재끔 커피 뿌려주었는데... 넥도리아(220.74) 01:25 14 0
2892839 밥짓는중. 넥도리아(220.74) 01:21 8 0
2892838 냥덩이 칼럼 추천 발명도둑잡기(118.216) 01:00 11 0
2892837 인구비례 발명도둑잡기(118.216) 00:57 11 0
2892836 패륜 막가파 부활 조짐 발명도둑잡기(118.216) 00:53 9 0
2892835 고양이 액체설 발명도둑잡기(118.216) 00:50 16 0
2892834 운영체제 충 돼서, 운영체제 모르면 개발이 불가능하다 생각하는데 ㅇㅇ(106.241) 00:47 20 0
2892833 오늘의 발명 실마리: n차원, 비유클리드 훈민정음 발명도둑잡기(118.216) 00:45 12 0
2892832 저희는 가짜 프로그래머입니다. 프갤러(110.8) 00:43 27 0
2892830 여기서 좀 정상적인 글쓰면 저능아들 꼬이는 그게 문제긴함 ㅆㅇㅆ(124.216) 00:09 27 0
2892829 님들 진짜 프로그래머 맞나요 [2] Fjeoeieie갤로그로 이동합니다. 09.28 46 0
2892828 드이어 야구 동영상 옮긴다. 외장 볼륨있게 넥도리아(220.74) 09.28 12 0
2892825 오늘의 소설, 영화 실마리: 모기, 파리가 외계인과 내통 발명도둑잡기(118.216) 09.28 13 0
2892824 나님 월 마일리지 인증 [7] ♥냥덩이♥갤로그로 이동합니다. 09.28 67 0
2892823 나님 이민 전략 칼럼 하나 쓰긴 써야하는데 [3] ♥냥덩이♥갤로그로 이동합니다. 09.28 58 0
2892822 "이대남 해결방안 간단합니다" #김태형 #ㅆㄷㄱ 발명도둑잡기(118.216) 09.28 14 0
2892811 발명도둑잡기(118.216) 09.28 16 0
2892804 나님 한국자격증 다 외국자격증으로 바꾸는중 [5] ♥냥덩이♥갤로그로 이동합니다. 09.28 58 0
2892799 70년간 조선 망한다 노래했지만 미국이 먼저 망할 듯 발명도둑잡기(118.216) 09.28 19 0
2892795 성룡이 짝사랑했던 발명도둑잡기(118.216) 09.28 19 0
2892789 오렌지족 발명도둑잡기(118.216) 09.28 24 0
2892788 클로드랑 GPT 물어봐서 앱 만들었어요 [2] zian갤로그로 이동합니다. 09.28 42 0
2892786 영포티 발명도둑잡기(118.216) 09.28 19 0
2892783 얼룩젖소무늬의 비밀 [1] ♥냥덩이♥갤로그로 이동합니다. 09.28 29 0
2892776 음원 차트 석권한 AI, 추억의 가수도 살려낸다 발명도둑잡기(118.216) 09.28 13 0
2892772 ❤+ [3] ♥냥덩이♥갤로그로 이동합니다. 09.28 47 0
2892769 오늘은 뭐할까 고민되네 ㅠㅠ 나르시갤로그로 이동합니다. 09.28 14 0
2892768 ㅂㅅ ㅉㄸ [1] 발명도둑잡기(118.216) 09.28 41 0
2892767 카톡 개편은 다 높으신 분들의 뜻 발명도둑잡기(118.216) 09.28 24 0
2892766 병신찐따 영포티 ㅋㅅㅋ ♥냥덩이♥갤로그로 이동합니다. 09.28 37 0
2892764 어흥! 호피무늬 모모링❤ [4] ♥냥덩이♥갤로그로 이동합니다. 09.28 62 0
2892761 인텔 ddr6 램64 10코어 이상 15w 미만 소비전력 아니면 안사줌 ♥냥덩이♥갤로그로 이동합니다. 09.28 17 0
2892754 주기적으로 컴퓨터, 노트북, 폰 포맷하는데 이상하나 ㅇㅇ(106.241) 09.28 19 0
2892753 웹개발 관련 여기서 물어봄? [2] Noitamina갤로그로 이동합니다. 09.28 48 0
2892747 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 09.28 16 0
2892746 AI 시대 최고 수혜 주식은? [4] 나르시갤로그로 이동합니다. 09.28 33 0
2892740 짱깨 대공황 국가부도 직전 [1] ♥냥덩이♥갤로그로 이동합니다. 09.28 41 0
2892739 자바로 뭐 할수있음? [8] ㅇㅇ갤로그로 이동합니다. 09.28 66 0
2892737 ㅆㅇㅆ 모기 새끼 프갤러(210.217) 09.28 43 4
2892735 아 조만간 연휴구나 [2] 루도그담당(58.239) 09.28 28 0
뉴스 '불꽃야구' 명품 투수전의 주인공 이대은, ‘에이스 그 자체’인 그가 당황한 이유는? 디시트렌드 09.27
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2