OSDN Git Service

NyLPC_cTcpSocketへTCP送信パケットライタを統合
[mimic/MiMicSDK.git] / lib / src / uip / NyLPC_cTcpSocket.h
1 /*********************************************************************************\r
2  * PROJECT: MiMic\r
3  * --------------------------------------------------------------------------------\r
4  *\r
5  * This file is part of MiMic\r
6  * Copyright (C)2011 Ryo Iizuka\r
7  *\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
12  *\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
17  *\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
20  *\r
21  * For further information please contact.\r
22  *      http://nyatla.jp/\r
23  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
24  *\r
25  *********************************************************************************/\r
26 #ifndef NYLPC_CTCPSOCKET_H_\r
27 #define NYLPC_CTCPSOCKET_H_\r
28 \r
29 \r
30 \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
35 \r
36 \r
37 #ifdef __cplusplus\r
38 extern "C" {\r
39 #endif /* __cplusplus */\r
40 \r
41 \r
42 typedef struct NyLPC_TcTcpSocket NyLPC_TcTcpSocket_t;\r
43 \r
44 \r
45 /**********************************************************************\r
46  *\r
47  * NyLPC_TTcpListenerSynInfo struct\r
48  *\r
49  **********************************************************************/\r
50 \r
51 \r
52 struct NyLPC_TTcpSocketSynParam\r
53 {\r
54         struct NyLPC_TIPv4Addr srcaddr;\r
55         NyLPC_TUInt16 rport;\r
56         NyLPC_TUInt16 mss;\r
57         NyLPC_TUInt32 rcv_nxt32;\r
58 };\r
59 \r
60 \r
61 /**********************************************************************\r
62  *\r
63  * NyLPC_TcTcpSocket_TxQItem struct\r
64  *\r
65  **********************************************************************/\r
66 \r
67 /**\r
68  * TXキューの数。この値は8未満にしてください。\r
69  */\r
70 #define NyLPC_TcTcpSocket_NUMBER_OF_TXQ 4\r
71 \r
72 struct NyLPC_TcTcpSocket_TxQItem\r
73 {\r
74         //最終更新時刻\r
75         NyLPC_TUInt32 tick_of_sent;\r
76         //このパケットのRTO(秒間隔)\r
77         NyLPC_TUInt32 rto32;\r
78         void* packet;\r
79         //パケットのACK番号。この番号を受信できれば、再送パケットは消去可能である。\r
80         NyLPC_TUInt32 ackno;\r
81 };\r
82 \r
83 /**********************************************************************\r
84  *\r
85  * uip_conn struct\r
86  *\r
87  **********************************************************************/\r
88 \r
89 struct uip_conn\r
90 {\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
99         /**Peerのウインドウサイズ*/\r
100         NyLPC_TUInt16 peer_win;\r
101         NyLPC_TUInt16 _padding;\r
102         /**現在ソケットのRTO*/\r
103         NyLPC_TUInt32 current_rto32;\r
104 };\r
105 \r
106 \r
107 \r
108 \r
109 \r
110 \r
111 \r
112 \r
113 /**********************************************************************\r
114  *\r
115  * NyLPC_TcTcpSocket class\r
116  *\r
117  **********************************************************************/\r
118 \r
119 /**\r
120  * uipサービスを使用したTCPソケットクラスです。\r
121  * この関数は2つのタスクから呼び出されます。\r
122  * [uipTask]  ->  [cTcpSocket] <- [Application]\r
123  * ApplicationとuipTaskとの間での排他処理はインスタンスで制御されています。\r
124  * Application側からのコールは内部でuipTaskとの間で排他処理を実行します。\r
125  * Application側からのコールはリエントラントではありません。\r
126  */\r
127 \r
128 struct NyLPC_TcTcpSocket\r
129 {\r
130         /** Base class*/\r
131         NyLPC_TcBaseSocket_t _super;\r
132         //この変数は、uipタスクの実行する関数のみが変更する。\r
133         struct uip_conn uip_connr;\r
134         NyLPC_TcFifoBuffer_t rxbuf;\r
135         struct{\r
136                 NyLPC_TUInt8 rp;\r
137                 NyLPC_TUInt8 wp;\r
138                 //送信キュー\r
139                 struct NyLPC_TcTcpSocket_TxQItem txq[NyLPC_TcTcpSocket_NUMBER_OF_TXQ];\r
140         }txbuf;\r
141         volatile NyLPC_TUInt8 tcpstateflags; /**< TCP state and flags. */\r
142 };\r
143 \r
144 \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
147 \r
148 /**\r
149  * 初期化関数です。\r
150  * uipserviceは初期化済である必要があります。\r
151  * また、暫定条件として、サービスが停止中である必要もあります。\r
152  * @param i_recv_buf\r
153  * 受信バッファを指定します。
154  */\r
155 NyLPC_TBool NyLPC_cTcpSocket_initialize(NyLPC_TcTcpSocket_t* i_inst,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len);\r
156 /**\r
157  * 終期化関数です。\r
158  * uipserviceは初期化済である必要があります。\r
159  * また、暫定条件として、サービスが停止中である必要もあります。\r
160  */\r
161 void NyLPC_cTcpSocket_finalize(NyLPC_TcTcpSocket_t* i_inst);\r
162 \r
163 NyLPC_TBool NyLPC_cTcpSocket_accept(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec);\r
164 /**\r
165  * @return\r
166  *  1 - 以上:受信に成功した。\r
167  *  0 - タイムアウト\r
168  * -1 - ソケットがクローズしている\r
169  */\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
172 /**\r
173  * 送信未達は保障されません。\r
174  * エラーを検出したら、基本的にはソケットをクローズしてください。\r
175  * @param i_wait_msec\r
176  * 送信失敗までの待ち時間を指定します。現在は、\r
177  * RTT推定ができないため、TCPの再送を考慮して、最低でも10秒(10000)程度を指定してください。\r
178  * @return\r
179  * 送信したバイト数を返します。エラーならば0未満の数を返します。\r
180  *\r
181  */\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
184 \r
185 \r
186 /**\r
187  * NyLPC_cTcpSocket_psendで送信するための送信バッファ準備します。\r
188  * @param i_hint\r
189  * 送信を希望するデータサイズを指定します。\r
190  * アロケータは出来る限り希望に沿ってメモリを返します。\r
191  * @param o_buf_size\r
192  * 取得できたバッファサイズを返します。\r
193  * @return\r
194  * 成功した場合、送信バッファを返します。\r
195  * アプリケーションは、可能な限り速やかにデータを書き込んで、NyLPC_cTcpSocket_psendをコールしてください。\r
196  * @note\r
197  * Optionフィールドを持つパケットを送信する場合は、オプションデータサイズの合計をデータサイズに指定して、payloadwriterで調整すること。\r
198  */\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
200 \r
201 /**\r
202  * NyLPC_cTcpSocket_allocSendBufで確保したメモリを開放します。\r
203  */\r
204 void NyLPC_cTcpSocket_releaseSendBuf(NyLPC_TcTcpSocket_t* i_inst,void* i_buf_ptr);\r
205 \r
206 /**\r
207  * 事前にAllocしたTxパケットを送信します。\r
208  * このAPIはゼロコピー送信をサポートするためのものです。\r
209  * @param i_buf_ptr\r
210  * allocSendBufで取得したメモリを指定します。\r
211  * @return\r
212  * 失敗した場合、メモリは開放されません。\r
213  */\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
215 \r
216 /**\r
217  * TCPソケットをクライアントとしてサーバへ接続します。\r
218  */\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
220 \r
221 \r
222 #ifdef __cplusplus\r
223 }\r
224 #endif /* __cplusplus */\r
225 \r
226 #endif /* NYLPC_CTCPSOCKET_H_ */\r