Tomotaka SUWA
t-suw****@users*****
2006年 1月 14日 (土) 20:01:59 JST
Index: AquaSKK/net/Socket.cpp diff -u AquaSKK/net/Socket.cpp:1.2.2.1 AquaSKK/net/Socket.cpp:1.2.2.2 --- AquaSKK/net/Socket.cpp:1.2.2.1 Sat Jan 7 16:22:29 2006 +++ AquaSKK/net/Socket.cpp Sat Jan 14 20:01:59 2006 @@ -1,22 +1,24 @@ /* -*- c++ -*- - $Id: Socket.cpp,v 1.2.2.1 2006/01/07 07:22:29 t-suwa Exp $ - - MacOS X implementation of the SKK input method. - Copyright (C) 2002-2004 phonohawk + $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*****> - 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> @@ -33,171 +35,156 @@ using namespace net; using namespace std; -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) { +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) { begin_listening(reuse, listen_queue_size); - } - else { + } else { connect(); } } -Socket::Socket(int fd) - : host(),port(0),listening(false),fd(fd),eof(false) { +Socket::Socket(int src) : host(), port(0), listening(false), fd(src), 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()); + 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)); } - 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Èç±±ÅêsIíè } - } - else { + } else { buf += c; } } @@ -208,31 +195,31 @@ string Socket::readline() { // CR,LF,CRLFÌ¢¸êàüsÆ©ô·B // ÊÉüsR[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; // ±±ÅêsIíè } - else if (c == 0x0a) { // LF + else if(c == 0x0a) { // LF break; // ±±ÅêsIíè } else { @@ -245,21 +232,20 @@ 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; } } @@ -268,94 +254,90 @@ } 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* read,bool* write,bool* except,long timeout_ms) const { - /* - read: - æ¤ÝðÒ©¾¤©B±êðñNULLɵÄs·éÆAæ¤ÝÂ\ÉȽÌÅ êÎ - sãÍáÁÉÈéBÈçȩ½ÌÅ êÎAsãÍEÉÈéB - NULLÅ ÂÄàÇA»ÌêÍ`FbNðsÍÈ¢B - - write: - except: - readƯéB - - timeout_ms: - ^CAEgBdÊÍ~bB-1Å êγ§ÀB - - ßl: - selectÌÔµ½lB - */ - 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* readflg, bool* writeflg, bool* exceptflg, long timeout_ms) const { + /* + read: + æ¤ÝðÒ©¾¤©B±êðñNULLɵÄs·éÆAæ¤ÝÂ\ÉȽÌÅ êÎ + sãÍáÁÉÈéBÈçȩ½ÌÅ êÎAsãÍEÉÈéB + NULLÅ ÂÄàÇA»ÌêÍ`FbNðsÍÈ¢B + + write: + except: + readƯéB + + timeout_ms: + ^CAEgBdÊÍ~bB-1Å êγ§ÀB + + ßl: + selectÌÔµ½lB + */ + int max_fd = -1; - 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); - } + 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); + } - return retval; + return retval; } int Socket::getPort() const { Index: AquaSKK/net/Socket.h diff -u AquaSKK/net/Socket.h:1.2 AquaSKK/net/Socket.h:1.2.2.1 --- AquaSKK/net/Socket.h:1.2 Sat Oct 8 00:08:37 2005 +++ AquaSKK/net/Socket.h Sat Jan 14 20:01:59 2006 @@ -1,22 +1,24 @@ /* -*- c++ -*- - $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 + $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 */ #pragma once @@ -26,43 +28,38 @@ } class net::Socket { -private: std::string host; unsigned int port; - bool listening; - int fd; - std::string overread; // ÇÝ߬½¶ - bool eof; - + std::string overread; // ÇÝ߬½¶ + void connect(); void begin_listening(bool reuse, int backlog); - + public: // XjOªÚ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;