Document of c-modernization-kit (porter) 1.0.0

Loading...
Searching...
No Matches
Functions
potrOpenService.c File Reference

potrOpenService 関数の実装。 More...

`#include <stdlib.h>`
`#include <inttypes.h>`
`#include <string.h>`
`#include <sys/socket.h>`
`#include <netinet/in.h>`
`#include <arpa/inet.h>`
`#include <unistd.h>`
`#include <time.h>`
`#include <porter_const.h>`
`#include <porter.h>`
`#include "../protocol/packet.h"`
`#include "../protocol/window.h"`
`#include "../potrContext.h"`
`#include "../potrPeerTable.h"`
`#include "../thread/potrRecvThread.h"`
`#include "../thread/potrHealthThread.h"`
`#include "../thread/potrConnectThread.h"`
`#include "../infra/compress/compress.h"`
`#include "../infra/potrSendQueue.h"`
`#include "../thread/potrSendThread.h"`
`#include "../util/potrIpAddr.h"`
`#include "../infra/potrLog.h"`
Include dependency graph for potrOpenService.c:

Go to the source code of this file.

## Functions
static PotrSocket | open_socket_unicast (struct in_addr bind_addr, uint16_t port)
static void | generate_session (struct PotrContext_ *ctx)
static PotrSocket | open_socket_multicast (const PotrServiceDef *def, struct in_addr src_if, int is_receiver)
static PotrSocket | open_socket_broadcast (uint16_t src_port, uint16_t dst_port, struct in_addr src_if, int is_receiver)
static void | cleanup_sockets (struct PotrContext_ *ctx)
static void | ctx_cleanup (struct PotrContext_ *ctx)
static int | open_socket_tcp_receiver (struct PotrContext_ *ctx, int path_idx)
static int | open_socket_tcp_sender (struct PotrContext_ *ctx, int path_idx)
POTR_EXPORT int POTR_API | potrOpenService (const PotrGlobalConfig *global, const PotrServiceDef *service, PotrRole role, PotrRecvCallback callback, PotrHandle *handle)
| 設定構造体から指定サービスを開きます。

## Detailed Description

potrOpenService 関数の実装。

Author
  c-modernization-kit sample team

Date
  2026/03/04

Version
  1.0.0

Copyright
  Copyright (C) CompanyName, Ltd. 2026. All rights reserved.

Definition in file potrOpenService.c.

## Function Documentation

## ◆ open_socket_unicast()

PotrSocket open_socket_unicast | ( | struct in_addr | bind_addr,
|  | uint16_t | port )
| static

Definition at line 47 of file potrOpenService.c.

References POTR_INVALID_SOCKET.

Referenced by potrOpenService().

Here is the caller graph for this function:

## ◆ generate_session()

void generate_session | ( | struct PotrContext_ * | ctx | ) |
| static

Definition at line 85 of file potrOpenService.c.

References PotrContext_::session_id, PotrContext_::session_tv_nsec, and PotrContext_::session_tv_sec.

Referenced by potrOpenService().

Here is the caller graph for this function:

## ◆ open_socket_multicast()

PotrSocket open_socket_multicast | ( | const PotrServiceDef * | def,
|  | struct in_addr | src_if,
|  | int | is_receiver )
| static

Definition at line 115 of file potrOpenService.c.

References PotrServiceDef::dst_port, PotrServiceDef::multicast_group, parse_ipv4_addr(), POTR_INVALID_SOCKET, POTR_SUCCESS, and PotrServiceDef::src_port.

Referenced by potrOpenService().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ open_socket_broadcast()

PotrSocket open_socket_broadcast | ( | uint16_t | src_port,
|  | uint16_t | dst_port,
|  | struct in_addr | src_if,
|  | int | is_receiver )
| static

Definition at line 211 of file potrOpenService.c.

References POTR_INVALID_SOCKET.

Referenced by potrOpenService().

Here is the caller graph for this function:

## ◆ cleanup_sockets()

