OSDN Git Service

受信効率化のため、データを受信する時1byteずつ読むのを止め、まとめて受信してから内容を処理する方法に変更。
authorhabu <habu@0568b783-4c39-0410-ac80-bf13821ea2a2>
Thu, 28 Sep 2006 23:46:38 +0000 (23:46 +0000)
committerhabu <habu@0568b783-4c39-0410-ac80-bf13821ea2a2>
Thu, 28 Sep 2006 23:46:38 +0000 (23:46 +0000)
描画タイミングキューが空のときに描画タイミングを受信した際に時刻情報をリセットするよう変更。
受信データのバッファ量を増加。

src/chuukei.c

index 99875c8..8802a57 100644 (file)
 #endif
 
 #define MAX_HOSTNAME 256
-#define RINGBUF_SIZE 65536*8
-#define FRESH_QUEUE_SIZE 1024
+#define RINGBUF_SIZE 1024*1024
+#define FRESH_QUEUE_SIZE 4096
 #define WAIT 100*1000 /* ¥Ö¥é¥¦¥ºÂ¦¤Î¥¦¥¨¥¤¥È(usñ°Ì) */
 #define DEFAULT_DELAY 50
+#define RECVBUF_SIZE 1024
 
 static int sd; /* ¥½¥±¥Ã¥È¤Î¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿ */
-static long epoch_time;  /* ³«»Ï»þ¹ï */
+static long epoch_time;  /* ¥Ð¥Ã¥Õ¥¡³«»Ï»þ¹ï */
 static long time_diff;   /* ¥×¥ì¥¤Â¦¤È¤Î»þ´Ö¤Î¤º¤ì(¤³¤ì¤ò¸«¤Ê¤¬¤é¥Ç¥£¥ì¥¤¤òÄ´À°¤·¤Æ¤¤¤¯) */
 static int browse_delay; /* É½¼¨¤¹¤ë¤Þ¤Ç¤Î»þ´Ö(100msñ°Ì)(¤³¤Î´Ö¤Ë¥é¥°¤òµÛ¼ý¤¹¤ë) */
 static int server_port;
