Tomotaka SUWA
t-suw****@users*****
2006年 4月 26日 (水) 22:36:15 JST
Index: AquaSKK/net/Socket.cpp diff -u AquaSKK/net/Socket.cpp:1.2 AquaSKK/net/Socket.cpp:removed --- AquaSKK/net/Socket.cpp:1.2 Sat Oct 8 00:08:37 2005 +++ AquaSKK/net/Socket.cpp Wed Apr 26 22:36:15 2006 @@ -1,391 +0,0 @@ -/* -*- c++ -*- - $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 -*/ - -#include <sstream> -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/uio.h> -#include <sys/select.h> -#include <sys/time.h> -#include <unistd.h> -#include <netinet/in.h> -#include <netdb.h> -#include <arpa/inet.h> -#include "Socket.h" -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) { - begin_listening(reuse, listen_queue_size); - } - else { - connect(); - } -} - -Socket::Socket(int fd) - : host(),port(0),listening(false),fd(fd),eof(false) { - -} - -Socket::~Socket(){ - 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); - - 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()); - } -} - -void Socket::connect() { - if (fd != 0) - return; - - 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); - - 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); - - ostringstream oss; - oss << "net::Socket - Couldn't connect to " << host << ':' << port << std::ends; - throw string(oss.str()); - } -} - -bool Socket::hasGotEOF() const { - return eof; -} - -void Socket::close() { - ::close(fd); -} - -Socket& Socket::operator<< (const string& str) { - 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; -} - -Socket& Socket::operator<< (long n) { - static char buf[50]; - sprintf(buf,"%ld",n); - - write(fd,buf,strlen(buf)); - return *this; -} - -Socket& Socket::flush() { - fsync(fd); - return *this; -} - -string Socket::readUntilCRLF() { - // CRLFª»ÍêéÜÅæ¤ÞBCRLF©éÍüçÈ¢B - if (eof) return string(""); - - string buf; - while (true) { - int c = read(); - if (c == -1) { - // EOFܽÍG[ - eof = true; - break; - } - - if (c == 0x0d) { // CR - // ̶ðÇÞB - int next = read(); - if (next == -1) { // EOFܽÍG[ - eof = true; - break; - } - - if (next != 0x0a) { // LFÅÈ¢ - buf += next; - } - else { - break; // LFÈç±±ÅêsIíè - } - } - else { - buf += c; - } - } - - return buf; -} - -string Socket::readline() { - // CR,LF,CRLFÌ¢¸êàüsÆ©ô·B - // ÊÉüsR[hÍüçÈ¢B - if (eof) return string(""); - - string buf; - while (true) { - int c = read(); - if (c == -1) { // EOFܽÍG[ - eof = true; - break; - } - - if (c == 0x0d) { // CR - // ̶ðÇÞB - int next = read(); - if (next == -1) { // EOFܽÍG[ - eof = true; - break; - } - - if (next != 0x0a) { // LFÅÈ¢ - // ÇÝ߬½B - overread += next; - } - break; // ±±ÅêsIíè - } - else if (c == 0x0a) { // LF - break; // ±±ÅêsIíè - } - else { - buf += c; - } - } - - return buf; -} - -string Socket::readUntil(char ch) { - // chªÒéÜÅæ¤ÞBÊÉchÍüçÈ¢B - if (eof) return string(""); - - string buf; - while (true) { - int c = read(); - if (c == -1) { // EOFܽÍG[ - eof = true; - break; - } - - if (c == ch) { - // ±±ÅIÍè - break; - } - else { - buf += c; - } - } - - return buf; -} - -int Socket::read() { - if (overread.length() > 0) { - unsigned char c = overread[0]; - overread.erase(0,1); - return c; - } - - if (eof) return -1; - - unsigned char c; - ssize_t bytes_read = ::read(fd,&c,1); - if (bytes_read == 0 || bytes_read == -1) { // EOFܽÍG[ - eof = true; - return -1; - } - 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; - - 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; -} - -int Socket::getPort() const { - return port; -} - -string Socket::getpeername() const { - // ÚãæAhXðÔ·Btø«ÍsÍÈ¢B - struct sockaddr_in addr; - int size = sizeof(addr); - ::getpeername(fd, (struct sockaddr *)&addr,(socklen_t *)&size); - - 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 { - // Úã³AhXðÔ·Btø«ÍsÍÈ¢B - struct sockaddr_in addr; - int size = sizeof(addr); - ::getsockname(fd, (struct sockaddr *)&addr, (socklen_t *)&size); - - 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()); -} Index: AquaSKK/net/Socket.h diff -u AquaSKK/net/Socket.h:1.2 AquaSKK/net/Socket.h:removed --- AquaSKK/net/Socket.h:1.2 Sat Oct 8 00:08:37 2005 +++ AquaSKK/net/Socket.h Wed Apr 26 22:36:15 2006 @@ -1,70 +0,0 @@ -/* -*- 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 -*/ - -#pragma once - -namespace net { - class Socket; -} - -class net::Socket { -private: - std::string host; - unsigned int port; - - bool listening; - - int fd; - std::string overread; // ÇÝ߬½¶ - - bool eof; - - 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, - 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 getPort() const; - virtual std::string getpeername() const; - virtual std::string getsockname() const; -};