void cleanup_sockets | ( | struct PotrContext_ * | ctx | ) |
| static

Definition at line 274 of file potrOpenService.c.

References POTR_INVALID_SOCKET, POTR_MAX_PATH, and PotrContext_::sock.

Referenced by ctx_cleanup().

Here is the caller graph for this function:

## ◆ ctx_cleanup()

void ctx_cleanup | ( | struct PotrContext_ * | ctx | ) |
| static

Definition at line 293 of file potrOpenService.c.

References cleanup_sockets(), PotrContext_::compress_buf, PotrContext_::crypto_buf, PotrContext_::frag_buf, PotrContext_::is_multi_peer, peer_table_destroy(), PotrContext_::peers, POTR_INVALID_SOCKET, POTR_MAX_PATH, PotrContext_::recv_buf, PotrContext_::recv_window, PotrContext_::send_window, PotrContext_::send_wire_buf, PotrContext_::tcp_conn_fd, PotrContext_::tcp_listen_sock, and window_destroy().

Referenced by potrOpenService().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ open_socket_tcp_receiver()

int open_socket_tcp_receiver | ( | struct PotrContext_ * | ctx,
|  | int | path_idx )
| static

Definition at line 343 of file potrOpenService.c.

References PotrServiceDef::dst_addr, PotrContext_::dst_addr_resolved, PotrServiceDef::dst_port, POTR_ERROR, POTR_INVALID_SOCKET, POTR_SUCCESS, resolve_ipv4_addr(), PotrContext_::service, PotrServiceDef::src_addr, PotrContext_::src_addr_resolved, and PotrContext_::tcp_listen_sock.

Referenced by potrOpenService().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ open_socket_tcp_sender()

int open_socket_tcp_sender | ( | struct PotrContext_ * | ctx,
|  | int | path_idx )
| static

Definition at line 417 of file potrOpenService.c.

References PotrServiceDef::dst_addr, PotrContext_::dst_addr_resolved, POTR_ERROR, POTR_LOG, POTR_SUCCESS, POTR_TRACE_ERROR, resolve_ipv4_addr(), PotrContext_::service, PotrServiceDef::src_addr, and PotrContext_::src_addr_resolved.

Referenced by potrOpenService().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ potrOpenService()

POTR_EXPORT int POTR_API potrOpenService | ( | const PotrGlobalConfig * | global,
|  | const PotrServiceDef * | service,
|  | PotrRole | role,
|  | PotrRecvCallback | callback,
|  | PotrHandle * | handle )

設定構造体から指定サービスを開きます。

Parameters

  [in] | global | グローバル設定構造体へのポインタ。
  [in] | service | サービス定義構造体へのポインタ。
  [in] | role | 役割種別。POTR_ROLE_SENDER または POTR_ROLE_RECEIVER。
  [in] | callback | イベント発生時に呼び出されるコールバック関数 (PotrRecvCallback)。 POTR_ROLE_RECEIVER の場合は必須。データ受信・接続検知・切断検知を受け取る。 POTR_ROLE_SENDER の場合は通常 NULL を指定すること。 ただし POTR_TYPE_TCP_BIDIR および POTR_TYPE_UNICAST_BIDIR では SENDER にもコールバックが必須。これらの種別では POTR_ROLE_SENDER でも callback が NULL の場合は失敗を返します。
  [out] | handle | 成功時にセッションハンドルを格納するポインタ。

Returns
  成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

設定構造体からサービス定義を取得し、UDP ソケットを初期化します。
role と callback の組み合わせが不正な場合は POTR_ERROR を返します。
role と設定の IP アドレスが不整合 (bind 失敗など) の場合も POTR_ERROR を返します。
通信種別・役割に応じて以下のソケット設定を行います。

