1 /*********************************************************************************
3 * --------------------------------------------------------------------------------
5 * This file is part of MiMic
6 * Copyright (C)2011 Ryo Iizuka
8 * MiMic is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * For further information please contact.
23 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
25 *********************************************************************************/
26 #ifndef NYLPC_IIUDPSOCKET_H_
27 #define NYLPC_IIUDPSOCKET_H_
30 #include "NyLPC_stdlib.h"
32 #include "NyLPC_NetIf_ip_types.h"
33 #include "NyLPC_iTcpSocket.h"
36 #endif /* __cplusplus */
38 typedef struct NyLPC_TiUdpSocket NyLPC_TiUdpSocket_t;
39 /**********************************************************************
41 **********************************************************************/
46 struct NyLPC_TIPv4RxInfo
48 NyLPC_TUInt16 size;//パケットサイズ
49 NyLPC_TUInt16 port;//受信ポート
50 NyLPC_TUInt16 peer_port;//PEERポート
51 struct NyLPC_TIPv4Addr ip;//受信IP
52 struct NyLPC_TIPv4Addr peer_ip;//PEERIP
56 /**********************************************************************
58 **********************************************************************/
64 * TRUEならパケットを受信キューへ追加する。FALSEならパケットを受信キューへ追加しない。
66 typedef NyLPC_TBool (*NyLPC_TiUdpSocket_onRxHandler)(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info);
72 typedef void (*NyLPC_TiUdpSocket_onPeriodicHandler)(NyLPC_TiUdpSocket_t* i_inst);
74 /**********************************************************************
76 **********************************************************************/
83 * 参加するマルチキャストグループを指定する。
84 * 同じマルチキャスとグループに参加できるのは、システムの中で1つに限られます。
85 * 0を指定した場合、マルチキャスとグループから離脱します。
87 typedef void (*NyLPC_TiUdpSocket_joinMulticast)(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr);
92 typedef void (*NyLPC_TiUdpSocket_setBroadcast)(NyLPC_TiUdpSocket_t* i_inst);
96 * この関数は、ソケットの受信バッファの読み取り位置と、読み出せるデータサイズを返却します。
97 * 関数はポインターを返却するだけで、バッファの読み取り位置をシークしません。
98 * シークするにはNyLPC_cTcpSocket_pseekを使います。
100 typedef NyLPC_TInt32 (*NyLPC_TiUdpSocket_precv)(NyLPC_TiUdpSocket_t* i_inst,const void** o_buf_ptr,const struct NyLPC_TIPv4RxInfo** o_info,NyLPC_TUInt32 i_wait_msec);
102 * 受信バッファを次のバッファまでシークします。
104 typedef void (*NyLPC_TiUdpSocket_pseek)(NyLPC_TiUdpSocket_t* i_inst);
108 * 割り当てたメモリは、releaseSendBuf関数か、psend関数を成功させて開放する必要があります。
110 * 取得したいメモリサイズをセットします。
111 * 関数は要求サイズより小さいメモリを返すことがあります。
113 typedef void* (*NyLPC_TiUdpSocket_allocSendBuf)(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec);
115 typedef void (*NyLPC_TiUdpSocket_releaseSendBuf)(NyLPC_TiUdpSocket_t* i_inst,void* i_buf_ptr);
118 * 事前にAllocしたTxパケットを送信します。
119 * このAPIはゼロコピー送信をサポートするためのものです。
121 * allocSendBufで取得したメモリを指定します。
123 * 関数が失敗した場合、i_buf_ptrは「開放されません。」
125 typedef NyLPC_TBool (*NyLPC_TiUdpSocket_psend)(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,void* i_buf_ptr,int i_len);
130 typedef NyLPC_TInt32 (*NyLPC_TiUdpSocket_send)(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec);
135 typedef void (*NyLPC_TiUdpSocket_setOnRxHandler)(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onRxHandler i_handler);
138 * 非同期タイマ呼び出しハンドラを設定する。
140 typedef void (*NyLPC_TiUdpSocket_setOnPeriodicHandler)(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onPeriodicHandler i_handler);
144 * ソケットのローカルIPのアドレスを返す。
147 typedef const struct NyLPC_TIPv4Addr* (*NyLPC_TiUdpSocket_getSockIP)(const NyLPC_TiUdpSocket_t* i_inst);
149 typedef void (*NyLPC_TiUdpSocket_finalize)(NyLPC_TiUdpSocket_t* i_inst);
152 /**********************************************************************
154 **********************************************************************/
158 struct NyLPC_TiUdpSocket_Interface
160 NyLPC_TiUdpSocket_joinMulticast joinMulticast;
161 NyLPC_TiUdpSocket_setBroadcast setBroadcast;
162 NyLPC_TiUdpSocket_precv precv;
163 NyLPC_TiUdpSocket_pseek pseek;
164 NyLPC_TiUdpSocket_allocSendBuf allocSendBuf;
165 NyLPC_TiUdpSocket_releaseSendBuf releaseSendBuf;
166 NyLPC_TiUdpSocket_psend psend;
167 NyLPC_TiUdpSocket_send send;
168 NyLPC_TiUdpSocket_setOnRxHandler setOnRxHandler;
169 NyLPC_TiUdpSocket_setOnPeriodicHandler setOnPeriodicHandler;
170 NyLPC_TiUdpSocket_getSockIP getSockIP;
171 NyLPC_TiUdpSocket_finalize finalize;
174 struct NyLPC_TiUdpSocket
176 const struct NyLPC_TiUdpSocket_Interface* _interface;
181 #define NyLPC_iUdpSocket_joinMulticast(i_inst,i_addr) ((i_inst)->_interface->joinMulticast((i_inst),(i_addr)))
182 #define NyLPC_iUdpSocket_setBroadcast(i_inst) ((i_inst)->_interface->setBroadcast((i_inst)))
183 #define NyLPC_iUdpSocket_precv(i_inst,o_buf_ptr,o_info,i_wait_msec) ((i_inst)->_interface->precv((i_inst),(o_buf_ptr),(o_info),(i_wait_msec)))
184 #define NyLPC_iUdpSocket_pseek(i_inst) ((i_inst)->_interface->pseek((i_inst)))
185 #define NyLPC_iUdpSocket_allocSendBuf(i_inst,i_hint,o_buf_size,i_wait_in_msec) ((i_inst)->_interface->allocSendBuf((i_inst),(i_hint),(o_buf_size),(i_wait_in_msec)))
186 #define NyLPC_iUdpSocket_releaseSendBuf(i_inst,i_buf_ptr) ((i_inst)->_interface->releaseSendBuf((i_inst),(i_buf_ptr)))
187 #define NyLPC_iUdpSocket_psend(i_inst,i_addr,i_port,i_buf_ptr,i_len) ((i_inst)->_interface->psend((i_inst),(i_addr),(i_port),(i_buf_ptr),(i_len)))
188 #define NyLPC_iUdpSocket_send(i_inst,i_addr,i_port,i_buf_ptr,i_len,i_wait_in_msec) ((i_inst)->_interface->send((i_inst),(i_addr),(i_port),(i_buf_ptr),(i_len),(i_wait_in_msec)))
189 #define NyLPC_iUdpSocket_setOnRxHandler(i_inst,i_handler) ((i_inst)->_interface->setOnRxHandler((i_inst),(i_handler)))
190 #define NyLPC_iUdpSocket_setOnPeriodicHandler(i_inst,i_handler) ((i_inst)->_interface->setOnPeriodicHandler((i_inst),(i_handler)))
191 #define NyLPC_iUdpSocket_getSockIP(i_inst) ((i_inst)->_interface->getSockIP((i_inst)))
192 #define NyLPC_iUdpSocket_finalize(i_inst) ((i_inst)->_interface->finalize((i_inst)))
197 #endif /* __cplusplus */
199 #endif /* NYLPC_IIUDPSOCKET_H_ */