# Lindaã©ã€ãã©ãªå ¥ææ¹æ³
Linda serverã®æ¬äœãšãéä¿¡ããã°ã©ã ã®äŸã§ãã éä¿¡ããã°ã©ã äœæ ã®åèã«ããŠäžãããcvsã§ã¯ã以äžã®ãããžã§ã¯ãåã§ãã§ãã¯ã¢ãŠãããŠãã ããã
Game_project/Linda
# Lindaã®è§£èª¬
# Lindaãšã¯
Lindaãšã¯ãã¿ãã«ãšåŒã°ããIDãšDataãã»ããã«ãªã£ããã®ããåã¯ã©ã€ã¢ ã³ãããµãŒãã«å¯Ÿã㊠out,in,rd ãªã©ã®ã³ãã³ããçšããŠèªã¿æžãããããšã«ãã£ãŠéä¿¡ãè¡ãã·ã¹ãã ã§ãã ãµãŒããŒã«èããããããŒã¿ãžã®ã¢ã¯ã»ã¹ã¯ã APIã«ã¿ãã«ã®IDãæå®ããããšã«ãã£ãŠè¡ããŸãã
#ref(linda.jpeg,center)
# ãµãŒã (ldserv.c)
LindaãµãŒãã®ãœãŒã¹ã¯ldserv.cã®ã¿ã§ãã~ ãµãŒãã¯ãæ°èŠã«ã¯ã©ã€ã¢ã³ããæ¥ç¶ ãããšãã¿ãã«ã¹ããŒã¹ã®ID65535ã«ã·ãŒã±ã³ã¹ãªçªå·ãããŒã¿ãšããŠãã€ã¿ãã« ãèããŸããã¯ã©ã€ã¢ã³ãããã®ID65535ããã¿ãã«ãååŸ(inãçšãã)ããããšã§ã ã¯ã©ã€ã¢ã³ãæ¯ã«ãŠããŒã¯ãªçªå·ãå²ãåœãŠãããšãã§ããŸãã
# ã¯ã©ã€ã¢ã³ãAPI (lindaapi.c,lindaapi.h)
ã¿ãã«éåä¿¡ã®API(out,in,rdãªã©)ãçšãããšãã¿ãã«ã¯COMMANDãã¥ãŒã«æºããããŠãããããã°ã©ããæå®ããã¿ã€ãã³ã°ã§(psx_sync_n()å®è¡æ)ã äžæ°ã«ãµãŒãã«éä¿¡ãããŸããã€ãŸãAPIãå®è¡ãã段éã§ããã«éä¿¡ã å§ãŸãããã§ã¯ãããŸããã
ãŸãããµãŒãããã®ã¿ãã«ã®åä¿¡ã psx_sync_n() ãå®è¡ãããšãã«è¡ã ãŸããåä¿¡ããã¿ãã«ã¯ REPLYãã¥ãŒ ã«èããããpsx_reply() ã§åãåºããŸãã ãŸããã³ãŒã«ããã¯ãçšããŠåä¿¡ãããšãã®ã¢ã¯ã·ã§ã³ãç»é²ããããšãã§ããŸãã
# Lindaã®API
* void start_linda(hostname);
éä¿¡ãåæåããŠãLinda APIã䜿çšå¯èœã«ãã
* void psx_sync_n();
|
|
* int psx_out(int id, char *data, int size);
dataã瀺ãã¢ãã¬ã¹ãããæå®ããIDã®ã¿ãã«ãžsize byteã®ããŒã¿
ã æžã蟌ãã³ãã³ããè¿ãå€ã¯ããã®ã³ãã³ãã®sequenceçªå·ã§ãã
* int psx_in(int id);
æå®ããIDçªå·ã®ã¿ãã«ãããããŒã¿ãèªã¿èŸŒãã³ãã³ãã ãã®ã³
ãã³ãã䜿çšããå ŽåããµãŒããŒäžã«ååšããã¿ãã«ã¯ã ã¯ã©ã€ã¢
ã³ããããŒã¿ãèªã¿èŸŒãã åŸåé€ãããŸãã è¿ãå€ãšããŠã
sequenceçªå·ãååŸããŸãã
* int psx_rd(int id);
æå®ããIDçªå·ã®ã¿ãã«ãããããŒã¿ãèªã¿èŸŒãã³ãã³ãã ãã®ã³
ãã³ãã䜿çšããå ŽåããµãŒããŒäžã«ååšããã¿ãã«ã¯ã ã¯ã©ã€ã¢
ã³ããããŒã¿ãèªã¿èŸŒãã åŸãæ®ããŸãã è¿ãå€ãšããŠãsequence
çªå·ãååŸããŸãã
* int psx_wait_rd(int id);
åºæ¬çã« psx_rd() ãšåãã§ããããµãŒãã® ã¿ãã«ã¹ããŒã¹ã«ããŒ
ã¿ããã£ãŠãããã«ã¯èªã¿ããŸãã 次ã«ããŒã¿ãæžã蟌ãŸãããŸã§
åŸ
ã¡ãŸãã ããŒã¿ãæžã蟌ãŸãããšãã®ã¿ãã®ããŒã¿ãèªã¿ãã¿ã
ã¯ã©ã€ã¢ã³ããžéä¿¡ããŸãã è¿ãå€ãšããŠãsequenceçªå·ãååŸã
ãŸãã
* unsigned char psx_reply(int seq);
REPLYãã¥ãŒããããŒã¿ãåãåºããšãã«äœ¿çšããŸãã
psx_in/psx_rdã§åãåã£ãsequenceçªå·ãåŒæ°ãšãææž¡ããšã èŠæ±
ããããŒã¿ãREPLYãã¥ãŒã«ããå Žåã ããŒã¿ãæ ŒçŽãããŠãã
TUPLEãžã®ãã€ã³ã¿ãè¿ããŸãã psx_replyã«ãã£ãŠè¿ããããã€ã³
ã¿ã®å
ã«ã¯ã å
é ã«ãã®TUPLEã®ãããæ
å ±ã 12bytes æ ŒçŽãããŠ
ããŠã ãã®åŸã«ããŒã¿ãã®ãã®ãæ ŒçŽãããŠããŸãã
* int psx_callback_in(int id,void (*callback)(char *tuple,void *),void *obj);
ãµãŒãããããŒã¿ãååŸããéãREPLYãã¥ãŒã«æºããã«ä»»æã®é¢æ°
ã åŒã³åºãããæå®ããããšãã§ããŸããåŒãæ°ã® callback é¢æ°
ãã€ã³ã¿ã¯ psx_sync_n() ã®äžã§(ããŒã¿ãå°çããã)åŒã°ããŸãã
äžèšã® psx_in / psx_rd / psx_wait_rd ã®ã³ãã³ãã®åã
ã«å¯ŸããŠ
psx_callback_in / psx_callback_rd / psx_callback_wait_rd ãçš
æãããŠããŸãã现ããªèª¬æã¯åŸã®æ¹ã§ããŸãã
* int psx_get_datalength(unsigned char *tuple);
|
|
# ãã¯ã
Lindaã§ã¯TUPLEã®ãããæ å ±ãããŒã¿éšåãžã¢ã¯ã»ã¹ããããã®ãªã ã»ãããš ããŠä»¥äžã®ãã¯ããçšæããŠããŸãã
#define LINDA_MODE_OFFSET 0
#define LINDA_ID_OFFSET 1
#define LINDA_SEQ_OFFSET 3
#define LINDA_DATA_LENGTH_OFFSET 7
#define LINDA_HEADER_SIZE 12
ãããã¯psx_reply()ã§åŸãTUPLE(unsigned char ã®ãã€ã³ã¿)ã®ããŒã¿éšå ããããéšåãªã©ãååŸãããšããªã©ã«çšããããŸããããŒã¿ååŸã¯ ããã° ã©ãã³ã°ã®æµãã§ç€ºããŸãã 以äžã¯psx_reply()ã§åŸãTUPLE (tuple) ã®ãã ãéšåã衚瀺ããŠãããšããã§ãã
printf("MODE: %c\n",*(char*)(tuple+LINDA_MODE_OFFSET));
printf("ID: %d\n",*(short*)(tuple+LINDA_ID_OFFSET));
printf("SEQ: %d\n",*(int*)(tuple+LINDA_SEQ_OFFSET));
printf("DATA_LENGTH: %d\n",*(int*)(tuple+LINDA_DATA_LENGTH_OFFSET);
ãããæ å ±ã¯ç°ãªããšã³ãã£ã¢ã³ã®CPUäžã§ã察å¿ããããã«æŽæ°ã®äžäœ ãã ããé åã®åã®æ¹ã«ããããã«æããŠããã®ã§ãäºæ³ããçµæãã§ãªã ãã ãããŸããããããéšåã®æ å ±ãåŸãã«ã¯psx_getããå§ãŸãé¢æ°çŸ€ã 䜿ã£ãŠ äžããã
# ããã°ã©ãã³ã°ã®æµã
ãŸãã¯ãããã°ã©ã åé ã§start_linda(hostname)ã å®è¡ããŠéä¿¡ãè¡ããããã«ããŠãããŸãã
# ããŒã¿ãéä¿¡ããå Žå
- psx_outã§ã³ãã³ãããã¥ãŒã«è¿œå (1)
psx_outããæç¹ã§ã¯ãåã«ãã¥ãŒãžãããããã ãã§ã ãŸã éä¿¡ã¯éå§ãã ãŠããŸããã Lindaã§ã¯ãcharåãunsigned charåã§ããããŒã¿ãéãããš ãã§ããŸããã ãã ã笊å·ã®åé¡ãé¿ãããããcharåã§ã¯ãªãunsigned charåã䜿ããŸãããã å°ã詳ããèšããšãpsx_outã¯ãäžãããããã€ã³ã¿ ã®ã¢ãã¬ã¹ããã 1byteãã€é çªã«æå®ãããbyteæ°ãµãŒããŒãžéä¿¡ãããšã ãäºã§ãã ããã§ãéãããããŒã¿ãæ ŒçŽããŠããå€æ°ãæ§é äœçã unsigned charå以å€ã§ããå Žåãunsigned charåã« ã¿ã€ããã£ã¹ãããŠã ãå¿ èŠããããŸãã
- psx_sync_nã§å®éã«ã³ãã³ããå®è¡ (2)
ãã®æãä»ãŸã§ãã¥ãŒã«ããããŸããLindaã®ã³ãã³ãããäžæ°ã«å®è¡ãã㟠ãã PlayStation 2ã§ã¯ãæç»ã«é¢ããæŒç®ã¯Emotion Engineã®äžã§è¡ãã ãŸãããå®éã®æç»åŠçã¯Graphics Synthesizerã§è¡ãããŸãã ã€ãŸããæ ç»åŠçãè¡ãããŠããéãEmotion Engineã«ã¯äœè£ãã§ããŸãã ãã®æéã 䜿çšããŠãéä¿¡ãè¡ãããã«ããã°ã©ãã³ã°ããŸãããã (ãã®ã¿ã€ãã³ã°ã èæ ®ããŠãpsx_sync_nãå®è¡ããŸããã)
- åä¿¡çžæã®ããŒã¿åãåãã®ç¢ºèª (3)
éä¿¡ãè¡ãéãåŸ ã¡åãããè¡ãã ããŒã¿ãåãåããŸã§ã²ãŒã ãæ¢ãŸã£ãŠ ããŸã£ãŠã¯ãããªãã®ã§ã LindaãµãŒããŒã§ã¯éåæéä¿¡ãæ¡çšããŠããŸãã ããéä¿¡ããããŒã¿ã確å®ã«çžæã«åãåã£ãŠæ¬²ããå Žåã¯ã çžæããåã åãã®ãµã€ã³(Ack)ãåãåãããã«ãããšããã§ãããã
äŸãã°ãéä¿¡åŽã psx_out() ããããŒã¿ãåä¿¡åŽã psx_in() ã§åãå ãã ããªéä¿¡ãäœåãç¹°ããããå ŽåãèããŸãã ãã®ãšããéä¿¡çžæãããŒã¿ ãåãåã£ãäºã確èªããŠããã 次ã®ããŒã¿ãéããªããã°ãªããŸããã äœ ãèããã«ã©ãã©ãpsx_outãè¡ããšãã¿ãã«ã«ãŸã ããŒã¿ãæ®ã£ãŠããã®ã§ æ°ããªããŒã¿ãæžã蟌ããããã¥ãŒã«ããããŸããŠãããŸãã ãããŠãåŠç ãã©ãã©ãéããªã£ãŠããŸããã²ãŒã èªäœãã¹ããŒããèœã¡ã ãã¥ãŒãã㣠ã±ãã«ãªããšã倱æããããã«ãªã£ãŠããŸããŸãã ããã§ããã®ç¢ºèªãåã ããã«ãçžæãããŒã¿ãåãåã£ããšãã ãµã€ã³(Ack)ããpsx_inãpsx_rdã 䜿çšããŠåãåããŸãã Ackãåãåã£ãŠããã次ã®ããŒã¿ãpsx_outããŠäž ããã ãã ããããã°ã©ã ãéä¿¡ãè¡ãéšåã§ã«ãŒãããŠã AckãåŸããŸã§ ããã°ã©ã ãæ¢ãŸããšããããã§ã¯ãããŸããïŒ ã¡ã€ã³ã®åŠçã¯ãAckãåŸ ã€ éãåãç¶ããæžãæ¹ãããŠäžããïŒïŒ ãã®ããã«ãpsx_replyã¯ifæã§æ¬ã ããŠããã®ã§ãã
- (1)ãããç¹°ãè¿ãè¡ã (4)
# ããŒã¿ãåä¿¡ããå Žå(callbackã䜿ããªã)
- psx_in/psx_rd/psx_wait_rdã§sequenceçªå·ãååŸ (1)
sequenceçªå·ãšã¯ãå®è¡ããã³ãã³ãã«äžããããã æŽçåžã®çªå·ã¿ãã㪠ãã®ã§ãã
- psx_replyã§ããŒã¿ã®å°çãç£èŠ (2)
ããŒã¿ãæ¥ãŠããã°ãããã«å¯ŸããåŠçãå®è¡ããã æ¥ãŠããªããã°ããã® ãŸãŸããã°ã©ã ã®åŠçãé²ããã
- ããŒã¿ãä»»æã®å€æ°ã»æ§é äœã«å±é (3)
ããŒã¿ãä»»æã®å€æ°ã»æ§é äœã«å±éããéã ããŒã¿ãéä¿¡ããããã« 1byte ãã€ã«å解ãããããŒã¿ ããå ã®åœ¢ã«çµã¿ç«ãŠãå¿ èŠããããŸãã psx_reply() ã§åŸããã€ã³ã¿(仮㫠reply ãšããŸã)ã® æãå ã«ã¯TUPLEã®ãã ããå«ãŸããŠããã®ã§ã å®ããŒã¿(RealDataåãšããŸã)ã¯
(RealData *)(reply + LINDA_HEADER_SIZE)
ã§ã¢ã¯ã»ã¹ã§ããŸããLINDA_HEADER_SIZE 㯠12 ã«çœ®ãæããããŸãã memcpyã䜿çšãããšäŸ¿å©ã§ãã
- psx_replyã§åãåã£ããã€ã³ã¿ãfreeãã (4)
éä¿¡ã§åãåã£ãããŒã¿ã¯ãpsx_sync_n()ã®å®è¡æã«mallocããã ã¡ã¢ãªã® äœåŠããžèããããŠããŸãã ãããæŸã£ãŠãããšãããŒã¿ãåãåã床ã«ã ãã®ããŒã¿åã ã ã¡ã¢ãªãæ¶è²»ãç¶ããäºã«ãªããã¡ã¢ãªäžè¶³ã®åå ãšãªã ãŸãã ãã£ãŠããŒã¿ãä»»æã®å€æ°ã»æ§é äœã«å±éãçµããåŸã«ã psx_reply ã§åŸããã€ã³ã¿ãfreeããŠäžããã å¿è«ãããŒã¿ãä»»æã®å€æ°ã»æ§é äœã«æ Œ çŽããã«ã psx_reply ã§åŸããã€ã³ã¿ããã®ãŸãŸäœ¿çšããŠãæ§ããŸããã
- éä¿¡çžæãžAckãéã (5)
å¿ èŠãªãã° ããŒã¿ãåãåã£ããšãããµã€ã³ãéä¿¡è ãžéããŸãã éä¿¡è ã Ack ã ãšåãããã®ãªããéãããŒã¿ã¯äœã§ãæ§ããŸããã
- (1)ãããç¹°ãè¿ãè¡ã (6)
# ããŒã¿ãåä¿¡ããå Žå(callbackã䜿ã)
- psx_callback_in / psx_callback_rd / psx_callback_wait_rd ã䜿ã (1)
callbackã䜿ãå Žåã¯åŒãæ°ã«callbackããé¢æ°ãšã ãã®é¢æ°ã®åŒãæ°ã«ã ãæ§é äœãæå®ããŸãããã® callbackããé¢æ°ã®åã¯
void function(char * tuple, void * obj);
ãšãªã£ãŠããŸããobjã¯ä»»æã®æ§é äœã®ãã€ã³ã¿ã§ãã ãã®é¢æ°ã®å éšã®åŠç ã¯èªç±ã«äœã£ãŠãããã®ã§ããã REPLYãã¥ãŒã«èããããªãã®ã§ã psx_reply()ãå®è¡ããŠã çãã¯è¿ã£ãŠããŸããã åŒãæ°ã® tuple 㯠psx_sync_n() å 㧠malloc ãããŠããã®ã§ã ãããªããªã£ãã free() ã㊠ãã ããã
ããŒã¿ãåãåã£ããšããæ å ±ãAckãšã㊠éãå Žåãªã©ã¯ããã® function() å ã§éä¿¡ããå¿ èŠããããŸãã
ãã® callback é¢æ°ãäžæãæžããšãpsx_reply() ããããªã ãããªããã° ã©ã ãå¯èœã§ãã
# Linda’s Tips
Lindaã§ã®ããã°ã©ãã³ã°ã®éã以äžã®ããšã«çæããŠãããŠäžããã
- éä¿¡ããã°ã©ã ã®äŸé¡ããGame_project/Linda/example ã«ãããŸãã
- IDã®å¹ ã¯ã0ã65534ã§ãã
- 65535ã®IDã§psx_inãããšãLindaãµãŒããŒããåå¥ã®IDãåŸãããŸãã
- IDã¯ã1ããé ã«äžããããŸãããŸããIDã¯ASCIIæåã§éãããŠæ¥ãã®ã§ã atoi é¢æ°ã§æ°å€ã«å€æããŠäžããã
- äžåºŠã«éããããŒã¿ã¯ã1ã€ã®ã¿ãã«ã«ã€ãã2^32-12bytes(intã®æ倧å€-ããããµã€ãº)ã§ãã
- NULLã¯éããªãã§äžããã
- ã³ãã³ãããããããŠãã¥ãŒããã£ã±ãã«ãªããšå€±æããŸãã