Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
potrDisconnectPeer.c
Go to the documentation of this file.
1
13
14#include <inttypes.h>
15#include <porter_const.h>
16#include <porter.h>
17
18#include "../potrContext.h"
19#include "../potrPeerTable.h"
20#include "../infra/potrLog.h"
21
22#ifndef _WIN32
23 #include <pthread.h>
24 typedef pthread_mutex_t PotrMutexLocal;
25 #define POTR_MUTEX_LOCK_LOCAL(m) pthread_mutex_lock(m)
26 #define POTR_MUTEX_UNLOCK_LOCAL(m) pthread_mutex_unlock(m)
27#else /* _WIN32 */
28 #include <winsock2.h>
29 typedef CRITICAL_SECTION PotrMutexLocal;
30 #define POTR_MUTEX_LOCK_LOCAL(m) EnterCriticalSection(m)
31 #define POTR_MUTEX_UNLOCK_LOCAL(m) LeaveCriticalSection(m)
32#endif /* _WIN32 */
33
34/* doxygen コメントは、ヘッダに記載 */
36{
37 struct PotrContext_ *ctx = (struct PotrContext_ *)handle;
38
39 if (ctx == NULL)
40 {
41 POTR_LOG(POTR_TRACE_ERROR, "potrDisconnectPeer: handle is NULL");
42 return POTR_ERROR;
43 }
44
45 if (peer_id == POTR_PEER_NA || peer_id == POTR_PEER_ALL)
46 {
48 "potrDisconnectPeer: service_id=%" PRId64 " invalid peer_id=%u"
49 " (POTR_PEER_NA or POTR_PEER_ALL not allowed)",
50 ctx != NULL ? ctx->service.service_id : 0, (unsigned)peer_id);
51 return POTR_ERROR;
52 }
53
54 if (!ctx->is_multi_peer)
55 {
57 "potrDisconnectPeer: service_id=%" PRId64 " not in N:1 mode",
58 ctx->service.service_id);
59 return POTR_ERROR;
60 }
61
63
64 {
65 PotrPeerContext *peer = peer_find_by_id(ctx, peer_id);
66
67 if (peer == NULL)
68 {
71 "potrDisconnectPeer: service_id=%" PRId64 " peer_id=%u not found",
72 ctx->service.service_id, (unsigned)peer_id);
73 return POTR_ERROR;
74 }
75
77 "potrDisconnectPeer: service_id=%" PRId64 " peer_id=%u disconnecting",
78 ctx->service.service_id, (unsigned)peer_id);
79
80 /* FIN を送信 */
81 peer_send_fin(ctx, peer);
82
83 /* 接続済み状態のみ DISCONNECTED コールバックを発火 */
84 if (peer->health_alive && ctx->callback != NULL)
85 {
86 peer->health_alive = 0;
87 ctx->callback(ctx->service.service_id, peer->peer_id,
89 }
90
91 /* ピアリソースを解放 */
92 peer_free(ctx, peer);
93 }
94
96 return POTR_SUCCESS;
97}
#define POTR_PEER_NA
ピア ID 未割当を示す予約値。 1:1 モードのコールバックで渡される (ピアの概念がない)。 potrSend() に N:1 モードで指定した場合はエラーを返す。
#define POTR_PEER_ALL
全接続ピアへの一斉送信を指示する予約ピア ID。 N:1 モードでは全アクティブピアへユニキャスト送信する。 1:1 モードでは唯一のピアへの送信として動作する。
#define POTR_SUCCESS
成功の戻り値を表す定数。
#define POTR_ERROR
失敗の戻り値を表す定数。
通信ライブラリ (動的リンク用) のヘッダーファイル。
#define POTR_API
呼び出し規約マクロ。
Definition porter.h:46
#define POTR_EXPORT
DLL エクスポート/インポート制御マクロ。
Definition porter.h:37
通信ライブラリの定数ファイル。
@ POTR_TRACE_ERROR
エラー。操作の失敗を記録。TRACE_LV_ERROR (1) と同値。
@ POTR_TRACE_INFO
情報。TRACE_LV_INFO (3) と同値。
struct PotrContext_ * PotrHandle
セッションハンドル。
uint32_t PotrPeerId
ピア識別子。
Definition porter_type.h:32
@ POTR_EVENT_DISCONNECTED
切断を検知 (タイムアウト / FIN 受信 / REJECT 受信)。data=NULL, len=0。
セッションコンテキスト内部定義ヘッダー。
struct PotrPeerContext_ PotrPeerContext
N:1 モードにおける個別ピアのコンテキスト。
#define POTR_MUTEX_LOCK_LOCAL(m)
#define POTR_MUTEX_UNLOCK_LOCAL(m)
pthread_mutex_t PotrMutexLocal
POTR_EXPORT int POTR_API potrDisconnectPeer(PotrHandle handle, PotrPeerId peer_id)
指定ピアを切断します (N:1 モード専用)。
porter 内部ログマクロ定義ヘッダー。
#define POTR_LOG(level,...)
porter 内部ログ出力マクロ。
Definition potrLog.h:68
void peer_send_fin(struct PotrContext_ *ctx, PotrPeerContext *peer)
ピアの全パスへ FIN パケットを送信する。
PotrPeerContext * peer_find_by_id(struct PotrContext_ *ctx, PotrPeerId peer_id)
peer_id でピアを検索する。
void peer_free(struct PotrContext_ *ctx, PotrPeerContext *peer)
ピアリソースを解放してスロットをクリアする。
N:1 モード用ピアテーブル管理モジュールの内部ヘッダー。
セッションコンテキスト構造体。PotrHandle の実体。
PotrRecvCallback callback
受信コールバック。
int is_multi_peer
1: N:1 モード (src_addr/src_port 省略), 0: 1:1 モード。
PotrServiceDef service
サービス定義。
PotrMutex peers_mutex
ピアテーブル保護用ミューテックス。
volatile int health_alive
疎通状態 (1: alive, 0: dead/未接続)。
PotrPeerId peer_id
外部公開用ピア識別子 (単調増加カウンタから付与)。
int64_t service_id
サービス ID。