디시인사이드 갤러리

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

갤러리 본문 영역

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

나르시갤로그로 이동합니다. 2025.09.27 19:15:01
조회 44 추천 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 - -
2892830 여기서 좀 정상적인 글쓰면 저능아들 꼬이는 그게 문제긴함 [1] ㅆㅇㅆ(124.216) 09.29 72 0
2892829 님들 진짜 프로그래머 맞나요 [3] Fjeoeieie갤로그로 이동합니다. 09.28 125 0
2892828 드이어 야구 동영상 옮긴다. 외장 볼륨있게 넥도리아(220.74) 09.28 41 0
2892824 나님 월 마일리지 인증 [7] ♥냥덩이♥갤로그로 이동합니다. 09.28 132 0
2892823 나님 이민 전략 칼럼 하나 쓰긴 써야하는데 [3] ♥냥덩이♥갤로그로 이동합니다. 09.28 93 0
2892804 나님 한국자격증 다 외국자격증으로 바꾸는중 [5] ♥냥덩이♥갤로그로 이동합니다. 09.28 115 0
2892788 클로드랑 GPT 물어봐서 앱 만들었어요 [2] zian갤로그로 이동합니다. 09.28 84 0
2892783 얼룩젖소무늬의 비밀 [1] ♥냥덩이♥갤로그로 이동합니다. 09.28 62 0
2892772 ❤+ [3] ♥냥덩이♥갤로그로 이동합니다. 09.28 102 0
2892769 오늘은 뭐할까 고민되네 ㅠㅠ 나르시갤로그로 이동합니다. 09.28 41 0
2892766 병신찐따 영포티 ㅋㅅㅋ ♥냥덩이♥갤로그로 이동합니다. 09.28 80 0
2892764 어흥! 호피무늬 모모링❤ [4] ♥냥덩이♥갤로그로 이동합니다. 09.28 119 0
2892761 인텔 ddr6 램64 10코어 이상 15w 미만 소비전력 아니면 안사줌 ♥냥덩이♥갤로그로 이동합니다. 09.28 43 0
2892754 주기적으로 컴퓨터, 노트북, 폰 포맷하는데 이상하나 ㅇㅇ(106.241) 09.28 41 0
2892753 웹개발 관련 여기서 물어봄? [2] Noitamina갤로그로 이동합니다. 09.28 81 0
2892747 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥냥덩이♥갤로그로 이동합니다. 09.28 30 0
2892746 AI 시대 최고 수혜 주식은? [4] 나르시갤로그로 이동합니다. 09.28 80 0
2892739 자바로 뭐 할수있음? [8] ㅇㅇ갤로그로 이동합니다. 09.28 101 0
2892737 ㅆㅇㅆ 모기 새끼 프갤러(210.217) 09.28 69 5
2892735 아 조만간 연휴구나 [2] 루도그담당(58.239) 09.28 61 0
2892732 모기떄문에 짜증난다. 프갤러(210.217) 09.28 53 4
2892729 게임쪽은 계속하고 싶은데 돈이 없다. ㅆㅇㅆ(124.216) 09.28 55 0
2892726 게임쪽 창업<<이거 씨발 사기만 안먹었어도 계속했는데 [4] ㅆㅇㅆ(124.216) 09.28 102 0
2892725 if문에서 3일 걸리는 애들도 있네 [6] 루도그담당(58.239) 09.28 124 0
2892720 매일 4~5시간 자니까 건강이 엉망되었네 ㅎㅎ 나르시갤로그로 이동합니다. 09.28 34 0
2892719 ㅆㅇㅆ 이사람 요즘 뭐함? [2] ㅇㅇ갤로그로 이동합니다. 09.28 79 3
2892718 SM이 기술력이 낮다? 규모도 기술력이란걸 간과하지마 프갤러(219.115) 09.28 39 0
2892708 나님두 부천Bj 하구 싶당 야차 뜨실 분 구함 ♥냥덩이♥갤로그로 이동합니다. 09.28 56 0
2892697 하... 할 일들은 많고 시간은 없고 마음만 조급하고 후달린다... ㅇㅇ(223.38) 09.28 71 0
2892691 게임 같은거 만들 때, 컴하하스러운 내용 넣으면 어떤거 같음? [1] ㅇㅇ(106.241) 09.28 38 0
2892688 [애니뉴스] 소설 연재 사이트 제작 현황 프갤러(121.172) 09.28 31 0
2892677 러스트는 어려운 언어가 아니라 귀찮은 언어입니다. 프갤러(110.8) 09.28 54 0
2892676 방정리중 [1] 넥도리아(220.74) 09.28 63 0
2892673 크래시 에러 찾기 겁나 쉬워요 나르시갤로그로 이동합니다. 09.28 54 0
2892672 ■개발자 평균 정년은 40대 중반.jpg ㅇㅇ갤로그로 이동합니다. 09.28 102 1
2892670 리액트 이런거 쓰는거 진짜 위험하네 [3] ㅇㅇ(118.34) 09.28 177 0
2892669 러스트 아무도 안 써요 [9] 나르시갤로그로 이동합니다. 09.28 82 0
2892667 비전공자 특 ㅇㅇ(211.234) 09.28 61 2
2892666 바이브코딩으로는 넘을 수 없는 장벽은 존재함. 컴파일러라거나 ㅆㅇㅆ(124.216) 09.28 82 0
2892665 그래도 도저히 프로그래밍 못 놓겠다하면 바이브코딩부터 시작해라 ㅆㅇㅆ(124.216) 09.28 66 0
2892664 그냥 중딩이면 바이브 코딩으로 시작해라 파이썬, 자바 배우는건 그때가서 ㅆㅇㅆ(124.216) 09.28 74 0
2892662 중고등학생이면 그냥 대학 성적 잘받아라. 이거밖에 없다 [4] ㅆㅇㅆ(124.216) 09.28 108 0
2892661 중학교 2학년 프로그래밍 시작하려면 [2] 프갤러(221.154) 09.28 94 0
2892656 지피티 코드만 수정하면되요 이러는 애들 뺨때리고 싶은데 정상이냐 ㅆㅇㅆ(124.216) 09.28 58 0
2892650 근데 카톡 서비스 절대 못되돌릴거 같은데 저거 광고비 받았을거 아냐 ㅆㅇㅆ(124.216) 09.28 64 1
2892649 임마들 맨날 유튜브에 낚이는 이유도 알거 같노 ㅇㅇ갤로그로 이동합니다. 09.28 49 0
2892648 냥덩 versus 야옹 ㅇㅇ(223.38) 09.28 51 0
2892647 런타임 디버깅 개지랄해야하는 것들 러스트는 컴파일 타임에 거의 잡습니다. 프갤러(223.63) 09.28 47 0
2892646 러스트 홍보는 영업이 아닙니다. 프갤러(223.63) 09.28 46 0
2892645 나님은 마허라임 ㅇㅅㅇ ♥냥덩이♥갤로그로 이동합니다. 09.28 50 0
뉴스 의왕시, 2025년 제12회 의왕한글한마당 개최 디시트렌드 10.02
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2