Changeset 15355 for lang/cplusplus

Show
Ignore:
Timestamp:
07/07/08 13:56:27 (6 years ago)
Author:
kazuho
Message:

merge CoopSocketReader? and Writer

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/cplusplus/friends_framework/trunk/test/proxy.cpp

    r9774 r15355  
    66 
    77struct ProxyConn { 
    8   CoopRunnable<CoopSocketReader, ProxyConn> clientRead; 
    9   CoopRunnable<CoopSocketWriter, ProxyConn> clientWrite; 
    10   CoopRunnable<CoopSocketReader, ProxyConn> serverRead; 
    11   CoopRunnable<CoopSocketWriter, ProxyConn> serverWrite; 
    12   CoopRunnable<CoopSleeper, ProxyConn> timeout; 
     8  CoopRunnableSocket<ProxyConn> clientSock; 
     9  CoopRunnableSocket<ProxyConn> serverSock; 
     10  CoopRunnableSleeper<ProxyConn> timeout; 
    1311  CoopBuffer dataToServer; 
    1412  CoopBuffer dataToClient; 
    15   ProxyConn(int clientSock, sockaddr *, socklen_t); 
    16   ~ProxyConn() { 
    17     close(clientRead.sock); 
    18     close(serverRead.sock); 
     13  ProxyConn(int clientFd, sockaddr *, socklen_t) 
     14  : clientSock(this, &ProxyConn::OnClientRead, &ProxyConn::OnClientWrite), 
     15    serverSock(this, &ProxyConn::OnServerRead, &ProxyConn::OnServerConnect), 
     16    timeout(this, &ProxyConn::OnTimeout) { 
     17    clientSock.SetSock(clientFd); 
    1918  } 
    2019  ProxyConn *Init(); 
     
    2524    delete this; 
    2625  } 
    27   void OnServerConnect(CoopSocketWriter *) { 
     26  void OnServerConnect(CoopSocket *) { 
    2827    ResetTimeout(); 
    29     clientRead.isActive = true; 
    30     serverRead.isActive = true; 
    31     serverWrite.func = &ProxyConn::OnServerWrite; 
     28    clientSock.SetState(CoopSocket::WaitForRead); 
     29    serverSock.SetState(CoopSocket::WaitForRead); 
     30    serverSock.writef = &ProxyConn::OnServerWrite; 
    3231  } 
    33   void OnClientRead(CoopSocketReader *) { 
     32  void OnClientRead(CoopSocket *) { 
    3433    ResetTimeout(); 
    35     if (! CoopTransfer::OnRead(&clientRead, &serverWrite, dataToServer)) 
     34    if (! CoopTransfer::OnRead(&clientSock, &serverSock, dataToServer)) 
    3635      delete this; 
    3736  } 
    38   void OnServerWrite(CoopSocketWriter *) { 
     37  void OnServerWrite(CoopSocket *) { 
    3938    ResetTimeout(); 
    40     if (! CoopTransfer::OnWrite(&clientRead, &serverWrite, dataToServer)) 
     39    if (! CoopTransfer::OnWrite(&clientSock, &serverSock, dataToServer)) 
    4140      delete this; 
    4241  } 
    43   void OnServerRead(CoopSocketReader *) { 
     42  void OnServerRead(CoopSocket *) { 
    4443    ResetTimeout(); 
    45     if (! CoopTransfer::OnRead(&serverRead, &clientWrite, dataToClient)) 
     44    if (! CoopTransfer::OnRead(&serverSock, &clientSock, dataToClient)) 
    4645      delete this; 
    4746  } 
    48   void OnClientWrite(CoopSocketWriter *) { 
     47  void OnClientWrite(CoopSocket *) { 
    4948    ResetTimeout(); 
    50     if (! CoopTransfer::OnWrite(&serverRead, &clientWrite, dataToClient)) 
     49    if (! CoopTransfer::OnWrite(&serverSock, &clientSock, dataToClient)) 
    5150      delete this; 
    5251  } 
    5352}; 
    5453 
    55 ProxyConn::ProxyConn(int clientSock, sockaddr *, socklen_t) 
    56 : clientRead(this, &ProxyConn::OnClientRead), 
    57   clientWrite(this, &ProxyConn::OnClientWrite), 
    58   serverRead(this, &ProxyConn::OnServerRead), 
    59   serverWrite(this, &ProxyConn::OnServerConnect), 
    60   timeout(this, &ProxyConn::OnTimeout) 
    61 { 
    62   clientRead.sock = clientWrite.sock = clientSock; 
    63   serverRead.sock = serverWrite.sock = ::socket(AF_INET, SOCK_STREAM, 0); 
    64   assert(serverRead.sock != -1); 
    65 } 
    66  
    6754ProxyConn *ProxyConn::Init() 
    6855{ 
    69   ::fcntl(serverRead.sock, O_NONBLOCK); 
    70   if (::connect(serverRead.sock, reinterpret_cast<sockaddr*>(&server_addr), 
    71               sizeof(server_addr)) == -1) { 
     56  serverSock.SetSock(::socket(AF_INET, SOCK_STREAM, 0)); 
     57  assert(serverSock.GetSock() != -1); 
     58  ::fcntl(serverSock.GetSock(), O_NONBLOCK); 
     59  if (::connect(serverSock.GetSock(), reinterpret_cast<sockaddr*>(&server_addr), 
     60                sizeof(server_addr)) == -1) { 
    7261    switch (errno) { 
    7362    case EWOULDBLOCK: 
    74       serverWrite.isActive = true; 
     63      serverSock.SetState(serverSock.GetState() | CoopSocket::WaitForWrite); 
    7564      break; 
    7665    default: 
     
    8069      } 
    8170  } else { 
    82     OnServerConnect(&serverWrite); 
     71    OnServerConnect(&serverSock); 
    8372  } 
    8473  ResetTimeout();