Mercurial > hg > RemoteEditor > vim7
changeset 27:a01d8d9b2a58 first-working-version
first working version.
author | kono |
---|---|
date | Mon, 27 Oct 2008 18:28:11 +0900 |
parents | 7101f8c1fa21 |
children | 24808249e776 |
files | src/REPNOTE.txt src/reditor.c src/reditor.h |
diffstat | 3 files changed, 162 insertions(+), 175 deletions(-) [+] |
line wrap: on
line diff
--- a/src/REPNOTE.txt Sun Oct 26 21:45:13 2008 +0900 +++ b/src/REPNOTE.txt Mon Oct 27 18:28:11 2008 +0900 @@ -1,8 +1,15 @@ +Mon Oct 27 15:53:19 JST 2008 + +直せば直すほど、動かなくなってくる。joinのsessionが変らしい。 + Sun Oct 26 21:45:49 JST 2008 なんか、でたらめな感じ... そもそも、コマンドがちゃんと 読めてない? +command を読み込む時に、 三回も malloc して、しかも、 +一回は、free してない。 + Sun Sep 7 20:18:14 JST 2008 ml_replace
--- a/src/reditor.c Sun Oct 26 21:45:13 2008 +0900 +++ b/src/reditor.c Mon Oct 27 18:28:11 2008 +0900 @@ -43,25 +43,23 @@ static void free_session_list(Session *head); static Session* set_cursession(Session *sn); static Session* find_session_by_buf(BUFTYPE *buf); -static Session* find_session_by_name(char *name); +// static Session* find_session_by_name(char *name); static char* get_fullpath(Session *session); static Session * get_waiting_session(int sid, char *text) ; static void set_waiting_session(Session *session); static int writen(int fd, char *textbuf, unsigned int len); -static char* make_packet(unsigned int cmd, - unsigned int sid, - unsigned int eid, - unsigned int seq, - unsigned int lnum, - char *text); static rep_cmd* make_cmd(unsigned int cmd, unsigned int sid, unsigned int eid, unsigned int seq, unsigned int lnum, + unsigned int length, char *text); +static void forwardCommand(rep_T *rep, rep_cmd *command); +static void set_cmd_seq( rep_cmd *command, unsigned int cmd, unsigned int seq); + static int free_cmd(rep_cmd *cmd); static int free_cmdlist(rep_cmdlist *cmdlist); static void add_cmd_to_list(rep_cmdlist *cmdlist, rep_cmd *cmd); @@ -69,14 +67,7 @@ static unsigned int get_header(char *buf,int offset); static int set_header(unsigned int num, char *pkt, int offset); -static int rep_exe_cmd(rep_cmd *command, unsigned int cmd, - unsigned int sid, - unsigned int eid, - unsigned int seq, - unsigned int lnum, - unsigned int textsize, - char *text); -static int rep_exe_pkt(rep_cmd *command, char *pkt); +static int rep_exe_cmd(rep_cmd *command); static int rep_exe_pktlist(rep_cmdlist *cmdlist); static int rep_recv_cmds(int fd, rep_cmdlist *txtcmdlist); @@ -267,9 +258,10 @@ static void puts_sys_err() { - char errmsg[50]; + char *err = strerror(errno); + char *errmsg = (char*)alloca(strlen(err)+50); - sprintf(errmsg, "rep>> %d:%s", errno, strerror(errno)); + sprintf(errmsg, "rep>> %d:%s", errno, err); e_msg_wrp(errmsg); return; } @@ -344,7 +336,7 @@ if (gethostname(g_rep.hostname, sizeof(g_rep.hostname)) < 0) { strncpy(g_rep.hostname, def_hostname, sizeof(def_hostname)+1); } - g_rep.nop = make_cmd(REPCMD_NOP, 0, 0, 0, 0, ""); + g_rep.nop = make_cmd(REPCMD_NOP, 0, 0, 0, 0, 0, ""); return TRUE; } @@ -560,6 +552,7 @@ return cursn; } +/* static Session* find_session_by_name(name) char *name; @@ -568,6 +561,7 @@ buf = find_buf_by_name_wrp(name); return find_session_by_buf(buf); } + */ static char* @@ -608,8 +602,6 @@ int sock; rep_T *rep; - rep_cmdlist cmdlist = {NULL,0}; - rep = get_rep(); if ((sock = rep_connect(NULL)) < 0) { return FALSE; @@ -631,10 +623,9 @@ set_waiting_session(session); - add_cmd_to_list(&cmdlist, make_cmd(cmd, 0, rep->eid, rep->seqno++, 0, "")); - rep_send_cmds(sock, &cmdlist); - - free_cmdlist(&cmdlist); + rep_cmd *command = make_cmd(cmd, 0, rep->eid, rep->seqno++, 0, 0, ""); + forwardCommand(rep, command); + free_cmd(command); return TRUE; @@ -656,7 +647,6 @@ { rep_T *rep = get_rep(); rep_cmd *cmd; - rep_cmdlist cmdlist = {NULL, 0}; Session *sn = rep->cursession; if (rep->smfd < 0) { /* session does not exist */ @@ -664,10 +654,9 @@ return FALSE; } - cmd = make_cmd(REPCMD_CLOSE, sn->sid, rep->eid, rep->seqno, 0, NULL); - add_cmd_to_list(&cmdlist, cmd); - rep_send_cmds(rep->smfd, &cmdlist); - free_cmdlist(&cmdlist); + cmd = make_cmd(REPCMD_CLOSE, sn->sid, rep->eid, rep->seqno, 0, 0, ""); + forwardCommand(rep,cmd); + free_cmd(cmd); return TRUE; } @@ -680,7 +669,7 @@ rep_free(obj) void *obj; { - if (obj) free(obj); + if (obj) vim_free(obj); return; } @@ -726,80 +715,67 @@ } -static char* -make_packet(cmd, sid, eid, seq, lnum, text) +static void +make_packet(command, cmd, sid, eid, seq, lnum, len, text) + rep_cmd *command; unsigned int cmd; unsigned int sid; unsigned int eid; unsigned int seq; unsigned int lnum; + unsigned int len; char * text; { - char *packet; - unsigned int len = 0; - - if (text) len = strlen(text); + char *packet = &command->pkt[0]; + set_header(cmd, packet, REP_CMD_OFFSET); + set_header(sid, packet, REP_SID_OFFSET); + set_header(eid, packet, REP_EID_OFFSET); + set_header(seq, packet, REP_SEQNUM_OFFSET); + set_header(lnum, packet, REP_LNUM_OFFSET); + set_header(len, packet, REP_T_SIZE_OFFSET); - if ((packet = (char *)malloc(REP_HEADER_SIZE+len)) == NULL) { - return(NULL); - } - - set_header(cmd, packet, REP_CMD_OFFSET); - set_header(sid, packet, REP_SID_OFFSET); - set_header(eid, packet, REP_EID_OFFSET); - set_header(seq, packet, REP_SEQNUM_OFFSET); - set_header(lnum, packet, REP_LNUM_OFFSET); - set_header(len, packet, REP_T_SIZE_OFFSET); - - if (text) { + if (text && len>0) { memcpy(packet+REP_TEXT_OFFSET, text, len); } - - return(packet); + return; } static rep_cmd* -make_cmd(cmd, sid, eid, seq, lnum, text) +make_cmd(cmd, sid, eid, seq, lnum, length, text) unsigned int cmd; unsigned int sid; unsigned int eid; unsigned int seq; unsigned int lnum; + unsigned int length; char *text; { rep_cmd *cmd_p; - char *pkt; - unsigned int length = 0; - - pkt = make_packet(cmd, sid, eid, seq, lnum, text); - if (pkt == NULL) { - e_msg_wrp("make_cmd: no memory: ERROR"); - return(NULL); - } - - cmd_p = (rep_cmd*)malloc(sizeof(rep_cmd)); - if (cmd_p == NULL) { - e_msg_wrp("make_cmd: no memory: ERROR"); - return(NULL); - } - - if (text) { - length = strlen(text); //+1; // include '\0' - } - + cmd_p = (rep_cmd*)malloc(sizeof(rep_cmd)+REP_HEADER_SIZE+length); + if (cmd_p == NULL) return(NULL); cmd_p->next = NULL; - cmd_p->cmd = cmd; cmd_p->sid = sid; cmd_p->eid = eid; cmd_p->seq = seq; cmd_p->len = length; cmd_p->lnum = lnum; - - cmd_p->pkt = pkt; + make_packet(cmd_p, cmd, sid, eid, seq, lnum, length, text); + return(cmd_p); +} - return(cmd_p); +static void +set_cmd_seq(cmd_p,cmd,seq) + rep_cmd *cmd_p; + unsigned int cmd; + unsigned int seq; +{ + char *packet = &cmd_p->pkt[0]; + cmd_p->seq = seq; + cmd_p->cmd = cmd; + set_header(cmd, packet, REP_CMD_OFFSET); + set_header(seq, packet, REP_SEQNUM_OFFSET); } static int @@ -807,13 +783,7 @@ rep_cmd *cmd; { if (cmd == NULL) return(FALSE); - - if (cmd->pkt) { - rep_free(cmd->pkt); - cmd->pkt=NULL; - } rep_free(cmd); - cmd = 0; return(TRUE); } @@ -894,14 +864,20 @@ cursn->prevline=lnum; if (xtr<0) { // ml_delete case - cmd = make_cmd(REPCMD_DELETE, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, get_memline_wrp(cursn->buf, lnum)); + char *text = get_memline_wrp(cursn->buf, lnum); + unsigned int length = strlen(text); + cmd = make_cmd(REPCMD_DELETE, cursn->sid, rep->eid, rep->seqno++, + cursn->prevline, length, text); rep_send_cmd(rep->smfd,cmd); rep_free(cmd); } else if (xtr==0) { // ml_replace case if (cursn->del_cmd) return; // already line saved do nothing - cursn->del_cmd = make_cmd(REPCMD_DELETE, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, get_memline_wrp(cursn->buf, lnum)); + char *text = get_memline_wrp(cursn->buf, lnum); + unsigned int length = strlen(text); + cursn->del_cmd = make_cmd(REPCMD_DELETE, cursn->sid, rep->eid, + rep->seqno++, cursn->prevline, length, text); } else { // ml_append case @@ -938,12 +914,15 @@ // append case for(i=lnum;i<lnume;i++) { + char *text = get_memline_wrp(cursn->buf, i+1); + unsigned int length = strlen(text); // make INSERT_CMD for insert or changed lines if any - cmd = make_cmd(REPCMD_INSERT, cursn->sid, rep->eid, rep->seqno++, i+1, get_memline_wrp(cursn->buf, i+1)); - add_cmd_to_list(&(cursn->new_cmdlist), cmd); + cmd = make_cmd(REPCMD_INSERT, cursn->sid, rep->eid, rep->seqno++, + i+1, length, text); + rep_send_cmd(rep->smfd,cmd); + free_cmd(cmd); } cursn->prevline=-1; - rep_send_cur_cmdlist(); } else if (xtra==0) { // replace case @@ -951,7 +930,7 @@ if (cursn->del_cmd!=0) { cmd = cursn->del_cmd; rep_send_cmd(rep->smfd,cmd); - rep_free(cmd); + rep_free(cmd); cmd=0; cursn->del_cmd = 0; } else { // first insert case? @@ -959,12 +938,15 @@ cursn->prevline = lnum; // send saved new line as INSERT command - cmd = make_cmd(REPCMD_INSERT, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, get_memline_wrp(cursn->buf, lnum)); + char *text = get_memline_wrp(cursn->buf, lnum); + unsigned int length = strlen(text); + cmd = make_cmd(REPCMD_INSERT, cursn->sid, rep->eid, rep->seqno++, + cursn->prevline, length,text); rep_send_cmd(rep->smfd,cmd); - rep_free(cmd); - + // reuse cmd // save current line for next replace - cursn->del_cmd = make_cmd(REPCMD_DELETE, cursn->sid, rep->eid, rep->seqno++, cursn->prevline, get_memline_wrp(cursn->buf, lnum)); + set_cmd_seq(cmd,REPCMD_DELETE,rep->seqno++); + cursn->del_cmd = cmd; } } @@ -1050,44 +1032,54 @@ } static int -rep_exe_cmd(command, cmd, sid, eid, seq, lnum, textsize, text) +rep_exe_cmd(command) rep_cmd *command; - unsigned int cmd; - unsigned int sid; - unsigned int eid; - unsigned int seq; - unsigned int lnum; - unsigned int textsize; - char *text; { + char *text = command->pkt+REP_TEXT_OFFSET; + int eid = command->eid; rep_T *rep = get_rep(); Session *session; - session = find_session_by_id(sid); + session = find_session_by_id(command->sid); - switch (cmd) { + switch (command->cmd) { case SMCMD_JOIN_ACK: - session = get_waiting_session(sid,text); + { + session = get_waiting_session(command->sid,text); set_cursession(session); rep_start_create_cmds(); - rep->eid = eid; + rep->eid = command->eid; + + char *msg = alloca(150); + sprintf(msg,"joined eid=%d sid=%d",rep->eid,session->sid); + e_msg_wrp(msg); + } + break; case SMCMD_PUT_ACK: + { /* Enter Session */ - session = get_waiting_session(sid,text); + session = get_waiting_session(command->sid,text); /* set session to cursession */ set_cursession(session); rep_start_create_cmds(); + rep->eid = command->eid; + + char *msg = alloca(150); + sprintf(msg,"put eid=%d sid=%d",rep->eid,session->sid); + e_msg_wrp(msg); + } + break; case REPCMD_INSERT: - append_memline_wrp(lnum, text); + append_memline_wrp(command->lnum, text); if (eid!=MERGE_EID) addNop(session, rep); forwardCommand(rep,command); if (eid!=MERGE_EID) update_screen_now_wrp(); break; case REPCMD_DELETE: - delete_memline_wrp(lnum); + delete_memline_wrp(command->lnum); if (eid!=MERGE_EID) update_screen_now_wrp(); case REPCMD_NOP: if (eid!=MERGE_EID) addNop(session, rep); @@ -1095,6 +1087,8 @@ break; case SMCMD_SYNC: rep->syncMode = 1; + set_cmd_seq(command,SMCMD_SYNC_ACK,rep->seqno++); + forwardCommand(rep,command); break; case SMCMD_QUIT: forwardCommand(rep,command); @@ -1107,35 +1101,10 @@ default: break; } - + free_cmd(command); return(TRUE); } -static int -rep_exe_pkt(command, pkt) - rep_cmd *command; - char *pkt; -{ - unsigned int cmd; - unsigned int sid; - unsigned int eid; - unsigned int seq; - unsigned int lnum; - unsigned int len; - char *text; - - cmd = get_header(pkt, REP_CMD_OFFSET); - sid = get_header(pkt, REP_SID_OFFSET); - eid = get_header(pkt, REP_EID_OFFSET); - seq = get_header(pkt, REP_SEQNUM_OFFSET); - lnum = get_header(pkt, REP_LNUM_OFFSET); - len = get_header(pkt, REP_T_SIZE_OFFSET); - text = pkt + REP_TEXT_OFFSET; - - rep_exe_cmd(command, cmd, sid, eid, seq, lnum, len, text); - - return(TRUE); -} /* execute command list based cmd packet */ static int @@ -1149,7 +1118,7 @@ } for (repcmd = cmdlist->head; repcmd; repcmd = repcmd->next) { - rep_exe_pkt(repcmd, repcmd->pkt); + rep_exe_cmd(repcmd); } return(TRUE); @@ -1168,14 +1137,18 @@ unsigned int seq; unsigned int lnum; unsigned int textsize; - - char header[REP_HEADER_SIZE]; - - char *text = NULL; + rep_cmd *cmd_p ; + char *text; + char *header; if (fd < 0) { return(FALSE); } + if ((cmd_p = (rep_cmd *)malloc(sizeof(rep_cmd)+128)) == NULL) + return FALSE; + + text = &cmd_p->pkt[REP_TEXT_OFFSET]; + header = &cmd_p->pkt[0]; /* read header part */ if (readn(fd, header, REP_HEADER_SIZE) < 0) { @@ -1189,21 +1162,27 @@ seq = get_header(header, REP_SEQNUM_OFFSET); lnum = get_header(header, REP_LNUM_OFFSET); textsize = get_header(header, REP_T_SIZE_OFFSET); + if ((cmd_p = (rep_cmd *)realloc(cmd_p,sizeof(rep_cmd)+textsize)) == NULL) + return FALSE; if (textsize > 0) { - if ((text = (char*)malloc(textsize)+1) == NULL) { - return(FALSE); - } /* read text part */ if (readn(fd, text, textsize) < 0) { puts_sys_err(); - rep_free(text); + rep_free(cmd_p); return(FALSE); } text[textsize] = 0; } + cmd_p->next = NULL; + cmd_p->cmd = cmd; + cmd_p->sid = sid; + cmd_p->eid = eid; + cmd_p->seq = seq; + cmd_p->len = textsize; + cmd_p->lnum = lnum; - add_cmd_to_list(txtcmdlist, make_cmd(cmd, sid, eid, seq, lnum, text)); + add_cmd_to_list(txtcmdlist, cmd_p); return(TRUE); } @@ -1256,7 +1235,6 @@ return(TRUE); } - int rep_fd_check(fd, rfds_p, efds_p) int fd; // input from keyboard or something... @@ -1273,7 +1251,6 @@ rep_p->smfd = -1; } else { rep_exe_pktlist(&txtcmdlist); - free_cmdlist(&(txtcmdlist)); } } @@ -1312,6 +1289,8 @@ rep_T *rep_p; rep_p = get_rep(); + // What should I do? + rep_end(); } @@ -1328,50 +1307,51 @@ int sk; int max_fds = MAX_FDS; - struct timeval tv; struct timeval zerotime; zerotime.tv_sec = 0; zerotime.tv_usec = 0; if (fd < 0) return FALSE; - /* select の中で modify されてるので、初期化 */ - tv.tv_sec = 0; - tv.tv_usec = 100000; - FD_ZERO(&rfds_p); - FD_ZERO(&efds_p); + for(;;) { + /* select の中で modify されてるので、初期化 */ + // struct timeval tv; + // tv.tv_sec = 0; + // tv.tv_usec = 100000; + FD_ZERO(&rfds_p); + FD_ZERO(&efds_p); - FD_SET(fd,&rfds_p); + FD_SET(fd,&rfds_p); - max_fds = rep_fd_set(&rfds_p, &efds_p, max_fds); + max_fds = rep_fd_set(&rfds_p, &efds_p, max_fds); - if ((sk = select(max_fds+1, &rfds_p, NULL, &efds_p, &tv)) < 0) { - if (errno == EBADF){ - int i; - for(i = 0;i < max_fds;i++){ - fd_set suspect; - FD_ZERO(&suspect); - if (FD_ISSET(i, &rfds_p)){ - FD_SET(i, &suspect); - if (select(max_fds, &suspect, NULL, NULL, &zerotime) == FAIL){ - FD_CLR(i, &rfds_p); - rep_close(i); - // we have to something to prevent to write to this - // port... - return(TRUE); + if ((sk = select(max_fds+1, &rfds_p, NULL, &efds_p, NULL)) < 0) { + if (errno == EBADF){ + int i; + for(i = 0;i < max_fds;i++){ + fd_set suspect; + FD_ZERO(&suspect); + if (FD_ISSET(i, &rfds_p)){ + FD_SET(i, &suspect); + if (select(max_fds, &suspect, NULL, NULL, &zerotime) == FAIL){ + FD_CLR(i, &rfds_p); + rep_close(i); + // we have to something to prevent to write to this + // port... + return(TRUE); + } + FD_CLR(i, &suspect); } - FD_CLR(i, &suspect); } + } else { + e_msg_wrp("rep_select(): ERROR"); + return(FALSE); } - } else { - e_msg_wrp("rep_select(): ERROR"); - return(FALSE); + } - + if ( rep_fd_check(fd, &rfds_p, &efds_p)) + return TRUE; // fd に入力があったので抜ける } - - if (rep_fd_check(fd, &rfds_p, &efds_p)) return TRUE; - return FALSE; } void @@ -1410,4 +1390,4 @@ fprintf(stderr,"\n"); } - +/* end */
--- a/src/reditor.h Sun Oct 26 21:45:13 2008 +0900 +++ b/src/reditor.h Mon Oct 27 18:28:11 2008 +0900 @@ -88,7 +88,7 @@ unsigned int seq; /* sequence number */ unsigned int len; /* length of text */ - char *pkt; /* packed rep command */ + char pkt[1]; /* packed rep command */ } rep_cmd; typedef struct rep_cmdlist {