通信種別 | 役割 | bind アドレス | bind ポート
--- | --- | --- | ---
POTR_TYPE_UNICAST | 送信者 | src_addr | src_port
POTR_TYPE_UNICAST | 受信者 | dst_addr | dst_port
POTR_TYPE_MULTICAST | 送信者 | INADDR_ANY | src_port
POTR_TYPE_MULTICAST | 受信者 | INADDR_ANY | dst_port
POTR_TYPE_BROADCAST | 送信者 | src_addr | src_port
POTR_TYPE_BROADCAST | 受信者 | INADDR_ANY | dst_port

POTR_ROLE_RECEIVER の場合、内部で受信スレッドを起動します。

使用例 (受信者)
  void on_recv(int64_t service_id, PotrPeerId peer_id,
  PotrEvent event, const void *data, size_t len) {
  (void)peer_id; // 1:1 モードでは常に POTR_PEER_NA
  if (event == POTR_EVENT_CONNECTED)
  printf("service %" PRId64 ": connected\n", service_id);
  else if (event == POTR_EVENT_DISCONNECTED)
  printf("service %" PRId64 ": disconnected\n", service_id);
  else
  printf("service %" PRId64 ": received %zu bytes\n", service_id, len);
  }
  PotrGlobalConfig global = {0};
  global.window_size = 16;
  global.max_payload = 1400;
  global.max_message_size = 65535;
  global.send_queue_depth = 64;
  PotrServiceDef service = {0};
  service.service_id = 1001;
  service.type = POTR_TYPE_UNICAST;
  service.dst_port = 49001;
  strncpy(service.src_addr[0], "127.0.0.1", POTR_MAX_ADDR_LEN - 1);
  strncpy(service.dst_addr[0], "127.0.0.1", POTR_MAX_ADDR_LEN - 1);
  PotrHandle handle;
  if (potrOpenService(&global, &service,
  POTR_ROLE_RECEIVER, on_recv, &handle) == POTR_SUCCESS) {
  // 受信待機中 (受信スレッドが動作)
  potrCloseService(handle);
  }
  POTR_MAX_ADDR_LEN
  #define POTR_MAX_ADDR_LEN
  アドレス文字列の最大長 (バイト、終端 NUL を含む)。
  Definition porter_const.h:93
  POTR_SUCCESS
  #define POTR_SUCCESS
  成功の戻り値を表す定数。
  Definition porter_const.h:30
  potrOpenService
  POTR_EXPORT int POTR_API potrOpenService(const PotrGlobalConfig *global, const PotrServiceDef *service, PotrRole role, PotrRecvCallback callback, PotrHandle *handle)
  設定構造体から指定サービスを開きます。
  Definition potrOpenService.c:445
  potrCloseService
  POTR_EXPORT int POTR_API potrCloseService(PotrHandle handle)
  サービスを閉じます。
  Definition potrCloseService.c:120
  POTR_TYPE_UNICAST
  @ POTR_TYPE_UNICAST
  1:1 通信 (UDP ユニキャスト)。
  Definition porter_type.h:90
  PotrHandle
  struct PotrContext_ * PotrHandle
  セッションハンドル。
  Definition porter_type.h:238
  POTR_ROLE_RECEIVER
  @ POTR_ROLE_RECEIVER
  受信者。
  Definition porter_type.h:116
  PotrPeerId
  uint32_t PotrPeerId
  ピア識別子。
  Definition porter_type.h:32
  PotrEvent
  PotrEvent
  受信イベント種別。
  Definition porter_type.h:284
  POTR_EVENT_DISCONNECTED
  @ POTR_EVENT_DISCONNECTED
  切断を検知 (タイムアウト / FIN 受信 / REJECT 受信)。data=NULL, len=0。
  Definition porter_type.h:287
  POTR_EVENT_CONNECTED
  @ POTR_EVENT_CONNECTED
  送信者からの疎通を初検知 or 復帰。data=NULL, len=0。
  Definition porter_type.h:286
  on_recv
  static void on_recv(int64_t service_id, PotrPeerId peer_id, PotrEvent event, const void *data, size_t len)
  受信コールバック関数。
  Definition recv.c:122
  PotrGlobalConfig
  グローバル設定。
  Definition porter_type.h:177
  PotrGlobalConfig::window_size
  uint16_t window_size
  スライディングウィンドウサイズ (パケット数)。
  Definition porter_type.h:178
  PotrGlobalConfig::send_queue_depth
  uint32_t send_queue_depth
  非同期送信キューの最大エントリ数。デフォルト: POTR_SEND_QUEUE_DEPTH。
  Definition porter_type.h:184
  PotrGlobalConfig::max_payload
  uint16_t max_payload
  最大ペイロード長 (バイト)。
  Definition porter_type.h:179
  PotrGlobalConfig::max_message_size
  uint32_t max_message_size
  1 回の potrSend で送信できる最大メッセージ長 (バイト)。デフォルト: POTR_MAX_MESSAGE_SIZE。
  Definition porter_type.h:183
  PotrServiceDef
  サービス定義。
  Definition porter_type.h:130
  PotrServiceDef::dst_port
  uint16_t dst_port
  宛先ポート番号。サービスの識別子。受信者の bind ポート / 送信者の送信先ポート。(全通信種別で必須)
  Definition porter_type.h:135
  PotrServiceDef::src_addr
  char src_addr[POTR_MAX_PATH][POTR_MAX_ADDR_LEN]
  送信元アドレス [0]=src_addr1 〜 [3]=src_addr4。送信者は bind / 送信インターフェース、受信者は送信元フィルタ。(全通信種別で必須)
  Definition porter_type.h:149
  PotrServiceDef::type
  PotrType type
  通信種別。
  Definition porter_type.h:132
  PotrServiceDef::service_id
  int64_t service_id
  サービス ID。
  Definition porter_type.h:131
  PotrServiceDef::dst_addr
  char dst_addr[POTR_MAX_PATH][POTR_MAX_ADDR_LEN]
  宛先アドレス [0]=dst_addr1 〜 [3]=dst_addr4。送信者は送信先、受信者は bind アドレス。(unicast のみ)
  Definition porter_type.h:150