@@ -91,7 +92,7 @@ static void disable_chuukei_server(void)
 {
        term *t = angband_term[0];
 
-        if (!chuukei_server) return;
+       if (!chuukei_server) return;
 
        chuukei_server = FALSE;
 
@@ -119,7 +120,7 @@ static errr init_chuukei(void)
 static long get_current_time(void)
 {
 #ifdef WINDOWS
-       return GetTickCount() / 100;
+       return timeGetTime() / 100;
 #elif defined(MACINTOSH)
        return TickCount();
 #else
@@ -143,7 +144,7 @@ static errr insert_ringbuf(char *buf)
                if (chuukei_server) disable_chuukei_server();
                else chuukei_client = FALSE;
 
-               prt("¥Ð¥Ã¥Õ¥¡¤¬°î¤ì¤Þ¤·¤¿¡£¥µ¡¼¥Ð¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤¹¡£", 0, 0);
+               prt("Á÷¼õ¿®¥Ð¥Ã¥Õ¥¡¤¬°î¤ì¤Þ¤·¤¿¡£¥µ¡¼¥Ð¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤¹¡£", 0, 0);
                inkey();
 
                close(sd);
@@ -179,7 +180,6 @@ void flush_ringbuf(void)
 #ifndef MACINTOSH
        fd_set fdset;
        struct timeval tv;
-       int writen = 0;
 
        if (!chuukei_server) return;
 
@@ -220,29 +220,19 @@ void flush_ringbuf(void)
 
                ring.rptr += result;
                ring.inlen -= result;
-               writen += result;
 
                if (ring.rptr == RINGBUF_SIZE) ring.rptr = 0;
                if (ring.inlen == 0) break;
        }
 #else
-       struct timeval tv;
-       int writen = 0;
-
        if (!chuukei_server) return;
 
        if (ring.inlen == 0) return;
 
-       tv.tv_sec = 0;
-       tv.tv_usec = 0;
-
        while (1)
        {
-               struct timeval tmp_tv;
                int result;
 
-               tmp_tv = tv;
-
                /* ¥½¥±¥Ã¥È¤Ë¥Ç¡¼¥¿¤ò½ñ¤­¹þ¤á¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë */
                result = OTSnd(ep, ring.buf + ring.rptr, ((ring.wptr > ring.rptr ) ? ring.wptr : RINGBUF_SIZE) - ring.rptr, 0);
 
@@ -260,7 +250,6 @@ void flush_ringbuf(void)
 
                ring.rptr += result;
                ring.inlen -= result;
-               writen += result;
 
                if (ring.rptr == RINGBUF_SIZE) ring.rptr = 0;
                if (ring.inlen == 0) break;
@@ -381,8 +370,6 @@ int connect_chuukei_server(char *prf_name)
                return (-1);
        }
 
-       epoch_time = get_current_time();
-
        return (0);
 #else  /* MACINTOSH */
        OSStatus err;
@@ -442,11 +429,11 @@ int connect_chuukei_server(char *prf_name)
                        
                        sndCall.addr.len        = sizeof(InetAddress);                          
                        sndCall.addr.buf        = (unsigned char*) &inAddr;
-                       sndCall.opt.buf         = nil;        /* no connection options */
+                       sndCall.opt.buf         = nil;          /* no connection options */
                        sndCall.opt.len         = 0;
-                       sndCall.udata.buf       = nil;        /* no connection data */
+                       sndCall.udata.buf       = nil;          /* no connection data */
                        sndCall.udata.len       = 0;
-                       sndCall.sequence        = 0;          /* ignored by OTConnect */
+                       sndCall.sequence        = 0;            /* ignored by OTConnect */
                        
                        err = OTConnect(ep, &sndCall, NULL);
                        
@@ -551,8 +538,6 @@ static errr send_xtra_to_chuukei_server(int n, int v)
 {
        char buf[1024];
 
-       (void)v;
-
        if (n == TERM_XTRA_CLEAR || n == TERM_XTRA_FRESH || n == TERM_XTRA_SHAPE)
        {
                sprintf(buf, "x%c", n+1);
@@ -618,65 +603,93 @@ void prepare_chuukei_hooks(void)
 }
 
 
+static int handle_timestamp_data(int timestamp)
+{
+       long current_time = get_current_time();
+
+       /* ÉÁ²è¥­¥å¡¼¤Ï¶õ¤«¤É¤¦¤«¡© */
+       if (fresh_queue.tail == fresh_queue.next)
+       {
+               /* ¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤·»Ï¤á¤Î»þ´Ö¤òÊݸ¤·¤Æ¤ª¤¯ */
+               epoch_time = current_time;
+               epoch_time += browse_delay;
+               epoch_time -= timestamp;
+               time_diff = current_time - timestamp;
+       }
+
+       /* ÉÁ²è¥­¥å¡¼¤ËÊݸ¤·¡¢Êݸ°ÌÃÖ¤ò¿Ê¤á¤ë */
+       fresh_queue.time[fresh_queue.tail] = timestamp;
+       fresh_queue.tail ++;
+
+       /* ¥­¥å¡¼¤ÎºÇ¸åÈø¤ËÅþ㤷¤¿¤éÀèƬ¤ËÌ᤹ */
+       fresh_queue.tail %= FRESH_QUEUE_SIZE;
+
+       if (fresh_queue.tail == fresh_queue.next)
+       {
+               /* ÉÁ²è¥­¥å¡¼°î¤ì */
+               prt("ÉÁ²è¥¿¥¤¥ß¥ó¥°¥­¥å¡¼¤¬°î¤ì¤Þ¤·¤¿¡£¥µ¡¼¥Ð¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤¹¡£", 0, 0);
+               inkey();
+               close(sd);
+
+               return -1;
+       }
+
+       /* ¥×¥ì¥¤Â¦¤È¤Î¥Ç¥£¥ì¥¤¤òÄ´À° */
+       if (time_diff != current_time - timestamp)
+       {
+               long old_time_diff = time_diff;
+               time_diff = current_time - timestamp;
+               epoch_time -= (old_time_diff - time_diff);
+       }
+
+       /* Success */
+       return 0;
+}
+
 static int read_sock(void)
 {
-       char buf[1024];
+       static char recv_buf[RECVBUF_SIZE];
+       static int remain_bytes = 0;
+       int recv_bytes;
        int i;
 
-       for (i = 0;; i++)
-       {
-               if (recv(sd, buf+i, 1, 0) <= 0) 
-                       return -1;
+       /* Á°²ó»Ä¤Ã¤¿¥Ç¡¼¥¿¤Î¸å¤Ë¤Ä¤Å¤±¤ÆÇÛ¿®¥µ¡¼¥Ð¤«¤é¥Ç¡¼¥¿¼õ¿® */
+       recv_bytes = recv(sd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes, 0);
+       if (recv_bytes <= 0)
+               return -1;
 
-               if (buf[i] == '\0')
-               {
-                       if (buf[0] == 'd')
-                       {
-                               int timestamp = atoi(buf + 1);
-                               long current_time = get_current_time();
-
-                               /* ºÇ½é¤Î»þ´Ö¤òÊݸ¤·¤Æ¤ª¤¯ */
-                               if (!fresh_queue.time[0])
-                               {
-                                       epoch_time = current_time;
-                                       epoch_time += browse_delay;
-                                       epoch_time -= timestamp;
-                                       time_diff = current_time - timestamp;
-                               }
-
-                               fresh_queue.time[fresh_queue.tail] = timestamp;
-                               fresh_queue.tail ++;
-
-                               if (fresh_queue.tail == FRESH_QUEUE_SIZE)
-                                       fresh_queue.tail = 0;
-
-                               /* ¥×¥ì¥¤Â¦¤È¤Î¥Ç¥£¥ì¥¤¤òÄ´À° */
-                               if (time_diff > current_time - timestamp)
-                               {
-                                       long old_time_diff = time_diff;
-                                       time_diff = current_time - timestamp;
-                                       epoch_time -= (old_time_diff - time_diff);
-                               }
-
-                               if (fresh_queue.tail == fresh_queue.next)
-                               {
-                                       /* queue°î¤ì */
-                                       close(sd);
-                                       exit(1);
-                               }
+       /* Á°²ó»Ä¤Ã¤¿¥Ç¡¼¥¿Î̤˺£²óÆɤó¤À¥Ç¡¼¥¿Î̤òÄɲà*/
+       remain_bytes += recv_bytes;
 
-                       }
+       for (i = 0; i < remain_bytes; i ++)
+       {
+               /* ¥Ç¡¼¥¿¤Î¤¯¤®¤ê('\0')¤òõ¤¹ */
+               if (recv_buf[i] == '\0')
+               {
+                       /* 'd'¤Ç»Ï¤Þ¤ë¥Ç¡¼¥¿(¥¿¥¤¥à¥¹¥¿¥ó¥×)¤Î¾ì¹ç¤Ï
+                          ÉÁ²è¥­¥å¡¼¤ËÊݸ¤¹¤ë½èÍý¤ò¸Æ¤Ö */
+                       if ((recv_buf[0] == 'd') &&
+                           (handle_timestamp_data(atoi(recv_buf + 1)) < 0))
+                               return -1;
 
-                       if (insert_ringbuf(buf) < 0) 
+                       /* ¼õ¿®¥Ç¡¼¥¿¤òÊݸ */
+                       if (insert_ringbuf(recv_buf) < 0) 
                                return -1;
-                       return (i);
+
+                       /* ¼¡¤Î¥Ç¡¼¥¿°Ü¹Ô¤òrecv_buf¤ÎÀèƬ¤Ë°ÜÆ° */
+                       memmove(recv_buf, recv_buf + i + 1, remain_bytes - i - 1);
+
+                       remain_bytes -= (i+1);
+                       i = 0;
                }
        }
+
+       return 0;
 }
 
 
 #ifndef WINDOWS
-/* WinÈǤÎÃæÅÀ¤ÈÊɤÎƦÉå¤ò¥Ô¥ê¥ª¥É¤È¥·¥ã¡¼¥×¤Ë¤¹¤ë¡£ */
+/* WinÈǤÎÃæÅÀ¤ÈÊɤÎƦÉå¤ò¥Ô¥ê¥ª¥É¤È¥·¥ã¡¼¥×¤Ë¤¹¤ë¡£*/
 static void win2unix(int col, char *buf)
 {
        char kabe;
@@ -847,27 +860,19 @@ void browse_chuukei()
                if (!chuukei_client && fresh_queue.next == fresh_queue.tail ) break;
        }
 #else
-       struct timeval tv;
-
-       tv.tv_sec = 0;
-       tv.tv_usec = WAIT;
-
        Term_clear();
        Term_fresh();
        Term_xtra(TERM_XTRA_REACT, 0);
 
        while (1)
        {
-               struct timeval tmp_tv;
                UInt32  unreadData = 0;
                int n;
-               
-               if (flush_ringbuf_client()) continue;
 
-               tmp_tv = tv;
+               if (flush_ringbuf_client()) continue;
 
                /* ¥½¥±¥Ã¥È¤Ë¥Ç¡¼¥¿¤¬Íè¤Æ¤¤¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë */
-               
+
                OTCountDataBytes(ep, &unreadData);
                if(unreadData <= 0 ){
                        Term_xtra(TERM_XTRA_FLUSH, 0);