peerConnection.cpp

00001 /*
00002   TCP/IP の相互接続を実現するクラス
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "peerConnection.h"
00008 #include "tcpipServer.h"
00009 
00010 
00011 PeerConnection::PeerConnection(void)
00012   : con(NULL), error_message("Connection device is not specified") {
00013 }
00014 
00015 
00016 PeerConnection::~PeerConnection(void) {
00017   delete con;
00018 }
00019 
00020 
00021 const char* PeerConnection::what(void) {
00022   return error_message.c_str();
00023 }
00024 
00025 
00026 bool PeerConnection::isConnected(void) {
00027   return (con) ? con->isConnected() : false;
00028 }
00029 
00030 
00031 int PeerConnection::connect(const char* host, long port, long timeout) {
00032 
00033   // 相手側に接続を行う
00034   int ret_value = raw_connect(host, port);
00035   error_message = con->what();
00036   if (ret_value >= 0) {
00037     return ret_value;
00038   }
00039   delete con;
00040 
00041   // 接続用ポートを立てる
00042   TcpipServer server_con;
00043   server_con.activate(port);
00044   con = server_con.accept(timeout);
00045   if (con) {
00046     error_message = con->what();
00047     return 0;
00048   }
00049   error_message = "connection timeout";
00050   return -1;
00051 }
00052 
00053 
00054 void PeerConnection::raw_disconnect(void) {
00055   delete con;
00056   con = NULL;
00057   error_message = "disconnected";
00058 }
00059 
00060 
00061 int PeerConnection::raw_connect(const char* device, long baudrate) {
00062   con = new TcpipDevice();
00063   int ret_value = con->connect(device, baudrate);
00064   error_message = con->what();
00065 
00066   return ret_value;
00067 }
00068 
00069 
00070 void PeerConnection::raw_check(int size, long timeout) {
00071 }
00072 
00073 
00074 int PeerConnection::raw_send(const char* data, int len) {
00075   if (!con) {
00076     return ConnectionDevice::NotConnected;
00077   }
00078   return con->send(data, len);
00079 }
00080 
00081 
00082 int PeerConnection::recv(char* data, int maxlen, long timeout) {
00083   if (!con) {
00084     return ConnectionDevice::NotConnected;
00085   }
00086   return con->recv(data, maxlen, timeout);
00087 }
00088 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1