1 /*********************************************************************************
\r
3 * --------------------------------------------------------------------------------
\r
5 * This file is part of MiMic
\r
6 * Copyright (C)2011 Ryo Iizuka
\r
8 * MiMic is free software: you can redistribute it and/or modify
\r
9 * it under the terms of the GNU Lesser General Public License as published
\r
10 * by the Free Software Foundation, either version 3 of the License, or
\r
11 * (at your option) any later version.
\r
13 * This program is distributed in the hope that it will be useful,
\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 * GNU General Public License for more details.
\r
18 * You should have received a copy of the GNU Lesser General Public License
\r
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
21 * For further information please contact.
\r
23 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
\r
25 *********************************************************************************/
\r
26 #ifndef NYLPC_CTCPSOCKET_H_
\r
27 #define NYLPC_CTCPSOCKET_H_
\r
31 #include "NyLPC_uip.h"
\r
32 #include "NyLPC_os.h"
\r
33 #include "NyLPC_cIPv4Payload.h"
\r
34 #include "NyLPC_cBaseSocket.h"
\r
39 #endif /* __cplusplus */
\r
42 typedef struct NyLPC_TcTcpSocket NyLPC_TcTcpSocket_t;
\r
45 /**********************************************************************
\r
47 * NyLPC_TTcpListenerSynInfo struct
\r
49 **********************************************************************/
\r
52 struct NyLPC_TTcpSocketSynParam
\r
54 struct NyLPC_TIPv4Addr srcaddr;
\r
55 NyLPC_TUInt16 rport;
\r
57 NyLPC_TUInt32 rcv_nxt32;
\r
61 /**********************************************************************
\r
63 * NyLPC_TcTcpSocket_TxQItem struct
\r
65 **********************************************************************/
\r
68 * TXキューの数。この値は8未満にしてください。
\r
70 #define NyLPC_TcTcpSocket_NUMBER_OF_TXQ 4
\r
72 struct NyLPC_TcTcpSocket_TxQItem
\r
75 NyLPC_TUInt32 tick_of_sent;
\r
77 NyLPC_TUInt32 rto32;
\r
79 //パケットのACK番号。この番号を受信できれば、再送パケットは消去可能である。
\r
80 NyLPC_TUInt32 ackno;
\r
83 /**********************************************************************
\r
87 **********************************************************************/
\r
91 struct NyLPC_TIPv4Addr ripaddr; /**< The IP address of the remote host. */
\r
92 const struct NyLPC_TIPv4Addr* lipaddr; /**< ローカルIP*/
\r
93 NyLPC_TUInt16 lport; /**< The local TCP port, in network byte order. */
\r
94 NyLPC_TUInt16 rport; /**< The local remote TCP port, in network byte order. */
\r
95 NyLPC_TUInt32 rcv_nxt32; /**< The sequence number that we expect to receive next. */
\r
96 NyLPC_TUInt32 snd_nxt32; /**< 送信用sqカウンター*/
\r
97 NyLPC_TUInt16 peer_mss; /**< PeerのMSS*/
\r
98 NyLPC_TUInt16 default_mss; /**< Peerの初期MMS*/
\r
100 NyLPC_TUInt16 peer_win;
\r
101 NyLPC_TUInt16 _padding;
\r
103 NyLPC_TUInt32 current_rto32;
\r
113 /**********************************************************************
\r
115 * NyLPC_TcTcpSocket class
\r
117 **********************************************************************/
\r
120 * uipサービスを使用したTCPソケットクラスです。
\r
121 * この関数は2つのタスクから呼び出されます。
\r
122 * [uipTask] -> [cTcpSocket] <- [Application]
\r
123 * ApplicationとuipTaskとの間での排他処理はインスタンスで制御されています。
\r
124 * Application側からのコールは内部でuipTaskとの間で排他処理を実行します。
\r
125 * Application側からのコールはリエントラントではありません。
\r
128 struct NyLPC_TcTcpSocket
\r
131 NyLPC_TcBaseSocket_t _super;
\r
132 //この変数は、uipタスクの実行する関数のみが変更する。
\r
133 struct uip_conn uip_connr;
\r
134 NyLPC_TcFifoBuffer_t rxbuf;
\r
139 struct NyLPC_TcTcpSocket_TxQItem txq[NyLPC_TcTcpSocket_NUMBER_OF_TXQ];
\r
141 volatile NyLPC_TUInt8 tcpstateflags; /**< TCP state and flags. */
\r
145 #define NyLPC_cTcpSocket_getPeerAddr(i_inst) (&((i_inst)->uip_connr.ripaddr))
\r
146 #define NyLPC_cTcpSocket_getPeerPort(i_inst) (((i_inst)->uip_connr.rport))
\r
150 * uipserviceは初期化済である必要があります。
\r
151 * また、暫定条件として、サービスが停止中である必要もあります。
\r
152 * @param i_recv_buf
\r
155 NyLPC_TBool NyLPC_cTcpSocket_initialize(NyLPC_TcTcpSocket_t* i_inst,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len);
\r
158 * uipserviceは初期化済である必要があります。
\r
159 * また、暫定条件として、サービスが停止中である必要もあります。
\r
161 void NyLPC_cTcpSocket_finalize(NyLPC_TcTcpSocket_t* i_inst);
\r
163 NyLPC_TBool NyLPC_cTcpSocket_accept(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec);
\r
168 * -1 - ソケットがクローズしている
\r
170 NyLPC_TInt32 NyLPC_cTcpSocket_precv(NyLPC_TcTcpSocket_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec);
\r
171 void NyLPC_cTcpSocket_pseek(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt16 i_seek);
\r
174 * エラーを検出したら、基本的にはソケットをクローズしてください。
\r
175 * @param i_wait_msec
\r
176 * 送信失敗までの待ち時間を指定します。現在は、
\r
177 * RTT推定ができないため、TCPの再送を考慮して、最低でも10秒(10000)程度を指定してください。
\r
179 * 送信したバイト数を返します。エラーならば0未満の数を返します。
\r
182 NyLPC_TInt32 NyLPC_cTcpSocket_send(NyLPC_TcTcpSocket_t* i_inst,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec);
\r
183 void NyLPC_cTcpSocket_close(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec);
\r
187 * NyLPC_cTcpSocket_psendで送信するための送信バッファ準備します。
\r
189 * 送信を希望するデータサイズを指定します。
\r
190 * アロケータは出来る限り希望に沿ってメモリを返します。
\r
191 * @param o_buf_size
\r
192 * 取得できたバッファサイズを返します。
\r
194 * 成功した場合、送信バッファを返します。
\r
195 * アプリケーションは、可能な限り速やかにデータを書き込んで、NyLPC_cTcpSocket_psendをコールしてください。
\r
197 * Optionフィールドを持つパケットを送信する場合は、オプションデータサイズの合計をデータサイズに指定して、payloadwriterで調整すること。
\r
199 void* NyLPC_cTcpSocket_allocSendBuf(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec);
\r
202 * NyLPC_cTcpSocket_allocSendBufで確保したメモリを開放します。
\r
204 void NyLPC_cTcpSocket_releaseSendBuf(NyLPC_TcTcpSocket_t* i_inst,void* i_buf_ptr);
\r
207 * 事前にAllocしたTxパケットを送信します。
\r
208 * このAPIはゼロコピー送信をサポートするためのものです。
\r
210 * allocSendBufで取得したメモリを指定します。
\r
212 * 失敗した場合、メモリは開放されません。
\r
214 NyLPC_TBool NyLPC_cTcpSocket_psend(NyLPC_TcTcpSocket_t* i_inst,void* i_buf_ptr,int i_len,NyLPC_TUInt32 i_wait_in_msec);
\r
217 * TCPソケットをクライアントとしてサーバへ接続します。
\r
219 NyLPC_TBool NyLPC_cTcpSocket_connect(NyLPC_TcTcpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_peer_port,NyLPC_TUInt32 i_wait_in_msec);
\r
224 #endif /* __cplusplus */
\r
226 #endif /* NYLPC_CTCPSOCKET_H_ */
\r