디시인사이드 갤러리

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

갤러리 본문 영역

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

나르시갤로그로 이동합니다. 2025.09.27 19:15:01
조회 21 추천 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/09/22 - -
공지 프로그래밍 갤러리 이용 안내 [96] 운영자 20.09.28 47492 65
2892637 미소녀랑 아기 만들자 ㅇㅅㅇ 류류(118.235) 13:38 5 0
2892636 프갤럼들 맨날 하는게 영업 아니었나?? ㅇㅇ갤로그로 이동합니다. 13:09 12 0
2892635 ■AI 때문에 웹개발 자체를 포기해야하나 고민중이다 [2] ㅇㅇ갤로그로 이동합니다. 12:38 43 0
2892634 친중무능극좌 리짜이밍 부정평가 48% 과반 붕괴 ♥냥덩이♥갤로그로 이동합니다. 12:20 23 0
2892632 무비자 짱깨들 한국오면 전염병 퍼질듯 ♥냥덩이♥갤로그로 이동합니다. 12:15 12 0
2892631 이재명 때문에 국가방역 무너진다 9월29일 중국인 무비자 입국 ♥냥덩이♥갤로그로 이동합니다. 12:14 26 0
2892630 사람이 정말 열심히 살아야 하는것 같아... 항상 제대로 똑바로 살아야해 ㅇㅇ(223.38) 12:13 14 0
2892627 월요일날 우체국에서 틀딱들 정모 하겟네 야옹아저씨갤로그로 이동합니다. 11:33 20 0
2892626 냥덩이 ♥냥덩이♥갤로그로 이동합니다. 11:22 19 0
2892624 컴퓨터 수리 고수 발명도둑잡기(175.223) 10:46 17 0
2892621 비가 어제 왔었다면 발명도둑잡기(183.96) 10:26 14 0
2892620 [대한민국] 미 - 콜로비아 대통령 비자 취소 프갤러(121.172) 10:10 12 0
2892619 자러감..ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 10:07 10 0
2892618 미용실에 왔어요 ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 09:17 19 0
2892617 태연 ㅇㅅㅇ 헤르 미온느갤로그로 이동합니다. 09:03 16 0
2892616 하루 한 번 헤르미온느 찬양 헤르 미온느갤로그로 이동합니다. 08:56 25 0
2892614 러빨러는 왜 러스트 코드 안 올리는 거임? [2] 나르시갤로그로 이동합니다. 07:21 33 0
2892613 병렬처리 먹통 디버깅 ㅋㅋ 나르시갤로그로 이동합니다. 07:20 20 0
2892611 친중극좌 이재명 때문에 관세폭탄 100% 환율 1410원 돌파 [1] ♥냥덩이♥갤로그로 이동합니다. 06:29 46 0
2892567 난데요. 고장한 SSD 펌웨어 복구 어려워요. [1] 넥도리아(223.38) 03:03 33 0
2892565 야동 보는데, 미쳐버릴 것 같다. 넥도리아(223.38) 02:46 49 0
2892562 보통 외주받는 랜딩페이지는 길게 세로로 휴대폰 스크롤에 [4] ㅆㅇㅆ(118.235) 01:38 58 0
2892561 동그라미 세모 네모 다 있는 세계에서 유일한 문자 [1] 발명도둑잡기(118.216) 01:32 31 0
2892559 이동건, 외상후 스트레스 장애→송곳 찌르는 듯한 고통 발명도둑잡기(118.216) 01:25 35 0
2892555 일단 타로 관련 도메인 사려고 고민중 ㅆㅇㅆ(124.216) 01:17 25 0
2892551 음기 충전 발명도둑잡기(118.216) 01:13 17 0
2892547 로우 포인터 안 쓸거면 뭐하러 C++ 씀 [2] ㅇㅇ(106.241) 00:58 58 0
2892545 Ada 프로그래밍, 3.2 예약어 (reserved words) 나르시갤로그로 이동합니다. 00:49 14 0
2892544 Ada 프로그래밍, 3.1 식별자 (identifier) 나르시갤로그로 이동합니다. 00:48 16 0
2892543 근데 영업 전환이라는게 결국은 CTO로써 도태됐거나 그 이상의 기술이 [2] ㅆㅇㅆ(124.216) 00:41 54 0
2892542 어딜가도 커리어 최종은 영업인게 맞냐? [3] 프갤러(125.128) 00:31 53 0
2892541 러스트 사용하는 이유 프갤러(110.8) 00:12 35 0
2892540 C++ 을 사용하는 이유. 프갤러(59.16) 00:12 37 0
2892539 주요 대기업 임원 다 합쳐도 몇천명 안되서 국내외 정부가 인사 관여 가능 발명도둑잡기(118.216) 00:07 27 0
2892537 아씨 MBC 불꽃놀이 유튜브 생방 안끝나서 계속 듣는데 [1] 발명도둑잡기(118.216) 09.27 26 0
2892536 카톡 업데이트 욕하는 애들 특징 [6] 야옹아저씨갤로그로 이동합니다. 09.27 126 7
2892535 c++ 왜쓰냐 [2] 프갤러(1.245) 09.27 57 0
2892534 노드VPN "블루투스 해킹 급증…스마트홈·자동차까지 위협" 발명도둑잡기(118.216) 09.27 24 0
2892533 aws 공부를 한다는게 뭔 말임? [1] 프갤러(220.93) 09.27 42 0
2892532 리액트를 하려다가도 안하게 되는게 프갤러(220.93) 09.27 22 0
2892531 음치를 일컫는 병명 발명도둑잡기(118.216) 09.27 13 0
2892529 진보당 청년 국회의원 손솔의 다짐과 포부 [3] 발명도둑잡기(118.216) 09.27 22 0
2892528 터미네이터3가 시리즈 중 최고임 [1] ♥냥덩이♥갤로그로 이동합니다. 09.27 64 0
2892527 핵전쟁 ♥냥덩이♥갤로그로 이동합니다. 09.27 22 0
2892526 매크로 키보드 자작하는 이유 발명도둑잡기(118.216) 09.27 20 0
2892525 아이폰폴드 펜지원 안하면 절대 안 사줌 ㅅㄱ [1] ♥냥덩이♥갤로그로 이동합니다. 09.27 71 0
2892524 저장용 [1] ♥냥덩이♥갤로그로 이동합니다. 09.27 20 0
2892523 어제 밤 12시, 트위터 라이브 발명도둑잡기(118.216) 09.27 49 0
2892522 야 아래 포폴 사이트 찾는놈아 내가 찾음 ㅇㅇ ㅆㅇㅆ(124.216) 09.27 37 0
뉴스 '트롯챔피언' 홍지윤, 러블리한 '트롯 바비' 출격! '오라'로 성장 입증 디시트렌드 09.27
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2