使用例 (送信者)
  PotrGlobalConfig global = {0};
  global.window_size = 16;
  global.max_payload = 1400;
  global.max_message_size = 65535;
  global.send_queue_depth = 64;
  PotrServiceDef service = {0};
  service.service_id = 1001;
  service.type = POTR_TYPE_UNICAST;
  service.dst_port = 49001;
  strncpy(service.src_addr[0], "127.0.0.1", POTR_MAX_ADDR_LEN - 1);
  strncpy(service.dst_addr[0], "127.0.0.1", POTR_MAX_ADDR_LEN - 1);
  PotrHandle handle;
  if (potrOpenService(&global, &service,
  POTR_ROLE_SENDER, NULL, &handle) == POTR_SUCCESS) {
  potrSend(handle, POTR_PEER_NA, "hello", 5, 0);
  potrCloseService(handle);
  }
  POTR_PEER_NA
  #define POTR_PEER_NA
  ピア ID 未割当を示す予約値。 1:1 モードのコールバックで渡される (ピアの概念がない)。 potrSend() に N:1 モードで指定した場合はエラーを返す。
  Definition porter_type.h:258
  potrSend
  POTR_EXPORT int POTR_API potrSend(PotrHandle handle, PotrPeerId peer_id, const void *data, size_t len, int flags)
  メッセージを送信します。
  Definition potrSend.c:88
  POTR_ROLE_SENDER
  @ POTR_ROLE_SENDER
  送信者。
  Definition porter_type.h:115

スレッド セーフティ
  本関数はスレッドセーフです。
  異なるスレッドから独立したハンドルを取得するために並行して呼び出すことができます。
  ただし取得したハンドルはスレッドセーフではありません。
  同一ハンドルに対する操作は 1 スレッドから行ってください。

