+NyLPC_TBool NyLPC_cTcpSocket_connect(NyLPC_TcTcpSocket_t* i_inst,struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_peer_port,NyLPC_TUInt32 i_wait_in_msec)\r
+{\r
+ volatile NyLPC_TUInt8 f;\r
+ NyLPC_TUInt32 sq;\r
+ NyLPC_TcStopwatch_t sw;\r
+ NyLPC_TUInt16 lport;\r
+ lockResource(i_inst);\r
+ //ソケットが無効であること。\r
+ if(i_inst->tcpstateflags!=UIP_CLOSED)\r
+ {\r
+ NyLPC_OnErrorGoto(Error);\r
+ }\r
+ //ポート番号の取得(lockResourceが他のソケットと共有なので、重複ポートの割当は起こりえない。でもちょっと注意して)\r
+ lport=NyLPC_htons(NyLPC_cIPv4_getNewPortNumber(i_inst->_super._parent_ipv4));\r
+ if(lport==0){\r
+ NyLPC_OnErrorGoto(Error);\r
+ }\r
+ //connectの為の準備\r
+\r
+ //localipとdefault_mmsは別枠で設定\r
+ /* Fill in the necessary fields for the new connection. */\r
+ i_inst->uip_connr.current_rto32 = UIP_TCP_RTO_CONNECTION_INITIAL;//RTOを短くしてARP発行時の再接続短縮を期待する。\r
+ i_inst->uip_connr.lport = lport;\r
+ i_inst->uip_connr.rport = NyLPC_htons(i_peer_port);\r
+ i_inst->uip_connr.ripaddr=*i_addr;\r
+ i_inst->uip_connr.snd_nxt32=iss32;//should be random\r
+ /* rcv_nxt should be the seqno from the incoming packet + 1. */\r
+ i_inst->uip_connr.rcv_nxt32=0;\r
+ //MSSの設定\r
+ i_inst->uip_connr.peer_mss=i_inst->uip_connr.default_mss;\r
+ i_inst->uip_connr.peer_win=1;//periodicの再送信を期待するために相手のWindowサイズは1と仮定する。\r
+ NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf));\r
+ //ここでステータスがかわる。\r
+ i_inst->tcpstateflags = UIP_SYN_SENT;\r
+ //前回のデータが残っていた場合の保険\r
+ if(i_inst->txbuf.rp!=i_inst->txbuf.wp){\r
+ resetTxQWithUnlock(i_inst);\r
+ }else{\r
+ unlockResource(i_inst);\r
+ }\r
+\r
+ NyLPC_cStopwatch_initialize(&sw);\r
+\r
+ NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec);\r
+ if(sendWithRetransmit(i_inst,TCP_SYN,NULL,0,&sw,&sq)==0){\r
+ //ちょっと待つ。\r
+ NyLPC_cThread_yield();\r
+ //キューにあるTXが消えるのを待つ。\r
+ if(waitForTxRemove(i_inst,sq,&sw)){\r
+ //ACK受信に成功して、TXが消失\r
+ NyLPC_cStopwatch_finalize(&sw);\r
+ return NyLPC_TBool_TRUE;\r
+ }\r
+ }\r
+ //ロックして、強制的なステータス遷移\r
+ lockResource(i_inst);\r
+ f=i_inst->tcpstateflags;\r
+ if(f!=UIP_CLOSED){\r
+ //もし、強制CLOSE遷移であれば、RSTも送信。\r
+ i_inst->tcpstateflags=UIP_CLOSED;\r
+ unlockResource(i_inst);\r
+ sendRst(i_inst);\r
+ }else{\r
+ unlockResource(i_inst);\r
+ }\r
+ return NyLPC_TBool_FALSE;\r
+Error:\r
+ unlockResource(i_inst);\r
+ return NyLPC_TBool_FALSE;\r
+}\r
+\r