[aquaskk-changes 140] CVS update: AquaSKK/net

Tomotaka SUWA t-suw****@users*****
2006年 2月 18日 (土) 02:22:43 JST


Index: AquaSKK/net/Socket.h
diff -u AquaSKK/net/Socket.h:1.2.2.1 AquaSKK/net/Socket.h:1.2
--- AquaSKK/net/Socket.h:1.2.2.1	Sat Jan 14 20:01:59 2006
+++ AquaSKK/net/Socket.h	Sat Oct  8 00:08:37 2005
@@ -1,24 +1,22 @@
 /*  -*- c++ -*-
-  $Id: Socket.h,v 1.2.2.1 2006/01/14 11:01:59 t-suwa Exp $
-
-  MacOS X implementation of the SKK input method.
-
-  Copyright (C) 2002-2004 phonohawk
-  Copyright (C) 2006 Tomotaka SUWA <t.suw****@mac*****>
-
-  This program is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
-  any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    $Id: Socket.h,v 1.2 2005/10/07 15:08:37 t-suwa Exp $
+	
+    MacOS X implementation of the SKK input method.
+    Copyright (C) 2002-2004 phonohawk
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
 #pragma once
@@ -28,38 +26,43 @@
 }
 
 class net::Socket {
+private:
     std::string host;
     unsigned int port;
+    
     bool listening;
+    
     int fd;
-    bool eof;
     std::string overread; // “ǂ݉߂¬‚½•¶Žš
-
+    
+    bool eof;
+    
     void connect();
     void begin_listening(bool reuse, int backlog);
-
+    
 public:
     // ƒŠƒXƒjƒ“ƒO‚ª–Ú“I‚Å‚ ‚ê‚΁Ahost‚Í‹ó‚Å‚à—Ç‚¢B
-    Socket(const std::string& host, unsigned int port, bool listen = false, bool reuse = false,
+    Socket(const std::string &host, unsigned int port,
+	   bool listen = false, bool reuse = false,
 	   int listen_queue_size = 5);
     Socket(int fd);
     virtual ~Socket();
-
+    
     bool hasGotEOF() const;
     void close();
-
+    
     virtual Socket& operator<< (const std::string& str);
     virtual Socket& operator<< (const char* c_str);
     virtual Socket& operator<< (long n);
     virtual Socket& flush();
-
+    
     virtual std::string readUntilCRLF();
     virtual std::string readline();
     virtual std::string readUntil(char ch);
     virtual int read();
-
+    
     virtual Socket* accept();
-    virtual int poll(bool* read, bool* write, bool* except, long timeout_ms) const;
+    virtual int poll(bool* read,bool* write,bool* except,long timeout_ms) const;
 
     virtual int getPort() const;
     virtual std::string getpeername() const;
Index: AquaSKK/net/Socket.cpp
diff -u AquaSKK/net/Socket.cpp:1.2.2.2 AquaSKK/net/Socket.cpp:1.2
--- AquaSKK/net/Socket.cpp:1.2.2.2	Sat Jan 14 20:01:59 2006
+++ AquaSKK/net/Socket.cpp	Sat Oct  8 00:08:37 2005
@@ -1,24 +1,22 @@
 /*  -*- c++ -*-
-  $Id: Socket.cpp,v 1.2.2.2 2006/01/14 11:01:59 t-suwa Exp $
-
-  MacOS X implementation of the SKK input method.
-
-  Copyright (C) 2002-2004 phonohawk
-  Copyright (C) 2006 Tomotaka SUWA <t.suw****@mac*****>
+    $Id: Socket.cpp,v 1.2 2005/10/07 15:08:37 t-suwa Exp $
+	
+    MacOS X implementation of the SKK input method.
+    Copyright (C) 2002-2004 phonohawk
 
-  This program is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
-  any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
 #include <sstream>
@@ -35,156 +33,171 @@
 using namespace net;
 using namespace std;
 
-Socket::Socket(const string& strHost, unsigned int uPort, bool listen, bool reuse, int listen_queue_size)
-    : host(strHost), port(uPort), listening(listen), fd(0), eof(false) {
-    if(listen) {
+Socket::Socket(
+    const string &host,
+    unsigned int port,
+    bool listen,
+    bool reuse,
+    int listen_queue_size)
+    : host(host),
+      port(port),
+      listening(listen),
+      fd(0),
+      eof(false) {
+    
+    if (listen) {
 	begin_listening(reuse, listen_queue_size);
-    } else {
+    }
+    else {
 	connect();
     }
 }
 
-Socket::Socket(int src) : host(), port(0), listening(false), fd(src), eof(false) {
+Socket::Socket(int fd)
+  : host(),port(0),listening(false),fd(fd),eof(false) {
   
 }
 
 Socket::~Socket(){
-    close();
+  close();
 }
 
 void Socket::begin_listening(bool reuse, int backlog) {
-    if(fd != 0) {
-	return;
-    }
-
-    if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-	throw string("net::Socket -  Couldn't create inet-socket.");
-    }
-
-    struct sockaddr_in addr;
-    addr.sin_family = AF_INET;
-    addr.sin_port = htons(port);
-    if(host.length() > 0) {
-	struct hostent *host_entry = gethostbyname(const_cast<char*>(host.c_str()));
-	if(host_entry == NULL) {
-	    ::close(fd);
+  if (fd != 0) {
+    return;
+  }
+
+  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+    throw string("net::Socket -  Couldn't create inet-socket.");
+  }
+
+  struct sockaddr_in addr;
+  addr.sin_family = AF_INET;
+  addr.sin_port = htons(port);
+  if (host.length() > 0) {
+    struct hostent *host_entry = gethostbyname(const_cast<char*>(host.c_str()));
+    if (host_entry == NULL) {
+      ::close(fd);
       
-	    ostringstream oss;
-	    oss << "net::Socket -  host " << host << " not found" << std::ends;
-	    throw string(oss.str());
-	}
-	bcopy(host_entry->h_addr,&addr.sin_addr,sizeof(struct in_addr));
-    } else {
-	addr.sin_addr.s_addr = INADDR_ANY;
-    }
-
-    if(reuse) {
-	int val = 1;
-	setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
+      ostringstream oss;
+      oss << "net::Socket -  host " << host << " not found" << std::ends;
+      throw string(oss.str());
     }
+    bcopy(host_entry->h_addr,&addr.sin_addr,sizeof(struct in_addr));
+  }
+  else {
+    addr.sin_addr.s_addr = INADDR_ANY;
+  }
+
+  if (reuse) {
+      int val = 1;
+      setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
+  }
   
-    if(bind(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) {
-	ostringstream oss;
-	oss << "net::Socket -  Couldn't bind to " << host << ':' << port << std::ends;
-	throw string(oss.str());
-    }
-
-    if(listen(fd,backlog) < 0) {
-	ostringstream oss;
-	oss << "net::Socket -  Couldn't listen to " << host << ':' << port << std::ends;
-	throw string(oss.str());
-    }
+  if (bind(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) {
+    ostringstream oss;
+    oss << "net::Socket -  Couldn't bind to " << host << ':' << port << std::ends;
+    throw string(oss.str());
+  }
+
+  if (listen(fd,backlog) < 0) {
+    ostringstream oss;
+    oss << "net::Socket -  Couldn't listen to " << host << ':' << port << std::ends;
+    throw string(oss.str());
+  }
 }
 
 void Socket::connect() { 
-    if(fd != 0)
-	return;
+  if (fd != 0)
+    return;
   
-    if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-	throw string("net::Socket -  Couldn't create inet-socket.");
-    }
+  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+    throw string("net::Socket -  Couldn't create inet-socket.");
+  }
   
-    struct hostent *host_entry = gethostbyname(const_cast<char*>(host.c_str()));
-    if(host_entry == NULL) {
-	::close(fd);
+  struct hostent *host_entry = gethostbyname(const_cast<char*>(host.c_str()));
+  if (host_entry == NULL) {
+    ::close(fd);
     
-	ostringstream oss;
-	oss << "net::Socket -  host " << host << " not found" << std::ends;
-	throw string(oss.str());
-    }
+    ostringstream oss;
+    oss << "net::Socket -  host " << host << " not found" << std::ends;
+    throw string(oss.str());
+  }
   
-    struct sockaddr_in addr;
-    bzero(&addr,sizeof(addr));
-    bcopy(host_entry->h_addr,&addr.sin_addr,sizeof(struct in_addr));
-    addr.sin_family = AF_INET;
-    addr.sin_port = htons(port);
-    if(::connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) {
-	::close(fd);
+  struct sockaddr_in addr;
+  bzero(&addr,sizeof(addr));
+  bcopy(host_entry->h_addr,&addr.sin_addr,sizeof(struct in_addr));
+  addr.sin_family = AF_INET;
+  addr.sin_port = htons(port);
+  if (::connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) {
+    ::close(fd);
     
-	ostringstream oss;
-	oss << "net::Socket -  Couldn't connect to " << host << ':' << port << std::ends;
-	throw string(oss.str());
-    }
+    ostringstream oss;
+    oss << "net::Socket -  Couldn't connect to " << host << ':' << port << std::ends;
+    throw string(oss.str());
+  }
 }
 
 bool Socket::hasGotEOF() const {
-    return eof;
+  return eof;
 }
 
 void Socket::close() {
-    ::close(fd);
+  ::close(fd);
 }
 
 Socket& Socket::operator<< (const string& str) {
-    write(fd, str.c_str(), str.length());
-    return *this;
+  write(fd,str.c_str(),str.length());
+  return *this;
 }
 
 Socket& Socket::operator<< (const char* c_str) {
-    write(fd, c_str, strlen(c_str));
-    return *this;
+  write(fd,c_str,strlen(c_str));
+  return *this;
 }
 
 Socket& Socket::operator<< (long n) {
-    static char buf[50];
-    sprintf(buf, "%ld", n);
+  static char buf[50];
+  sprintf(buf,"%ld",n);
   
-    write(fd, buf, strlen(buf));
-    return *this;
+  write(fd,buf,strlen(buf));
+  return *this;
 }
   
 Socket& Socket::flush() {
-    fsync(fd);
-    return *this;
+  fsync(fd);
+  return *this;
 }
 
 string Socket::readUntilCRLF() {
     // CRLF‚ªŒ»‚Í‚ê‚é‚Ü‚Å椂ށBCRLFŽ©é“‚Í“ü‚ç‚È‚¢B
-    if(eof) return string("");
+    if (eof) return string("");
     
     string buf;
-    while(true) {
+    while (true) {
 	int c = read();
-	if(c == -1) {
+	if (c == -1) {
 	    // EOF‚Ü‚½‚̓Gƒ‰[
 	    eof = true;
 	    break;
 	}
 	
-	if(c == 0x0d) { // CR
+	if (c == 0x0d) { // CR
 	    // ŽŸ‚Ì•¶Žš‚ð“ǂށB
 	    int next = read();
-	    if(next == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
+	    if (next == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
 		eof = true;
 		break;
 	    }
 	    
-	    if(next != 0x0a) { // LF‚Å‚È‚¢
+	    if (next != 0x0a) { // LF‚Å‚È‚¢
 		buf += next;
-	    } else {
+	    }
+	    else {
 		break; // LF‚Ȃ炱‚±‚ňêsI‚í‚è
 	    }
-	} else {
+	}
+	else {
 	    buf += c;
 	}
     }
@@ -195,31 +208,31 @@
 string Socket::readline() {
     // CR,LF,CRLF‚Ì‚¢‚¸‚ê‚à‰üs‚ÆŒ©˜ô‚·B
     // Œ‹‰Ê‚ɉüsƒR[ƒh‚Í“ü‚ç‚È‚¢B
-    if(eof) return string("");
-
+    if (eof) return string("");
+    
     string buf;
-    while(true) {
+    while (true) {
 	int c = read();
-	if(c == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
+	if (c == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
 	    eof = true;
 	    break;
 	}
 	
-	if(c == 0x0d) { // CR
+	if (c == 0x0d) { // CR
 	    // ŽŸ‚Ì•¶Žš‚ð“ǂށB
 	    int next = read();
-	    if(next == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
+	    if (next == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
 		eof = true;
 		break;
 	    }
 	    
-	    if(next != 0x0a) { // LF‚Å‚È‚¢
+	    if (next != 0x0a) { // LF‚Å‚È‚¢
 		// “ǂ݉߂¬‚½B
 		overread += next;
 	    }
 	    break; // ‚±‚±‚ňêsI‚í‚è
 	}
-	else if(c == 0x0a) { // LF
+	else if (c == 0x0a) { // LF
 	    break; // ‚±‚±‚ňêsI‚í‚è
 	}
 	else {
@@ -232,20 +245,21 @@
 
 string Socket::readUntil(char ch) {
     // ch‚ª˜Ò‚é‚Ü‚Å椂ށBŒ‹‰Ê‚Éch‚Í“ü‚ç‚È‚¢B    
-    if(eof) return string("");
+    if (eof) return string("");
     
     string buf;
-    while(true) {
+    while (true) {
 	int c = read();
-	if(c == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
+	if (c == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
 	    eof = true;
 	    break;
 	}
 
-	if(c == ch) {
+	if (c == ch) {
 	    // ‚±‚±‚ŏI‚Í‚è
 	    break;
-	} else {
+	}
+	else {
 	    buf += c;
 	}
     }
@@ -254,90 +268,94 @@
 }
 
 int Socket::read() {
-    if(overread.length() > 0) {
+    if (overread.length() > 0) {
 	unsigned char c = overread[0];
 	overread.erase(0,1);
 	return c;
     }
     
-    if(eof) return -1;
+    if (eof) return -1;
 
     unsigned char c;
     ssize_t bytes_read = ::read(fd,&c,1);
-    if(bytes_read == 0 || bytes_read == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
+    if (bytes_read == 0 || bytes_read == -1) { // EOF‚Ü‚½‚̓Gƒ‰[
 	eof = true;
 	return -1;
-    } else {
+    }
+    else {
 	return c;
     }
 }
 
 Socket* Socket::accept() {
-    int new_fd = ::accept(fd, NULL, NULL);
-    if(new_fd > 0) {
-	return new Socket(new_fd);
-    } else {
-	throw string("net::Socket - couldn't accept");
-    }
-}
-//#include <iostream>
-int Socket::poll(bool* readflg, bool* writeflg, bool* exceptflg, long timeout_ms) const {
-    /*
-      read:
-      植ž‚Ý‚ð‘҂‚©‚¾‚¤‚©B‚±‚ê‚ð”ñNULL‚É‚µ‚Ä›‰s‚·‚é‚ƁA植ž‚݉”\‚ɂȂ‚½‚Ì‚Å‚ ‚ê‚Î
-      ›‰sŒã‚ÍáÁ‚É‚È‚éB‚È‚ç‚È‚©‚‚½‚Ì‚Å‚ ‚ê‚΁A›‰sŒã‚Í™E‚É‚È‚éB
-      NULL‚Å‚ ‚‚Ăà—Ç‚­A‚»‚̏ꍇ‚̓`ƒFƒbƒN‚ðs‚Í‚È‚¢B
-
-      write:
-      except:
-      read‚Æ“¯žéB
-
-      timeout_ms:
-      ƒ^ƒCƒ€ƒAƒEƒgBšdˆÊ‚̓~ƒŠ•bB-1‚Å‚ ‚ê‚Ζ³§ŒÀB
-
-      –ß’l:
-      select‚Ì•Ô‚µ‚½’lB
-    */
-    int max_fd = -1;
+  int new_fd = ::accept(fd,NULL,NULL);
+  if (new_fd > 0) {
+    return new Socket(new_fd);
+  }
+  else {
+    throw string("net::Socket - couldn't accept");
+  }
+}
+#include <iostream>
+int Socket::poll(bool* read,bool* write,bool* except,long timeout_ms) const {
+  /*
+    read:
+    植ž‚Ý‚ð‘҂‚©‚¾‚¤‚©B‚±‚ê‚ð”ñNULL‚É‚µ‚Ä›‰s‚·‚é‚ƁA植ž‚݉”\‚ɂȂ‚½‚Ì‚Å‚ ‚ê‚Î
+    ›‰sŒã‚ÍáÁ‚É‚È‚éB‚È‚ç‚È‚©‚‚½‚Ì‚Å‚ ‚ê‚΁A›‰sŒã‚Í™E‚É‚È‚éB
+    NULL‚Å‚ ‚‚Ăà—Ç‚­A‚»‚̏ꍇ‚̓`ƒFƒbƒN‚ðs‚Í‚È‚¢B
+
+    write:
+    except:
+    read‚Æ“¯žéB
+
+    timeout_ms:
+    ƒ^ƒCƒ€ƒAƒEƒgBšdˆÊ‚̓~ƒŠ•bB-1‚Å‚ ‚ê‚Ζ³§ŒÀB
+
+    –ß’l:
+    select‚Ì•Ô‚µ‚½’lB
+  */
+  int max_fd = -1;
   
-    fd_set rfds;
-    FD_ZERO(&rfds);
-    if (readflg != NULL) {
-	max_fd = fd;
-	FD_SET(fd, &rfds);
-    }
-
-    fd_set wfds;
-    FD_ZERO(&wfds);
-    if (writeflg != NULL) {
-	max_fd = fd;
-	FD_SET(fd, &wfds);
-    }
-
-    fd_set efds;
-    FD_ZERO(&efds);
-    if (exceptflg != NULL) {
-	max_fd = fd;
-	FD_SET(fd, &efds);
-    }
-
-    struct timeval tv;
-    tv.tv_sec = 0;
-    tv.tv_usec = timeout_ms * 1000;
-
-    int retval = ::select(max_fd + 1, &rfds, &wfds, &efds, (timeout_ms == -1 ? NULL : &tv));
-
-    if(readflg != NULL) {
-	*readflg = FD_ISSET(fd, &rfds);
-    }
-    if(writeflg != NULL) {
-	*writeflg = FD_ISSET(fd, &wfds);
-    }
-    if(exceptflg != NULL) {
-	*exceptflg = FD_ISSET(fd, &efds);
-    }
+  fd_set rfds;
+  FD_ZERO(&rfds);
+  if (read != NULL) {
+    max_fd = fd;
+    FD_SET(fd, &rfds);
+  }
+
+  fd_set wfds;
+  FD_ZERO(&wfds);
+  if (write != NULL) {
+    max_fd = fd;
+    FD_SET(fd, &wfds);
+  }
+
+  fd_set efds;
+  FD_ZERO(&efds);
+  if (except != NULL) {
+    max_fd = fd;
+    FD_SET(fd, &efds);
+  }
+
+  struct timeval tv;
+  tv.tv_sec = 0;
+  tv.tv_usec = timeout_ms * 1000;
+
+  int retval =
+    ::select(max_fd + 1, &rfds, &wfds, &efds,
+	     (timeout_ms == -1 ? NULL : &tv));
+
+  if (read != NULL) {
+    *read = FD_ISSET(fd, &rfds);
+  }
+  if (write != NULL) {
+    *write = FD_ISSET(fd, &wfds);
+  }
+  if (except != NULL) {
+    *except = FD_ISSET(fd, &efds);
+  }
   
-    return retval;
+  return retval;
 }
 
 int Socket::getPort() const {
@@ -350,7 +368,12 @@
     int size = sizeof(addr);
     ::getpeername(fd, (struct sockaddr *)&addr,(socklen_t *)&size);
 
-    return inet_ntoa(addr.sin_addr);
+    ostringstream oss;
+    oss << ((addr.sin_addr.s_addr >> 24) & 0xff) << "."
+	<< ((addr.sin_addr.s_addr >> 16) & 0xff) << "."
+	<< ((addr.sin_addr.s_addr >>  8) & 0xff) << "."
+	<< ( addr.sin_addr.s_addr        & 0xff) << std::ends;
+    return string(oss.str());
 }
 
 string Socket::getsockname() const {
@@ -359,5 +382,10 @@
     int size = sizeof(addr);
     ::getsockname(fd, (struct sockaddr *)&addr, (socklen_t *)&size);
 
-    return inet_ntoa(addr.sin_addr);
+    ostringstream oss;
+    oss << ((addr.sin_addr.s_addr >> 24) & 0xff) << "."
+	<< ((addr.sin_addr.s_addr >> 16) & 0xff) << "."
+	<< ((addr.sin_addr.s_addr >>  8) & 0xff) << "."
+	<< ( addr.sin_addr.s_addr        & 0xff) << std::ends;
+    return string(oss.str());
 }


aquaskk-changes メーリングリストの案内