Warning
  global が NULL の場合は失敗を返します。
  service が NULL の場合は失敗を返します。
  handle が NULL の場合は失敗を返します。
  POTR_ROLE_RECEIVER かつ callback が NULL の場合は失敗を返します。
  POTR_ROLE_SENDER かつ callback が NULL でない場合は失敗を返します。
  ただし POTR_TYPE_TCP_BIDIR および POTR_TYPE_UNICAST_BIDIR では SENDER にも
  コールバックが必須であり、この場合 callback が NULL の場合は失敗を返します。

Definition at line 445 of file potrOpenService.c.

References PotrServiceDef::broadcast_addr, PotrContext_::callback, comm_recv_thread_start(), PotrContext_::compress_buf, PotrContext_::compress_buf_size, PotrContext_::crypto_buf, PotrContext_::crypto_buf_size, ctx_cleanup(), PotrContext_::dest_addr, PotrServiceDef::dst_addr, PotrContext_::dst_addr_resolved, PotrServiceDef::dst_port, PotrServiceDef::encrypt_enabled, PotrContext_::frag_buf, generate_session(), PotrContext_::global, PotrGlobalConfig::health_interval_ms, PotrServiceDef::health_interval_ms, PotrContext_::health_mutex, PotrGlobalConfig::health_timeout_ms, PotrServiceDef::health_timeout_ms, PotrContext_::health_wakeup, PotrContext_::is_multi_peer, PotrGlobalConfig::max_message_size, PotrGlobalConfig::max_payload, PotrContext_::max_peers, PotrServiceDef::max_peers, PotrServiceDef::multicast_group, PotrContext_::n_path, open_socket_broadcast(), open_socket_multicast(), open_socket_tcp_receiver(), open_socket_tcp_sender(), open_socket_unicast(), PACKET_HEADER_SIZE, parse_ipv4_addr(), peer_table_init(), POTR_API, POTR_COMPRESS_HEADER_SIZE, potr_connect_thread_start(), POTR_CRYPTO_TAG_SIZE, POTR_ERROR, POTR_EXPORT, potr_health_thread_start(), potr_health_thread_stop(), POTR_INVALID_SOCKET, potr_is_tcp_type(), POTR_LOG, POTR_MAX_PATH, POTR_MAX_PAYLOAD, POTR_MAX_WINDOW_SIZE, potr_raw_base_type(), POTR_ROLE_RECEIVER, POTR_ROLE_SENDER, potr_send_queue_destroy(), potr_send_queue_init(), potr_send_thread_start(), potr_send_thread_stop(), POTR_SUCCESS, POTR_TRACE_ERROR, POTR_TRACE_INFO, POTR_TRACE_VERBOSE, POTR_TYPE_BROADCAST, POTR_TYPE_BROADCAST_RAW, POTR_TYPE_MULTICAST, POTR_TYPE_MULTICAST_RAW, POTR_TYPE_TCP, POTR_TYPE_TCP_BIDIR, POTR_TYPE_UNICAST, POTR_TYPE_UNICAST_BIDIR, POTR_TYPE_UNICAST_BIDIR_N1, POTR_TYPE_UNICAST_RAW, PotrContext_::recv_buf, PotrContext_::recv_window, PotrContext_::recv_window_mutex, resolve_ipv4_addr(), PotrContext_::role, PotrContext_::send_queue, PotrGlobalConfig::send_queue_depth, PotrContext_::send_window, PotrContext_::send_wire_buf, PotrContext_::service, PotrServiceDef::service_id, PotrContext_::sock, PotrServiceDef::src_addr, PotrContext_::src_addr_resolved, PotrServiceDef::src_port, PotrContext_::tcp_conn_fd, PotrGlobalConfig::tcp_health_interval_ms, PotrGlobalConfig::tcp_health_timeout_ms, PotrContext_::tcp_listen_sock, PotrContext_::tcp_send_mutex, PotrContext_::tcp_state_cv, PotrContext_::tcp_state_mutex, PotrServiceDef::type, window_init(), and PotrGlobalConfig::window_size.

Referenced by potrOpenServiceFromConfig().

Here is the call graph for this function:
Here is the caller graph for this function: