Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
potrSend.c File Reference

potrSend 関数の実装。 More...

#include <stdlib.h>
#include <inttypes.h>
#include <porter_const.h>
#include <porter.h>
#include "../potrContext.h"
#include "../potrPeerTable.h"
#include "../infra/potrSendQueue.h"
#include "../infra/compress/compress.h"
#include "../infra/potrLog.h"
#include <pthread.h>
Include dependency graph for potrSend.c:

Go to the source code of this file.

Macros

#define POTR_MUTEX_LOCK_LOCAL(m)
#define POTR_MUTEX_UNLOCK_LOCAL(m)

Functions

static int send_to_peer (struct PotrContext_ *ctx, PotrPeerId peer_id, const uint8_t *ptr, size_t len, int flags, uint16_t base_flags)
POTR_EXPORT int POTR_API potrSend (PotrHandle handle, PotrPeerId peer_id, const void *data, size_t len, int flags)
 メッセージを送信します。

Detailed Description

potrSend 関数の実装。

Author
c-modernization-kit sample team
Date
2026/03/04
Version
1.0.0

Definition in file potrSend.c.

Macro Definition Documentation

◆ POTR_MUTEX_LOCK_LOCAL

#define POTR_MUTEX_LOCK_LOCAL ( m)
Value:
pthread_mutex_lock(m)

Definition at line 29 of file potrSend.c.

Referenced by potrSend().

◆ POTR_MUTEX_UNLOCK_LOCAL

#define POTR_MUTEX_UNLOCK_LOCAL ( m)
Value:
pthread_mutex_unlock(m)

Definition at line 30 of file potrSend.c.

Referenced by potrSend().

Function Documentation

◆ send_to_peer()

int send_to_peer ( struct PotrContext_ * ctx,
PotrPeerId peer_id,
const uint8_t * ptr,
size_t len,
int flags,
uint16_t base_flags )
static

Definition at line 39 of file potrSend.c.

References PotrServiceDef::encrypt_enabled, PotrContext_::global, PotrGlobalConfig::max_payload, POTR_CRYPTO_TAG_SIZE, POTR_ERROR, POTR_FLAG_MORE_FRAG, POTR_PAYLOAD_ELEM_HDR_SIZE, POTR_SEND_BLOCKING, potr_send_queue_push_wait(), potr_send_queue_wait_drained(), POTR_SUCCESS, PotrContext_::send_queue, PotrContext_::send_thread_running, and PotrContext_::service.

Referenced by potrSend().

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

◆ potrSend()

POTR_EXPORT int POTR_API potrSend ( PotrHandle handle,
PotrPeerId peer_id,
const void * data,
size_t len,
int flags )

メッセージを送信します。

Parameters
[in]handlepotrOpenService() で取得したセッションハンドル。
[in]peer_id送信先ピア識別子。
N:1 モード: 有効なピア ID (POTR_PEER_NA / POTR_PEER_ALL 以外) を指定します。
N:1 モード: POTR_PEER_ALL を指定すると全接続ピアへ一斉送信します。
N:1 モード: POTR_PEER_NA を指定すると POTR_ERROR を返します。
1:1 モードおよびその他の通信種別: POTR_PEER_NA または POTR_PEER_ALL を指定します (通常は POTR_PEER_NA を使用)。
[in]data送信するメッセージへのポインタ。
[in]len送信するメッセージのバイト数。
[in]flags送信オプションフラグ。以下のフラグを論理和で組み合わせて指定します。 0 を指定すると非圧縮・ノンブロッキング送信になります。
フラグ 説明
POTR_SEND_COMPRESS メッセージを圧縮して送信します。
POTR_SEND_BLOCKING ブロッキング送信を行います。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

通信種別に応じて以下の宛先へ UDP パケットを送信します。

通信種別 送信先
POTR_TYPE_UNICAST 接続相手の dst_port 宛にユニキャスト送信
POTR_TYPE_MULTICAST multicast_group:dst_port へ送信
POTR_TYPE_BROADCAST broadcast_addr:dst_port へ送信
POTR_TYPE_UNICAST_BIDIR (N:1) peer_id で指定したピアへ送信

compress に POTR_SEND_COMPRESS を指定した場合、内部で圧縮処理を行ってから送信します。
圧縮後のサイズが元のサイズ以上になった場合は、自動的に非圧縮で送信します。
受信側の PotrRecvCallback には、解凍済みの元メッセージが渡されます。
送受信ともにフラグメント化と組み合わせて使用できます。

ノンブロッキング送信 (flags に POTR_SEND_BLOCKING を指定しない場合)
メッセージを内部送信キューに登録して即座に返ります。
実際の sendto はバックグラウンド送信スレッドが非同期に実行します。
キューが満杯の場合は空きが生じるまで待機してからメッセージを登録し、登録後に返ります。
ブロッキング送信 (flags に POTR_SEND_BLOCKING を指定した場合)
呼び出し前に滞留しているノンブロッキング送信のメッセージが すべて sendto 完了するまで待機します。
その後、本呼び出しのメッセージをキューを通じて sendto して返ります。
本関数が返った時点で、自身のメッセージの sendto は完了しています。
Note
圧縮フォーマットには raw DEFLATE (RFC 1951) を使用します。
Linux (zlib) と Windows (Compression API MSZIP|COMPRESS_RAW) は 同一フォーマットを出力するため、クロスプラットフォーム通信に対応します。
圧縮効果がない場合 (圧縮後サイズ >= 元サイズ) は、アプリケーションへの通知なしに 内部で非圧縮に切り替えて送信します。送受信のデータ内容に影響はありません。
スレッド セーフティ
本関数はスレッドセーフではありません。
同一ハンドルへの並行呼び出しは未定義動作です。
送信は 1 スレッドから行ってください。
Warning
handle が NULL の場合は失敗を返します。
data が NULL の場合は失敗を返します。
len が 0 の場合は失敗を返します。
len が POTR_MAX_MESSAGE_SIZE を超える場合は失敗を返します。
送信スレッドが停止している場合 (potrCloseService 呼び出し後など) は失敗を返します。
N:1 モードで peer_id = POTR_PEER_NA (0) を指定した場合は失敗を返します。
TCP 通信種別 (POTR_TYPE_TCP / POTR_TYPE_TCP_BIDIR) で全 TCP path が切断中に
呼び出した場合は POTR_ERROR_DISCONNECTED (1) を返します
(connect スレッドが非同期に接続を試みている間)。

Definition at line 88 of file potrSend.c.

References PotrPeerContext_::active, PotrContext_::compress_buf, PotrContext_::compress_buf_size, PotrContext_::global, PotrContext_::is_multi_peer, PotrGlobalConfig::max_message_size, PotrContext_::max_peers, peer_find_by_id(), PotrPeerContext_::peer_id, PotrContext_::peers, PotrContext_::peers_mutex, POTR_API, potr_compress(), POTR_ERROR, POTR_ERROR_DISCONNECTED, POTR_EXPORT, POTR_FLAG_COMPRESSED, potr_is_raw_type(), potr_is_tcp_type(), POTR_LOG, POTR_MUTEX_LOCK_LOCAL, POTR_MUTEX_UNLOCK_LOCAL, POTR_PEER_ALL, POTR_PEER_NA, POTR_SEND_BLOCKING, POTR_SEND_COMPRESS, POTR_SUCCESS, POTR_TRACE_ERROR, POTR_TRACE_VERBOSE, send_to_peer(), PotrContext_::service, PotrServiceDef::service_id, PotrContext_::tcp_active_paths, and PotrServiceDef::type.

Referenced by bidir_send_thread_func(), and main().

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