6 2014 Deskull rearranged comment for Doxygen.
25 #elif defined(MACINTOSH)
26 #include <OpenTransport.h>
27 #include <OpenTptInternet.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
34 #include <arpa/inet.h>
40 #define MAX_HOSTNAME 256
43 #define RINGBUF_SIZE 1024*1024
44 #define FRESH_QUEUE_SIZE 4096
48 #define WAIT 100*1000 /* ブラウズ側のウエイト(us単位) */
50 #define DEFAULT_DELAY 50
51 #define RECVBUF_SIZE 1024
53 static long epoch_time; /* バッファ開始時刻 */
54 static int browse_delay; /* 表示するまでの時間(100ms単位)(この間にラグを吸収する) */
56 static int sd; /* ソケットのファイルディスクリプタ */
57 static long time_diff; /* プレイ側との時間のずれ(これを見ながらディレイを調整していく) */
58 static int server_port;
59 static GAME_TEXT server_name[MAX_HOSTNAME];
63 static int movie_mode;
67 #define close closesocket
71 static InetSvcRef inet_services = nil;
72 static EndpointRef ep = kOTInvalidEndpointRef;
75 /* 描画する時刻を覚えておくキュー構造体 */
78 int time[FRESH_QUEUE_SIZE];
94 int recv(int s, char *buffer, size_t buflen, int flags)
97 int n = OTRcv(ep, (void *) buffer, buflen, &junkFlags);
107 static errr (*old_xtra_hook)(int n, int v);
108 static errr (*old_curs_hook)(int x, int y);
109 static errr (*old_bigcurs_hook)(int x, int y);
110 static errr (*old_wipe_hook)(int x, int y, int n);
111 static errr (*old_text_hook)(int x, int y, int n, TERM_COLOR a, concptr s);
113 static void disable_chuukei_server(void)
115 term *t = angband_term[0];
117 chuukei_server = FALSE;
119 t->xtra_hook = old_xtra_hook;
120 t->curs_hook = old_curs_hook;
121 t->bigcurs_hook = old_bigcurs_hook;
122 t->wipe_hook = old_wipe_hook;
123 t->text_hook = old_text_hook;
126 /* ANSI Cによればstatic変数は0で初期化されるが一応初期化する */
127 static errr init_buffer(void)
129 fresh_queue.next = fresh_queue.tail = 0;
130 ring.wptr = ring.rptr = ring.inlen = 0;
131 fresh_queue.time[0] = 0;
132 ring.buf = malloc(RINGBUF_SIZE);
133 if (ring.buf == NULL) return (-1);
138 /* 現在の時間を100ms単位で取得する */
139 static long get_current_time(void)
142 return timeGetTime() / 100;
143 #elif defined(MACINTOSH)
147 gettimeofday(&tv, NULL);
149 return (tv.tv_sec * 10 + tv.tv_usec / 100000);
154 /* リングバッファ構造体に buf の内容を加える */
155 static errr insert_ringbuf(char *buf)
158 len = strlen(buf) + 1; /* +1は終端文字分 */
162 fd_write(movie_fd, buf, len);
164 if (!chuukei_server) return 0;
171 if (ring.inlen + len >= RINGBUF_SIZE)
174 if (chuukei_server) disable_chuukei_server();
175 else chuukei_client = FALSE;
177 prt("送受信バッファが溢れました。サーバとの接続を切断します。", 0, 0);
186 if (ring.wptr + len < RINGBUF_SIZE)
188 memcpy(ring.buf + ring.wptr, buf, len);
191 /* バッファの終端までに収まらない(ピッタリ収まる場合も含む) */
194 int head = RINGBUF_SIZE - ring.wptr; /* 前半 */
195 int tail = len - head; /* 後半 */
197 memcpy(ring.buf + ring.wptr, buf, head);
198 memcpy(ring.buf, buf + head, tail);
209 void flush_ringbuf(void)
215 if (!chuukei_server) return;
217 if (ring.inlen == 0) return;
232 /* ソケットにデータを書き込めるかどうか調べる */
233 select(sd+1, (fd_set *)NULL, &tmp_fdset, (fd_set *)NULL, &tv);
236 if (FD_ISSET(sd, &tmp_fdset) == 0) break;
238 result = send(sd, ring.buf + ring.rptr, ((ring.wptr > ring.rptr ) ? ring.wptr : RINGBUF_SIZE) - ring.rptr, 0);
243 if (chuukei_server) disable_chuukei_server();
245 prt("サーバとの接続が切断されました。", 0, 0);
253 ring.inlen -= result;
255 if (ring.rptr == RINGBUF_SIZE) ring.rptr = 0;
256 if (ring.inlen == 0) break;
259 if (!chuukei_server) return;
261 if (ring.inlen == 0) return;
267 /* ソケットにデータを書き込めるかどうか調べる */
268 result = OTSnd(ep, ring.buf + ring.rptr, ((ring.wptr > ring.rptr ) ? ring.wptr : RINGBUF_SIZE) - ring.rptr, 0);
273 if (chuukei_server) disable_chuukei_server();
275 prt("サーバとの接続が切断されました。", 0, 0);
283 ring.inlen -= result;
285 if (ring.rptr == RINGBUF_SIZE) ring.rptr = 0;
286 if (ring.inlen == 0) break;
292 static int read_chuukei_prf(concptr prf_name)
297 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA, prf_name);
298 fp = my_fopen(buf, "r");
300 if (!fp) return (-1);
305 browse_delay = DEFAULT_DELAY;
307 while (0 == my_fgets(fp, buf, sizeof(buf)))
310 if (!strncmp(buf, "server:", 7))
312 strncpy(server_name, buf + 7, MAX_HOSTNAME - 1);
313 server_name[MAX_HOSTNAME - 1] = '\0';
317 if (!strncmp(buf, "port:", 5))
319 server_port = atoi(buf + 5);
323 if (!strncmp(buf, "delay:", 6))
325 browse_delay = atoi(buf + 6);
332 if (server_port == -1 || server_name[0] == 0) return (-1);
337 int connect_chuukei_server(char *prf_name)
343 WORD wVersionRequested = (WORD) (( 1) | ( 1 << 8));
346 struct sockaddr_in ask;
349 if (read_chuukei_prf(prf_name) < 0)
351 printf("Wrong prf file\n");
355 if (init_buffer() < 0)
357 printf("Malloc error\n");
362 if (WSAStartup(wVersionRequested, &wsaData))
364 msg_print("Report: WSAStartup failed.");
369 printf("server = %s\nport = %d\n", server_name, server_port);
371 if ((hp = gethostbyname(server_name)) != NULL)
373 memset(&ask, 0, sizeof(ask));
374 memcpy(&ask.sin_addr, hp->h_addr_list[0], hp->h_length);
378 if ((ask.sin_addr.s_addr=inet_addr(server_name)) == 0)
380 printf("Bad hostname\n");
385 ask.sin_family = AF_INET;
386 ask.sin_port = htons((unsigned short)server_port);
389 if ((sd=socket(PF_INET,SOCK_STREAM, 0)) < 0)
391 if ((sd=socket(PF_INET,SOCK_STREAM, 0)) == INVALID_SOCKET)
394 printf("Can't create socket\n");
398 if (connect(sd, (struct sockaddr *)&ask, sizeof(ask)) < 0)
401 printf("Can't connect %s port %d\n", server_name, server_port);
406 #else /* MACINTOSH */
408 InetHostInfo response;
412 Boolean bind = false;
415 if (read_chuukei_prf(prf_name) < 0){
416 printf("Wrong prf file\n");
422 printf("server = %s\nport = %d\n", server_name, server_port);
425 #if TARGET_API_MAC_CARBON
426 err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
428 err = InitOpenTransport();
431 memset(&response, 0, sizeof(response));
434 #if TARGET_API_MAC_CARBON
435 inet_services = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, &err, NULL);
437 inet_services = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err);
441 err = OTInetStringToAddress(inet_services, (char *)server_name, &response);
444 host_addr = response.addrs[0];
446 printf("Bad hostname\n");
449 #if TARGET_API_MAC_CARBON
450 ep = (void *)OTOpenEndpointInContext(OTCreateConfiguration(kTCPName), 0, nil, &err, NULL);
452 ep = (void *)OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, nil, &err);
456 err = OTBind(ep, nil, nil);
457 bind = (err == noErr);
460 OTInitInetAddress(&inAddr, server_port, host_addr);
462 sndCall.addr.len = sizeof(InetAddress);
463 sndCall.addr.buf = (unsigned char*) &inAddr;
464 sndCall.opt.buf = nil; /* no connection options */
466 sndCall.udata.buf = nil; /* no connection data */
467 sndCall.udata.len = 0;
468 sndCall.sequence = 0; /* ignored by OTConnect */
470 err = OTConnect(ep, &sndCall, NULL);
473 printf("Can't connect %s port %d\n", server_name, server_port);
477 err = OTSetSynchronous(ep);
479 err = OTSetBlocking(ep);
488 if (ep != kOTInvalidEndpointRef) {
492 if (inet_services != nil) {
493 OTCloseProvider(inet_services);
506 /* strが同じ文字の繰り返しかどうか調べる */
507 static bool string_is_repeat(char *str, int len)
512 if (len < 2) return (FALSE);
514 if (iskanji(c)) return (FALSE);
517 for (i = 1; i < len; i++)
520 if(c != str[i] || iskanji(str[i])) return (FALSE);
522 if(c != str[i]) return (FALSE);
529 static errr send_text_to_chuukei_server(TERM_LEN x, TERM_LEN y, int len, TERM_COLOR col, concptr str)
534 strncpy(buf2, str, len);
539 sprintf(buf, "s%c%c%c%c", x+1, y+1, col, buf2[0]);
541 else if(string_is_repeat(buf2, len))
544 for (i = len; i > 0; i -= 127)
546 sprintf(buf, "n%c%c%c%c%c", x+1, y+1, MIN(i, 127), col, buf2[0]);
551 #if defined(SJIS) && defined(JP)
554 sprintf(buf, "t%c%c%c%c%s", x+1, y+1, len, col, buf2);
559 return (*old_text_hook)(x, y, len, col, str);
562 static errr send_wipe_to_chuukei_server(int x, int y, int len)
566 sprintf(buf, "w%c%c%c", x+1, y+1, len);
570 return (*old_wipe_hook)(x, y, len);
573 static errr send_xtra_to_chuukei_server(int n, int v)
577 if (n == TERM_XTRA_CLEAR || n == TERM_XTRA_FRESH || n == TERM_XTRA_SHAPE)
579 sprintf(buf, "x%c", n+1);
583 if (n == TERM_XTRA_FRESH)
585 sprintf(buf, "d%ld", get_current_time() - epoch_time);
590 /* Verify the hook */
591 if (!old_xtra_hook) return -1;
593 return (*old_xtra_hook)(n, v);
596 static errr send_curs_to_chuukei_server(int x, int y)
600 sprintf(buf, "c%c%c", x+1, y+1);
604 return (*old_curs_hook)(x, y);
607 static errr send_bigcurs_to_chuukei_server(int x, int y)
611 sprintf(buf, "C%c%c", x+1, y+1);
615 return (*old_bigcurs_hook)(x, y);
620 * Prepare z-term hooks to call send_*_to_chuukei_server()'s
622 void prepare_chuukei_hooks(void)
624 term *t0 = angband_term[0];
626 /* Save original z-term hooks */
627 old_xtra_hook = t0->xtra_hook;
628 old_curs_hook = t0->curs_hook;
629 old_bigcurs_hook = t0->bigcurs_hook;
630 old_wipe_hook = t0->wipe_hook;
631 old_text_hook = t0->text_hook;
633 /* Prepare z-term hooks */
634 t0->xtra_hook = send_xtra_to_chuukei_server;
635 t0->curs_hook = send_curs_to_chuukei_server;
636 t0->bigcurs_hook = send_bigcurs_to_chuukei_server;
637 t0->wipe_hook = send_wipe_to_chuukei_server;
638 t0->text_hook = send_text_to_chuukei_server;
643 * Prepare z-term hooks to call send_*_to_chuukei_server()'s
645 void prepare_movie_hooks(void)
654 if (!chuukei_server) disable_chuukei_server();
656 disable_chuukei_server();
659 msg_print(_("録画を終了しました。", "Stopped recording."));
663 sprintf(tmp, "%s.amv", player_base);
664 if (get_string(_("ムービー記録ファイル: ", "Movie file name: "), tmp, 80))
668 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp);
670 fd = fd_open(buf, O_RDONLY);
679 (void)sprintf(out_val, _("現存するファイルに上書きしますか? (%s)", "Replace existing file %s? "), buf);
682 if (!get_check(out_val)) return;
684 movie_fd = fd_open(buf, O_WRONLY | O_TRUNC);
688 movie_fd = fd_make(buf, 0644);
693 msg_print(_("ファイルを開けません!", "Can not open file."));
699 if (!chuukei_server) prepare_chuukei_hooks();
701 prepare_chuukei_hooks();
709 static int handle_timestamp_data(int timestamp)
711 long current_time = get_current_time();
714 if (fresh_queue.tail == fresh_queue.next)
716 /* バッファリングし始めの時間を保存しておく */
717 epoch_time = current_time;
718 epoch_time += browse_delay;
719 epoch_time -= timestamp;
720 time_diff = current_time - timestamp;
723 /* 描画キューに保存し、保存位置を進める */
724 fresh_queue.time[fresh_queue.tail] = timestamp;
727 /* キューの最後尾に到達したら先頭に戻す */
728 fresh_queue.tail %= FRESH_QUEUE_SIZE;
730 if (fresh_queue.tail == fresh_queue.next)
733 prt("描画タイミングキューが溢れました。サーバとの接続を切断します。", 0, 0);
741 if (time_diff != current_time - timestamp)
743 long old_time_diff = time_diff;
744 time_diff = current_time - timestamp;
745 epoch_time -= (old_time_diff - time_diff);
753 static int handle_movie_timestamp_data(int timestamp)
755 static int initialized = FALSE;
760 /* バッファリングし始めの時間を保存しておく */
761 epoch_time = get_current_time();
762 epoch_time += browse_delay;
763 epoch_time -= timestamp;
764 //time_diff = current_time - timestamp;
768 /* 描画キューに保存し、保存位置を進める */
769 fresh_queue.time[fresh_queue.tail] = timestamp;
772 /* キューの最後尾に到達したら先頭に戻す */
773 fresh_queue.tail %= FRESH_QUEUE_SIZE;
780 static int read_sock(void)
782 static char recv_buf[RECVBUF_SIZE];
783 static int remain_bytes = 0;
787 /* 前回残ったデータの後につづけて配信サーバからデータ受信 */
788 recv_bytes = recv(sd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes, 0);
793 /* 前回残ったデータ量に今回読んだデータ量を追加 */
794 remain_bytes += recv_bytes;
796 for (i = 0; i < remain_bytes; i ++)
798 /* データのくぎり('\0')を探す */
799 if (recv_buf[i] == '\0')
801 /* 'd'で始まるデータ(タイムスタンプ)の場合は
803 if ((recv_buf[0] == 'd') &&
804 (handle_timestamp_data(atoi(recv_buf + 1)) < 0))
808 if (insert_ringbuf(recv_buf) < 0)
811 /* 次のデータ移行をrecv_bufの先頭に移動 */
812 memmove(recv_buf, recv_buf + i + 1, remain_bytes - i - 1);
814 remain_bytes -= (i+1);
823 static int read_movie_file(void)
825 static char recv_buf[RECVBUF_SIZE];
826 static int remain_bytes = 0;
830 recv_bytes = read(movie_fd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes);
835 /* 前回残ったデータ量に今回読んだデータ量を追加 */
836 remain_bytes += recv_bytes;
838 for (i = 0; i < remain_bytes; i ++)
840 /* データのくぎり('\0')を探す */
841 if (recv_buf[i] == '\0')
843 /* 'd'で始まるデータ(タイムスタンプ)の場合は
845 if ((recv_buf[0] == 'd') &&
846 (handle_movie_timestamp_data(atoi(recv_buf + 1)) < 0))
850 if (insert_ringbuf(recv_buf) < 0)
853 /* 次のデータ移行をrecv_bufの先頭に移動 */
854 memmove(recv_buf, recv_buf + i + 1, remain_bytes - i - 1);
856 remain_bytes -= (i+1);
866 /* Win版の床の中点と壁の豆腐をピリオドとシャープにする。*/
867 static void win2unix(int col, char *buf)
870 if ( col == 9 ) kabe = '%';
882 if (*buf == 127) *buf = kabe;
883 else if(*buf == 31) *buf = '.';
889 static bool get_nextbuf(char *buf)
895 *ptr = ring.buf[ring.rptr ++];
897 if (ring.rptr == RINGBUF_SIZE) ring.rptr = 0;
898 if (*ptr++ == '\0') break;
901 if (buf[0] == 'd') return (FALSE);
906 /* プレイホストのマップが大きいときクライアントのマップもリサイズする */
907 static void update_term_size(int x, int y, int len)
911 Term_get_size(&ox, &oy);
916 if (x + len > ox) nx = x + len;
918 if (y + 1 > oy) ny = y + 1;
920 if (nx != ox || ny != oy) Term_resize(nx, ny);
923 static bool flush_ringbuf_client(void)
928 if (fresh_queue.next == fresh_queue.tail) return (FALSE);
931 if (fresh_queue.time[fresh_queue.next] > get_current_time() - epoch_time) return (FALSE);
933 /* 時間情報(区切り)が得られるまで書く */
934 while (get_nextbuf(buf))
940 unsigned char tmp1, tmp2, tmp3, tmp4;
943 sscanf(buf, "%c%c%c%c%c", &id, &tmp1, &tmp2, &tmp3, &tmp4);
944 x = tmp1-1; y = tmp2-1; len = tmp3; col = tmp4;
958 #if defined(SJIS) && defined(JP)
961 update_term_size(x, y, len);
962 (void)((*angband_term[0]->text_hook)(x, y, len, (byte)col, mesg));
963 strncpy(&Term->scr->c[y][x], mesg, len);
964 for (i = x; i < x+len; i++)
966 Term->scr->a[y][i] = col;
971 for (i = 1; i < len; i++)
976 update_term_size(x, y, len);
977 (void)((*angband_term[0]->text_hook)(x, y, len, (byte)col, mesg));
978 strncpy(&Term->scr->c[y][x], mesg, len);
979 for (i = x; i < x+len; i++)
981 Term->scr->a[y][i] = col;
986 update_term_size(x, y, 1);
987 (void)((*angband_term[0]->text_hook)(x, y, 1, (byte)col, mesg));
988 strncpy(&Term->scr->c[y][x], mesg, 1);
989 Term->scr->a[y][x] = col;
993 update_term_size(x, y, len);
994 (void)((*angband_term[0]->wipe_hook)(x, y, len));
998 if (x == TERM_XTRA_CLEAR) Term_clear();
999 (void)((*angband_term[0]->xtra_hook)(x, 0));
1003 update_term_size(x, y, 1);
1004 (void)((*angband_term[0]->curs_hook)(x, y));
1007 update_term_size(x, y, 1);
1008 (void)((*angband_term[0]->bigcurs_hook)(x, y));
1014 if (fresh_queue.next == FRESH_QUEUE_SIZE) fresh_queue.next = 0;
1019 void browse_chuukei()
1033 Term_xtra(TERM_XTRA_REACT, 0);
1038 struct timeval tmp_tv;
1040 if (flush_ringbuf_client()) continue;
1045 /* ソケットにデータが来ているかどうか調べる */
1046 select(sd+1, &tmp_fdset, (fd_set *)NULL, (fd_set *)NULL, &tmp_tv);
1047 if (FD_ISSET(sd, &tmp_fdset) == 0)
1049 Term_xtra(TERM_XTRA_FLUSH, 0);
1053 if (read_sock() < 0)
1055 chuukei_client = FALSE;
1058 /* 接続が切れた状態で書くべきデータがなくなっていたら終了 */
1059 if (!chuukei_client && fresh_queue.next == fresh_queue.tail ) break;
1064 Term_xtra(TERM_XTRA_REACT, 0);
1068 UInt32 unreadData = 0;
1071 if (flush_ringbuf_client()) continue;
1073 /* ソケットにデータが来ているかどうか調べる */
1075 OTCountDataBytes(ep, &unreadData);
1076 if(unreadData <= 0 ){
1077 Term_xtra(TERM_XTRA_FLUSH, 0);
1080 if (read_sock() < 0)
1082 chuukei_client = FALSE;
1085 /* 接続が切れた状態で書くべきデータがなくなっていたら終了 */
1086 if (!chuukei_client && fresh_queue.next == fresh_queue.tail ) break;
1088 #endif /*MACINTOSH*/
1090 #endif /* CHUUKEI */
1092 void prepare_browse_movie_aux(concptr filename)
1094 movie_fd = fd_open(filename, O_RDONLY);
1096 browsing_movie = TRUE;
1101 void prepare_browse_movie(concptr filename)
1104 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
1106 prepare_browse_movie_aux(buf);
1109 void browse_movie(void)
1113 Term_xtra(TERM_XTRA_REACT, 0);
1115 while (read_movie_file() == 0)
1117 while (fresh_queue.next != fresh_queue.tail)
1119 if (!flush_ringbuf_client())
1121 Term_xtra(TERM_XTRA_FLUSH, 0);
1123 /* ソケットにデータが来ているかどうか調べる */