çŽ1幎åãç§ã®èšäºãåºãŠããŸãããããã¯ãã®èšäºã®ã æåã®éšå ããšåŒã¶ããšãã§ããŸãã æåã®éšåã§ã¯ãã§ããéããç±å¿ãªéçºè ã®å°é£ãªéãæŽçããŸããã ãããã®åªåã®çµæãèªå® ã§ãšã³ãžã³ããã¶ã€ããŒããã®ä»ã®ææ°ã®éçºããŒã«ãªãã§äœæãããRTSãžã£ã³ã«ã Land of Onimode ãã®ã²ãŒã ãã§ããŸããã ãããžã§ã¯ãã§ã¯ã C ++ãšAssemblerã䜿çšãããã¡ã€ã³ããŒã«ãšããŠç§èªèº«ã®é ã䜿çšãããŸããã
ãã®èšäºã§ã¯ãç§ãã©ã®ããã«ãèçè ãã®åœ¹å²ãåŒãåãããã®ãããžã§ã¯ããã埩掻ããããããšã決ãããã«ã€ããŠã話ããŸãã ç¬èªã®ã²ãŒã ãµãŒããŒã®äœæã«ã¯ãå€ãã®æ³šæãæãããŸãã
ããã§èšäºã®çµããã§ã ãå§ãŸãã¯ãã¡ãã§ãã
â èšäºã®åé ïŒã²ãŒã ã®åŸ©æŽ»
â èšäºã®ç¶ãïŒGUI
ãããã¯ãŒã¯
æ·»ä»ã®äŸãåæããªããã°ãç§ã話ãããšããŠããããšã®è€éããç解ã§ãããšã¯æããŸããã ãããããããã«ããããã®äž»é¡ã«é¢ããäžè¬çãªèããäŒããããšãã§ããããšãé¡ã£ãŠããŸãã ãŸããç§ã¯èªåã§ãã®ãããªè§£æ±ºçãèãåºãããšãå¥ã«æ³šæããå¿ èŠããããŸãããã®ãããåé¡ã«å¯Ÿããç§ã®è§£æ±ºçã®äžéšã次åãšãªããªã¹ã¯ãåžžã«ãããŸãã ç§ã¯libuvãããã¯ãŒã¯ã®ã©ã€ãã©ãªãäž»ã«ãç§åŠçãªçªããæ¹æ³ã§ç 究ããŸããããŸãããããã¯ãŒã¯ããã°ã©ãã³ã°ã«åžžã«æºãã£ãŠãã人ã ãç§ã®è§£éã®äžéšãä¿®æ£ã§ããããšãèªããŠããŸãã ãããŠä»ããããããç§ã¯èšäºã®ãããã¯ã«æ»ããŸã...
æåã¯ã WinSockã©ã€ãã©ãªããããã¯ãŒã¯ã«äœ¿çšãããã£ãã®ã§ãã ãããã圌ã¯ããã«èããå€ããŸããããã®æ¹æ³ã§ç§ãåã³å®å šã«Windowsã«æ¥ç¶ãããããšã¯æããã ã£ãããã§ãã ããã§ãç§ã¯ã€ã³ã¿ãŒãããã調ã¹ãŠã libuvãšåŒã°ããèå³æ·±ã解決çãèŠã€ããŸããã ãã®ã©ã€ãã©ãªã¯ç¡æã§ãã Windows ã Unix ã Mac OS ã Android㧠åäœããŸã ã ãŸããææ°ã®C ++èšèªæšæºã䜿çšããèŠä»¶ãªã©ãææ°ã®æè¡é©æ°ãåŒãä»ããŸããã ãšã«ãããããã¯çŽç²ãªCã§æžãããŠãããããã¯éçºè ã«ãšã£ãŠãããªãå©ç¹ã«ãªããšèããŠããŸãã
æåã¯ã2ã€ã®åé¡ããããŸããã
- ãµãŒããŒããŒãããäœæããããšã¯ãªããããäžè¬çãªããã°ã©ã æ§é ãèãåºãå¿
èŠããããŸããã
- ç§ã®æèŠã§ã¯ã libuvã®ããã¥ã¡ã³ãã«ã¯å€ãã®èŠæããããŸãã äžè¬çã«ãç§ã¯ãã€ãããªããéåžžã«ãŸãšããªè£œåãäœããå€ããå°ãªãããã®æ©èœã説æããã®ãé¢åã ãšæãã®ã§ãã ããããæ®å¿µãªãããã»ãšãã©ãã¹ãŠã®éçºè ãããã«èŠããã§ããŸãã ãã®åéã®æ ã®æåŸã®æ®µéã¯ã DoxygenãŠãŒãã£ãªãã£ãªã©ã䜿çšããèªåããã¥ã¡ã³ãçæã®äœ¿çšã§ããããã«ãããã³ã¡ã³ããã³ãŒãã«å€æãããã¯ã©ã¹ãšæ§é éã®ãªã³ã¯ãèªåçã«çæãããŸãã ç§ã¯ãããã人çã«é ãããšã£ãŠãããããããŸããããèªåã®ã©ã€ãã©ãªã«å®³ãåãŒãããè¯ãæ¹æ³ã¯ãããããçš®é¡ã®æ§é ãå³ããèªåçã«ã¬ããŒãžãã³ãçæããããšããããããããŸããã
ç§ã®2çªç®ã®åé¡ã¯ã WinSockã䜿çšããããšããªããšããäºå®ã«ãã£ãŠéšåçã«è£ãããããã WinSockãŸãã¯libuvãã©ã®ããã«æ±ããã¯ããã»ã©éèŠã§ã¯ãããŸããã§ããã ãããã libuvã¯äœããæžãæããå¿ èŠãªãããã€ãã®ãã©ãããã©ãŒã ãçŽæããŠãããã®ã§ãç§ã®ç®ã«ã¯ééããªãåã¡ãŸããã
çŸåšãã²ãŒã éçºè ã¯ããŒã«ã«ãããã¯ãŒã¯ã§ãã¬ã€ããæ©èœãåé€ããããšãå€ããšããäºå®ã«ãããããããç§ã¯ãã®ããŒãžã§ã³ã®ãããã¯ãŒã¯ã²ãŒã ãä¿æããããšã«ããŸããã èšäºã®æåã®éšåã§ãããŒã«ã«ãããã¯ãŒã¯ã§RTSããã¬ã€ããå Žåãã²ãŒã ã¯åã³ã³ãã¥ãŒã¿ãŒã§ç¬ç«ããŠåäœãã調æŽã»ã³ã¿ãŒã¯å¿ èŠãªãããããµãŒããŒã¯ãŸã£ããå¿ èŠãªããšå人çã«èª¬æããŸããã ãããŠãæ¬åœã«ããã§ãã ããããç§ã®å Žåããããã¯ãŒã¯ã²ãŒã ã®2ã€ã®ãªãã·ã§ã³ãååŸããããšèããŸãããã€ã³ã¿ãŒãããçµç±ãšããŒã«ã«ãããã¯ãŒã¯çµç±ã§ãã ããŒã«ã«ãããã¯ãŒã¯ã§ã³ã³ãã¥ãŒã¿ãŒçžäºäœçšã®ãã¢ããŒãã¢æ¹åŒã䜿çšãå§ããå Žåããã®ãããªãœãªã¥ãŒã·ã§ã³ã¯ã³ãŒãã®2çªç®ã®ãã©ã³ããäœæããŸãã ãã®ãããªãœãªã¥ãŒã·ã§ã³ã¯ããéãåäœããŸããïŒ ããããããã§ãããLANã§ã¯ããŒã¿ã¯éåžžããã«ã¿ãŒã²ããã«å°éããããããã¬ã€ã€ãŒãéããæããããšã¯ãŸããããŸããã
ãã®çµæãããŒã«ã«ãããã¯ãŒã¯ã«ããµãŒããŒã䜿çšãããšè«ççã«å€æããŸããã
ã¢ãã¬ã¹ãšããŒãã«é¢ããå°ãã®çè«
å€ãã®äººãããããç¥ã£ãŠããããã«ãã€ã³ã¿ãŒãããã«æ¥ç¶ãããŠããäžçã®åã³ã³ãã¥ãŒã¿ãŒã«ã¯äžæã®ã¢ãã¬ã¹ãå²ãåœãŠãå¿ èŠããããŸãïŒãã¡ãããNATã䜿çšãããŠããããåæ§ã®å Žåãé€ããŸãïŒã ãã®ã¢ãã¬ã¹ã¯IPãšåŒã°ããŸãã IPã¢ãã¬ã¹ã¯4æ¡ã§æ§æãããã人éã®åœ¢ãã§ã¯æ¬¡ã®ããã«ãªããŸãïŒ234.123.34.18
ãããã®ã¢ãã¬ã¹ã§ãã³ã³ãã¥ãŒã¿ãŒã¯ãäºããèŠã€ããŸãã ãã ããéåžžãå€ãã®ããã°ã©ã ãåãã³ã³ãã¥ãŒã¿ãŒã§åæã«å®è¡ãããŠããããããããŒãããšããæŠå¿µãè¿œå ãããŠããŸãã ããã°ã©ã ã¯ãããã®ããŒããæ€åºããããããä»ããŠçžäºäœçšã確ç«ã§ããŸãã ããæ確ã«ããããã«... IPã¢ãã¬ã¹ã¯ããã·ã¢ãããããã¹ã«ã€å·ãããªã·ã§ã€ãŽããã¥ããæãulã§ãã ãNew Russian Descentããd.18ãããã³ãportãã¯ãç¹å®ã®äººãäœã
ã¡ãã»ãŒãžã¯ããããã³ã«ãšåŒã°ããç¹å¥ãªããã°ã©ã ã«ãã£ãŠã³ã³ãã¥ãŒã¿ãŒéã§é ä¿¡ãããŸãã ã€ã³ã¿ãŒãããå šäœã眮ãããŠããæãæåãªãããã³ã«ã¯TCPãšåŒã°ããŸã ã UDPãšåŒã°ããå¥ã®éåžžã«éèŠãªãããã³ã«ããããŸãããããã䜿çšããããšã¯ããå°é£ã§ãã
ãã®ãããã¯ã«ããŸã詳ãããªã人ã®ããã«ããã®éããç°¡åã«èª¬æããŠãã ããã
UDPã䜿çšãããšãããŒã¿ããããã§è»¢éã§ããŸãã ãã®ããŒã¿ã®æçã¯ããŒã¿ã°ã©ã ãšåŒã°ããŸãã ããŒã¿ã°ã©ã ã¯ãæå®ãããIPã¢ãã¬ã¹ãšããŒãã«éä¿¡ã§ããŸãã ããã... UDPãããã³ã«ã¯ ãäœãçŽæããŸããããã€ãŸã éä¿¡ãããããŒã¿ã°ã©ã ã¯éäžã§ç°¡åã«å€±ãããå¯èœæ§ãããããã®å Žåãåä¿¡è ã¯äœãåä¿¡ããŸããã ãããŠãããã€ãã®ããŒã¿ã°ã©ã ãéä¿¡ãããå Žåããããã¯ç°ãªãé åºã§æ¥ããããŸãã¯ããã€ãã¯æ¥ãªããããããŸããã ããã§ã¯ä»»æã®ãªãã·ã§ã³ãå¯èœã§ãããããã¯UDPã®å®å šã«æ£åœãªåäœã§ãã UDPãä¿èšŒããå¯äžã®ããšã¯ãããŒã¿ã°ã©ã ããŸã å°çããå Žåãå®å šã«å°çãããšããäºå®ã§ãã ããŒã¿ã°ã©ã ã®ãååãã¯æ¥ãããšãã§ããŸããã
TCPã¯å®å šã«ç°ãªãåçã§æ©èœããŸãã 圌ã¯ãç解ã§ããªãããŒã¿ã°ã©ã ãéåœã«ç¿»åŒãããŠæªç¥ã®ãããã¯ãŒã¯ç©ºéã«éä¿¡ããããšã¯ãããŸãããæåã«åä¿¡è ãšã®éä¿¡ãã£ãã«ã確ç«ãããã®ãã£ãã«ãéããŠããŒã¿ãæ確ã«éä¿¡ããŸãã TCPã¯ããã¹ãŠã®éä¿¡ããŒã¿ãåä¿¡è ã«å±ããéä¿¡ãããæ£ç¢ºãªé åºã§å°çããããšãä¿èšŒããŸãã ããŒã¿èªäœã¯ãåºå®é·ã®ããŒã¿ã°ã©ã ã®åœ¢åŒã§ã¯ãªãããã€ãã¹ããªãŒã ã®åœ¢åŒã§æäŸãããŸãïŒãã¡ã€ã«ã«ãã€ãããŒã¿ãèšé²ããã®ãè¯ãäŸãã§ãïŒã ãã®ã¢ãããŒãã§ã¯ã TCPã¯éä¿¡ãããã¡ãã»ãŒãžãå¿ èŠã«å¿ããŠéšåã«åå²ã§ããããšã«æ³šæããŠãã ããã åä¿¡è ã¯æåã«éä¿¡ãããããŒã¿ã®äžéšã®ã¿ãåä¿¡ã§ãããã°ãããããšæ®ãã®ååãæéå ã«å°çããŸãã
ããã°ã©ã ãšããŒãéã®éä¿¡ã«ã¯ããããããœã±ããã䜿çšãããŸãã ãœã±ããã¯ããŒãã«æ¥ç¶ãããããŒããšã®ãã¹ãŠã®éä¿¡ã¯ãã®ãœã±ãããä»ããŠè¡ãããŸãã ãœã±ããã¯ããŒããšå¯Ÿè©±ããããšä»¥å€ã¯äœãåŠçããªããããããŒãã®ãªããœã±ããã®ååšã¯ããŸãæå³ããããŸããã
ãããã³ã«éžæ
åæ段éã§ã¯ãçžäºäœçšã®ããã®ãããã³ã«ãéžæããå¿ èŠããããŸããã ç¬èªã®ãœãªã¥ãŒã·ã§ã³ãäœæããªãå Žåã TCPãšUDPã® 2ã€ã®ãªãã·ã§ã³ãããããŸããã UDPã¯ãã²ãŒã ãã¬ã€ã®é 延ãèš±å¯ã§ããªãå Žåã«é©ããŠããŸãã éåžžãã²ãŒã èªäœã¯ãµãŒããŒã§å®è¡ãããŸãããã¯ã©ã€ã¢ã³ãã¯ã²ãŒã ã®ç¶æ³ã®å€åã«é¢ããããŒã¿ã®ã¿ããµãŒããŒããåä¿¡ããŸãã ãã®ã¢ãããŒãã«ãããæ¥ç¶ãäžååãªãã¬ã€ã€ãŒãæåéããç¡èŠãã§ããŸãã ãµãŒããŒã¯é ããŠããã²ãŒã ãåŸ ã€ããã«ã²ãŒã ãåæ¢ããªããããä»ã®ãã¹ãŠã®ãã¬ã€ã€ãŒã¯éåžžã«å¿«é©ã«ãã¬ã€ãç¶ããŸãã ãã®ãããªã²ãŒã ã®äŸã¯Counter Strikeã§ãã
RTSã®å Žåãã²ãŒã ããã»ã¹ã¯å€ãã®èšç®ãå¿ èŠãšãããããã£ãŠåã³ã³ãã¥ãŒã¿ãŒã§å®è¡ãããããããã®ã¢ãããŒãã¯ã»ãšãã©ã®å Žåäžé©åã§ãããããã®ã³ã³ãã¥ãŒã¿ãŒã¯ãã¹ãŠåãæ¹æ³ã§å®è¡ããå¿ èŠããããŸãã ãããã£ãŠãä»ã®åã³ã³ãã¥ãŒã¿ãŒã®åã³ã³ãã¥ãŒã¿ãŒã¯ã1ã€ã®ããããã¯ãŒã¯ã¿ã¯ããã§ãã¬ãŒã€ãŒãå®è¡ããã¢ã¯ã·ã§ã³ã®ãªã¹ããåžžã«åä¿¡ããå¿ èŠããããŸãã ãªã¹ããé ããŠããå Žåã¯ãåä¿¡ããããŸã§åŸ ã€å¿ èŠããããŸãã ã€ãŸã UDPã䜿çšããå Žåã§ããã¡ãã»ãŒãžã®é ä¿¡ãèªåã§å¶åŸ¡ããå¿ èŠããããŸãã ãããã£ãŠã TCPãéžæãããŸããã
ç§ãèããããã«ãStarcraft2ã¯RTSãžã£ã³ã«ã«å±ããŠãããšããäºå®ã«ãããããããäŸç¶ãšããŠãµãŒããŒäžã§åäœããŸãã çŸä»£ã®éã¯ããã®æ¹æ³ã䜿çšããããã«ãã®ãããªã¬ãã«ã«éããŠããå¯èœæ§ããããŸãã ããããç§ã®å Žåããããã¯ãŒã¯ã¯RTSã® ãã¯ã©ã·ãã¯ãã«ãªããŸãã
ãµãŒããŒã¯äœãããŸããïŒ
å®éã圌ã¯ã¯ã©ã€ã¢ã³ãããã¡ãã»ãŒãžãåãåããŸã§ã»ãšãã©äœãããŸããã TCPã®å ŽåããµãŒããŒã¿ã¹ã¯ã¯ããŒããéããŠãªãã¹ã³ããããšã§ãã ã¯ã©ã€ã¢ã³ãããã®ããŒããžã®CONNECTæ¥ç¶ã確ç«ããèŠæ±ãåä¿¡ããå ŽåããµãŒããŒã¯ACCEPTãå®è¡ããå¿ èŠããããŸããACCEPTã¯æ°ããã©ã³ãã ããŒããéããã¯ã©ã€ã¢ã³ãã«å ±åããŸãã ãµãŒããŒããã©ã³ãã ãªããŒããåãåã£ãã¯ã©ã€ã¢ã³ãã¯ãç¬èªã®ã©ã³ãã ãªããŒããéãããããã®ããŒãã§ãµãŒããŒãšã¯ã©ã€ã¢ã³ãã®éã«æ¥ç¶ã確ç«ãããŸãã æ¥ç¶ã¯ãåœäºè ã®1人ãæ¥ç¶ãéãããŸã§ããŸãã¯æè¡çãªçç±ã§æ¥ç¶ãåæããããŸã§ååšããŸãã ãµãŒããŒã¯ãåã¯ã©ã€ã¢ã³ããš1ã€ã®æ¥ç¶ã確ç«ããŸãã ã¯ã©ã€ã¢ã³ããäºãã«éä¿¡ãããã¹ãŠã®ããŒã¿ã¯ãµãŒããŒãééããŸããããã¢ããŒãã¢ãããã¯ãŒã¯ã®å Žåã®ããã«ãã¯ã©ã€ã¢ã³ãããã¯ã©ã€ã¢ã³ãã«çŽæ¥éä¿¡ãããããšã¯ãããŸããã
ç§ã®ãµãŒããŒã¯æ¬¡ã®ã¿ã¹ã¯ãå®è¡ããŸãã
- ã¯ã©ã€ã¢ã³ããšããããã€ãã亀æãããã¹ãŠãæ£åžžã«è¡ãããŠããå Žåãã¯ã©ã€ã¢ã³ãã«èªåèªèº«ãä»ã®ã¯ã©ã€ã¢ã³ããšã¡ãã»ãŒãžã亀æããæ©äŒãæäŸããŸãã
- ã¯ã©ã€ã¢ã³ãéã§ã¡ãã»ãŒãžãæãã仲ä»è ãšããŠæ©èœããŸãã
- æ°ããã²ãŒã ã»ãã·ã§ã³ãç»é²ããæ¢åã®ã»ãã·ã§ã³ã®ãªã¹ããèŠæ±ãã顧客ã«ãã®ã²ãŒã ã«é¢ããæ å ±ãéä¿¡ããŸãïŒã²ãŒã ã»ãã·ã§ã³ã¯ãã¹ããã€ãŸããããã¯ãŒã¯ã²ãŒã ãäœæããä»ã®ãã¬ã€ã€ãŒãåå ããããšãæåŸ ããã¯ã©ã€ã¢ã³ãã«ãã£ãŠäœæãããŸãïŒã
- ãã¬ã€ã€ãŒãããããªãå Žåãã²ãŒã ã»ãã·ã§ã³ãåé€ããŸãã ãµãŒããŒã¯ãã©ã®ãã¬ã€ã€ãŒãã©ã®ã»ãã·ã§ã³ã«å ¥ãããç£èŠããŸãã
- ãã¬ã€ã€ãŒéã§ããã¹ãã¡ãã»ãŒãžãéä¿¡ããããšã«åŸäºããŠãããã€ãŸã ããšãã°ããã¬ãŒã€ãŒã®ååã«çŠç¹ãåœãŠããšããã®ãã¬ãŒã€ãŒã«ã®ã¿ãã©ã€ããŒãã¡ãã»ãŒãžãéä¿¡ã§ããŸãã å®éã«ã¯ãããã¯å®æçãªãã£ããã§ãã
- ãã¬ãŒã€ãŒã®ç·æ¥åæã§ç¶æ³ã远跡ããŸã-ãã®äºå®ãã²ãŒã ã»ãã·ã§ã³ã®ä»ã®åå è ã«å ±åããŸãã
- åãã¬ãŒã€ãŒã®ãããã¯ãŒã¯ã¢ã¯ãã£ããã£ãç£èŠããŸãã ãã¬ãŒã€ãŒããããŒã¿ãé·æéåä¿¡ãããªãå ŽåããµãŒããŒã¯ããã§äœããééã£ãŠãããšå€æããæ¥ç¶ããªã»ããããŸãã
- ã¯ã©ã€ã¢ã³ãããã®pingèŠæ±ã«å¿çããŸãã ãµãŒããŒã«pingãå®è¡ãããšãæ¥ç¶ã®é 延ããããã決å®ã§ãããµãŒããŒã«ãã£ãŠç£èŠãããã¯ã©ã€ã¢ã³ãã®ã¢ã¯ãã£ããã£ãç¶æã§ããŸãã
- äœæ¥ã«å¯Ÿãã管çå¶åŸ¡ãå®è¡ã§ããŸãã äžè¬çã«ã管çã¯ã²ãŒã ãšã¯é¢ä¿ã®ãªãå¥ã®ã¹ããŒãªãŒã§ãã ç§ã®å Žåãã²ãŒã ã®ã€ã³ã¿ãŒããããµãŒããŒãå¶åŸ¡ããããã®ãŠãŒãã£ãªãã£ãèªåã§äœæããŸããã ãã®ãŠãŒãã£ãªãã£ã¯ããµãŒããŒã®çµ±èšæ å ±ã衚瀺ãããšããã«äœãããŸããïŒãã¬ã€ã€ãŒã®æ°ãã²ãŒã ã»ãã·ã§ã³ã®æ°ããã¬ã€ã€ãŒãšã»ãã·ã§ã³ã®é¢ä¿ãªã©ã ãŸãããµãŒããŒãåèµ·åããã³ã·ã£ããããŠã³ããããšãã§ããŸãã
ã¯ã©ã€ã¢ã³ãã¯äœãããŸããïŒ
ã²ãŒã èªäœã¯ã¯ã©ã€ã¢ã³ãã§è¡ãããŸãã ã¯ã©ã€ã¢ã³ãã¯ãèªåãã²ãŒã ã«äžäººã§ã¯ãªãããšãç¥ã£ãŠããããã¬ãŒã€ãŒã®è¡åã«é¢ããä»ã®åå è ã¡ãã»ãŒãžãéä¿¡ããåãã¡ãã»ãŒãžãåä¿¡ããå¿ èŠããããŸãã
- ãµãŒããŒãšããããã€ãã亀æããæåãããšããããã¯ãŒã¯ãä»ããŠã¡ãã»ãŒãžã亀æããæ©äŒãåŸãŸãã
- ç¬èªã®ã²ãŒã ã»ãã·ã§ã³ãäœæããŸãïŒãã®å Žåããã®ãããªã¯ã©ã€ã¢ã³ãã¯ãã»ãã·ã§ã³ã§æãå€ããããããã¹ããšåŒã°ããŸãïŒã
- æ¢åã®ã²ãŒã ã»ãã·ã§ã³ã®ãªã¹ãããµãŒããŒã«èŠæ±ããŸãã
- æ¢åã®ã²ãŒã ã»ãã·ã§ã³ã«åå ããæ©èœããããŸãã
- ã»ãã·ã§ã³ã®åå è ãšããŠã圌ã¯ç¹æ§ãå€æŽããããšãã§ããŸããããšãã°ãæ¿æ²»é£åãšäººçš®ãéžæããŸãã ããã«ããã¹ãã¯ã»ãã·ã§ã³ã«ããããè¿œå ããããã»ãã·ã§ã³ããä»ã®ãã¬ãŒã€ãŒãåé€ããããã»ãã·ã§ã³ããã£ã³ã»ã«ãããã§ããŸãã
- ãµãŒããŒäžã«ååšããããã·ã¢èªã§ã¯ãªãããã£ããããšã®éã§ã®ããã¹ãã¡ãã»ãŒãžã®äº€æã
- ã»ãã·ã§ã³ã®åå è ã®1人ãã²ãŒã ãè¡ãã«ãŒããæã£ãŠããªãå Žåããã¹ãã¯ãã®ã«ãŒãã圌ã«è»¢éããå¿ èŠããããŸãã
- ãã¹ãŠã®ãã¬ãŒã€ãŒã«ãæºåå®äºãã®ãã©ã°ãä»ããŠããå Žåããã¹ãã«ã¯ã²ãŒã ãéå§ããæš©å©ããããŸãã
- ã²ãŒã äžãã¯ã©ã€ã¢ã³ãã¯äžå®æéïŒãããã¯ãŒã¯ã¯ããã¯ïŒã®éãããŠã¹ãšããŒããŒããããã¬ãŒã€ãŒã®ã¢ã¯ã·ã§ã³ãèç©ããä»ã®ãã¬ãŒã€ãŒã«éä¿¡ããŸãã ã²ãŒã ã»ãã·ã§ã³ã®ãã¹ãŠã®åå è éã§ãããã®ã¡ãã»ãŒãžã®å®å šãªäº€æãè¡ãããŠããªãå Žåãã²ãŒã ãç¶è¡ããããšã¯ã§ããŸããã ãã¬ãŒã€ãŒãé ããå Žåãæ®ãã®ãã¬ãŒã€ãŒã¯åœŒãåŸ ã£ãŠããŸãã åŸ æ©ãé ãããšãããã®ãããªãã¬ãŒã€ãŒãšã®éä¿¡ã®åé¡ããšãããããã¯ã«ã¡ãã»ãŒãžã衚瀺ãããæéãšå察æ¹åã®æéã0ã«ã«ãŠã³ãããã¿ã€ã ã«ãŠã³ã¿ãŒã衚瀺ãããŸãã
- ããããã¯ãŒã¯ãåæããŠããªããããšã«æ°ã¥ããå ŽåïŒã²ãŒã ãç°ãªãã³ã³ãã¥ãŒã¿ãŒäžã§ç°ãªãæ¹æ³ã§å®è¡ããå§ããå ŽåïŒããã¹ãã¯ç Žæããã²ãŒã ã埩å ããããšããŸãã ãããè¡ãããã«ãå®éã«ã¯ããã¹ãŠã®ã²ãŒã ããŒã¿ããã¡ã€ã«ã«æžã蟌ãŸãããã¡ã€ã«ã¯ã»ãã·ã§ã³ã®ä»ã®åå è ã«éä¿¡ãããŸãã 次ã«ãä»ã®åå è ããã®ãã¡ã€ã«ãèªã¿åãããã¹ãããåä¿¡ããããŒã¿ã§ããŒã¿ãå®å šã«çœ®ãæããŸãã å®éãããã¯Save / Loadã«é¡äŒŒããŠããŸã ã
ã€ã³ã¿ãŒããããµãŒããŒãšLANãµãŒããŒã®éã
é説çã«èããããããããŸããããLANãµãŒããŒã¯ã€ã³ã¿ãŒããããµãŒããŒãããè€éã§ãã ãªããã
ã€ã³ã¿ãŒããããµãŒããŒã¯å¥ã®ããã°ã©ã ãšããŠåäœããŸãããã®ããã°ã©ã ã¯äžåºŠã©ããã§èµ·åãããçæ³çã«ã¯æ°žä¹ ã«åäœããã¯ãã§ãã ããŒã«ã«ãµãŒããŒã¯ããã¹ããšããŠæ©èœããã³ã³ãã¥ãŒã¿ãŒäžã«äžæçã«äœæãããŸãã ãã®ãããªãµãŒããŒã¯ãå¥åã®ããã°ã©ã ã®åœ¢ã§ã¯ãªããã¡ã€ã³ã¢ããªã±ãŒã·ã§ã³ã®ããŒã¿ã«ã¢ã¯ã»ã¹ããå¥åã®ã¹ããªãŒã ã®åœ¢ã§ã®ã¿ååšããŸããããã¯ãã¹ã¬ããã®åæã«ãã£ãŠåŠçãããå°èŠæš¡ãªåé¡ã䌎ããŸãã ãŸããããã«ãã¹ã¬ãããã¯éåžžããããã°ã®ç¬ç«ããã»ã¯ã·ã§ã³ã§ãããéçºè ã®ç掻ãé·ãæãªãå¯èœæ§ããããŸãã
ããã«ãã¯ã©ã€ã¢ã³ããã€ã³ã¿ãŒããããµãŒããŒãšããŒã«ã«ãµãŒããŒã«æ¥ç¶ããåçã¯å®å šã«ç°ãªãããšã«æ³šæããŠãã ããã ã¯ã©ã€ã¢ã³ããã€ã³ã¿ãŒããããµãŒããŒã«æ¥ç¶ã§ããããã«ããã«ã¯ãã¯ã©ã€ã¢ã³ãã¯IPã¢ãã¬ã¹ãšããŒããç¥ã£ãŠããå¿ èŠããããŸãã ãããŠããã®ããŒã¿ã¯ããã¬ãŒã€ãŒã«ãã£ãŠããã¹ããã£ãŒã«ãã«ç€ºãããå¿ èŠããããŸãã ãŸããããŒã«ã«ãããã¯ãŒã¯ã§ã¯ãIPã¢ãã¬ã¹ãæå®ããã«æ¢åã®ã²ãŒã ã»ãã·ã§ã³ãæ€åºããå¿ èŠããããŸãã ãã®ã¢ã¯ã·ã§ã³ã¯ãIPã255.255.255.255ã§ãããããããããããŒããã£ã¹ããªã¯ãšã¹ããã«ãã£ãŠå®çŸãããŸãã ãã®ã¢ãã¬ã¹ã¯ãããŒã«ã«ãããã¯ãŒã¯å šäœããæå³ããŸããã TCPã®å Žåã¯æãçŽç²ãªåœ¢åŒã®UDPæ©èœã§ããããæ©èœããŸããã TCPããããŒããã£ã¹ãã¢ãã¬ã¹ã§åäœããªãã®ã¯ãªãã§ããïŒ ããŠãäžã§èª¬æããããã«ã TCPã¯éä¿¡ãã£ãã«ã確ç«ããå³å¯ã«äžå¯Ÿäžã§éä¿¡ããå¿ èŠããããŸãã ãããŠããã§ãããããããã«èª°ãããïŒããšããååã«åºã¥ããŠãããŒã«ã«ãããã¯ãŒã¯å šäœããåŒã³ããããå¿ èŠããããŸãã çããŠããïŒã ããŠããããã人ã¯å¿çããã²ãŒã ã»ãã·ã§ã³ã«é¢ããç³è«è æ å ±ãæž¡ããªããã°ãªããŸããã ãããã£ãŠãããŒã«ã«ãµãŒããŒã§UDPã䜿çšããå¿ èŠããããŸãã
ããŠããã¶ãŒãã«ã€ããŠã¯...ã²ãŒã äžã«ããŒã«ã«ãµãŒããŒãå®è¡ããŠãããã¹ããçªç¶å€±ãããããŸãã¯äœããã®çç±ã§ã²ãŒã ãçµäºãããšæ³åããŠãã ããã ããªãã¯ã©ããªããŸããïŒ ã²ãŒã ãäœæãã人ãæ®ãã®åã«ã²ãŒã ã»ãã·ã§ã³ãçµäºããããšã«ãããšããã²ãŒã Diablo2ã§çºçããåãããšãèµ·ãããŸãã Diablo2ã§ã¯ ãæ®ãã®äžå¹žãªããšã«ãããã¹ããå©çšã§ããªããªããŸããããšããã¹ã¿ã€ã«ã§ã¡ãã»ãŒãžãé»ãç»é¢ã«ãããã¢ãã衚瀺ãããŸããå®éãããã ãã§ã...ãã¬ã€ã€ãŒã¯ã²ãŒã ããè¿œãåºãããŸãã ãã®åäœã®çç±ã¯ããã¹ããã»ãã·ã§ã³ãé¢ãããšããã¹ããããŒã«ã«ãµãŒããŒãéãããã¹ãŠã®ãã¬ãŒã€ãŒãæ¥ç¶ãããããã§ãã ãã®ãåäœã«å¯ŸåŠããããã DirectPlayã«ã¯ãã€ãŠãã¹ã移è¡ãšåŒã°ããçŽ æŽãããæ©èœããããŸããã äžèšã§èšãã°ããã®ããã«èŠããŸã...ãã®çªç¶ã®æ»ã«é¢ããæ å ±ãããŒã«ã«ãµãŒããŒããæ¥ããšãæ®ãã®ã¯ã©ã€ã¢ã³ãã¯æ°ãããµãŒããŒãèµ·åããŠåæ¥ç¶ããããšã決å®ããŸãã ããŒã«ã«ãµãŒããŒã®èµ·åã¯ã¯ã©ã€ã¢ã³ãã§å®è¡ã§ããŸããããã¯ãã»ãã·ã§ã³å ã®ãã¬ãŒã€ãŒã®ãªã¹ãã®äžçªæåã§ãã ãã®åŸããµãŒããŒã¯ã以åã«ã²ãŒã ã«åå ããŠããå šå¡ãæ¥ç¶ã§ããããã«ãªããŸã§åŸ æ©ããå¿ èŠããããŸãã
ãã ããã€ã³ã¿ãŒããããµãŒããŒã«ã¯ç¬èªã®ç¹æ§ããããŸãã äž»ãªæ©èœã¯ãéçºè ãŸãã¯OSã®ãšã©ãŒã«ããããµãŒããŒãã¯ã©ãã·ã¥ãŸãã¯ããªãŒãºããå±éºæ§ãåžžã«ããããšã§ãã ãã®å ŽåãçŸåšã²ãŒã ã«åå ããŠãããã¹ãŠã®ãã¬ã€ã€ãŒã¯éåžžã«ã€ã©ã€ã©ããŸãã ãããããã®ç¶æ³ã§ã¯ããªãªãŒã¹åã«ã§ããã ãå€ãã®ãã°ãç¹å®ããããšããããšãé€ããŠãäœãããããšã¯ã»ãšãã©äžå¯èœã§ãã ããããããã§ãããšã©ãŒã¯è€éãªããã°ã©ã ã«æ®ã£ãŠãããå¯äžã®åé¡ã¯ãããã®çºçã®å¯èœæ§ã§ãã
ãããããµãŒããŒãçªç¶äœ¿çšãããŠãæ»äº¡ããããã¬ãŒã€ãŒãåæããããšä»®å®ããŸãã éçºè ããæ²ãã£ãæãã«å¯ŸããåªãããããåŸããã¬ãŒã€ãŒã¯éåžžããµãŒããŒã«åã³æ¥ç¶ããããšããŸãã ããã...ãµãŒããŒããæ»ãã ãå Žåã誰ããåèµ·åãããŸã§åäœããŸããããµãŒããŒãç£èŠããŠãã人ãããªãå Žåãããã¯ããã«èµ·ãããŸãã ãã®çµæããã¬ãŒã€ãŒã®æããèªç¶ã«å¢å€§ãå§ããæ·±å»ãªå¿çççµæã«ã€ãªããå¯èœæ§ããããŸãã 圌ã¯ã²ã©ãèœã¡å§ãããã¹ãã¯æ¥æ¿ã«æªåããå®éã«ã¯ããã®äººã¯ç¥çµè¡°åŒ±ã«è¿ããªããŸãã å人çã«ã¯ããã®ãããªè²¬ä»»ãè² ããããªãã®ã§ãäºåã«å®å šã«ãã¬ã€ããããšã«ããŸããã
ã€ã³ã¿ãŒããããµãŒããŒã¯ç¬ç«ããŠåäœããã¹ãã§ã¯ãããŸããããµãŒããŒãèµ·åããŠãã®åäœãç£èŠããããšããã³ã³ãããŒã©ãŒãå¿ èŠã§ãã ããã«ã¯ããµãŒããŒã«å®æçã«ä¿¡å·ãéä¿¡ããå¿çãåä¿¡ããå¿ èŠããããŸãã ä¿¡å·ã«å¿çããªãå Žåãããã¯ãµãŒããŒããã¯ã©ãã·ã¥ãããŠããããšãæå³ããŸãã ãã®å Žåãæåã«ãµãŒããŒããã»ã¹ãå®å šã«åŒ·å¶çµäºããŠããããµãŒããŒãåèµ·åããå¿ èŠããããŸãã ãµãŒããŒãå®éã«ã©ãã»ã©å¹æçãã¯ããããŸãããããã®ã¢ãããŒãã¯äœããã®åœ¢ã§ãµãŒããŒãä¿è·ããã¯ãã§ããããããæ£ããæ¹åãžã®åãã§ããããšã¯ééããããŸããã
ããã«ãã³ã³ãããŒã©ãŒããã°ã©ã ã¯ããã€ãã®è¿œå ã¢ã¯ã·ã§ã³ãå®è¡ã§ããŸãã ããšãã°ããµãŒããŒãæŽæ°ããããã«åœŒå¥³ã«æ瀺ã§ããŸãã ç§ã®å Žåã次ã®ããã«ãªããŸãã
1ïŒããšãã°ããµãŒããŒäžã§äœããä¿®æ£ããŠæ°ããããŒãžã§ã³ã®ãµãŒããŒãäœæããããšã«ããŸãããã誰ããã²ãŒã ã«èå³ãæã£ãŠããå Žåã¯ã誰ãããµãŒããŒäžã§åžžã«ãã¬ã€ããŠããŸãã ãµãŒããŒãåèµ·åãããšããã¬ã€ã€ãŒã¯ã²ãŒã ããè¿œãåºãããŸãããããã«ã¯åžžã«ææ çãªèŠçã䌎ããŸãã
2ïŒãããç¥ã£ãŠããµãŒããŒãšç£èŠãŠãŒãã£ãªãã£ã«ããœããã¢ãŒããã§ãµãŒããŒãæŽæ°ããããšãæããŸããããã¯æ¬¡ã®ããã«å®è¡ãããŸãã 管çãŠãŒãã£ãªãã£ãä»ããŠãæ°ãããµãŒããŒã¯éåžžã®ãã¡ã€ã«ã®åœ¢åŒã§è»¢éãããã¡ã€ã³ãµãŒããŒãå®è¡ãããŠããã³ã³ãã¥ãŒã¿ãŒã«å±ããŸãã ãã®åŸãã¡ã€ã³ãµãŒããŒã¯ãªãã¹ã³ããŒããéããŸãããã·ã£ããããŠã³ããŸãããã€ãŸããå®éã«ã¯æ°ãããã¬ãŒã€ãŒã¯åå ã§ããªããªããããã§ã«ããã«ããããŠãŒã¶ãŒã¯èœã¡çããŠãã¬ã€ãç¶ããŸãã ããã«ããã®ãµãŒããŒã¯ããã¹ãŠã®ãã¬ã€ã€ãŒããµãŒããŒããåæããç¶æ³ãå®æçã«ãã§ãã¯ãããã®åŸéãã«äœæ¥ãå®äºããŸãã ãã®æç¹ã§ãã³ã³ãããŒã©ãŒããã°ã©ã ã¯æ°ãããµãŒããŒã®éä¿¡ãã¡ã€ã«ãæ€åºããå€ããµãŒããŒãšäžŠè¡ããŠå®è¡ããŸãã ãªã¹ãã³ã°ããŒãã¯å€ããµãŒããŒããæ¢ã«è§£æŸãããŠãããããæ°ãããµãŒããŒã®ãªãã¹ã³ãéå§ããŸããããã«ããããã¹ãŠã®æ°ãããã¬ãŒã€ãŒãæ¥ç¶ãããŸãã ãã°ãããããšãå€ããµãŒããŒã¯èªåçã«ãªãã«ãªãããµãŒããŒã®æ°ããããŒãžã§ã³ã1ã€ã ãåäœããŸãã
ããã¯ããªãè¯ãããã«èãããŸãããæ®å¿µãªããããã®èšäºãæžããŠããæç¹ã§ã¯ãå®éã®è² è·ãããã£ãŠããç¶æ ã§ãµãŒããŒããã§ãã¯ã§ããŸããã§ããã ããã¯ãç±å¿ã«ãããžã§ã¯ããéçºããéã®å žåçãªåé¡ã§ããåé¡ãçºçããããšãåã«å ±åããŠå ±åããã ãã®ãã¹ã¿ãŒããŒã ãããªãå Žåã§ãã ããããåžžèã¯ç§ãæ£ããæ¹åã«ãã¹ãŠãããŠããããšãæããŠãããã®ã§ããã®ããŒãã«é¢ããç§ã®èããèšäºã«å«ããŸããã
Libuvã©ã€ãã©ãªé¢æ°
ãã®ã»ã¯ã·ã§ã³ã§ã¯ããããã¯ãŒãã³ã°ã®éçºäžã«å¯ŸåŠããªããã°ãªããªãã£ãlibuvã©ã€ãã©ãªã®æ©èœã«ã€ããŠç°¡åã«èª¬æããŸãã è¯ãèšæ¶ã¯ç§ã®åŒ·ãå質ã§ã¯ãªãã®ã§ãå°ãªããšãããã®æ å ±ã¯ç§ã«ãšã£ãŠã圹ç«ã€åèã«ãªãããšããããŸãã
libuvãšãŠãŒã¶ãŒéã®çžäºäœçšã¯ ãã³ãŒã«ããã¯é¢æ°ãŸãã¯ã³ãŒã« ããã¯é¢æ°ã®äœ¿çšã«å®å šã«åºã¥ããŠããŸãã ã©ã®ããã«æ©èœããŸããïŒ ããšãã°ããŠãŒã¶ãŒãTCPãä»ããŠã¡ãã»ãŒãžãéä¿¡ããããšããŸã ã ãã®ããã«ã uv_writeïŒïŒé¢æ°ã䜿çšãããŸããããã¯ããã¡ããããéä¿¡ãããã®ãããã³ãã©ã®ãœã±ãããçµç±ããããããã©ã¡ãŒã¿ãŒãšããŠåãåããŸãã ãã ããããã«å ããŠãéä¿¡ãæ£åžžã«å®äºãããšãã«åŒã³åºããããŠãŒã¶ãŒå®çŸ©é¢æ°ã®ã¢ãã¬ã¹ãæå®ããå¿ èŠããããŸãã çºçããã€ãã³ããå¶åŸ¡ã§ããã®ã¯ããããã®é¢æ°ã§ãã ã¡ãã»ãŒãžã®åä¿¡ã«ãåãããšãåœãŠã¯ãŸããŸãããããã«ã¯uv_read_startïŒïŒé¢æ°ã䜿çšãããŸããããã¯ãããŒã¿ã®å¥ã®éšåãåä¿¡ããåŸã«åŒã³åºããããŠãŒã¶ãŒå®çŸ©é¢æ°ã瀺ããŸãã
æãéèŠãªé¢æ°ã¯uv_runïŒïŒé¢æ°ã§ããããã¯åºæ¬çã«Windowsã®ã¡ãã»ãŒãžåŠçã«ãŒãã®ãããªãã®ã§ããã©ã€ãã©ãªã¯ãuv_runïŒïŒå ã§ã®ã¿ã³ãŒã«ããã¯é¢æ°ãåŒã³åºããŸããããã¯ãlibuvèªäœãäœããã®çš®é¡ã®ã¡ãã»ãŒãžããããã¯ãŒã¯çµç±ã§åä¿¡ããå Žåã§ãããŠãŒã¶ãŒã¯uv_runïŒïŒãåŒã³åºããããŸã§ãã®ããšãç¥ããªãããšãæå³ããŸããuv_runïŒïŒ
é¢æ°ã¯æ¬¡ã®ããã«å®£èšãããŸãã
int uv_run(uv_loop_t* loop, uv_run_mode mode);
æåã®ãã©ã¡ãŒã¿ãŒuv_loop_t * loopã¯ã libuvãå人çãªããŒãºã®ããã€ãã«äœ¿çšããæ§é äœãžã®ãã€ã³ã¿ãŒã§ãã ãã®å€æ°ãäžåºŠäœæããå¿ èŠããããäºåºŠãšè§Šããªãã§ãã ããã ããšãã°ã次ã®ããã«äœæã§ããŸãã
uv_loop_t loop; memset(&loop, 0, sizeof(loop)); uv_loop_init(&loop);
ãããã«ãŒããã©ã¡ãŒã¿ã®ãã¹ãŠã§ããã libuvå ã§ã©ã®ããã«äœ¿çšãããããç¥ãå¿ èŠã¯ç¹ã«ãããŸããã ããã... 1ã€ã®å€§ããªãã¥ã¢ã³ã¹ããããŸãã ãã«ãã¹ã¬ããããã°ã©ã ã䜿çšããŠããå Žåãã¹ã¬ããããšã«ç¬èªã®ã«ãŒãã䜿çšããå¿ èŠããããŸã ã ç§ã®å Žåãã²ãŒã èªäœã«1ã€ã®ã«ãŒããäœæããå¥ã®ã¹ã¬ããã§å®è¡ãããããŒã«ã«ãµãŒããŒã«2ã€ç®ã®ã«ãŒããäœæããŸãã ããã«å¿ããŠãåã¹ã¬ããã§ç¬èªã®uv_runïŒïŒãåŒã³åºãããŸãã
2çªç®ã®ãã©ã¡ãŒã¿ãŒuv_run_mode modeã¯ã uv_runïŒïŒé¢æ°ãæ©èœããã¢ãŒãã決å®ããŸãã ãµãŒããŒã®å Žåã¯å€UV_RUN_DEFAULTã䜿çšããã¯ã©ã€ã¢ã³ãã®å Žåã¯UV_RUN_NOWAITã䜿çšããŸã ã çç±ãç解ããŠã¿ãŸãããã
UV_RUN_DEFAULTãã©ã¡ãŒã¿ãŒã«ãããå°ãªããšãäœããã®äœæ¥ãè¡ãããŠããéãã uv_runïŒïŒé¢æ°ãå®è¡ãããŸãã ãããŠããã®ãããªä»äºã¯ãäŸãã°ãããŒããèãã¿ã¹ã¯ã§ãã ã€ãŸã ããŒãããªãã¹ã³ãããœã±ãããæåã«äœæãããå Žåã uv_runïŒïŒã¯ãã®ãœã±ãããååšããŠããéã¯çµäºããŸããã ãããŠãããã¯ãµãŒããŒã®ã¡ã€ã³ã¿ã¹ã¯ã§ã-ã¯ã©ã€ã¢ã³ãããã®æ¥ç¶ãåŸ ã£ãŠããã確ç«ããã ãããã£ãŠã UV_RUN_DEFAULTã䜿çšãããªãã·ã§ã³ã¯ãµãŒããŒã«ãšã£ãŠéåžžã«é©åã§ããã次ã®è¡ã§ãã
uv_run(&loop, UV_RUN_DEFAULT)
å€ãã®å Žåãããã°ã©ã ã®æåŸã®è¡ã§ãããã®ãµã€ã¯ã«ãçµäºãããšããµãŒããŒãåçŽã«ã·ã£ããããŠã³ããããã§ãã
ãŠãŒã¶ãŒããªã¹ãã³ã°ãœã±ãããç Žæ£ãããšã uv_runïŒïŒloopãUV_RUN_DEFAULTïŒé¢æ°ã¯èªåçã«çµäºããŸãã
uv_runïŒïŒloopãUV_RUN_DEFAULTïŒé¢æ°ããã®ç·æ¥çµäºã«ã¯ã uv_stopïŒ ïŒé¢æ°ã䜿çšãããŸããããã¯ããã©ã¡ãŒã¿ãŒãšåãã«ãŒããåããŸãã ãã®ãããªåŒã³åºãã®åŸã uv_runïŒïŒã¯çµäºããŸããããšã©ãŒãè¿ãããŸããããã¯ãããã«äžæããããŸã äœãããããšãããããšãæå³ããŸãã ã¡ãªã¿ã«ããã®å Žåã誰ãuv_runïŒïŒãå床åŒã³åºãå¿ èŠã¯ãããŸããã
UV_RUN_NOWAITãã©ã¡ãŒã¿ãŒã¯ã uv_runïŒïŒé¢æ°ããããŸã§ã«çºçããã€ãã³ãã®ã¿ãåŠçããããã«åŒ·å¶ããŸãã ã€ãŸãããããã¯ãŒã¯ã¡ãã»ãŒãžãåä¿¡ãããå Žåãã³ãŒã«ããã¯é¢æ°ãåŒã³åºãããŸãã ãã®åŸã uv_runïŒïŒé¢æ°ãå®äºããŸãã ã¯ã©ã€ã¢ã³ãã¯ããããã¯ãŒã¯ã¡ãã»ãŒãžã®äº€æã«å ããŠãã²ãŒã èªäœãåŠçããããããã®åäœã¯ã¯ã©ã€ã¢ã³ãã«æé©ã§ãã ç§ã®å Žåã uv_runïŒïŒloopãUV_RUN_NOWAITïŒãã²ãŒã ã¡ãžã£ãŒã®éå§æã«1åãçµäºæã«1åïŒã¯ããã¯åšæ³¢æ°çŽ60 HzïŒåŒã³åºããŸãã ããã¯ãããŒãã®éå§åã«åä¿¡ããã¡ãã»ãŒãžãåŠçããããŒãã®çŽåŸã«ç¬èªã®ã¡ãã»ãŒãžãéä¿¡ã§ããããã«ããããã«è¡ãããŸãã
åè¿°ã®ããã«ã TCPã«ã¯æ¥ç¶ãå¿ èŠã§ãã æ¥ç¶èŠæ±ã¯ãã¯ã©ã€ã¢ã³ãã«ãã£ãŠåžžã«ç¹å®ã®IPã¢ãã¬ã¹ãšãµãŒããŒããŒãã«éä¿¡ãããŸãã ãã®ããã«ã uv_tcp_connectïŒïŒé¢æ°ã䜿çšãããŸãã
uv_tcp_connectïŒïŒé¢æ°ã¯æ¬¡ã®ããã«å®£èšãããŸãã
int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, const sockaddr* addr, uv_connect_cb cb);
æåã®ãã©ã¡ãŒã¿ãŒuv_connect_t * reqã¯ãäœããã®æ§é äœãžã®ãã€ã³ã¿ãŒã§ã ãæããã«ã libuvã¯äœãã®ããã«æ¬åœã«å¿ èŠã§ã ã ãŠãŒã¶ãŒã®ã¿ã¹ã¯ã¯ããã®æ§é ãäœæããŠé¢æ°ã«æž¡ãããšã§ãã æ§é ã®äœæã¯åçŽã§ã¯ãããŸããïŒ
uv_connect_t connect_data;
念ã®ããããŒããæžã蟌ã¿ãŸãããããã¯å¿ èŠã§ã¯ãªãããã§ãïŒ
memset(&connect_data, 0, sizeof(connect_data));
ãŸãããã®å€æ°ã¯ãã¢ãã¬ã¹ãã³ãŒã«ããã¯é¢æ°ã§äœ¿çšãããããã uv_tcp_connectïŒïŒãåŒã³åºãããåŸãååšãç¶ããå¿ èŠãããããšã«æ³šæããŠãã ããã
2çªç®ã®ãã©ã¡ãŒã¿ãŒuv_tcp_t *ãã³ãã«ã¯ãäºåã«äœæããå¿ èŠãããããã©ã®ããŒãã«ããã€ã³ããããŠããªãTCPãœã±ããã§ãã TCPãœã±ããã®äœæã¯uv_tcp_initïŒïŒé¢æ°ã«ãã£ãŠå®è¡ãããŸããããã«ã€ããŠã¯åŸã§èª¬æããŸãã
3çªç®ã®ãã©ã¡ãŒã¿ãŒconst sockaddr * addrã¯ãæ¥ç¶ãèŠæ±ãããŠãããµãŒããŒã®IPã¢ãã¬ã¹ãšããŒãã§ãã Libuvã«ã¯ããã®æ§é ã«ããŒã¿ãåã蟌ãã®ã«åœ¹ç«ã€uv_ip4_addrïŒïŒé¢æ°ããããŸãã
sockaddr_in dest; uv_ip4_addr("234.123.34.18", 57, &dest);
4çªç®ã®ãã©ã¡ãŒã¿ãŒuv_connect_cb cbã¯ãã«ã¹ã¿ã ã³ãŒã«ããã¯é¢æ°ã§ãã ãŸãããã®æ©èœã§ã¯ããŠãŒã¶ãŒã¯æ¥ç¶ã確ç«ããããã©ãããå€æããäœããã®åœ¢ã§ãã®äºå®ã«åå¿ããããšãã§ããŸãã
ç§ã®å Žåãã³ãŒã«ããã¯é¢æ°ã¯æ¬¡ã®ããã«ãªããŸãã
void OnConnect(uv_connect_t* req, int status) { if (status==0) { // ... ... ... } else { // ... ... ... } }
ãœã±ãããäœæãã
TCPãœã±ãã㯠ã uv_tcp_tæ§é äœã«ãã£ãŠèšè¿°ãããŸã ã ãŸãããã®æ§é ã«ã¡ã¢ãªãå²ãåœãŠãå¿ èŠããããŸãã
uv_tcp_t* tcp_socket=malloc(sizeof(uv_tcp_t));
åžæããå Žåã¯ãå²ãåœãŠãããã¡ã¢ãªããŒãã§ã¯ãªã¢ã§ããŸãããããã¯ãªãã·ã§ã³ã®æäœã§ãã
memset(tcp_socket, 0, sizeof(uv_tcp_t));
次ã«ããœã±ããèªäœãäœæã§ããŸãã
uv_tcp_init(&loop, tcp_socket);
here ã«ãŒãã¯ã uv_runïŒïŒã§æäŸãããåãèŠçã®ã«ãŒãã§ãã
ãããŠä»ãã²ãŒã äœå®¶ã«ãšã£ãŠéåžžã«éèŠãªãã€ã³ãïŒ
uv_tcp_nodelay(tcp_socket, true);
ãã®èšå®ãäœã§ãããã説æããããšããŸãã å®éã«ã¯ã TCPãããã³ã«ã¯ããèªäœãéåžžã«ã¹ããŒããªãããã³ã«ãšèŠãªããéä¿¡ãããããŒã¿ã®æå°ãµã€ãºã¯ãããã«ããŠããã±ãããµã€ãºãšçãããªããããå°ããªéšåã§ããŒã¿ãéä¿¡ããããšã¯æçã§ã¯ãªãããšãç¥ã£ãŠããŸãã èšãæãããšã1ãã€ããéä¿¡ãããšãçµæãšããŠãã±ããå šäœãéä¿¡ãããŸãããã®å Žåã1ãã€ãã®æçšãªããŒã¿ã®ã¿ãéä¿¡ãããæ®ãã¯ãŽãã«ãªããŸãã ãããã£ãŠãããã©ã«ãã§ã¯ãã¹ããŒãTCPã¯ããŠãŒã¶ãŒãéä¿¡ããããŒã¿ãè¿œå ããŠãã¹ãŠãäžåºŠã«è»¢éãããŸã§200ããªç§åŸ æ©ããŸãã ãã®åŸ æ©ã¡ã«ããºã ã¯ã Nagle Algorithm ããšåŒã°ããã²ãŒã ã«ã¯ãŸã£ããé©ããŠããŸããã ãããã£ãŠããã®èšå®ã¯ããã®ãŸãŸã§ã TCPãããã³ã«ã«æ瀺ããŸã-ãèããŠã芪æãªããè³¢ããªããŠãããã«ããŒã¿ãéä¿¡ããŸããããã å®éã«ã¯ããã®èšå®ã¯TCPãããã³ã«ããã€ã«ã¢ã«ãŽãªãºã ã䜿çšããããšãçŠæ¢ããŸã ã
ããããµãŒããŒã®å Žåãã¡ã€ã³ãœã±ããã¯ããã«ããŒãã«ãã€ã³ãããå¿ èŠããããŸãã
sockaddr_in address; uv_ip4_addr("0.0.0.0", , &address); uv_tcp_bind(tcp_socket, (const struct sockaddr*)&address, 0);
ãã®å Žåãã0.0.0.0ããã¢ãã¬ã¹ãšããŠç€ºãããŸããããã¯ããœã±ããã1ã€ã ãã§ãªããã³ã³ãã¥ãŒã¿ãŒäžã«ååšãããã¹ãŠã®ãããã¯ãŒã¯ã¢ããã¿ãŒã«ãã€ã³ããããããšãæå³ããŸãã ãµãŒããŒããŒãçªå·ã¯ç¬ç«ããŠéžæããå¿ èŠããããŸããããã§ã®äž»ãªããšã¯ãä»ã®ããã°ã©ã ãšç«¶åããªãããã«ããããã®äžææ§ã§ãã
次ã«ããµãŒããŒã¯ãœã±ããã®ããŒããªãã¹ã³ãæå¹ã«ããå¿ èŠããããŸãã
uv_listen((uv_stream_t*)tcp_socket, 1024, OnAccept);
ãã¡ãããæåã®ãã©ã¡ãŒã¿ãŒtcp_socketã¯ãœã±ããèªäœã§ããã2çªç®ã®ãã©ã¡ãŒã¿ãŒã¯ããå ·äœçã§ãã ããã¯ãã€ã³ã©ã€ã³ã§åŸ æ©ã§ããæ¥ç¶èŠæ±ã®æ倧æ°ã§ãã éåžžã«äººæ°ã®ãããµãŒããŒãããããã¬ã€ã€ãŒããã®ãµãŒããŒã§ãã¬ã€ããããšããŠãããšæ³åããŠãã ããã ã¯ã©ã€ã¢ã³ãããå€ãã®æ¥ç¶èŠæ±ããããŸãã ãµãŒããŒããããã«å¿çããæéããªãå ŽåããµãŒããŒã¯ãããããã¥ãŒã«å ¥ããŸãã ãã®å Žåããã®æ°å€1024ã¯ãã®ãã¥ãŒã®æ倧ãµã€ãºã§ãã ãã¥ãŒã«å ¥ããããŠããªã人ã®ããã«ããµãŒããŒã¯3ã€ã®åèªã§å¿çããŸãã
3çªç®ã®OnAcceptãã©ã¡ãŒã¿ãŒã¯ãã¯ã©ã€ã¢ã³ããuv_tcp_connectïŒïŒé¢æ°ã䜿çšããã¯ã©ã€ã¢ã³ãããCONNECTæ¥ç¶èŠæ±ãåä¿¡ãããšãã«åŒã³åºãããã³ãŒã«ããã¯é¢æ°ã§ãã
OnAcceptïŒïŒé¢æ°ã¯æ¬¡ã®ããã«ãªããŸãã
void OnAccept(uv_stream_t *server, int status) { if (status<0) { // return; } // , uv_tcp_t* tcp_socket=malloc(sizeof(uv_tcp_t)); memset(tcp_socket, 0, sizeof(uv_tcp_t)); uv_tcp_init(&loop, tcp_socket); if (uv_accept(server, (uv_stream_t*)tcp_socket)==0) // { // uv_read_start((uv_stream_t*)tcp_socket, OnAllocBuffer, OnReadTCP); // } else { // uv_close((uv_handle_t*) tcp_socket, OnCloseSocket); // , } }
OnAcceptïŒïŒé¢æ°ã®å éšãããã«è©³ãã調ã¹ãŠã¿ãŸãããã
- æåã«ãæ°ãããœã±ãããäœæãããŸããããã¯ãæ¥ç¶ã確ç«ããèŠæ±ã®éä¿¡å
ã§ããã¯ã©ã€ã¢ã³ãã«æ¥ç¶ããããã«äœ¿çšãããŸãã ã¯ã©ã€ã¢ã³ãã¯uv_tcp_connectïŒïŒã䜿çšããŠæ¥ç¶ã確ç«ããŸãã
- Uv_acceptïŒïŒãåŒã³åºããããµãŒããŒãšã¯ã©ã€ã¢ã³ãéã®æ¥ç¶ãå®è¡ãããŸãã uv_acceptïŒïŒé¢æ°ã«ãããã¯ã©ã€ã¢ã³ãã¯ã uv_tcp_connectïŒïŒã§æåŸã®ãã©ã¡ãŒã¿ãŒãšããŠæå®ãããã³ãŒã«ããã¯é¢æ°ãåŒã³åºããŸãã äžèšã®äŸã§ã¯ã OnConnectïŒïŒé¢æ°ã§ããã
- æ¥ç¶ãæ£åžžã«ç¢ºç«ãããå ŽåããµãŒããŒã¯äœæããããœã±ããããã®æ¥ç¶ããããŒã¿ãèªã¿åãããšãèš±å¯ããå¿
èŠããããŸãã uv_read_startïŒïŒé¢æ°ã¯ãæ°ããäœæãããtcp_socketãœã±ããã®ããŒã¿ã®èªã¿åããæå¹ã«ããŸãã ãããŒã¿ã®èªã¿åãããšããœã±ããã®ãªãã¹ã³ãã¯ç°ãªãæäœã§ããããšã«æ³šæããŠãã ããã ãããŒã¿ã®èªã¿åããã¯ããã¡ã€ã«ããã®ãã€ãã®èªã¿åããšåæ§ã«ãæåéããèªã¿åããã§ãããããœã±ããããªãã¹ã³ãã¯ãæ¥ç¶ã確ç«ããããã®CONNECTèŠæ±ãåŸ
æ©ããŠããŸãã
uv_read_startïŒïŒé¢æ°ã¯ã2ã€ã®æŽæ°ã³ãŒã«ããã¯é¢æ°ã䜿çšããŸãã
-OnAllocBufferïŒïŒã¯ãããŒã¿ãèªã¿åãåã«åŒã³åºãããããŒã¿ãåä¿¡ããããã®ã¡ã¢ãªãæå®ãããããŠãŒã¶ãŒã«èŠæ±ããŸãã
é¢æ°èªäœã¯æ¬¡ã®ããã«å®çŸ©ãããŸãã
void OnAllocBuffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
æåã®ãã©ã¡ãŒã¿ãŒuv_handle_t * handleã¯ãã¡ã¢ãªãå¿ èŠãšãããœã±ãããžã®ãã€ã³ã¿ãŒã§ãã
2çªç®ã®ãã©ã¡ãŒã¿ãŒsize_t suggest_sizeã¯ãå¿ èŠãªãããã¡ãŒãµã€ãºã§ãã
3çªç®ã®ãã©ã¡ãŒã¿ãŒuv_buf_t * bufã¯ããŠãŒã¶ãŒããããã¡ãŒæ å ±ïŒãµã€ãºãšã¢ãã¬ã¹ïŒãlibuvã©ã€ãã©ãªã«è¿ãããã®æ§é ã§ãã
buf->len=; buf->base=;
ã€ãŸãããããã¡èªäœã¯ãŠãŒã¶ãŒãäœæãããŠãŒã¶ãŒãåé€ããå¿ èŠããããŸãã ãŸãã libuvã¯ãã®ãããã¡ãŒãžã®ããŒã¿ã®ã¿ãåãå ¥ããŸãã åããããã¡ãè€æ°ã®ãœã±ããã«äœ¿çšã§ããŸãã
ç§ã®å Žåãäœããã®çç±ã§libuvã¯åžžã«65536ãã€ãã®ãããã¡ãŒãèŠæ±ããŸããã ç§ã«ãšã£ãŠã¯ãããã¯å°ãå¥åŠã§ããããã®ã¡ã¢ãªã1åå²ãåœãŠãã®ã§ãåé¡ã¯ãªãããã§ãã
-OnReadTCPïŒïŒã¯OnAllocBufferïŒïŒã®åŸã«åŒã³åºããããœã±ããããããã¡ã§åä¿¡ããããŒã¿ããŠãŒã¶ãŒã«æž¡ããŸãã
é¢æ°èªäœã¯æ¬¡ã®ããã«å®çŸ©ãããŸãã
void OnReadTCP(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
æåã®ãã©ã¡ãŒã¿ãŒuv_handle_t *ãã³ãã«ã¯ãããŒã¿ãåä¿¡ãããœã±ãããžã®ãã€ã³ã¿ãŒã§ãã
2çªç®ã®ssize_t nreadãã©ã¡ãŒã¿ãŒã¯ãåä¿¡ãããã€ãæ°ã§ãã nreadããŒã以äžã®å Žåãããã¯æ¥ç¶ãåæãããããšã瀺ããŠããŸãã ã€ãŸã ããã¯ããŒã¿ãèªã¿åãã®ã§ã¯ãªãã2çªç®ã®åŽããã®æ¥ç¶ã«é¢é£ãããœã±ãããåãå€ãããšããäºå®ããŸãã¯ãã±ãŒãã«ç Žæããªã©ã®ããŒããŠã§ã¢ã®çç±ã«ããçºçããå¯èœæ§ã®ããåæã«é¢ããæ å ±ã§ãã ãã®ç¶æ³ãç£èŠããããã«å¿ããŠå¯Ÿå¿ããå¿ èŠããããŸãã
3çªç®ã®ãã©ã¡ãŒã¿ãŒã¯const uv_buf_t * buf-èªã¿åãããŒã¿ã®ãããããã¡ãŒã®ã¢ãã¬ã¹ãå«ã¿ãŸãã ããã¯ããŠãŒã¶ãŒãOnAllocBufferïŒïŒé¢æ°ã§æå®ãããã®ãšåãã¢ãã¬ã¹ã«ãªããŸãã
ããŒã¿ãèªã¿åãæãéèŠãªç¬éã åè¿°ããããã«ã TCPã¯éä¿¡ã¡ãã»ãŒãžãéšåçã«é ä¿¡ã§ããŸããããšãã°ãéä¿¡è ããHelloããšãããã¬ãŒãºãéä¿¡ãã次ã«ãServerããšãããã¬ãŒãºãéä¿¡ããå Žåãããã2ã€ã®ã¡ãã»ãŒãžãåä¿¡ãããšãã«æ å ±ããŸã£ããåãããã«èŠããå¿ èŠã¯ãããŸããé é圢æ ã§ã ãŸããäž¡æ¹ã®ã¡ãã»ãŒãžã1ã€ã«ãŸãšãããšãåä¿¡åŽã¯ãHelloãServerããšããã¡ãã»ãŒãžå šäœãåä¿¡ããŸããæåã«ãWhenãã次ã«ãVetãSerãããVerããªã©ã®ã¡ãã»ãŒãžãåä¿¡ããŸãã ã€ãŸã çè«äžã®ã¡ãã»ãŒãžã¯ãä»»æã®æ°ã®éšåã«æçåã§ããŸãã ãããã£ãŠããããã¯ãŒã¯ãä»ããŠéä¿¡ããããã¹ãŠã®ã¡ãã»ãŒãžã«ã¯ãåžžã«ãããã¡ãã»ãŒãžãä»ã®ãœã±ããããã®ãã€ãã¹ããªãŒã ããåé¢ã§ããããããŒãå¿ èŠã§ãã ããã¯ãã¹ãŠã TCPãœã±ããããããŒã¿ãèªããšããéåžžã«äžå¿«ãªæ©èœã«ã€ãªãããŸã ã å®éã«ã¯ããã®ãããªãœã±ããã«ã¯ãããã次ã®ã¡ãã»ãŒãžã®çä¿¡ãã€ããæ ŒçŽããç¬èªã®ãããã¡ãŒãå¿ èŠã§ããã¡ãã»ãŒãžãéšåçã«ããåä¿¡ãããªãå Žåãå®å šã«åä¿¡ããããŸã§åŸ æ©ããå¿ èŠãããããã®å Žåã«ã®ã¿äœããã®æ¹æ³ã§å¿çã§ããããã§ã
- æ¥ç¶ã確ç«ã§ããªãã£ãå Žåããã®æ¥ç¶çšã®ãœã±ãããåé€ããå¿
èŠããããŸãã ãããè¡ãã«ã¯ã uv_closeïŒïŒé¢æ°ã䜿çšããŸãã ããã...ãã®é¢æ°ã¯ãæåŸã®ãã©ã¡ãŒã¿ãŒãšããŠOnCloseSocketïŒïŒã³ãŒã«ããã¯é¢æ°ãåãå
¥ããããšã«æ³šæããŠãã ããã ãŸãããã®é¢æ°ãåŒã³åºããæç¹ã§ãlibuvã¯ãœã±ãããã¡ã¢ãªããç©ççã«åé€ã§ããããã«ãªã£ãããšããŠãŒã¶ãŒã«äŒããŸãã
void OnCloseSocket(uv_handle_t* handle) { free(handle); // malloc(), free() }
ã¡ãã»ãŒãžãéä¿¡ãã
ãœã±ãããä»ããŠã¡ãã»ãŒãžãéä¿¡ããã«ã¯ã uv_writeïŒïŒé¢æ°ã䜿çšããŸãã
int uv_write(uv_write_t* req, uv_stream_t* handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb);
æåã®ãã©ã¡ãŒã¿ãŒuv_write_t * reqã¯ã libuvãããŒã¿ã転éããããã«å¿ èŠãªããçš®ã®å€æ°ã§ãã é¢æ°ãã©ã¡ãŒã¿ãŒã«ååšããæå³ãæãäžããããšã¯äŸ¡å€ããããŸããããããšãã°æ¬¡ã®ããã«äœæããå¿ èŠããããŸãã
uv_write_t write_data;
çŸåšããã®å€æ°ã¯ã¡ãã»ãŒãžã®é£ç¶éä¿¡ã«ç¹°ãè¿ã䜿çšã§ããŸãããè€æ°ã®ã¡ãã»ãŒãžã®éä¿¡ã«åæã«äœ¿çšããããšã¯ã§ããŸããã
2çªç®ã®ãã©ã¡ãŒã¿ãŒuv_stream_t * handleã¯ãéä¿¡åŽãœã±ããã§ãã
3çªç®ã®ãã©ã¡ãŒã¿ãŒconst uv_buf_t bufs []ã¯ãéä¿¡ããã¡ãã»ãŒãžã®é åã§ãã uv_buf_tåã®èŠçŽ ã§æ§æãããŸãããããã®èŠçŽ ã«ã¯lenãã£ãŒã«ããšbaseãã£ãŒã«ãããããããããSIZEãšADDRESSãå«ãŸããŠããå¿ èŠããããŸãã
4çªç®ã®ãã©ã¡ãŒã¿ãŒunsigned int nbufsã¯ãã¡ãã»ãŒãžé åå ã®èŠçŽ ã®æ°ã§ãã ç§ã®å Žåãéä¿¡ããã¡ãã»ãŒãžã¯åžžã«1ã€ã ãã§ããã
5çªç®ã®ãã©ã¡ãŒã¿ãŒuv_write_cb cbã¯ãã¡ãã»ãŒãžãéä¿¡ããããšãã«åŒã³åºãããã³ãŒã«ããã¯é¢æ°ã§ãã ãªãå¿ èŠãªã®ã§ããïŒ å®éããŠãŒã¶ãŒãéä¿¡ããã¡ãã»ãŒãžã¯ãéä¿¡ããããŸã§ã¡ã¢ãªã«ä¿åããå¿ èŠããããŸãã ã€ãŸã ãã®ã³ãŒã«ããã¯é¢æ°ãããªã¬ãŒããããšãéä¿¡ãããã¡ãã»ãŒãžãå«ãããŒã¿ãããã¡ãŒãlibuvã§äžèŠã«ãªãããšãæå³ããŸãã ãããŠä»ããã®ãããã¡ã¯åã³ãŠãŒã¶ãŒå¶åŸ¡äžã«ãããæ°ããããŒã¿ã§æºããããæ°ããã¡ãã»ãŒãžãéä¿¡ã§ããŸãã
ç§ã®å ŽåãããŒã¿ãããã¡ãŒãšããããŸãã ãå¿ èŠãªuv_write_t write_dataã®äž¡æ¹ã1ã€ã®æ§é ã«å ¥ããŠããŸãã ãããã£ãŠãåãæ§é ã®äžéšãšããŠãã¢ã§æ©èœããŸãã
libuvæ§é ãããã䟿å©ãªããŒã¿åãžã®ç§»è¡
ã¡ãã»ãŒãžãåãå ¥ããuv_tcp_tã®ãããªå€ãã®ãœã±ããããããšæ³åããŠãã ããã å°ãäžã§è¿°ã¹ãããã«ãããŒã¿ã¯ãããŒã®ååã«åŸã£ãŠèªã¿åãããã¡ãã»ãŒãžã¯ä»»æã®éšåã«åå²ã§ãããããçä¿¡ããŒã¿ãæ ŒçŽããã³åæããããã«åãœã±ããã«ãããã¡ãè¿œå ã§å¿ èŠã«ãªããŸãã ãããŠã OnReadTCPïŒïŒã³ãŒã«ããã¯é¢æ°ãããäžåºŠèŠãŠã¿ãŸãããã
void OnReadTCP(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
ãŸãã uv_stream_t *ã¹ããªãŒã ãã©ã¡ãŒã¿ãŒãä»ããŠãããŒã¿ãåä¿¡ãããœã±ããã衚瀺ãããããšã«æ³šæããŠãã ããã ããã... ...ãœã±ãããç®çã®ããŒã¿ãããã¡ã«ã©ã®ããã«é¢é£ä»ããŸããïŒ 1000åã®ãœã±ãããããã誰ããããã«äœããåãå ¥ãããšããŸãããã ãœã±ããããšã«ãåä¿¡ããããŒã¿ãç¬èªã®ãããã¡ãŒã«å ¥ããå¿ èŠããããŸãã ããããçµå±ã®ãšããããã®ãããã¡ãŒã¯ãœã±ããã«ãã£ãŠæ±ºå®ãããããã§ã¯ãããŸãã-ãœã±ããæ§é å ã«ã¯ã䜿çšå¯èœãªæ°åã®ãããã¡ãŒã®ã©ãã«å¯Ÿå¿ãããã®å åã®ãªããã³ã»ã³ã¹ããããŸãã
ãããã£ãŠããããã¯ãŒãã³ã°ã®åçã¯å®å šã«ç°ãªãã¯ãã§ãã次ã«ããã®åé¡ãã©ã¡ãã®åŽã§è§£æ±ºã§ãããã説æããããšããŸãã
ãšããããã libuvã®ååšãå¿ããŠãå ±éã®ãµãŒããŒæ§é ãäœæããŠã¿ãŸãããã
GNetServerãšãããµãŒããŒã¯ã©ã¹ããããšããŸã ã ãã®ã¯ã©ã¹ã®ãªããžã§ã¯ãã¯åžžã«åäžã®ã€ã³ã¹ã¿ã³ã¹ã«ååšãããµãŒããŒæ©èœãå®å šã«æ³å®ããŠããŸãã ç§ã®å ŽåããµãŒããŒã«ã¯2ã€ã®ã¡ã€ã³ã¢ã¬ã€ãå¿ èŠã§ãã
- ãã¬ã€ã€ãŒã®é å
- æ¢åã®ã»ãã·ã§ã³ã®é åïŒãŸãã¯äœæãããã²ãŒã ã®é åïŒ
ããã2ã€ã®é åã®éã«ã¯å¯æ¥ãªé¢ä¿ã確ç«ãããŠããå¿ èŠããããŸãã ãã¬ã€ã€ãŒã¯èªåãæå±ããŠããã»ãã·ã§ã³ãç¥ã£ãŠããå¿ èŠããããŸãïŒãã§ã«ã»ãã·ã§ã³ã«åå ããŠããå ŽåïŒããŸããã©ã®ã»ãã·ã§ã³ããã®ãã¬ã€ã€ãŒãã³ã³ããžã·ã§ã³ã«å«ãŸããŠããããšãç¥ã£ãŠããå¿ èŠããããŸãã
ãã¬ãŒã€ãŒãšã»ãã·ã§ã³ã®èŠä»¶ã¯äœã§ããïŒ äž»ãªèŠä»¶ã¯ãå人ã®äžæã®èå¥åã«ããç®çã®ãã¬ãŒã€ãŒãŸãã¯ã»ãã·ã§ã³ãžã®è¿ éãªã¢ã¯ã»ã¹ã§ãã ãããŠããã®èå¥åãåçŽã«é åå ã®ãã¬ãŒã€ãŒãŸãã¯ã»ãã·ã§ã³ã®ã€ã³ããã¯ã¹ã«ãããããé«éãªæ¹æ³ãèãåºãããšã¯ããããäžå¯èœã§ãã ãã®ããããã¹ãŠã®ãã¬ãŒã€ãŒã«ã¯IDããããŸããããã¯ããã¬ãŒã€ãŒã®åèšé åå ã®IDãšåçŽã«åãã§ãã ãããŠä»ãå¥ã®ãã¬ãŒã€ãŒãèå¥å5ã10ã21ãããã³115ã®ãã¬ãŒã€ãŒã«ããŒã¿ãéä¿¡ããå ŽåããµãŒããŒã¯ãèå¥åãã€ã³ããã¯ã¹ãšããŠäœ¿çšããã ãã§ããããã®åä¿¡è ãããã«èå¥ã§ããŸãã
次ã«ããµãŒããŒã®èŠ³ç¹ããããã¬ãŒã€ãŒããäœã§ããããå€æããŸãããã å®éãããã¬ãŒã€ãŒãã¯ããœã±ãããã§ãããããã€ãã®è¿œå æ å ±ãããããŸããã è¿œå æ å ±ã«ã¯æ¬¡ã®ããŒã¿ãå«ãŸããŸãã
- ãã¬ã€ã€ãŒIDïŒãã¬ã€ã€ãŒã®é åã®ã€ã³ããã¯ã¹ïŒ
- åä¿¡ããŒã¿çšã®ãããã¡
- ãœã±ããäœææé
- ãœã±ããã§æåŸã«ããŒã¿ãåä¿¡ãããæé
- çç¶UDP / TCP
- ãã¬ãŒã€ãŒåïŒãããã°ããã³äžè¬æ å ±ã«ã®ã¿å¿ èŠïŒ
- ããã»ã©éèŠã§ãªãäºçŽ°ãªããš
ãã®æ å ±ã¯ãã¹ãŠGNetSocketã¯ã©ã¹ã«ä¿åãããŸãã ãã ãã libuvããã®ããŒã¿ã¯ãªãããšã«æ³šæããŠãã ããã ããã¯ãå¿ èŠã«å¿ããŠlibuvãå¥ã®ãã®ã«çœ®ãæããããšãã§ããããã«è¡ãããŸãã
ç§ã®å Žåã GNetSocketãç¶æ¿ããGNetSocketLibUVã¯ã©ã¹ããããŸãã å®éã«ã¯GNetSocketLibUVã¯ã©ã¹ã®ãªããžã§ã¯ãã®ã¿ãäœæãããå ŽåããªãäžéGNetSocketã¯ã©ã¹ãå¿ èŠãªã®ã§ããïŒ äºå®ãç§ã®ä»äºã¯ãå¯èœãªéãäžè¬çãªãããã¯ãŒã¯æ§é ããlibuvã©ã€ãã©ãªãåé¢ããããšã§ããã ãã®çµæãã¡ã€ã³ãµãŒããŒ/ã¯ã©ã€ã¢ã³ããã¡ã€ã«ã¯7000è¡ä»¥äžãå æãã libuvåºæã®ãã¡ã€ã«ã¯ 600è¡ããããŸãã ãŸãã libuvã眮ãæããå¿ èŠãããå Žåã¯ãæ¯èŒçç°¡åã«è¡ãããšãã§ããŸãã ãŸããããŒã¯ã©ã¹ã®ãªããžã§ã¯ããnewãä»ããŠçŽæ¥äœæãããã®ã§ã¯ãªããä»®æ³é¢æ°ãä»ããŠäœæãããå Žåã«ãååã䜿çšããŸããããšãã°ãããã«ãããµãŒããŒã®ãœã±ãããªããžã§ã¯ããäœæãããŸãã
GNetSocket* GNetServerLibUV::NewSocket(GNet* net) { return new GNetSocketLibUV(net); }
ã€ãŸã 1ã€ã®å Žæã§äº€æããŠã æ°ããGNetSocketLibUVïŒnetïŒãè¿ãå¿ èŠããããŸãã ä»ã®ã¿ã€ãã®ãªããžã§ã¯ãã«å¯ŸããŠã¯ãããã°ã©ã å ã§ã¯ãã®ã¿ã€ãã®ã¿ãäœæãããŸãã
GNetSocketLibUVã¯ã©ã¹ã¯ãåºæ¬ã¯ã©ã¹ã®ãã¹ãŠã®æœè±¡é¢æ°ããªãŒããŒã©ã€ãããŸãã 次ã®ããã«ãªããŸãã
class GNetSocketLibUV : public GNetSocket { public: void* sock; public: GNetSocketLibUV(GNet* net); virtual ~GNetSocketLibUV(); // UDP TCP- , listaen=true, virtual bool Create(bool udp_tcp, int port, bool listen); // TCP- virtual bool SetConnectedSocketToReadMode(); // virtual void Destroy(); // IP- TCP- // own_or_peer virtual bool GetIP(CMagicString& addr, bool own_or_peer); // ( TCP-) virtual bool Connect(NET_ADDRESS* addr); // ( TCP-) virtual bool Accept(); virtual void SendTCP(NET_BUFFER_INDEX* buf); virtual void SendUDP(NET_BUFFER_INDEX* buf); virtual void ReceiveTCP(); virtual void ReceiveUPD(); };
1ã€ã®å€æ°void * sockã®ã¿ãGNetSocketLibUVã¯ã©ã¹ã«è¿œå ãããŸãããããã¯ãlibuvãœã±ãããžã®ã»ãšãã©ã®ãã€ã³ã¿ãŒã«ãªããŸããã泚æãå¿ èŠã§ããç§ãã¡ã¯ãããã«ãœã±ãããžã®èœåãå¿ èŠlibuv察å¿ãããœã±ããã¿ã€ãã決å®GNetSocketãåã«ãããã¡ãèªãããã«ã©ã®åããããŠãã¬ã€ã€ãŒã®IDãã©ããã£ãŠããã®ïŒäžéæ§é ãè¿œå ããŸããïŒ
struct NET_SOCKET_PTR { GNetSocket* net_socket; }; struct TCP_SOCKET : public NET_SOCKET_PTR, public uv_tcp_t { }; struct UDP_SOCKET : public NET_SOCKET_PTR, public uv_udp_t { };
ã ãã... ...ä»ãç§ãã¡ã¯ã®ããã®æ°ããæ§é ãæã£ãŠããTCPãœã±ãããšåŒã°TCP_SOCKETãšã®ããã®æ°æ§é UDPãœã±ãããšåŒã°ããUDP_SOCKETããããã...ãããã®æ§é ã¯äž¡æ¹ãšããœã±ããæ§é ã®åã«æ°ãããã£ãŒã«ããæã¡ãŸããããã¯GNetSocketã¯ã©ã¹ã®èŠªãªããžã§ã¯ããžã®ãã€ã³ã¿ã§ãã
ãã1ã€éèŠãªç¹ã§ããããã°ã©ã ã¯ãã©ãã«ãããã€ãã£ãããªlibuvãœã±ãããäœæããã¹ãã§ã¯ãªããTCP_SOCKETãUDP_SOCKETãªã©ã®ãœã±ããã®ã¿ãäœæããå¿ èŠããããŸããäœæçŽåŸã«ããã®äžéšãšããŠäœæãããGNetSocketãªããžã§ã¯ãã®ã¢ãã¬ã¹ãnet_socketãã£ãŒã«ãã«èšé²ããå¿ èŠããããŸãTCP_SOCKETãŸãã¯UDP_SOCKETã
å®éã«ã¯ããœã±ããã®äœæã¯æ¬¡ã®ããã«ãªããŸãã
// UDP TCP- , listen=true, bool GNetSocketLibUV::Create(bool udp_tcp, int port, bool listen) { GNetSocket::Create(udp_tcp, port, listen); uv_loop_t* loop=GetLoop(net); if (udp_tcp) { sock=malloc(sizeof(TCP_SOCKET)); memset(sock, 0, sizeof(TCP_SOCKET)); ((TCP_SOCKET*)sock)->net_socket=this; ... ... ... }
ããŠããšãã«æã ã®ãã€ã*éŽäžã¯ã¢ãã¬ã¹ã§ãTCP_SOCKETãŸãã¯UDP_SOCKETããšç§ãã¡ã¯åžžã«æ§é ã®æåã¯åžžã«æ ¹åºã«ãããœã±ãããžã®ãã€ã³ã¿ã«ãªãããšãç¥ã£ãŠããGNetSocket * Net_Socketããè¿ éãªã€ã³ã¹ããŒã«é©åæ§ãã®ã¿ã¹ã¯ã¯ã»ãšãã©è§£æ±ºããŸããã
å¿ èŠãªããŒã¿ãååŸããã®ã«åœ¹ç«ã€é¢æ°ãããã€ãè¿œå ããŸããsockãTCP_SOCKETã®
å Žåã次ã®é¢æ°ã«sockã¢ãã¬ã¹ãæž¡ããšãlibuv TCPãœã±ãããç°¡åã«æœåºãããŸãã
uv_tcp_t* GetPtrTCP(void* ptr) { return (uv_tcp_t*)(((char*)ptr)+sizeof(void*)); }
sockãUDP_SOCKETã®å Žåã次ã®é¢æ°ã«sockã¢ãã¬ã¹ãæž¡ããšãlibuv UDPãœã±ãããç°¡åã«æœåºãããŸãã
uv_udp_t* GetPtrUDP(void* ptr) { return (uv_udp_t*)(((char*)ptr)+sizeof(void*)); }
GetNetSocketPtré¢æ°ïŒãœã±ããã®uv_tcp_tãŸãã¯uv_udp_tã®ã¢ãã¬ã¹ïŒã䜿çšãããšããã®ãœã±ããã«å¯Ÿå¿ããGNetSocketã¿ã€ãã®ã¡ã€ã³ãœã±ããã®ã¢ãã¬ã¹ãååŸã§ããŸãã
GNetSocket* GetPtrSocket(void* ptr) { return *((GNetSocket**)ptr); } GNetSocket* GetNetSocketPtr(void* uv_socket) { return GetPtrSocket(((char*)uv_socket)-sizeof(void*)); }
å®éã«äœ¿çšããæ¹æ³ã¯ïŒããšãã°ãTCPãœã±ãããèªã¿åãã¢ãŒãã«ããå¿ èŠããããŸãã
// TCP- bool GNetSocketLibUV::SetConnectedSocketToReadMode() { if (udp_tcp) { uv_tcp_t* tcp=GetPtrTCP(sock); int r=uv_read_start((uv_stream_t*)tcp, OnAllocBuffer, OnReadTCP); return (r==0); } return false; }
éŽäžã¯GetPtrTCPïŒsockïŒã䜿çšããŠuv_tcp_t * tcpã«å€ããããã§ã«uv_read_startïŒïŒé¢æ°ã«æž¡ãããšãã§ããããšã«æ³šæããŠãã ãããããŠãç§ã®å ŽåãOnReadTCPïŒïŒã³ãŒã«ããã¯é¢æ°ã¯æ¬¡ã®ããã«ãªããŸãã
void OnReadTCP(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { GNetSocket* socket=GetNetSocketPtr(stream); if (nread>0) { NET_BUFFER* recv_buffer=socket->net->GetRecvBuffer(); assert(buf->base==(char*)recv_buffer->GetData()); recv_buffer->SetLength(nread); socket->ReceiveTCP(); } else { // , socket->net->OnLostConnection(socket); } }
æåã®è¡ïŒ
GNetSocketLibUV* socket=(GNetSocketLibUV*)GetNetSocketPtr(stream);
GNetSocketãªããžã§ã¯ãã®ã¢ãã¬ã¹ãååŸããŸãããã®ãªããžã§ã¯ãã«å¯ŸããŠãé¢æ°socket- > ReceiveTCPïŒïŒãå®è¡ããããœã±ãããåä¿¡ããã¡ãã»ãŒãžã®åä¿¡ãå®çŸããŸãã圌女ã¯ãã§ã«ãã®ããŒã¿ãèªåã®ãœã±ãããããã¡ãŒã«å ¥ããã¡ãã»ãŒãžãå®å šã«åä¿¡ãããããšã確èªããŠããããµãŒããŒãŸãã¯ã¯ã©ã€ã¢ã³ãã«éä¿¡ããŠåŠçããŸãïŒãµãŒããŒãšã¯ã©ã€ã¢ã³ãéã®ã³ãŒãã®å€§éšåã¯äžèŽããŠããŸãïŒã
ãŸãããœã±ãããåé€ããäŸã瀺ããŸãã
// void GNetSocketLibUV::Destroy() { if (sock) { if (udp_tcp) { uv_tcp_t* tcp=GetPtrTCP(sock); uv_close((uv_handle_t*)tcp, OnCloseSocket); ((TCP_SOCKET*)sock)->net_socket=NULL; } else { uv_udp_t* udp=GetPtrUDP(sock); int r=uv_read_stop((uv_stream_t*)udp); assert(r==0); uv_close((uv_handle_t*)udp, OnCloseSocket); ((UDP_SOCKET*)sock)->net_socket=NULL; } sock=NULL; } GNetSocket::Destroy(); }
ããã®è¡ã«æ³šæããŠãã ããã
((TCP_SOCKET*)sock)->net_socket=NULL;
ãã€ãŸãlibuvãœã±ããã¯ããã§ã¯åé€ãããŸããããã¡ã€ã³ã®GNetSocketãªããžã§ã¯ãã¯ãã®ãœã±ãããšã®æ¥ç¶ãæããªããªããããäžè¬çã«ããèªäœã§åé€ãããŸãããã ããlibuvããœã±ããã§ã®ãã¹ãŠã®äœæ¥ãå®äºãããšãOnCloseSocketïŒïŒã³ãŒã«ããã¯é¢æ°ãå¿ ç¶çã«æ©èœããfreeïŒïŒãå®è¡ãããŸãããããã£ãŠãã¡ã¢ãªãªãŒã¯ã¯çºçããŸããã
ããã§ãlibuvã©ã€ãã©ãªã«é¢ããäŒè©±ãçµäºã§ãããšæããŸããç§ã¯ããã®äœæ¥ã®æ ¹åºã«ããååã®æ¬è³ªãæ確ã«ããããšããŸãããããããã®ååã¯ãWinSockãå«ãã»ãšãã©ã®ãœãªã¥ãŒã·ã§ã³ã§å®è³ªçã«åãã§ãããšæããŸããããããç§ã®è§£éã§ã¯ååãªã³ãŒãäŸã¯ãããŸããããé¢æ°åã§ã€ã³ã¿ãŒãããäžã§èŠã€ããã®ã¯ããã»ã©é£ãããããŸããããããã®é¢æ°ã§äœãããå¿ èŠãããã®ãââããããŠããããäºãã«ã©ã®ããã«çžäºäœçšããã®ãã説æããããšããŸãããç§ã®ç®æšã¯ã²ãŒã ãå®æãããããšã§ããããããã¯ãŒã¯ããã°ã©ãã³ã°ã®åéã®å°é家ã«ãªãããšã§ã¯ãªãã£ããããç§ã®ç解ã§ã¯äžæ£ç¢ºãªç¹ãããå¯èœæ§ããããŸãããã®ããããå¿ èŠãã€ååãã«åºã¥ããŠãã®ã±ãŒã¹ãèŠã€ããŸããã
GNetClientãããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã®æ§é
ã¯ã©ã€ã¢ã³ãã¯ç°ãªãæç¹ã§å®å šã«ç°ãªãåäœãããå¿ èŠããããŸããããšãã°ãæåã«ãµãŒããŒã«æ¥ç¶ããŠã»ãã·ã§ã³ãäœæãŸãã¯åå ããå¿ èŠããããã²ãŒã ãæ¢ã«éå§ãããŠããå ŽåãããŠã¹ãšããŒããŒãããåä¿¡ããããŒã¿ãä»ã®ãã¬ãŒã€ãŒãšäº€æããå¿ èŠããããŸãã
ã¹ããŒãžã§ã¯ã©ã€ã¢ã³ãã®äœæ¥ã®è«çãç Žããåã¹ããŒãžã®åäœãåå¥ã«èšè¿°ããããšãã§ããŸãã
ç§ã®å Žåã次ã®æ®µéããããŸã
ãGNetStadyEnumSession / NET_STADY_ENUM_SESSION-æ¢åã®ã²ãŒã ã»ãã·ã§ã³ã®ãªã¹ããååŸãã段éã
GNetStadyJoinSession / NET_STADY_JOIN_SESSION-ã²ãŒã ã»ãã·ã§ã³ã«æ¥ç¶ããç¬èªã®ãã¬ãŒã€ãŒãã»ããã¢ãããã段éã
GNetStadyCreateSession / NET_STADY_CREATE_SESSION-ã»ãã·ã§ã³ãäœæãããããèšå®ããèªåã®ãã¬ãŒã€ãŒãèšå®ã§ããã¹ããŒãžã
GNetStadyStartGame / NET_STADY_START_GAME-ãªã³ã©ã€ã³ã²ãŒã ãèµ·åãã段éã
GNetStadyGame / NET_STADY_GAME-ã²ãŒã ã®ã¹ããŒãžã
GNetStadyMigrationHost / NET_STADY_MIGRATION_HOST-ãã¹ã移è¡ã®æ®µéã
ã¹ããŒãžã€ã³ããã¯ã¹ã¯æ¬¡ã®ããã«å®£èšãããŸãã
enum NET_STADY {NET_STADY_ENUM_SESSION, NET_STADY_JOIN_SESSION, NET_STADY_CREATE_SESSION, NET_STADY_START_GAME, NET_STADY_GAME, NET_STADY_MIGRATION_HOST, NET_STADY_NO=-1};
åã¹ããŒãžã®ãªããžã§ã¯ãã¯äºåã«äœæãããGNetClientå ã®ã¹ããŒãžã®é åã«ä¿åãããŸãã
// , class GNetClient : public GNet { protected: NET_STADY net_stady; // int k_net_stady; // GNetStady** m_net_stady; // ... ... ... } GNetClient::GNetClient() : GNet() { net_stady=NET_STADY_NO; k_net_stady=6; m_net_stady=new GNetStady*[k_net_stady]; m_net_stady[NET_STADY_ENUM_SESSION]=new GNetStadyEnumSession(this); m_net_stady[NET_STADY_JOIN_SESSION]=new GNetStadyJoinSession(this); m_net_stady[NET_STADY_CREATE_SESSION]=new GNetStadyCreateSession(this); m_net_stady[NET_STADY_START_GAME]=new GNetStadyStartGame(this); m_net_stady[NET_STADY_GAME]=new GNetStadyGame(this); m_net_stady[NET_STADY_MIGRATION_HOST]=new GNetStadyMigrationHost(this); ... ... ... }
ãã¹ãŠã®ã¹ããŒãžã®åºæ¬ã¯ã©ã¹ã¯GNetStadyã¯ã©ã¹ã§ãããããããä»ã®ãã¹ãŠã®ã¹ããŒãžãç¶æ¿ãããŸãã
// , class GNetStady { protected: GNetClient* owner; // - GNetClient unsigned int stady_period; unsigned int stady_tick; public: GNetStady(GNetClient* owner); virtual ~GNetStady(){} virtual bool OnStart(NET_STADY previous, void* init); virtual void OnFinish(NET_STADY next){} virtual void OnUpdate(); // , stady_period ( 0) virtual void OnPeriod(){} // virtual bool IsMessageCorrected(int message_type){return false;} };
ã¹ããŒãžã®æ©èœã«ã€ããŠç°¡åã«èª¬æããŸãããã
virtual bool OnStart(NET_STADY previous, void* init)
. , , . false , , . NET_STADY previous void* init , .
bool GNetClient::SetStady(NET_STADY stady, void* init) .
virtual void OnFinish(NET_STADY next)
, , .. , - . .
SetStady() :
// bool GNetClient::SetStady(NET_STADY stady, void* init) { if (stady!=net_stady) { if (net_stady!=NET_STADY_NO) { // m_net_stady[net_stady]->OnFinish(stady); } if (stady!=NET_STADY_NO) { // if (!m_net_stady[stady]->OnStart(net_stady, init)) return false; } net_stady=stady; } return true; }
virtual void OnUpdate()
, . , .
GNetStady::OnUpdate() GNetClient::OnUpdate():
void GNetClient::OnUpdate() { if (net_stady!=NET_STADY_NO) m_net_stady[net_stady]->OnUpdate(); }
virtual void OnPeriod()
, , - . GNetStady::stady_period , OnPeriod() . , stady_period=500 , , OnPeriod() 1 .
OnPeriod() OnUpdate() , :
void GNetStady::OnUpdate() { if (stady_period) { unsigned int tick=owner->game->platform->GetTick(); if (tick>=stady_tick+stady_period) { stady_tick=tick; OnPeriod(); } } }
virtual bool IsMessageCorrected(int message_type)
, . , , . . , , .
ãªããããå¿ èŠãªã®ã§ããïŒ , -, . , -, , .. , N- .
, , . , - ( Cancel ) NET_STADY_ENUM_SESSIONå¥ã®ã»ãã·ã§ã³ãéžæããŸãããã®åŸãã»ãã·ã§ã³ãçµäºãããšãã«ã圌ã¯ä»ã®ãã¬ã€ã€ãŒã«ãèªåã®éå Žãéç¥ããŸãããåé¡ã¯ã圌ãã圌ã®ã¡ãã»ãŒãžãåä¿¡ããŠââããã«åå¿ããéãæéãçµéããããšã§ãããã®æéäžãä»ã®ãã¬ã€ã€ãŒã¯ãã»ãã·ã§ã³èšå®ã«é¢ããã»ãã·ã§ã³ãæ¢ã«çµäºããŠãããã¬ã€ã€ãŒã«ç°¡åã«ã¡ãã»ãŒãžãéä¿¡ãç¶ããããšãã§ããŸãããããŠããããã®ãã§ã«äžèŠãªã¡ãã»ãŒãžã¯ãå»ã£ããã¬ã€ã€ãŒãå·é ·ã«é®æããªããã°ãªããªãããšã§ãã
ãããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã¹ããŒãžç®¡ç
ã¯ã©ã€ã¢ã³ãããŸã ãµãŒããŒã«æ¥ç¶ããŠããªãå Žåãé€ããäžéšã®ã¹ããŒãžã¯åžžã«ææ°ã§ãã
ãããã¯ãŒã¯ã§ã®äœæ¥ãéå§ããã«ã¯ã次ã®æ©èœã䜿çšããŸãã
// virtual bool GGame::StartNet(const char* ip);
ip = NULLã®å Žåãç¬èªã®ããŒã«ã«ãµãŒããŒãèµ·åããŸãããã以å€ã®å Žåãipã¯ã€ã³ã¿ãŒããããµãŒããŒã®IPã¢ãã¬ã¹ïŒ234.123.34.18:57ãªã©ïŒã§ããå¿ èŠããããŸãã
ãããã¯ãŒã¯ã§ã®äœæ¥ãå®äºããã«ã¯ã次ã®æ©èœããããŸãã
// virtual void StopNet();
ãã®é¢æ°ã¯ããããã¯ãŒã¯ã®ãã¹ãŠã®å åãã€ãŸãããŒã«ã«ãããã¯ãŒã¯ãµãŒããŒãååšããå Žåã¯åæ¢ããGNetClientã¯ã©ã€ã¢ã³ããªããžã§ã¯ããç Žæ£ããŸãã
ãããã¯ãŒã¯ã®æäœãéå§ããã«ã¯ãé¢æ°bool GGame :: StartNetïŒconst char * ipïŒãåŒã³åºãå¿ èŠããããŸãã IPã¢ãã¬ã¹ãæå®ãããŠããå Žåãé¢æ°ã¯ãããã¯ãŒã¯ãåæåããæå®ãããã¢ãã¬ã¹ã«ãããµãŒããŒãšã®æ¥ç¶ã確ç«ããããšããŸãããµãŒããŒãå¿çããŠæ¥ç¶ãèš±å¯ããå Žåãé¢æ°ã¯trueãè¿ããããã§ãªãå Žåã¯falseãè¿ããŸãããµãŒããŒãžã®ãã¹ãŠã®ãããªãåŒã³åºãã¯ç¢ºç«ãããTCPæ¥ç¶ãéããŠå®è¡ãããã§ããããip = NULLã®å Žåãé¢æ°ã¯ãµãŒããŒãžã®æ¥ç¶ãå®è¡ããŸããã代ããã«ãUDPãœã±ãããäœæããŸãããããŒããã£ã¹ãèŠæ±ãä»ããŠããŒã«ã«ãããã¯ãŒã¯äžã®ãµãŒããŒãæ€çŽ¢ããããã«äœ¿çšããŸããStartNetïŒïŒ
é¢æ°ã®å®è¡æã«åé¡ããªãã£ãå Žåããããã¯ãŒã¯ã¯NET_STADY_ENUM_SESSIONã¹ããŒãžã®å®è¡ãéå§ããŸããäœæãããã²ãŒã ã»ãã·ã§ã³ãæ€çŽ¢ããŸããããã¯æ¬¡ã®ããã«è¡ãããŸã...é¢æ°GNetStadyEnumSession :: OnPeriodïŒïŒã§ã0.5ç§ããšã«èªåçã«åŒã³åºãããã¿ã€ãMESSAGE_TYPE_ENUM_SESSIONã®ã¡ãã»ãŒãžãéä¿¡ãããŸããã¡ãã»ãŒãžã¯ãäºåã«ç¢ºç«ãããæ¥ç¶ãä»ããŠã€ã³ã¿ãŒããããµãŒããŒã«ã®ã¿éä¿¡ãããããããŒã«ã«ãããã¯ãŒã¯ãžã®ãããŒããã£ã¹ãèŠæ±ã«ãã£ãŠéä¿¡ãããŸãããããã®å Žåããã€ã³ã¿ãŒããããµãŒããŒãŸãã¯ããŒã«ã«ãããã¯ãŒã¯äžã®ãµãŒããŒããã®ã¡ãã»ãŒãžãåä¿¡ãããšãåãæ¹æ³ã§å¿çããŸããã€ãŸãããµãŒããŒã§å©çšå¯èœãªãã¹ãŠã®ã»ãã·ã§ã³ããªã¹ãããã¡ãã»ãŒãžMESSAGE_TYPE_ENUM_SESSION_REPLYã®åœ¢åŒã§è³ªåè ã«åçãéä¿¡ããŸããåã»ãã·ã§ã³ã¯ã®ç°¡åãªèª¬æãããã»ãã·ã§ã³ã®äœææéãIDãã¹ããååºã«ããã¬ã€ã€ãŒã®æ°ãšãã®ç¹æ§ãã»ãã·ã§ã³ã«ãããçç¶ã®ãã¹ã¯ãŒãã ãã§ãªãããµãŒããŒã®IPã¢ãã¬ã¹ããã®å Žåãã¯ã©ã€ã¢ã³ãã¯ãªã¯ãšã¹ããããŒããã£ã¹ããéä¿¡ããæ¥ç¶ã確ç«ããããã«ã¯ã©ã€ã¢ã³ããå¿çãéä¿¡ãããµãŒããŒã®ã¢ãã¬ã¹ãç¥ãå¿ èŠããããããã¯ã©ã€ã¢ã³ããæ¥ç¶ã§ããããã«ããããšã¯ããŒã«ã«ãããã¯ãŒã¯ã«ãšã£ãŠéåžžã«éèŠã§ãã
èŠã€ãã£ãã»ãã·ã§ã³ã«é¢ããæ å ±ãåãåã£ãåŸãã¯ã©ã€ã¢ã³ãã¯ä»®æ³é¢æ°virtual void GGame :: OnEnumSessionïŒGSessionList * sessionsãint count_general_sessionïŒãåŒã³åºããŸãããã®é¢æ°ã¯ãå®éã«ã¯ãŠãŒã¶ãŒã«ã»ãã·ã§ã³ã®ãªã¹ãã衚瀺ããããšãã¿ã¹ã¯ãªã®ã§ãGGameã¯ã©ã¹ã§ã¯å®çŸ©ãããŠããŸãããGGameã¯æ®éçãªåºæ¬ã¯ã©ã¹ã§ããããã䜿çšã§ããç¹å®ã®ã²ãŒã ã«ã€ããŠäœãç¥ããªããããGGameã¯ãã®ãããªã¿ã¹ã¯ãåŒãåããŸããããããã£ãŠãç§ã®å Žåããã®é¢æ°ã¯ã¯ã©ã¹ã§åå®çŸ©ãããŠããŸãGGameOnimodLandãšåä¿¡ããã»ãã·ã§ã³ãäœææéã§äžŠã¹æ¿ããGListBoxã³ã³ããŒãã³ãã«è¡ãè¿œå ããŠãŠãŒã¶ãŒã«è¡šç€ºããã®ã¯åœŒå¥³ã§ãã
ãŠãŒã¶ãŒãã»ãã·ã§ã³ãéžæããŠãåå ããã¯ãªãã¯ãããšã次ã®ããã«ãªããŸããã¯ã©ã€ã¢ã³ãã¯åçŽã«NET_STADY_JOIN_SESSIONã¹ããŒãžãéå§ããŸããããã¯GNetStadyJoinSession :: OnStartïŒïŒé¢æ°ã§ã»ãã·ã§ã³ãžã®æ¥ç¶ãè©Šè¡ããŸãããã®è©Šã¿ã¯ããŸããŸãªçç±ã§å€±æããå ŽåããããŸããããšãã°ãã²ãŒã ãéå§ããããå¥ã®ãã¬ãŒã€ãŒãæ¥ç¶ããããã»ãã·ã§ã³ã«ã¹ããŒã¹ããªããªã£ããªã©ã§ãããããã®å Žåããæ¥ç¶ã®èš±å¯ã¯ãã¹ãã«ãã£ãŠçºè¡ãããŸãïŒãµãŒããŒã§ã¯ãããŸããïŒã
ããã¯å®éã«ã¯ã©ã®ããã«èµ·ãããŸããïŒ
ããŒã«ã«ãããã¯ãŒã¯ã®å Žåãæåã«ãµãŒããŒã«æ¥ç¶ããå¿ èŠããããŸãããããè¡ãã«ã¯ãããŒã«é¢æ°GNetClient :: ConnectToServerïŒconst char * ipïŒã䜿çšããŸããtrueã®å Žåã¯trueãè¿ããŸãããµãŒããŒã®IPã¢ãã¬ã¹ã¯ãã»ãã·ã§ã³æ å ±ããååŸãããŸããæ¥ç¶ã確ç«ããåã«ãUDPãœã±ããã¯æåã«åŒ·å¶çµäºãããŸããããã¯ãäžèŠã«ãªãã代ããã«TCPãœã±ãããäœæãããããã§ããããã«ãããæ¥ç¶ã確ç«ãããŸããã¯ã©ã€ã¢ã³ãã¯CONNECTã䜿çšããŠãµãŒããŒã«ã¢ã¯ã»ã¹ãããµãŒããŒã¯èŠæ±ãåãå ¥ããŠACCEPTãä»ããŠå¿çããå¿ èŠããããŸããæ¥ç¶ã確ç«ãããããããã³ãã·ã§ã€ã¯ãã亀æããå¿ èŠããããŸãã
ããã¯äœã®ããã§ããïŒå®éã«ã¯ãå¥ã®ããã°ã©ã ã®ã¢ã¯ã»ã¹ãã誰ã§ããµãŒããŒã«æ¥ç¶ã§ããŸãã ããã³ãã·ã§ã€ã¯ãã¯ããµãŒããŒããèŠãŠã¯ã©ã€ã¢ã³ããèå¥ããŸããããã³ãã·ã§ã€ã¯ããæåãããšããµãŒããŒãã¯ã©ã€ã¢ã³ãã«ã¡ãã»ãŒãžã®éä¿¡ãèš±å¯ããŸãã ããã³ãã·ã§ã€ã¯ãèªäœã¯ãã¡ãã»ãŒãžMESSAGE_TYPE_HELLOãä»ããŠå®è¡ãããŸããã¡ãã»ãŒãžMESSAGE_TYPE_HELLOã«ã¯ãã¯ã©ã€ã¢ã³ãããŒãžã§ã³ãã©ã€ã»ã³ã¹ããŒãªã©ã®ãµãŒãã¹æ å ±ãè¿œå ãããŸãããµãŒããŒã¯ãã®æ å ±ã確èªããMESSAGE_TYPE_HELLO_REPLYã¡ãã»ãŒãžãã¯ã©ã€ã¢ã³ãã«è¿ããŸããã¡ãã»ãŒãžã«ã¯äžæã®ã¯ã©ã€ã¢ã³ãIDãå«ãŸããŠããŸããã¯ã©ã€ã¢ã³ãã¯ããããä»ããŠãµãŒããŒããã³ä»ã®ãã¬ãŒã€ãŒãšã®ãã¹ãŠã®ãããªãéä¿¡ãå®è¡ããŸãIDããµãŒããŒãID = 0ãè¿ããå Žåãããã¯ã¯ã©ã€ã¢ã³ãããµãŒããŒã«åãå ¥ããããªãã£ãããšãæå³ããŸãããã®å ŽåããµãŒããŒã¯ã¯ã©ã€ã¢ã³ãã«é害ã®çç±ã説æããããã¹ããè¿œå ã§éä¿¡ããŸãããã®ããã¹ãã¯ãããšãã°GMessageBoxã®åœ¢åŒã§ã¯ã©ã€ã¢ã³ãåŽã«è¡šç€ºã§ããŸããã€ã³ã¿ãŒããããµãŒããŒãžã®æ¥ç¶ãåæ§ã®æ¹æ³ã§å®è¡ãããããšã«æ³šæããŠãã ããã
ã¯ã©ã€ã¢ã³ãããµãŒããŒã«æ¥ç¶ãããŠããå Žåãã»ãã·ã§ã³ãžã®æ¥ç¶èŠæ±ãããã«ãã¹ãã«éä¿¡ããå¿ èŠããããŸãããã ãããã¹ãã¯ã»ãã·ã§ã³ããã¹ã¯ãŒãã§ä¿è·ã§ããããããèªåã®ãã ããæ¥ç¶ã§ããŸããã»ãã·ã§ã³ã«é¢ããæ å ±ããããã®ãããªãã¹ã¯ãŒãã®äºå®ãå€æãããŸãããã®å ŽåããŠãŒã¶ãŒã¯æåã«ãã¹ã¯ãŒãã®å ¥åãæ±ããããŸãã次ã«ãé¢æ°bool GNetClient :: ConnectSessionïŒNET_JOIN_DATA * joinïŒãããã¯ãã¿ã€ãMESSAGE_TYPE_CONNECTINGã®ã¡ãã»ãŒãžããã¹ãã«éä¿¡ããŸããã¡ãã»ãŒãžã¯ãã¹ãIDã«éä¿¡ãããã»ãã·ã§ã³IDããåã³ååŸãããŸãããŸãããµãŒããŒãã¡ãã»ãŒãžãåä¿¡ããŸãããµãŒããŒã¯åä¿¡è ã®ãªã¹ããåæããã¡ãã»ãŒãžããã¹ãã«æž¡ããŸãã
ã¡ãã»ãŒãžãåä¿¡ããåŸããã¹ãã¯ã»ãã·ã§ã³ãžã®ãã¬ãŒã€ãŒã®åãå ¥ããå¯èœãã©ããã確èªãããã¹ã¯ãŒãïŒååšããå ŽåïŒã確èªããŠæ±ºå®ããŸããçãããã¯ããã®å Žåããã¹ãã¯æåã«ä»®æ³é¢æ°virtual int GGame :: GetIndexOfConnectedPlayerïŒïŒã䜿çšããŠæ°ãããã¬ãŒã€ãŒã®ã²ãŒã ã¹ããããéžæããã»ãã·ã§ã³ã«ãã¬ãŒã€ãŒãå«ããŸããããã«ããã¹ãã«ã¯æ©èœããããŸãvirtual void GGame :: OnNewPlayerïŒint indexïŒãããã«ãããæ°ããã¡ã³ããŒã®åºçŸã«ããäœããã®ã¢ã¯ã·ã§ã³ãå®è¡ã§ããŸãã次ã«ããã¹ãã¯ã¡ãã»ãŒãžMESSAGE_TYPE_CONNECT_REPLYã®åœ¢åŒã§ã¯ã©ã€ã¢ã³ãã«å¿çãéä¿¡ããŸããè¯å®çãªåçã®å Žåãã»ãã·ã§ã³ã§ã¯ã©ã€ã¢ã³ãã«å²ãåœãŠãããã¹ãããã€ã³ããã¯ã¹ãã»ãã·ã§ã³å ã®ä»ã®ãã¬ãŒã€ãŒã®ã¹ããŒã¿ã¹ã«é¢ãããã¹ãŠã®æ å ±ãããã³ããã€ãã®è¿œå æ å ±ãéä¿¡ããŸããã¡ãã»ãŒãžMESSAGE_TYPE_CONNECT_REPLYãåä¿¡ãããšãã¯ã©ã€ã¢ã³ãã¯ã¹ããããèšæ¶ãããµãŒããŒããåä¿¡ããããŒã¿ã§ã»ãã·ã§ã³å€æ°ãåæåããŸãã
ããã§ãæ°ãããã¬ãŒã€ãŒã¯ã»ãã·ã§ã³ã®ç¹æ§ã調æŽãããã£ããã¡ãã»ãŒãžã亀æã§ããŸãããã¹ããã²ãŒã ãéå§ã§ããããã«ãã»ãã·ã§ã³ã®ååå è ã¯ãæºåãã§ããŠããŸãããã¿ã³ãã¯ãªãã¯ããå¿ èŠããããŸããããããªããšããã¹ãã®[ã¹ã¿ãŒã ]ãã¿ã³ã¯ç¡å¹ã«ãªããŸãã
ã²ãŒã ã»ãã·ã§ã³æ å ±
ã»ãã·ã§ã³æ å ±ã¯ãç¹å®ã®ã²ãŒã ã«äŸåããªãããã«æŽçããå¿ èŠããããŸããããšãã°ãçªç¶ãé»éã®åäŸæ代ãèŠããŠããšèšããã²ãŒã ãããçŽãããå Žåãã·ã§ã«ã®æ§é ãå€æŽããå¿ èŠã¯ãããŸããããã®ãããã»ãã·ã§ã³ã®èª¬æã®ãã£ãŒã«ããä»»æã§ãããç¹å®ã®ã²ãŒã ã®ã¿ãåæã«é¢äžãããµãŒããŒãŸãã¯ã¯ã©ã€ã¢ã³ããé¢äžããŠããªãããšã確èªããå¿ èŠããããŸãããã ããäžéšã®ãã£ãŒã«ãã¯æ確ã«å®çŸ©ããå¿ èŠããããŸããããã¯ããµãŒããŒãã»ãã·ã§ã³ã«ã€ããŠååãç¥ãå¿ èŠãããããã§ãããŸããæãããªãã®æ°ãç¶æ ãªã©ã®ã»ãã·ã§ã³ã«å«ãŸéžæãã«é¢ããããã€ãã®æ å ±ã解éããå¿ èŠã誰ã§ãããïŒäººãã³ã³ãã¥ãŒã¿ã空ãã¹ããããã¯ããŒãºãã¹ããããïŒ
ãã®æ å ±ã¯ãã¹ãŠNET_SESSION_INFOæ§é äœã«å ¥ããŸãããã®æ§é ã®äžéšãšããŠããã¬ã€ã€ãŒNET_PLAYER ** m_playerã®é åããããŸããããã³ãã®çªå·int k_player;
ããããæãéèŠãªã®ã¯ãä»»æã®æ å ±ãæ ŒçŽã§ãããã£ãŒã«ãããããŸãïŒ
int length_info; // . char* info; // .
åããã£ãŒã«ããNET_PLAYERæ§é ã«ãããä»»æã®ãã¬ãŒã€ãŒã®ä»»æã®æ å ±ãä¿åããããšãã§ããŸãã
次ã«ã次ã®é¢æ°ãšæŒç®åãNET_SESSION_INFOæ§é ã«è¿œå ããŸãã
virtual void Serialize(CMagicStream& stream); // / NET_SESSION_INFO& operator=(const NET_SESSION_INFO& si); // bool operator==(const NET_SESSION_INFO& si); // bool operator!=(const NET_SESSION_INFO& si) //
ãªãããããã¹ãŠå¿ èŠãªã®ã§ããïŒ
éèŠãªããšã1ã€èª¬æããããšæããŸããç§ã®æèŠã§ã¯ããªãã¬ãŒã¿ãŒã®äœ¿çšã¯ããªãå±éºãªããžãã¹ã§ããåé¡ã¯ãéçºäžã«é »ç¹ã«æ°ããå€æ°ãæ§é ã«è¿œå ãããããšã§ãããããŠå€ãã®å Žåãã³ã³ã¹ãã©ã¯ã¿ã§ããããåæåããããšãå¿ããããšããã§ããŸãããããŠããããã®å€æ°ã¯ãæ¯èŒæŒç®åãšå¹³çã«è¿œå ããå¿ èŠããããšããäºå®ãå¿ããŠ-ããã¯äžå以äžç§ã«èµ·ãã£ãããããŠåœŒå¥³ãšç§ã¯æã éåžžã«æã£ãŠããã®åŸã«ãtããªãã«é upuyuãç©å¿ãã®ããã«ãã ãã...çµæãšããŠãç§ã¯æ¬¡ã®æ±ºå®ã«æ¥ãŸãããéåžžãéèŠãªæ§é ã«ã¯ã·ãªã¢ã«åãå«ãŸããŸããããåçŽã«ã¯ãã¹ããªãŒã ããããŒã¿ãèªã¿åããã¹ããªãŒã ã«æžã蟌ãããšãã§ããŸããã»ãšãã©ã®å Žåãã¹ããªãŒã ã¯ãã€ããæžã蟌ãŸããéåžžã®ãã¡ã€ã«ã§ãããã¡ã¢ãªé åãã¹ããªãŒã ã«ã§ããå Žåã¯ã¯ããã«åªããŠããŸããç§ã®å Žåãç§ã¯ã¯ã©ã¹æžãCMagicStreamãããŠçæãããã¯ã©ã¹ããCMagicStreamFileãšCMagicStreamMemoryãããããã£ãŠãé¢æ°virtual void SerializeïŒCMagicStreamïŒstreamïŒ;å®éã«ã©ã®ã¯ã©ã¹ãªããžã§ã¯ãã¹ããªãŒã ã§ãããã«å¿ããŠããã¡ã€ã«ãšRAMãæäœããæ¹æ³ãç¥ã£ãŠããŸãã
ãšããã§ããã1çš®é¡ã®ã¯ã©ã¹ããããŸãCMagicStreamVirtualFileã¯ãç§ã®ãã·ã§ã«ãã®äžéšã§ãããä»®æ³ãã£ã¹ã¯ã§åäœããããã«èšèšãããŠããŸããä»®æ³ãã£ã¹ã¯ã¯ãç¬èªã®ãã¡ã€ã«ã·ã¹ãã ãå«ãããã€ãã®ãã¡ã€ã«ã§ããä»®æ³ãã£ã¹ã¯ã䜿çšããŠããã®å éšã«ã²ãŒã ãªãœãŒã¹ãé 眮ããŸãããä»®æ³ãã£ã¹ã¯ã¯ãCMagicString GPlatform :: OpenVirtualDriveïŒconst char * pathïŒãä»ããŠãä»®æ³ãã£ã¹ã¯ãã¡ã€ã«ã®ãã¹ãæå®ããããšã§éãããšãã§ããŸãããã®çµæãã¿ã€ã0ã®ãã¹ãè¿ãããŸãããã®ãã¹ã¯ã·ã§ã«ã§äœ¿çšãããä»®æ³ãã£ã¹ã¯å ã®ãã¡ã€ã«ãæäœã§ããŸããããã§éèŠãªããšã¯ããã¡ã€ã«ã·ã¹ãã ã§æ©èœãããã·ã§ã«ãé¢æ°ããã®æ¹æ³ãç解ããå¿ èŠã«å¿ããŠèŠæ±ããã¡ã€ã«ã«ãªãã€ã¬ã¯ãããããšã§ããããšãã°ãCMagicStream * GPlatform :: OpenStreamïŒconst char * fileãint modeïŒé¢æ°ãã®ãããªæ®éçãªååã§åäœãããã¡ã€ã«ã察å¿ãããã¹ãæããŠããå Žåãä»®æ³ãã£ã¹ã¯ã«æ£ããã¢ã¯ã»ã¹ããŸããåãããšããçŸåšã®ãã©ã«ããŒãã®ç¶æ³ã«ãåœãŠã¯ãŸããŸãã誰ãä»®æ³ãã£ã¹ã¯äžã«çŸåšã®ãã©ã«ããŒãäœæããå¿ èŠã¯ãããŸããã
ã ãã...ãã€ã³ãã«æ»ããŸããç§ã¯ãç©å¿ãã®ããã§ãªãã¬ãŒã¿ãŒãç§ã«ãšã£ãŠã©ã®ããã«å±éºã«ãªããã«ã€ããŠè©±ããŸããããªã¹ã¯ãæå°éã«æããããããããè¡ããŸãããã€ããªåœ¢åŒã§æ§é ã®ã·ãªã¢ã«åé¢æ°ãäœæãããã®é¢æ°ã«ãã¹ãŠã®æŒç®åãæž¡ããŸããã€ãŸãããšãã°ãå²ãåœãŠæŒç®åãèšè¿°ããå¿ èŠãããå Žåãæ§é ãã£ãŒã«ãã1ã€ãã€ã³ããŒãã代ããã«ãæžã蟌ã¿çšã«RAMã«CMagicStreamMemoryåã®ã¹ããªãŒã ãäœæããã³ããŒããæ§é ãªããžã§ã¯ãã®SerializeïŒïŒãå®è¡ããŸãã次ã«ãèªã¿åãçšãšåãã¹ããªãŒã ãäœæããŸããçµæã®æ§é ãªããžã§ã¯ãã¯ãåãã¡ã¢ãªäœçœ®ããSerializeïŒïŒãå®è¡ããŸãããã®ãããªä¿å+è² è·ãå€æããŸãRAMãä»ããŠãæ¯èŒæŒç®åã䜿çšããŠãåãããšãã§ããŸããæ¯èŒãããªããžã§ã¯ãã®ããŒã¿ã2ã€ã®ç°ãªãã¹ããªãŒã ã«æžã蟌ã¿ããããããã€ãåäœã§æ¯èŒãå§ããŸãããã¡ããããã®æ¹æ³ã¯ããªãã¬ãŒã¿ãŒãããããã®ãã£ãŒã«ããèšç®ããå Žåãããé ããªããŸãããã ããããã«ããã¯ã®å Žåã¯ãåžžã«åŸæ¥ã®ããŒãžã§ã³ã䜿çšã§ããŸããã·ãªã¢ã«åã®å©ç¹ã¯ãéåžžã®ä¿å / èªã¿èŸŒã¿ããŒã¿ã«ã䜿çšãããããšã§ãããããŠãå°ãªããšãç§ã®å ŽåãããŒã¿ã®ç Žæãä¿åãããŠããªããã£ãŒã«ãã®æ¹ãã¯ããã«å°è±¡çã§ãããããå¿ãã£ãœããããã«çŸããŸãã
ããã«ãã·ãªã¢ã«åã¯ãã¯ãªããããŒããä»ããŠããŒã¿ãã³ããŒããã®ã«æé©ã§ãããããŠããããã¯ãŒã¯ã®å Žå...ãããã¯ãŒã¯ã®çžäºäœçšã¯ããã¿ã€ãããããµã€ãºãããä»»æã®ããŒã¿ãã®ãã£ãŒã«ãã§ããã¡ãã»ãŒãžã®éä¿¡ã«éå®ãããããšã«æ³šæããŠãã ããããŸãããã€ããªã·ãªã¢ã«åã䜿çšãããšãããããã¿ã€ãã®ããŒã¿ããããã¯ãŒã¯çµç±ã§éä¿¡å¯èœãªãã€ãã¹ããªãŒã ã«å€æããåä¿¡åŽã§å ã®ããŒã¿ã«æ»ãããšãã§ããŸãã
ããã§ããããããããã¯ãŒã¯ã»ãã·ã§ã³ã®èšå®ãå®äºããŸããããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã¯ãã²ãŒã ã«ã€ããŠäœãç¥ããªãããã«ãããã¬ãŒã€ãŒã®èšå®ã®ã»ãšãã©ã«ã€ããŠã¯ç¥ããªãããã«ããŸããããšãã°ããæºåãã§ããŠããŸãããšãããã©ã°ãããã¯ç¢ºãã«å¿ èŠã¯ãããŸãã- ã圌ã¯ããã²ãŒã å¿ èŠãããããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã¯ããããã¯ãŒã¯ãä»ããŠéä¿¡ã¡ãã»ãŒãžã«ãããŸã
ãç§ã¯ãã®ããã«ããGGameã¯ãã¯ã©ã¹ã§å®çŸ©ãããªããã°ãªããªã4ã€ã®ç©ºvirutalnyeæ©èœãçºè¡šããŸããGGameOnimodLandãã
virtual bool GGame::GetSessionInfoStruct(NET_SESSION_INFO* si); // NET_SESSION_INFO* si . virtual void GGame::SetSessionInfoStruct(NET_SESSION_INFO* si); // NET_SESSION_INFO* si . virtual bool GGame::GetSessionPlayerStruct(int index, NET_PLAYER* np); // NET_PLAYER* np . index . virtual void GGame::SetSessionPlayerStruct(int index, NET_PLAYER* np); // NET_PLAYER* np .
å®éã«ã¯ããããã®æ©èœã¯ããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãããã²ãŒã ãžããŸãã¯ãã®éã«ããŒã¿ãæ®éçã«è»¢éããŸããåæã«ãGetSessionInfoStruct / SetSessionInfoStructé¢æ°ã¯ããã¬ãŒã€ãŒã«é¢ããããŒã¿ãå«ãã»ãã·ã§ã³å šäœã®ããŒã¿ãåŠçããŸãããŸããGetSessionPlayerStruct / SetSessionPlayerStructé¢æ°ã¯ãç¹å®ã®ãã¬ãŒã€ãŒã®ããŒã¿ãåŠçããŸããNET_SESSION_INFOã«ã¯NET_PLAYERãªããžã§ã¯ãã®é åãå«ãŸããŠãããããåœç¶ããã¬ãŒã€ãŒã®é¢æ°ã¯ã»ãã·ã§ã³é¢æ°ã«ãã£ãŠäœ¿çšãããŸãã
ãã®ã¢ãããŒãã®åŸããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã®ã²ãŒã èªäœã¯ããã©ãã¯ããã¯ã¹ãã«å€ããããããããäœãããçãŸãããäœãããåä¿¡ãããŸãããããŠä»ããã¬ãŒã€ãŒãèšå®ããããã»ã¹ãç°¡çŽ åããããã«å¿ èŠãªéèŠãªãã€ã³ãããã¬ãŒã€ãŒã«ã¯å€ãã®èšå®ããããå€æŽã§ããããšãæ³åããŠãã ãããããšãã°ãããŒã ã®è²ãå€æŽããããã¬ãŒã¹ãéžæãããã§ããŸããããããå°æ¥äœãèãããããã¯æ±ºããŠããããªãã®ã§ãåŸã§ããã°ã©ã æ§é ãä¿®æ£ããå¿ èŠã¯ãããŸãããNET_STADY_JOIN_SESSIONã®
段éã§ãã¿ã€ããŒã§void GNetStadyJoinSession :: OnUpdateïŒïŒé¢æ°ãåŒã³åºãããæ®éæ§ã®åé¡ã解決ãã1çµã®è¡ãå®è¡ããŸããGNetStadyJoinSession
ã¹ããŒãžã«ã¯å€æ°ããããŸãNET_SESSION_INFO * copy_session;æåŸã®ã¡ãžã£ãŒã®ã»ãã·ã§ã³ç¶æ ã®ã³ããŒãä¿åããŸããããã«ãããããããã³ãŒãå šäœã®ããªãã®éšåãæäŸããŸãã
void GNetStadyJoinSession::OnUpdate() { GNetStady::OnUpdate(); // NET_PLAYER* current_player=current_session->m_player[index_player]; NET_PLAYER* copy_player=copy_session->m_player[index_player]; owner->game->GetSessionPlayerStruct(index_player, current_player); if (*copy_player!=*current_player) { // *copy_player=*current_player; if (current_player->type==PLAYER_MAN) { GMemWriter* wr1=owner->wr1; wr1->Start(); (*wr1)<<index_player; current_player->Serialize(*wr1); MEM_DATA buf; wr1->Finish(buf); // , int k_receiver=owner->RefreshReceiverList(); NET_BUFFER_INDEX* result=owner->PrepareMessageForPlayers(MESSAGE_TYPE_PLAYER_INFO, buf.length, buf.data, k_receiver, owner->m_receiver); owner->GetMainSocket()->SendMessage(result); } } }
å®éã«ã¯ã次ã®ããšãçºçããŸããå€æ°index_playerã¯ãã»ãã·ã§ã³å ã®ãã¬ãŒã€ãŒã«å±ããã¹ãããçªå·ã§ãã
String owner-> game-> GetSessionPlayerStructïŒindex_playerãcurrent_playerïŒ;ã²ãŒã ããåããã¬ãŒã€ãŒã®çŸåšã®èšå®ã
ååŸ ããã¯ã©ã€ã¢ã³ããèšæ¶ããŠããèšå®ãšåçŽã«æ¯èŒããŸãïŒifïŒ* copy_playerïŒ= * current_playerïŒ
ãããŠäžäžèŽãããå Žåãã³ããŒãæåã«äžèŽããŸã* copy_player = * current_player;ãããŠãã¿ã€ãMESSAGE_TYPE_PLAYER_INFOã®ã¡ãã»ãŒãžãã»ãã·ã§ã³ã®ãã¹ãŠã®åå è ã«éä¿¡ããŸãããã®åå è ã§ã¯ããã¬ãŒã€ãŒã®æ°ããèšå®ãéä¿¡ãããŸãã
ãã®ã¢ãããŒãã®å€§ããªå©ç¹ã¯äœã§ããïŒäºå®ã¯ãã²ãŒã èªäœãä»ã®ãã¬ã€ã€ãŒã«éä¿¡ãããèšå®ã«åŸãã¹ãã§ã¯ãªããšããããšã§ããGNetStadyJoinSession :: OnUpdateïŒïŒã¯ããã«ãã®å€æŽãèªèããã»ãã·ã§ã³ã®ãã¹ãŠã®åå è ã«æ°ããããŒã¿ãèªåçã«éä¿¡ãããããæ§æã§å°ãªããšã1ãã€ããå€æŽãã䟡å€ããããŸããåæã«ãGNetStadyJoinSession :: OnUpdateïŒïŒã¯ãæ§æå¯èœãªå®éã®ããŒã¿ã«ã€ããŠäœãç¥ããŸãããããã¯ãæ¯èŒæŒç®åãã·ãªã¢ã©ã€ã¶ãŒãä»ããŠæ©èœããé·ããçããå Žåã«ãã€ãã¹ããªãŒã ãšã¹ããªãŒã ã®é·ããæ¯èŒãããããã§ãã
èšäºã®äŸã§ã¯ããã¬ãŒã€ãŒã®æ§æã®æ§é ã¯æ¬¡ã®ããã«ãªããŸãã
struct PlayerCfg { int type; CMagicString name; unsigned int player_id; unsigned int color; bool ready; void Serialize(CMagicStream& stream) { if (stream.IsStoring()) { stream<<type; stream<<name; stream<<player_id; stream<<color; stream<<ready; } else { stream>>type; stream>>name; stream>>player_id; stream>>color; stream>>ready; } } };
ããããã²ãŒã ã§ã¯ãã£ãŒã«ããå®å šã«ç°ãªããããã«å€ãã®ãã£ãŒã«ãããããŸãããã ãããã®ã¢ãããŒãã¯æ±çšæ§ã®ç¹ã§åªããŠããŸãã
ã»ãã·ã§ã³äœæ
ãã ããã»ãã·ã§ã³ã«åå ããåã«ã誰ãããã®ã»ãã·ã§ã³ãäœæããå¿ èŠããããŸããããã«ã¯NET_STADY_CREATE_SESSIONã¹ããŒãžããããŸãããã®ã¹ããŒãžã®ã¯ã©ã¹ã¯ãNET_STADY_JOIN_SESSIONã¹ããŒãžããç¶æ¿ãããŸãã
class GNetStadyCreateSession: public GNetStadyJoinSession
ããã¯ãã»ãã·ã§ã³ã®äœæãŸãã¯ã»ãã·ã§ã³ãžã®åå ã®ãããããå®è¡ããOnStartïŒïŒé¢æ°ãé€ãããããã®ã¹ããŒãžãéåžžã«é¡äŒŒããŠãããå€§å¹ ã«ç°ãªãããã§ããNET_STADY_CREATE_SESSIONã¹ããŒãžã§ã¯ãæ§æå€æŽã®ãã§ãã¯ãæ©èœããŸãããã»ãã·ã§ã³å ã®åãã¬ãŒã€ãŒã®ãã§ãã¯ããããŸããããã¯ããã¹ããã»ãã·ã§ã³å šäœãå¶åŸ¡ããããšãã°ä»ã®ãã¬ãŒã€ãŒãåé€ã§ããããã§ãã
ãšããã§ããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã¯ããã¬ãŒã€ãŒã®æ§æããŒã¿ã®åæã«ãŸã å°ãé¢äžããŠããŸããã¡ãã»ãŒãžMESSAGE_TYPE_PLAYER_INFOã¯ãã¯ã©ã€ã¢ã³ãã次ã®ããã«åæããæ§æãæ åœããŸããã¡ãã»ãŒãžãåãåã£ãæç¹ã§ã圌ã¯æ°ããæ§æãæ¥ããã¬ã€ã€ãŒãçããŠãã人ã§ãã£ããã©ãããèŠããŠããŸãïŒã¿ã€ã= PLAYER_MANïŒãã¡ãã»ãŒãžãåä¿¡ãããšãçŸåšã®æ§æãæ°ããæ§æã«çœ®ãæããããŸãããã ããã¯ã©ã€ã¢ã³ãã¯typeãã£ãŒã«ãã§PLAYER_MANã§ãããã©ããã®ã¿ããã§ãã¯ããŸãããŸãããã£ãŒã«ããçªç¶PLAYER_OPENEDã«å€æŽãããå Žåãããã¯ãããšãã°ããã¹ããã»ãã·ã§ã³ãããã¬ãŒã€ãŒãåé€ããã¹ããããéããŠããããšãæå³ããå ŽåããããŸããã¯ã©ã€ã¢ã³ãã¯ãã®ç¶æ³ã®åŠçã«é¢äžããŠããããã®çµæãšããŠbool GNetClient :: LostPlayerïŒunsigned int player_idïŒãåŒã³åºãããŸããã€ãŸãããã¬ãŒã€ãŒãšã®éä¿¡ã倱ãããŸããããã«ããããã¯ãã¹ãŠããªãã·ã§ã³ã®ããããã®åœ¢åŒã§ã²ãŒã ã«æäŸãããŸãã
// ( ) virtual void GGame::OnCancelSession(); // virtual void GGame::OnDeletingFromSession();
boolé¢æ°GNetStadyCreateSession :: OnStartïŒä»¥åã®NET_STADYãvoid * initïŒã¯ããã¬ãŒã€ãŒã[ã²ãŒã ã®äœæ]ãã¿ã³ãã¯ãªãã¯ãããšåŒã³åºãããŸãããããããŒã«ã«ãããã¯ãŒã¯äžã®ã²ãŒã ã§ããå ŽåããŸãç¬èªã®ãµãŒããŒãèµ·åããŠåå ããå¿ èŠããããŸãã
// bool GNetClient::CreateSession() { bool is=false; if (!internet) { // if (StartLocalServer()) { is=ConnectToServer("127.0.0.1"); } else is=false; } else { is=true; } return is; }
ConnectToServer é¢æ°ïŒã127.0.0.1ãïŒã§ã¯ãUDPãœã±ãããç Žæ£ãããT CPãœã±ãããäœæãããŸãã次ã«ããµãŒããŒãšã®æ¥ç¶ã確ç«ãããŸãããµãŒããŒã¯ãStartLocalServerïŒïŒé¢æ°ã«ãã£ãŠåãã³ã³ãã¥ãŒã¿ãŒã§èµ·åãããŸãããµãŒããŒã®IPã¢ãã¬ã¹ã¯ã127.0.0.1ãã§ããåãã³ã³ãã¥ãŒã¿ãŒããæå³ããŸãã
次ã«ããã¹ãã¯ä»®æ³void GGame :: OnCreateSessionïŒint index_playerïŒé¢æ°ãåŒã³åºããŸãããã®å Žåããã¹ãã®ã¹ãããã®ã¿ãåžžã«0ã«èšå®ãããŸãã
次ã«ãvoidé¢æ°GNetStadyCreateSession :: OnPeriodïŒïŒã䜿çšãããã¹ãã»ãã·ã§ã³ç¶æ ã«ã€ããŠãµãŒããŒã«å®æçã«éç¥ãå§ããŸãããã®é¢æ°ã¯ã0.5ç§ããšã«èªåçã«åŒã³åºãããŸããã¿ã€ãMESSAGE_TYPE_SESSION_INFOã®ã¡ãã»ãŒãžããµãŒããŒã«éä¿¡ããŸãããã®ã¡ãã»ãŒãžã¯åžžã«éä¿¡ãããããã§ã¯ãªããã»ãã·ã§ã³èšå®ãå€æŽãããå Žåã«ã®ã¿éä¿¡ãããŸããããã§ã¯ããã¬ãŒã€ãŒæ§æã®å€æŽãšåãååã䜿çšãããŸãã
ã¡ãã»ãŒãžMESSAGE_TYPE_SESSION_INFOãåä¿¡ãããšããµãŒããŒã¯ãŸãããã®ãããªã»ãã·ã§ã³ããã§ã«ã»ãã·ã§ã³ã®ãªã¹ãã«ãããã©ããã確èªãããªãå Žåã¯æ°ããã»ãã·ã§ã³ãè¿œå ããŸããã¡ãã»ãŒãžã®éä¿¡è ã¯ãæåã®åå è ããã³ãã¹ããšããŠæ°ããã»ãã·ã§ã³ã«è¿œå ãããŸãã
ããã«ããµãŒããŒã¯ãã¯ã©ã€ã¢ã³ãMESSAGE_TYPE_ENUM_SESSIONããã®èŠæ±ã«å¿ããŠãæ¢åã®ã»ãã·ã§ã³ã«é¢ããæ å ±ãéä¿¡ããŸãã
ã¹ããŒãžNET_STADY_CREATE_SESSIONã¯ããã¬ãŒã€ãŒããéå§ããã¿ã³ãæŒããŠã²ãŒã ã®èµ·åãéå§ãããŸã§ç¶ããŸããå®éã«ã¯ãçŸæç¹ã§ã¯ãNET_STADY_START_GAMEã¹ããŒãžã¯ãnet-> SetStadyïŒNET_STADY_START_GAMEãNULLïŒãžã®åŒã³åºããä»ããŠèšå®ãããŸãã ã
ã¡ãã»ãŒãžMESSAGE_TYPE_START_GAMEã¯
voidé¢æ°GNetStadyCreateSession :: OnFinishïŒNET_STADY nextïŒããèªåçã«éä¿¡ãããŸããããã¯NET_STADY_CREATE_SESSIONãå®äºãããšåŒã³åºãããŸããããã§ããã¹ãã¯ãµãŒããŒã«ãã»ãã·ã§ã³ãåå ã®ããã«éããããããšãéç¥ããä»ã®ãã¬ãŒã€ãŒã«éç¥ããå¿ èŠã¯ãããŸããã
ã¡ãã»ãŒãžMESSAGE_TYPE_START_GAMEã¯ããµãŒããŒã«ãã£ãŠã»ãã·ã§ã³ã®ãã¹ãŠã®åå è ã«éä¿¡ãããŸãããããåä¿¡ãããšãããããã¹ãŠãNET_STADY_START_GAMEã¹ããŒãžã«åãæ¿ããŸãã
ããã«ãã²ãŒã ã®èµ·å段éã®äž»ãªäœæ¥ã¯ãé¢æ°ïŒ
void GNetStadyStartGame :: OnPeriodïŒïŒã§å®è¡ãããŸããããã¯ãé¢æ°ãä»ããŠ5ãã1ãžã®ã«ãŠã³ãããŠã³ãå®è¡ããŸãä»®æ³ãã€ãGGame :: OnStartNetCounterïŒint counterïŒ;å®éã«ã¯ã5ã4ã3ã2ã1ãšããæ°åããã£ãããšãªã¢ã«åºåãããŸãã次ã«ãä»®æ³ãã€ãGGame :: OnStartNetGameïŒïŒãåŒã³åºãããã²ãŒã ã»ãã·ã§ã³ã®èµ·åæºåããã»ã¹ãéå§ãããŸãããã®æç¹ã§ãã²ãŒã ã«ãŒããèªã¿èŸŒãŸãããã¬ãŒã€ãŒããã®äžã«é 眮ãããŸãããã®ããã»ã¹å šäœãåã³ã³ãã¥ãŒã¿ãŒã§ç¬ç«ããŠå®è¡ãããããšã«æ³šæããŠãã ããããã¹ãŠã®ããŒã¿ãåæåããããšãä»®æ³ããŒã«GGame :: IsNetGameLoadedïŒïŒé¢æ°ã¯trueãè¿ãã¯ãã§ãããã®é¢æ°ã¯GNetStadyStartGame :: OnPeriodïŒïŒããç¶ç¶çã«åŒã³åºãããfalseãè¿ãããéããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã¯ã²ãŒã ã®åæåãç¶ç¶ãããšæ³å®ããŸããåž°ã£ãŠããã«trueã®å Žåãã¡ãã»ãŒãžMESSAGE_TYPE_PLAYER_STARTEDãããã«ä»ã®ãã¹ãŠã®ãã¬ãŒã€ãŒã«éä¿¡ãããŸãããã®æç¹ã§ããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã¯ãã»ãã·ã§ã³ã®ãã¹ãŠã®åå è ããã¡ãã»ãŒãžMESSAGE_TYPE_PLAYER_STARTEDãæ¢ã«åä¿¡ããŠããããšãæ€åºãããšãNET_STADY_GAMEã¹ããŒãžã«é²ã¿ãããã¯ã²ãŒã ã®éå§ãæå³ããŸããbool
é¢æ°GNetStadyGame :: OnStartïŒNET_STADY以åãvoid * initïŒã¯ããã«ä»®æ³void GGame :: OnLaunchNetGameïŒïŒãåŒã³åºãããããèµ·åã§ããããã ãã§ã ãã®åŸãã²ãŒã ãå§ãŸããŸãã
ãããã¯ãŒã¯ã²ãŒã
ã¹ããŒãžNET_STADY_GAMEã¯ãvoidé¢æ°GNetStadyGame :: OnUpdateïŒïŒãéããŠã²ãŒã ãã¬ã€å šäœãå¶åŸ¡ããŸããå®éã«ã¯ããã®ã¹ããŒãžã§ã¯ããã¬ãŒã€ãŒãäžå®æéããŠã¹ãšããŒããŒãã䜿çšããŠå ¥åããããŒã ãéä¿¡ããŸãããŸãããã®æ®µéã§ã¯ãä»ã®ãã¬ã€ã€ãŒããã®ãŸã£ããåãããŒã¿ãæåŸ ãããŸãã
ãŠãŒã¶ãŒã³ãã³ãã¯ãã¡ãã»ãŒãžMESSAGE_TYPE_PLAYER_GAMEãä»ããŠéä¿¡ãããŸãã GNetStadyGameã¹ããŒãžã«ã¯æ¬¡ã®ãã£ãŒã«ãããããŸãã
int k_player; PLAYER_MESSAGE* m_player;
ä»ã®ãã¬ã€ã€ãŒããããŒã ãåãåãããã«äœ¿çšãããŸããPLAYER_MESSAGEæ§é ã«ã¯ãNET_BUFFER next_messageã¡ãã»ãŒãžãåä¿¡ããããã®ãããã¡ããããŸãããã ãããã®ç®çã¯ãäžèŠãããšæããããããããŸãããå®éã«ã¯ããããã¯ãŒã¯ã¿ã¯ãçªå·ãšããæŠå¿µããããŸããããã¯ãã²ãŒã ãçµäºãããŸã§0ããç¡éã«å¢å ããã¯ããã¯ãã«ãŠã³ããããããªå€æ°ã§ãããããã¯ãŒã¯ã¯ããã¯ã¯ããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ããçŸåšã®ãããã¯ãŒã¯ã¯ããã¯ã®ãã¹ãŠã®ãã¬ãŒã€ãŒããã³ãã³ããåä¿¡ããå Žåã«ã®ã¿å¢å ããŸãããã以å€ã®å ŽåãåŸ æ©ããããã²ãŒã ãããªãŒãºããŸãããã ããéåžžã¯éä¿¡ã®å質ã«ããã¡ãã»ãŒãžãéåžžã«å®å®ããŠé ä¿¡ã§ããããããããã®é 延ã¯ãã¬ãŒã€ãŒã«æ°ä»ãããã«çºçããŸãã
ããã§ãã¯ã©ã€ã¢ã³ããé·æéåŸ æ©ãå§ããå Žåãä»®æ³ãã€ãé¢æ°GGame :: OnWaitingPlayersïŒunsigned int dtimeãint k_player_idãunsigned int * m_player_idïŒãä»ããŠãã®ã²ãŒã ãå ±åããã²ãŒã ã®ã¿ã¹ã¯ã¯ãã®ãã¬ã€ã€ãŒã®ãªã¹ããç»é¢ã«è¡šç€ºããããšã§ããåŸ æ©æéã¯å¶éãããŠãããã¯ã©ã€ã¢ã³ãã¯åŸ æ©æéãç¡éã«ãªããªãããã«ããŸããå¶éæéã«éãããšãã¯ã©ã€ã¢ã³ãã¯åé¡ã®ãããã¬ãŒã€ãŒãåæãå§ããæè ã§ãããšå®£èšããŸããããã«ãããããã«ã²ãŒã ãç¶è¡ãããããåå©ã«ããã²ãŒã ãå®äºããŸãã
ã¯ã©ã€ã¢ã³ããçŸåšã®ã¡ãžã£ãŒã«å¯Ÿããå¥ã®ãã¬ãŒã€ãŒããã³ãã³ããåãåããšããããã¯ä»®æ³ãã€ãé¢æ°GGame :: SetPlayerNetMessageïŒunsigned int senderãMEM_DATAïŒmessageïŒã䜿çšããŠããã«ã²ãŒã ã«è»¢éãããŸãããã ããåä¿¡ããã¡ãã»ãŒãžãããã«ã²ãŒã ã«è»¢éãããå Žåããªããã1ã€ã®NET_BUFFER next_messageãããã¡ãŒãå¿ èŠãªã®ãã¯ããŸãæ確ã§ã¯ãªãã§ããããïŒãããããªãã
ãã§ã«è¿°ã¹ãããã«ããããã¯ãŒã¯ã®çžäºäœçšã¯ãã«ãã¹ã¬ãããšéåžžã«ãã䌌ãŠãããã¹ã¬ããã®åæã«ããäžéšã®ã¢ã¯ã·ã§ã³ãç¡æéã«é 延ããå¯èœæ§ããããŸãããããã¯ãŒã¯ã²ãŒã ã§ã¯ãããã³ã³ãã¥ãŒã¿ãŒã1 ãããã¯ãŒã¯ã¯ããã¯ãµã€ã¯ã«ã ãå¥ã®ã³ã³ãã¥ãŒã¿ãŒãè¿œãè¶ãå§ãããšãç¶æ³ãç°¡åã«çºçããŸãããã®å Žåãå ã®æ¬¡ã®ãµã€ã¯ã«ã®ããã®ã¡ãã»ãŒãžãšããŠè¡šç€ºãããã³ã³ãã¥ãŒã¿ã¡ãã»ãŒãžãè¿œãè¶ãããæ¥ãç§ãã¡ã®ã³ã³ãã¥ãŒã¿ã¯ãŸã éããŠããŸããããããŠãã³ã³ãã¥ãŒã¿ã¯æ¬¡ã®ããšãè¡ãå¿ èŠããããŸã...ãã®ã¡ãã»ãŒãžãç¬èªã®ãããã¡ã«ä¿åããã ãã§ãNET_BUFFER next_messageãçŸåšã®ãšããäžæçã«ãã®ãµããžã§ã¯ãã«å¯ŸããŠã¢ã¯ã·ã§ã³ãå®è¡ããªããªããŸããã次ã®ãããã¯ãŒã¯ã¯ããã¯ã®ããã«ãã®ãããªãã¬ãŒã€ãŒããã¡ãã»ãŒãžãæ¢ã«åä¿¡ãããŠããããšãããããŸãããã€ã«ãªã次ã®ãµã€ã¯ã«ã®ãããã¯ãŒã¯ã¯ããŸãæåã«ç§ãã¡ã®ã³ã³ãã¥ãŒã¿vozmotãã®åŸããããã®ç¬èªã®ãããã¡ããã®ã³ãã³ããšã¯ããä»ããŠã²ãŒã ã«æž¡ãä»®æ³ç©ºGGame :: SetPlayerNetMessageïŒunsigned intåã®éä¿¡è ãMEM_DATAïŒã¡ãã»ãŒãžïŒãããã¯ãåæ§ã®ã²ãŒã ã§ãããã¯ãŒã¯ã®çžäºäœçšãæ§ç¯ããããã«ç解ããããšãæãŸãããšããéåžžã«éèŠãªãã€ã³ãã§ãã
ãŸãããä»ã®ãã¬ã€ã€ãŒãåŸ ã€ãããã»ã¹ãæ©èœããããã2ã¡ãžã£ãŒã®è¿œãè¶ããã§ããªããªãããšãç解ããå¿ èŠããããŸãã"ãããã£ãŠãæ倧ã®åé²ã¯1 ãããã¯ãŒã¯ã¯ããã¯ã®ã¿ã§ãã
ãã ããã³ãã³ããåä¿¡ããã«ã¯ãæåã«ããããéä¿¡ããå¿ èŠããããŸãããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã¯ãã²ãŒã ã§äœ¿çšãããã³ãã³ãã®è©³çŽ°ã«ã€ããŠäœãç¥ãå¿ èŠããªããããä»®æ³MEM_DATAé¢æ°ãåŒã³åºãã ãã§ã:: GetPlayerNetMessage GGameïŒïŒãªã©ã®ããŒã ã«å¯Ÿããç·©è¡æºåããããã«åœŒãè¿ããMEM_DATAãåŸããããããã¡ã¯ãä»ã®ãã¹ãŠã®ãã¬ã€ã€ãŒèªèº«ã®ãã¬ã€ã€ãŒã«åæã«éä¿¡ãããŸãã
MEM_DATA message=owner->game->GetPlayerNetMessage(); GNetSocket* socket=owner->GetMainSocket(); owner->game->SetPlayerNetMessage(socket->player_id, message); // , int k_receiver=owner->RefreshReceiverList(); owner->m_receiver[k_receiver]=takt; // , NET_BUFFER_INDEX* result=owner->PrepareMessageForPlayers(MESSAGE_TYPE_PLAYER_GAME, message.length, message.data, k_receiver, owner->m_receiver, 1); socket->SendMessage(result); //
ã²ãŒã ã¯ãããã¯ãŒã¯ã¯ããã¯ãå¶åŸ¡ããä»®æ³inté¢æ°GGame :: GetNetTaktïŒïŒãä»ããŠã¯ã©ã€ã¢ã³ãã«è¿ããŸãããã ããã¯ã©ã€ã¢ã³ãã¯ãããã¯ãŒã¯ã¿ã¯ãã®å®äºãå¶åŸ¡ããŸããããã¯ããã¹ãŠã®ãã¬ãŒã€ãŒãããã¹ãŠã®ããŒã ãåä¿¡ãããç¬éã§ããã¯ã©ã€ã¢ã³ãã¯ãä»®æ³ã²ãŒã GGame :: OnNextNetTaktïŒïŒãåŒã³åºããŠããã®ã²ãŒã ãããã«å ±åããŸããç§ã®å Žåããã®é¢æ°ã¯ãããã¯ãŒã¯ã®åæ解é€ããã§ãã¯ãããã¹ãŠãæ£åžžãªå Žåã«trueãè¿ããŸããfalseãè¿ãããå Žåããããã¯ãŒã¯ã¯ã©ã€ã¢ã³ãã¯éåæã®ä¿®æ£ããã»ã¹ãèªåçã«éå§ããŸãããã¹ãã¯ãã¹ãŠã®ããŒã¿ããã¡ã€ã«ã«æžã蟌ã¿ããã®ãã¡ã€ã«ãä»ã®ãã¹ãŠã®ãã¬ãŒã€ãŒã«è»¢éããŸããä»ã®ãã¬ãŒã€ãŒã¯ãã®ãã¡ã€ã«ãèªã¿åãããã®ããŒã¿ã§ã²ãŒã ãç¶è¡ããŸããå®éã«ã¯ããã¹ãã¯ä¿åãè¡ããŸããããŠãæ®ãã®ãã¬ãŒã€ãŒã¯Loadã§ãã1ã€ã®ãããã¯ãŒã¯ã¯ããã¯ãµã€ã¯ã«ã§çæãããä¹±æ°ã®åèšãã«ãŠã³ãããããšã«ããããããã¯ãŒã¯ã®åæãå¶åŸ¡ããŸããä¹±æ°ã¯é£ç¶çã«çæããããã¹ãŠã®ã³ã³ãã¥ãŒã¿ãŒã§åãã§ããå¿ èŠããããŸããããã§ãªãå Žåãããã¯ãããã¯ãŒã¯ãåæããŠããªãããšã瀺ããŠããŸããããä»®æ³BOOL GGame :: OnNextNetTaktïŒïŒãè¿ãtrueã«ãã¯ã©ã€ã¢ã³ãèªèº«ãå€æ°ã«ãã®äºå®ãææãon_next_net_takt -圌ã®ããã«ããã¯é»æºãµã€ã¯ã«ãå®äºããããšãæå³ããŸããã²ãŒã ã¯ãã¡ã€ã³ã«ãŒãã§ã¯ã©ã€ã¢ã³ãé¢æ°bool GNetClient :: IsNextNetTaktïŒïŒ{return on_next_net_takt;}ãå®æçã«åŒã³åºãå¿ èŠããããŸã
trueãè¿ããããšãã²ãŒã ã¯ãããã¯ãŒã¯ã¯ããã¯ã1 å¢ãããåãã¬ãŒã€ãŒã®æåŸã®ãããã¯ãŒã¯ã¯ããã¯ã§ãããã¯ãŒã¯çµç±ã§åä¿¡ãããã¹ãŠã®ã³ãã³ããå®è¡ããŸãããã®åŸãã³ãã³ãé åãã¯ãªã¢ããããã¹ãŠãæ°ããæ¹æ³ã§éå§ãããŸããããããã¯ãŒã¯ã¯ããã¯ã®å€ãå¢å ããŸãã
ãã¬ãŒã€ãŒãããŠã¹ãšããŒããŒãããå ¥åããã³ãã³ãã¯ãããã«ã¯ãããã¯ãŒã¯ã«å°éããŸãããå®éãçŸåšã®ãããã¯ãŒã¯ã¯ããã¯ãµã€ã¯ã«ã§ã¯ãæåŸã®ãããã¯ãŒã¯ã¯ããã¯ãµã€ã¯ã«ã§åéãããã³ãã³ãããããã¯ãŒã¯ã«éä¿¡ããããã®æç¹ã§ããŠã¹ãšããŒããŒãããã®æ°ããã³ãã³ããåéãããŸããã€ãŸã1ãããã¯ãŒã¯ã¯ããã¯ã®åå¿ã«é ãããããŸãããã®é 延ã¯ãããã¯ãŒã¯é 延ãšåŒã°ããŸãããã ãããããã¯ãŒã¯ã¯ããã¯ãã²ãŒã ã¯ããã¯ãšåãã«ããããšã«ã¯æå³ããããŸãããããšãã°ãã²ãŒã ã1ç§éã«60åæŽæ°ãããå Žåã1ã€ã®ãããã¯ãŒã¯ã¯ããã¯ã«å¯Ÿå¿ããã²ãŒã ã®è©Šåã10 åè¡ãããšã¯å®å šã«å¯èœã§ãããŠãŒã¶ãŒã1/6ç§ã®åå¿é 延ã«éåžžã«æ©ãŸãããããšã¯ã»ãšãã©ãããŸããã
ãã©ãã£ãã¯æå·å
ç§ã¯ãããŒã¿ä¿è·ã®ãããã¯ã«åŒ·ããªãããšãèªããªããã°ãªããŸããããããŠãçµå±ãç§ã¯ããã«æ³šæãåãããã ãã§ããéçºè ããããè¡ãããšãæåŸ ããŠãããããã²ãŒã ãããµãŒããŒã«æ¥ç¶ããå¿ èŠã¯ãããŸãããå®éã«ã¯ãä»»æã®IPã¢ãã¬ã¹ãšããŒããšã®æ¥ç¶ã確ç«ããæ©èœãæã€ä»»æã®ããã°ã©ã ã«æ¥ç¶ã§ããŸãããã®åŸããµãŒããŒãžã®éä¿¡ãéå§ã§ããŸãããµãŒããŒã¯ãå°ãªããšãã¡ãã»ãŒãžãæ£ãããªãããšã確èªããå¿ èŠããããŸããããã§ãªãå ŽåããµãŒããŒã¯åã«ãããåŠãã®äžéšãåãåãããã®äžã«å·»ã蟌ãŸããŸãã
ãŸããäž¡åŽã®ã¡ãã»ãŒãžã¯éåžžæå·åãããŸãã
ãã®ãããã¯ã«ã€ããŠã¯ç¹ã«èª¬æããŸããããèªå® ã§ã®ãã©ãã£ãã¯ã®æå°æå·åã䜿çšãããšã®ã¿èšããŸããé²åŸ¡ãç Žãããšã¯é£ãããšã¯æããªãããªããªãããã®æ®µéã§ã¯ããããè¡ãæ欲ã匷ãããªãããã ãåœåã®éãç§ã®ãããžã§ã¯ãã®å°ããªå声ã«å®ãããããã«ãããèŠãããããšãé¡ã£ãŠããŸã...
Windowsã«éåžžã«é·ãéååšãã1ã€ã®ãšã©ãŒã«ã€ããŠ
èšäºã®æåã®éšåã§ã¯ãRTSã®ãããã¯ãŒã¯ã²ãŒã ãäžè¬çãªã¢ã€ãã¢ãšããŠã®ã¿èª¬æããŸãããããããããã§ç§ã¯RTSã®ãããã¯ãŒã¯ãããµããŠããæ倧ã®åé¡ãææããŸãã-ãã¹ãŠã®ã³ã³ãã¥ãŒã¿ãŒã§å®å šã«åäžã®èšç®ãå¿ èŠã§ããã³ã³ãã¥ãŒã¿ãŒãå°ãªããšãå°ãééãå§ããããæ°åã§ãã¹ãŠãã³ã³ãã¥ãŒã¿ãŒã«ãã£ãŠå€§ããç°ãªããŸãããããŠããã¬ãŒã€ãŒãä»ã®ã³ã³ãã¥ãŒã¿ãŒã®ãéææŠã§æ®ºããããã³ã³ãã¥ãŒã¿ãŒã®ãŠããããã³ã³ãããŒã«ããããšãããšãã²ãŒã ã¯åçŽã«æ··ä¹±ã«é¥ããŸãããã®ãããªãšã©ãŒã¯ãç§ãèŠããã®ã®äžã§æãã²ã©ããã®ã ãšæããŸãããªããªãããã®ãããªãã°ãè«çã§èŠã€ããããšã¯å®éã«ã¯äžå¯èœã ããã§ããéåžžããã®ãããªãšã©ãŒã®åå ã¯ãããããã¯ãŒã¯ã²ãŒã ã®åèµ·åæã«å€æ°ãååæåããã®ãå¿ããããªã©ã®äºçŽ°ãªäºçŽ°ãªããšã§ãããã®çµæããã®å€æ°ã¯ãããã¯ãŒã¯ã®åæãç Žãããšãä¿èšŒãããŠãããã²ãŒã èªäœãæçµçã«åŽ©å£ãããã£ãšåããã§ãã
ãã®ãããã¯ã«é¢ããç§ã®æšè«ã«èå³ããã人ã¯ãèšäºã®æåã®éšåãåç §ããŠãã ãããç§ã®æèŠã§ã¯ãæããWindowsã«ååšããŠããéåžžã«æ±ãã°ãªããã«ã€ããŠã話ããããšæããŸããæµ®åå°æ°ç¹ã®èšç®ã§ãšã©ãŒãçºçããããšãä¿èšŒãããŠãããããã«ãããããã¯ãŒã¯ã匷å¶çµäºãããŸãã
ãã®åé¡ã¯ããããWindows 98ã§2003-2004幎ã«çºèŠãããããããWindows XPã«æ£åžžã«ç§»è¡ããæè¿Windows 8ã§ãäœãå€ãã£ãŠããªãããšãããããŸããã
ãšã©ãŒã®äž»ãªãã€ã³ãã¯ã察å¿ããWindowsé¢æ°ãFPUå¶åŸ¡ã¯ãŒããå€æŽããïŒãããŠæ»ããªãïŒããšã§ããããããŠããã¡ããããã®ãããªåäœã«ã€ããŠã¯ããã¥ã¡ã³ãã®ã©ãã«ãèšåãããŠããŸããã
ããã«ãWindows XPã®åé¡ã®ååšã蚌æããå€ãã³ãŒãããããŸããäžã®Windows 8ãç§ã¯ãããè©ŠããŠããªãããäžã®Windows 8ç§ã®ãã確ç«ããããããã¯ãŒã¯ã²ãŒã ã¯ãæçœãªçç±ããªãçªç¶éãäœæ¥ãéå§ãããšããç§ã¯ãŸãããç¶æ³ã«é£ã³ãŸãããããã®åé¡ãè£ãã³ãŒãã誀ã£ãŠåé€ããããšãå€æããŸããã
ãããã£ãŠãé¢æ°ã®äŸïŒ
int Error1() { double step=66.666664123535156; double start_position_interpolation=0; double position_interpolation=199.99998474121094; double vdiscret=(position_interpolation-start_position_interpolation)/step; int discret=(int)vdiscret; return discret; }
ãã¡ãããæ°åã¯å¥åŠã§ããããšã©ãŒã衚瀺ãããŸããError1ïŒïŒ
é¢æ°ãåŒã³åºããŠãããã¬ãŒã§åŠçãããšãçµæãšããŠæ°å€2ãdiscretå€æ°ã«ãªããŸãã
int result=Error1(); // result=2 ok=direct_3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3d9pp, device_3d ); result=Error1(); // result=3
ã€ãŸãé¢æ°éåŒã³åºãå ŽåERROR1ïŒïŒãŠã§ããžæ©èœã®DirectXäœæ-aããã€ã¹ã®DirectXãã第2ã®æ©èœåŒã³åºãã§ãããäºæããªãçµædiscret = 3ãããã¯ãvdiscretãåããŠ2.99999 .......ã«ãªãã2åç®ã«ã¯ãã§ã«3ã«ãªããšããäºå®ã«ãããã®ã§ããå®éã«ã¯éãã¯ãããã§ãããã²ãŒã ã§doubleåã®å€æ°ã䜿çšããŠããããããããã¯ãŒã¯ã²ãŒã å šäœã殺ãã®ã«ååã§ããããã«ãçç±ãç¥ããã«ãããã«äœããä¿®æ£ããæ¹æ³ã¯ãããŸããã圢åŒèªäœã¯ã³ãŒãèªäœãæ£ãããããã©ããã§äœããã®ããã»ããµç¶æ ãã©ã°ãç®çã®å€ã«æ»ããªãããã§ããWindows 98
ãã®åé¡ã¯ãWindows XPãããããã«æ¿ããçŸããŸãããããã§ããã®äžå ·åã¯ãWinAPIã䜿çšããŠãå©çšå¯èœãªã¢ãã¿ãŒè§£å床ãäžèŠ§è¡šç€ºããããšãããšãã«çºçããŸãããDirectXããªããŠããWindows 8ãç§ã¯ãã§ã«äœãããããäºåã«ç¥ã£ãŠããããšããããã®è³ªåã調æ»ããŠããŸããã
ç§ã¯ãã®æšåããæ²»ãã2ã€ã®è§£æ±ºçãç¥ã£ãŠããŸããå¥ã®ã¹ããªãŒã ãäœæããããç»é¢è§£å床ãåãæ¿ããŠããšã©ãŒãšãšãã«ã¹ããªãŒã ã匷å¶çµäºããŸããããã®å Žåããã®åé¡ã¯ã¡ã€ã³ã¹ã¬ããã«åœ±é¿ããŸããã§ããã
2çªç®ã®æ¹æ³ã¯ç°¡åã§ãã
unsigned int status=_controlfp(0,0); // // ... // ... // ... _controlfp(status,_MCW_DN | _MCW_IC | _MCW_RC | _MCW_PC);
ããã«ãããç»é¢ã®è§£å床ãåãæ¿ããåŸã«è¡šç€ºãããã°ãªããããªããªããæ°åŠã¯æ£ããåäœãç¶ããŸãã
ã²ãŒã ãŸãã¯ãªãªãŒã¹ã®çŸåšã®ç¶æ
ã²ãŒã ã®ãªãªãŒã¹æºåããã§ã«æŽã£ãŠãããšå€æããŸãããã¯ãããã©ã³ã¹ã調æŽããããããã€ãã®å°ããªééããä¿®æ£ããªããã°ãªããªãå¯èœæ§ããããŸãããå®éã«ã¯ã²ãŒã ããªãªãŒã¹ããæã§ãããããã«ããããããªãæ¹åã«åãçµãããã«ãç§ã¯ãã€ã³ããéåžžã®ãã¬ãŒã€ãŒãããå Žåã«ã®ã¿èŠãŸãã
æè¿ãç§ã¯ãcomãorgãnetãªã©ã®ããç¥ããããã¡ã€ã³ã«å ããŠãäžçã«éžå°ãã¡ã€ã³ãããããšãç¥ã£ãŠé©ãããç§ã®è±èªã®ã²ãŒã ã¯Onimod landãšåŒã°ããŠããã®ã§ãããã«ã²ãŒã ã®onimod.landãã¡ã€ã³ãååŸããŸããããã®ãããã²ãŒã ã¯ä»¥åã®ããã«ç¬èªã®å人ãµã€ãonimod.landãæã¡ãŸãã
Steamã§ã®ãªãªãŒã¹ã®åã«ãããã¯å°ãåŸã§æ¥ããšæããŸãããä»ã®ãšãããç§ã¯èªåã®ãµã€ããéããŠã²ãŒã ããªãªãŒã¹ããŸããç§ã®ãããžã§ã¯ãã財æ¿çã«æ¯æŽããã人ã¯ãã²ãŒã ã®ãããµã€ãã§ãããè¡ãããšãã§ããŸããããããç§èªèº«ã¯ãã·ã¢ã«äœãã§ãããããã®äººã ã¯ãœãããŠã§ã¢ãè³Œå ¥ãããããã¯ããã«ç·æ¥ã®è²»çšé ç®ãããããšãç解ããŠããŸãããããã£ãŠããã®ã²ãŒã ãæ°ã«å ¥ã£ãŠããŠãçµæžçã«ç§ããµããŒãããçµæžåããªãå Žåã¯ããµã€ãã®ãã£ãŒãããã¯ãã©ãŒã ã䜿çšããŠç¡æã§ããŒãèŠæ±ã§ããŸããèªå·±çŽ¹ä»ãæ ããªãã§ãã ããããããªããšãwertwq @ mail.ruããã®ãããŒããããã®ãããªæåãç§ã®äžã«ãã¬ãã£ããªææ ãåŒãèµ·ãããŸãã
ã²ãŒã ã¯åæ¥åãããããããããã«ç§ä»¥å€ã®èª°ãããããå¿ èŠãšããŠãããã©ãããç¥ãã§ãããã
ããããããã®å²åŠçãªã¡ã¢ã§ãç§ã¯ç§ã®è©±ãçµããã§ãããããã®èšäºãèªãã§ç€ºãããé©ãã¹ãæå¿åãšãç§ã®ãæåŠçãªæèœãã«å¯ŸããèŠäžããæ 床ã«çã«æè¬ããŸãã
æ¬å ·ãã¢ã¬ã¯ã»ã€ã»ã»ããïŒå¥åOdin_KGïŒ
PS
ã²ãŒã ã«é¢ããèšäºãè±èªã«ç¿»èš³ããããå°ãªããšãæåã®éšåãšãã¹æ€çŽ¢ã¢ã«ãŽãªãºã ã
誰ããè±èªã«ã€ããŠååãªç¥èããããå°ãªããšãç§ã話ãããšããŠããããšã«ã€ããŠããçšåºŠç解ãããã®åé¡ã§ç§ãå©ããããšããé¡æãæã£ãŠãããªããç§ã¯ãšãŠã幞ãã§ããã©ãããããã翻蚳è ãéãããšããŸãããã圌ã¯ææ³çã«æ£ããæå³çãªãã³ã»ã³ã¹ãç§ã«äžããŠãããã®ã§ãå¥ã®åæ§ã®è©Šã¿ãããç¹å¥ãªæ¬²æ±ã¯ãããŸããã
誰ããå¿çããå®éã«ä»äºã«åãæããå Žåããã®ãªã¯ãšã¹ããèšäºããåé€ããŸãã
â èšäºã®åé ïŒã²ãŒã ã®åŸ©æŽ»
â èšäºã®ç¶ãïŒGUI