00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <iostream>
00024 #include <unistd.h>
00025 #include <sys/socket.h>
00026 #include <sys/ioctl.h>
00027 #include <net/if.h>
00028 #include <netinet/in.h>
00029 #include <netinet/tcp.h>
00030 #include <arpa/inet.h>
00031 #include <errno.h>
00032 #include <limits.h>
00033 #include <fcntl.h>
00034 #include <sys/types.h>
00035
00036
00037 #include "NetInterface.h"
00038
00039
00040
00041 #define IP_STRING_SIZE 16
00042
00043
00044 #ifdef RIO_DEBUG_FILE
00045 #define INTERFACELOG m_NetTcp->m_log
00046 #define TRANSFERINFOLOG m_NetInterface->m_NetTcp->m_log
00047 #define STATICINTERFACELOG TellIdData->NetInterface->m_NetTcp->m_log
00048 #else
00049 #define INTERFACELOG RioErr
00050 #define TRANSFERINFOLOG RioErr
00051 #define STATICINTERFACELOG RioErr
00052 #endif
00053
00054
00055 CTransferInfo::CTransferInfo( CNetInterface *NetInterface )
00056 {
00057
00058
00059 m_NetInterface = NetInterface;
00060
00061 #ifdef RIO_DEBUG1
00062 TRANSFERINFOLOG << "[CTransferInfo - Construtor] Start" << endl;
00063 #endif
00064
00065
00066
00067
00068
00069 m_NextTransferID = 1;
00070
00071
00072
00073
00074 pthread_mutex_init( &m_Mutex, NULL );
00075
00076 #ifdef RIO_DEBUG1
00077 TRANSFERINFOLOG << "[CTransferInfo - Construtor] Finish" << endl;
00078 #endif
00079 }
00080
00081
00082
00083 CTransferInfo::~CTransferInfo()
00084 {
00085
00086 RioResult RioStatus;
00087
00088 #ifdef RIO_DEBUG1
00089 TRANSFERINFOLOG << "[CTransferInfo - Destrutor] Start" << endl;
00090 #endif
00091
00092
00093 RioStatus = FinalizeAllTransfers( 0, 0, UNDEFINEDTYPE,
00094 ERROR_TRANSFERINFO +
00095 ERROR_PROCESS_TERMINATED );
00096 #ifdef RIO_DEBUG2
00097 if( FAILED( RioStatus ) )
00098 {
00099 TRANSFERINFOLOG << "[CTransferInfo::~CTransferInfo] Erro 0x" << hex
00100 << RioStatus << dec << " ("
00101 << GetErrorDescription( RioStatus ) << ") ao executar "
00102 << "a funcao FinalizeAllTransfers." << endl;
00103 }
00104 #endif
00105
00106
00107 pthread_mutex_destroy( &m_Mutex );
00108
00109 #ifdef RIO_DEBUG1
00110 TRANSFERINFOLOG << "[CTransferInfo - Destrutor] Finish" << endl;
00111 #endif
00112 }
00113
00114
00115 TTransferInfoHash::iterator CTransferInfo::FindTransferID(
00116 TTransferID TransferID )
00117 {
00118 #ifdef RIO_DEBUG1
00119 TRANSFERINFOLOG << "[CTransferInfo - FindTransferID] Start" << endl;
00120 #endif
00121
00122
00123 TTransferInfoHash::iterator HashTransfer;
00124
00125
00126 if( TransferID == NULLTRANSFERID )
00127 {
00128 #ifdef RIO_DEBUG2
00129 TRANSFERINFOLOG << "CTransferInfo::FindTransferID O valor da ID "
00130 << TransferID << " nao e valido!" << endl;
00131 #endif
00132
00133 #ifdef RIO_DEBUG1
00134 TRANSFERINFOLOG << "[CTransferInfo - FindTransferID] Finish1" << endl;
00135 #endif
00136
00137 return m_TransferInfoHash.end();
00138
00139 }
00140
00141
00142 HashTransfer = m_TransferInfoHash.find( TransferID );
00143 if( HashTransfer == m_TransferInfoHash.end() )
00144 {
00145 #ifdef RIO_DEBUG2
00146 TRANSFERINFOLOG << "CTransferInfo::FindTransferID O valor da ID "
00147 << TransferID << " nao e valido, pois nao foi achado"
00148 "na hash!" << endl;
00149 #endif
00150
00151 #ifdef RIO_DEBUG1
00152 TRANSFERINFOLOG << "[CTransferInfo - FindTransferID] Finish2" << endl;
00153 #endif
00154
00155 return m_TransferInfoHash.end();
00156 }
00157
00158 #ifdef RIO_DEBUG2
00159 TRANSFERINFOLOG << "CTransferInfo::FindTransferID O valor da ID "
00160 << TransferID << " e valido!" << endl;
00161 #endif
00162
00163 #ifdef RIO_DEBUG1
00164 TRANSFERINFOLOG << "[CTransferInfo - FindTransferID] Finish3" << endl;
00165 #endif
00166
00167 return HashTransfer;
00168 }
00169
00170
00171 RioResult CTransferInfo::NewTransfer( int IP, int Port,
00172 callback_t FinalizeCallback,
00173 void *FinalizeCallbackParam,
00174 cmdcallback_t ProcessCallback,
00175 void *ProcessCallbackParam, char *Data,
00176 unsigned int DataSize,
00177 ETransferType TransferType,
00178 TTransferID DestTransferID,
00179 TTransferID *TransferID )
00180 {
00181
00182 STransferInfo TransferInfo;
00183
00184 TTransferInfoHash::iterator HashTransfer;
00185
00186
00187 bool searchTransferID;
00188
00189 #ifdef RIO_DEBUG1
00190 TRANSFERINFOLOG << "[CTransferInfo - NewTransfer] Start" << endl;
00191 #endif
00192
00193
00194 pthread_mutex_lock( &m_Mutex );
00195
00196
00197
00198
00199 searchTransferID = true;
00200 do
00201 {
00202 HashTransfer = m_TransferInfoHash.find( m_NextTransferID );
00203 if( ( HashTransfer != m_TransferInfoHash.end() ) &&
00204 ( m_NextTransferID != NULLTRANSFERID ) &&
00205 ( m_NextTransferID != 0 ) )
00206
00207
00208 m_NextTransferID++;
00209 else
00210 searchTransferID = false;
00211 } while( searchTransferID );
00212
00213
00214
00215 TransferInfo.IP = IP;
00216 TransferInfo.Port = Port;
00217 TransferInfo.FinalizeCallback = FinalizeCallback;
00218 TransferInfo.FinalizeCallbackParam = FinalizeCallbackParam;
00219 TransferInfo.ProcessCallback = ProcessCallback;
00220 TransferInfo.ProcessCallbackParam = ProcessCallbackParam;
00221 TransferInfo.Data = Data;
00222 TransferInfo.DataSize = DataSize;
00223 TransferInfo.TransferType = TransferType;
00224 TransferInfo.TransferID = m_NextTransferID;
00225 TransferInfo.DestTransferID = DestTransferID;
00226
00227
00228
00229 try
00230 {
00231
00232 m_TransferInfoHash[ m_NextTransferID ] = TransferInfo;
00233
00234
00235
00236 if( TransferID != NULL )
00237 *TransferID = m_NextTransferID;
00238
00239
00240
00241
00242
00243 m_NextTransferID++;
00244 }
00245 catch( bad_alloc& ba )
00246 {
00247
00248
00249
00250 if( TransferID != NULL )
00251 *TransferID = NULLTRANSFERID;
00252
00253
00254 pthread_mutex_unlock( &m_Mutex );
00255
00256 #ifdef RIO_DEBUG2
00257 TRANSFERINFOLOG << "CTransferInfo::NewTransfer Erro de alocacao de "
00258 << "memoria (bad_alloc, error = " << ba.what() << ") "
00259 << "ao criar uma nova entrada na hash "
00260 << "m_TransferInfoHash!" << endl;
00261 #endif
00262
00263 #ifdef RIO_DEBUG1
00264 TRANSFERINFOLOG << "[CTransferInfo - NewTransfer] Finish1" << endl;
00265 #endif
00266
00267 return ERROR_TRANSFERINFO + ERROR_MEMORY;
00268 }
00269
00270 #ifdef RIO_DEBUG2
00271 TRANSFERINFOLOG << "CTransferInfo::NewTransfer Uma nova transferencia com "
00272 << "a ID " << m_NextTransferID - 1 << " foi criada com "
00273 << "sucesso!" << endl;
00274 #endif
00275
00276
00277 pthread_mutex_unlock( &m_Mutex );
00278
00279 #ifdef RIO_DEBUG1
00280 TRANSFERINFOLOG << "[CTransferInfo - NewTransfer] Finish2" << endl;
00281 #endif
00282
00283 return S_OK;
00284 }
00285
00286
00287 RioResult CTransferInfo::RemoveTransfer( TTransferID TransferID )
00288 {
00289
00290 TTransferInfoHash::iterator HashTransfer;
00291
00292 #ifdef RIO_DEBUG1
00293 TRANSFERINFOLOG << "[CTransferInfo - RemoveTransfer] Start" << endl;
00294 #endif
00295
00296
00297 pthread_mutex_lock( &m_Mutex );
00298
00299
00300 HashTransfer = FindTransferID( TransferID );
00301 if( HashTransfer == m_TransferInfoHash.end() )
00302 {
00303
00304 pthread_mutex_unlock( &m_Mutex );
00305
00306 #ifdef RIO_DEBUG2
00307 TRANSFERINFOLOG << "CTransferInfo::RemoveTransfer O valor da ID "
00308 << TransferID << " nao e valido!" << endl;
00309 #endif
00310
00311 #ifdef RIO_DEBUG1
00312 TRANSFERINFOLOG << "[CTransferInfo - RemoveTransfer] Finish1" << endl;
00313 #endif
00314
00315 return ERROR_TRANSFERINFO + ERROR_INVALID_PARAM;
00316 }
00317
00318
00319 m_TransferInfoHash.erase( HashTransfer );
00320
00321 #ifdef RIO_DEBUG2
00322 TRANSFERINFOLOG << "CTransferInfo::RemoveTransfer A transferencia com a ID "
00323 << TransferID << " foi removida com sucesso!" << endl;
00324 #endif
00325
00326
00327 pthread_mutex_unlock( &m_Mutex );
00328
00329 #ifdef RIO_DEBUG1
00330 TRANSFERINFOLOG << "[CTransferInfo - RemoveTransfer] Finish2" << endl;
00331 #endif
00332
00333 return S_OK;
00334 }
00335
00336
00337
00338 RioResult CTransferInfo::GetTransfer( TTransferID TransferID, int *IP,
00339 int *Port, callback_t *FinalizeCallback,
00340 void **FinalizeCallbackParam,
00341 cmdcallback_t *ProcessCallback,
00342 void **ProcessCallbackParam,
00343 char **Data, unsigned int *DataSize,
00344 ETransferType *TransferType,
00345 TTransferID *DestTransferID )
00346 {
00347
00348 TTransferInfoHash::iterator HashTransfer;
00349
00350 #ifdef RIO_DEBUG1
00351 TRANSFERINFOLOG << "[CTransferInfo - GetTransfer] Start" << endl;
00352 #endif
00353
00354
00355 pthread_mutex_lock( &m_Mutex );
00356
00357
00358 HashTransfer = FindTransferID( TransferID );
00359 if( HashTransfer == m_TransferInfoHash.end() )
00360 {
00361
00362 pthread_mutex_unlock( &m_Mutex );
00363
00364
00365
00366 *IP = 0;
00367 *Port = 0;
00368 *FinalizeCallback = NULL;
00369 *FinalizeCallbackParam = NULL;
00370 *ProcessCallback = NULL;
00371 *ProcessCallbackParam = NULL;
00372 *Data = NULL;
00373 *DataSize = 0;
00374 *TransferType = UNDEFINEDTYPE;
00375
00376
00377
00378 if( DestTransferID != NULL )
00379 *DestTransferID = NULLTRANSFERID;
00380
00381 #ifdef RIO_DEBUG2
00382 TRANSFERINFOLOG << "CTransferInfo::GetTransfer O valor da ID "
00383 << TransferID << " nao e valido!" << endl;
00384 #endif
00385
00386 #ifdef RIO_DEBUG1
00387 TRANSFERINFOLOG << "[CTransferInfo - GetTransfer] Finish1" << endl;
00388 #endif
00389
00390 return ERROR_TRANSFERINFO + ERROR_INVALID_PARAM;
00391 }
00392
00393
00394 *IP = ( *HashTransfer ).second.IP;
00395 *Port = ( *HashTransfer ).second.Port;
00396 *FinalizeCallback = ( *HashTransfer ).second.FinalizeCallback;
00397 *FinalizeCallbackParam = ( *HashTransfer ).second.FinalizeCallbackParam;
00398 *ProcessCallback = ( *HashTransfer ).second.ProcessCallback;
00399 *ProcessCallbackParam = ( *HashTransfer ).second.ProcessCallbackParam;
00400 *Data = ( *HashTransfer ).second.Data;
00401 *DataSize = ( *HashTransfer ).second.DataSize;
00402 *TransferType = ( *HashTransfer ).second.TransferType;
00403
00404
00405
00406 if( DestTransferID != NULL )
00407 *DestTransferID = ( *HashTransfer ).second.DestTransferID;
00408
00409 #ifdef RIO_DEBUG2
00410 TRANSFERINFOLOG << "CTransferInfo::GetTransfer A transferencia com a ID "
00411 << TransferID << " foi obtida com sucesso!" << endl;
00412 #endif
00413
00414
00415 pthread_mutex_unlock( &m_Mutex );
00416
00417 #ifdef RIO_DEBUG1
00418 TRANSFERINFOLOG << "[CTransferInfo - GetTransfer] Finish2" << endl;
00419 #endif
00420
00421 return S_OK;
00422 }
00423
00424
00425
00426
00427
00428 RioResult CTransferInfo::FinalizeAllTransfers( int IP, int Port,
00429 ETransferType TransferType,
00430 RioResult RioError )
00431 {
00432
00433 TTransferInfoHash::iterator HashTransfer, NextHashTransfer;
00434
00435 #ifdef RIO_DEBUG2
00436
00437 in_addr ip, iph;
00438
00439 TTransferInfoHash::iterator AuxIterator;
00440
00441 ip.s_addr = IP;
00442 #endif
00443
00444 #ifdef RIO_DEBUG1
00445 TRANSFERINFOLOG << "[CTransferInfo - FinalizeAllTransfers] Start" << endl;
00446 #endif
00447
00448
00449 pthread_mutex_lock( &m_Mutex );
00450
00451 #ifdef RIO_DEBUG2
00452 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers hash das "
00453 << "transferencias, com " << m_TransferInfoHash.size()
00454 << " entradas: ";
00455
00456 AuxIterator = m_TransferInfoHash.begin();
00457 if( AuxIterator != m_TransferInfoHash.end() )
00458 {
00459 while( AuxIterator != m_TransferInfoHash.end() )
00460 {
00461 iph.s_addr = ( * AuxIterator ).second.IP;
00462 TRANSFERINFOLOG << "[" << ( * AuxIterator ).first << ", <IP="
00463 << inet_ntoa( iph ) << ", Port="
00464 << ntohs( ( * AuxIterator ).second.Port )
00465 << ", FinalizeCallback=0x" << hex
00466 << ( unsigned long )
00467 ( * AuxIterator ).second.FinalizeCallback
00468 << ", FinalizeCallbackParam=0x"
00469 << ( unsigned long )
00470 ( * AuxIterator ).second.FinalizeCallbackParam
00471 << ", ProcessCallback=0x"
00472 << ( unsigned long )
00473 ( * AuxIterator ).second.ProcessCallback
00474 << ", ProcessCallbackParam=0x"
00475 << ( unsigned long )
00476 ( * AuxIterator ).second.ProcessCallbackParam
00477 << ", Data=0x" << ( unsigned long )
00478 ( * AuxIterator ).second.Data
00479 << dec << ", DataSize="
00480 << ( * AuxIterator ).second.DataSize
00481 << ", TransferType="
00482 << ( * AuxIterator ).second.TransferType
00483 << ">]";
00484 AuxIterator++;
00485 if( AuxIterator != m_TransferInfoHash.end() )
00486 TRANSFERINFOLOG << ", ";
00487 else
00488 TRANSFERINFOLOG << "." << endl;
00489 }
00490 }
00491 else
00492 {
00493 TRANSFERINFOLOG << "<<< HASH VAZIA!>>>." << endl;
00494 }
00495 #endif
00496
00497
00498
00499
00500
00501
00502 HashTransfer = m_TransferInfoHash.begin();
00503 while( HashTransfer != m_TransferInfoHash.end() )
00504 {
00505 #ifdef RIO_DEBUG2
00506 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers antes de "
00507 << "incrementar o iterador HashTransfer e atribui-lo a "
00508 << "NextHashTransfer." << endl;
00509 #endif
00510
00511 NextHashTransfer = HashTransfer;
00512 NextHashTransfer++;
00513
00514 #ifdef RIO_DEBUG2
00515 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers depos de "
00516 << "incrementar o iterador HashTransfer e atribui-lo a "
00517 << "NextHashTransfer, e antes de executar o IP."
00518 << endl;
00519 #endif
00520
00521
00522
00523 if( ( ( ( IP == 0 ) && ( Port == 0 ) ) ||
00524 ( ( ( *HashTransfer ).second.IP == IP ) &&
00525 ( ( *HashTransfer ).second.Port == Port ) ) ) &&
00526 ( ( TransferType == UNDEFINEDTYPE ) ||
00527 ( TransferType == ( *HashTransfer ).second.TransferType ) ) )
00528 {
00529 #ifdef RIO_DEBUG2
00530 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers Iniciando "
00531 << "a remocao da transferencia com a ID "
00532 << ( *HashTransfer ).second.TransferID
00533 << " associada ao IP " << inet_ntoa( ip )
00534 << " e a porta " << ntohs( Port ) << "." << endl;
00535 #endif
00536
00537
00538 if( ( *HashTransfer ).second.FinalizeCallback != NULL )
00539 {
00540 #ifdef RIO_DEBUG2
00541 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers "
00542 << "chamando a callback associada a "
00543 << "transferencia com a ID "
00544 << ( *HashTransfer ).second.TransferID
00545 << " com o erro 0x" << hex << RioError << dec
00546 << " (" << GetErrorDescription( RioError )
00547 << ")." << endl;
00548 #endif
00549
00550 ( *HashTransfer ).second.FinalizeCallback(
00551 ( *HashTransfer ).second.FinalizeCallbackParam,
00552 RioError );
00553 }
00554
00555 #ifdef RIO_DEBUG2
00556 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers removendo "
00557 << "a transferencia com a ID "
00558 << ( *HashTransfer ).second.TransferID << "."
00559 << endl;
00560 #endif
00561
00562
00563 m_TransferInfoHash.erase( HashTransfer );
00564
00565
00566
00567 if( ( *HashTransfer ).second.TransferType == SENDINGDATA )
00568 delete[] ( *HashTransfer ).second.Data;
00569
00570 #ifdef RIO_DEBUG2
00571 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers depois de "
00572 << "ter removido a transferencia." << endl;
00573 #endif
00574 }
00575
00576 #ifdef RIO_DEBUG2
00577 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers antes de "
00578 << "atualizar HashTransfer." << endl;
00579 #endif
00580
00581
00582 HashTransfer = NextHashTransfer;
00583
00584 #ifdef RIO_DEBUG2
00585 TRANSFERINFOLOG << "CTransferInfo::FinalizeAllTransfers depois de "
00586 << "atualizar HashTransfer." << endl;
00587 #endif
00588 }
00589
00590
00591 pthread_mutex_unlock( &m_Mutex );
00592
00593 #ifdef RIO_DEBUG1
00594 TRANSFERINFOLOG << "[CTransferInfo - FinalizeAllTransfers] Finish" << endl;
00595 #endif
00596
00597 return S_OK;
00598 }
00599
00600
00601
00602 CNetInterface::CNetInterface()
00603 {
00604 #ifdef RIO_DEBUG1
00605 RioErr << "[CNetInterface - Construtor] Start" << endl;
00606 #endif
00607
00608
00609 m_NetTcp = NULL;
00610 m_NetUdp = NULL;
00611 m_Started = false;
00612 m_TransferInfo = NULL;
00613
00614
00615
00616 pthread_mutex_init( &m_MapMutex, NULL );
00617
00618 #ifdef RIO_DEBUG1
00619 RioErr << "[CNetInterface - Construtor] Finish" << endl;
00620 #endif
00621 }
00622
00623
00624 CNetInterface::~CNetInterface()
00625 {
00626 #ifdef RIO_DEBUG1
00627 INTERFACELOG << "[CNetInterface - Destrutor] Start" << endl;
00628 #endif
00629
00630
00631 if( m_Started )
00632 Stop();
00633
00634
00635 pthread_mutex_destroy( &m_MapMutex );
00636
00637 #ifdef RIO_DEBUG1
00638 INTERFACELOG << "[CNetInterface - Destrutor] Finish" << endl;
00639 #endif
00640 }
00641
00642
00643
00644 RioResult CNetInterface::StartNetTcp( SNetTcpConfig *NetConfig )
00645 {
00646
00647 RioResult RioStatus;
00648
00649 #ifdef RIO_DEBUG1
00650 INTERFACELOG << "[CNetInterface::StartNetTcp] Start" << endl;
00651 #endif
00652
00653
00654 m_NetTcp = new CNetTcp( this );
00655 if( m_NetTcp == NULL )
00656 {
00657 #ifdef RIO_DEBUG2
00658 INTERFACELOG << "CNetInterface::StartNetTcp erro de alocacao de "
00659 << "memoria ao criar o objeto da classe CNetTcp." << endl;
00660 #endif
00661
00662 #ifdef RIO_DEBUG1
00663 INTERFACELOG << "[CNetInterface - StartNetTcp] Finish1" << endl;
00664 #endif
00665
00666 return ERROR_NETINTERFACE + ERROR_MEMORY;
00667 }
00668
00669
00670 RioStatus = m_NetTcp->Start( NetConfig );
00671 if( FAILED( RioStatus ) )
00672 {
00673 #ifdef RIO_DEBUG2
00674 INTERFACELOG << "CNetInterface::StartNetTcp erro 0x" << hex << RioStatus
00675 << dec << " (" << GetErrorDescription( RioStatus ) << ") "
00676 << "ao executar a funcao Start do objeto da classe "
00677 << "CNetTcp." << endl;
00678 #endif
00679
00680 delete m_NetTcp;
00681 m_NetTcp = NULL;
00682
00683 #ifdef RIO_DEBUG1
00684 INTERFACELOG << "[CNetInterface - StartNetTcp ] Finish2" << endl;
00685 #endif
00686
00687 return RioStatus;
00688 }
00689
00690 #ifdef RIO_DEBUG2
00691 INTERFACELOG << "CNetInterface::StartNetTcp objeto criado e inicializado!"
00692 << endl;
00693 #endif
00694
00695 #ifdef RIO_DEBUG1
00696 INTERFACELOG << "[CNetInterface - StartNetTcp] Finish3" << endl;
00697 #endif
00698
00699 return S_OK;
00700 }
00701
00702
00703
00704 RioResult CNetInterface::StartNetUdp( SNetUdpConfig *NetConfig )
00705 {
00706 #ifdef RIO_DEBUG1
00707 INTERFACELOG << "[CNetInterface::StartNetUdp] Start" << endl;
00708 #endif
00709
00710
00711
00712 m_NetUdp = NULL;
00713
00714 #ifdef RIO_DEBUG2
00715 INTERFACELOG << "CNetInterface::StartNetUdp objeto criado e inicializado!"
00716 << endl;
00717 #endif
00718
00719 #ifdef RIO_DEBUG1
00720 INTERFACELOG << "[CNetInterface::StartNetUdp] Finish" << endl;
00721 #endif
00722
00723 return S_OK;
00724 }
00725
00726
00727 RioResult CNetInterface::StartTransferInfo()
00728 {
00729 #ifdef RIO_DEBUG1
00730 INTERFACELOG << "[CNetInterface - StartTransferInfo] Start" << endl;
00731 #endif
00732
00733 try
00734 {
00735 m_TransferInfo = new CTransferInfo( this );
00736 }
00737 catch( bad_alloc &ba )
00738 {
00739 #ifdef RIO_DEBUG2
00740 INTERFACELOG << "CNetInterface::StartTransferInfo Erro de alocacao de "
00741 << "memoria (bad_alloc, error = " << ba.what() << ") ao "
00742 << "criar o objeto do tipo CTransferInfo para gerenciar "
00743 << "as transferencias." << endl;
00744 #endif
00745
00746 #ifdef RIO_DEBUG1
00747 INTERFACELOG << "[CNetInterface - SendData] Finish1" << endl;
00748 #endif
00749
00750 return ERROR_NETINTERFACE + ERROR_MEMORY;
00751 }
00752
00753 #ifdef RIO_DEBUG2
00754 INTERFACELOG << "CNetInterface::StartTransferInfo objeto criado e "
00755 << "inicializado!" << endl;
00756 #endif
00757
00758 #ifdef RIO_DEBUG1
00759 INTERFACELOG << "[CNetInterface - StartTransferInfo] Finish2" << endl;
00760 #endif
00761
00762 return S_OK;
00763 }
00764
00765
00766
00767
00768
00769 RioResult CNetInterface::SendData( int IP, int Port,
00770 callback_t FinalizeCallback,
00771 void *FinalizeCallbackParam, char *Data,
00772 unsigned int DataSize, EDataType DataType,
00773 bool UseTCP, TTransferID DestTransferID,
00774 TTransferID *TransferID )
00775 {
00776
00777 RioResult RioStatus, RioStatus2;
00778
00779
00780 char *RawData;
00781
00782 unsigned int RawDataSize;
00783
00784 SDataHeader DataHeader;
00785
00786 TTransferID AuxTransferID;
00787
00788 #ifdef RIO_DEBUG2
00789 in_addr ip;
00790
00791 ip.s_addr = IP;
00792 #endif
00793
00794 #ifdef RIO_DEBUG1
00795 INTERFACELOG << "[CNetInterface - SendData] Start" << endl;
00796 #endif
00797
00798
00799 if( TransferID != NULL )
00800 *TransferID = NULLTRANSFERID;
00801
00802
00803
00804 RawDataSize = DataSize + sizeof( DataHeader );
00805 try
00806 {
00807 RawData = new char[ RawDataSize ];
00808 }
00809 catch( bad_alloc& ba )
00810 {
00811 #ifdef RIO_DEBUG2
00812 INTERFACELOG << "CNetInterface::SendData Erro de alocacao de memoria ("
00813 << "bad_alloc, error = " << ba.what() << ") ao criar o "
00814 << "buffer com o cabecalho mais os dados a serem "
00815 << "enviados. Nao foi possivel enviar os dados ao IP "
00816 << inet_ntoa( ip ) << " e a porta " << ntohs( Port )
00817 << endl;
00818 #endif
00819
00820 #ifdef RIO_DEBUG1
00821 INTERFACELOG << "[CNetInterface - SendData] Finish1" << endl;
00822 #endif
00823
00824 return ERROR_NETINTERFACE + ERROR_MEMORY;
00825 }
00826
00827
00828
00829 RioStatus = m_TransferInfo->NewTransfer( IP, Port, FinalizeCallback,
00830 FinalizeCallbackParam, NULL, NULL,
00831 RawData, RawDataSize, SENDINGDATA,
00832 DestTransferID, &AuxTransferID );
00833
00834 if( FAILED( RioStatus ) )
00835 {
00836 #ifdef RIO_DEBUG2
00837 INTERFACELOG << "CNetInterface::SendData Erro 0x" << hex << RioStatus
00838 << dec << " (" << GetErrorDescription( RioStatus )
00839 << ") ao executar a funcao NewTransfer do objeto "
00840 << "m_TransferInfo ao criar uma estrutura para ser "
00841 << "associada ao envio dos dados. Nao foi possivel enviar "
00842 << "os dados ao IP " << inet_ntoa( ip ) << " e a porta "
00843 << ntohs( Port ) << endl;
00844 #endif
00845
00846 #ifdef RIO_DEBUG1
00847 INTERFACELOG << "[CNetInterface - SendData] Finish2" << endl;
00848 #endif
00849
00850 return RioStatus;
00851 }
00852
00853
00854 DataHeader.DataType = htons( (uint16_t) DataType );
00855 DataHeader.SourceTransferID = htonl( (uint32_t) ( AuxTransferID ) );
00856 DataHeader.DataTransferID = htonl( (uint32_t) DestTransferID );
00857
00858
00859 memcpy( RawData, &DataHeader, sizeof( DataHeader ) );
00860 memcpy( &RawData[ sizeof( DataHeader ) ], Data, DataSize );
00861
00862
00863
00864
00865 if( UseTCP )
00866 RioStatus = m_NetTcp->SendData( IP, Port, RawData, RawDataSize,
00867 ( void * ) ( TLTransferID ) AuxTransferID );
00868 else
00869
00870
00871
00872
00873 if( FAILED( RioStatus ) )
00874 {
00875 #ifdef RIO_DEBUG2
00876 INTERFACELOG << "CNetInterface::SendData Erro 0x" << hex << RioStatus
00877 << dec << " (" << GetErrorDescription( RioStatus )
00878 << ") ao executar a funcao SendData do objeto m_Net ao "
00879 << "enviar os dados ao destino. Nao foi possivel enviar "
00880 << "os dados ao IP " << inet_ntoa( ip ) << " e a porta "
00881 << ntohs( Port ) << endl;
00882 #endif
00883
00884
00885 if( FinalizeCallback != NULL )
00886 FinalizeCallback( FinalizeCallbackParam, ERROR_NETINTERFACE +
00887 ERROR_SOCKET_SEND );
00888
00889
00890 RioStatus2 = m_TransferInfo->RemoveTransfer( AuxTransferID );
00891 #ifdef RIO_DEBUG2
00892 if( FAILED( RioStatus2 ) )
00893 {
00894 INTERFACELOG << "CNetInterface::SendData Erro 0x" << hex
00895 << RioStatus2 << dec << " ("
00896 << GetErrorDescription( RioStatus2 ) << ") ao "
00897 << "executar a funcao RemoveTransfer do objeto "
00898 << "m_TransferInfo ao remover a estrutura criada mas "
00899 << "que nao sera usada!" << endl;
00900 }
00901 #endif
00902
00903 #ifdef RIO_DEBUG1
00904 INTERFACELOG << "[CNetInterface - SendData] Finish3" << endl;
00905 #endif
00906
00907 return RioStatus;
00908 }
00909
00910 #ifdef RIO_DEBUG2
00911 INTERFACELOG << "CNetInterface::SendData enviando os dados do endereco 0x"
00912 << hex << ( unsigned long ) Data << dec << " com o tamanho de "
00913 << DataSize << " bytes ao endereco endereco IP "
00914 << inet_ntoa( ip ) << " e a porta " << ntohs( Port )
00915 << " com a ID " << AuxTransferID << ". Cabecalho enviado com "
00916 << "os dados: DataType = 0x" << hex << DataHeader.DataType
00917 << ", DataHeader.SourceTransferID = 0x"
00918 << DataHeader.SourceTransferID
00919 << ", DataHeader.DataTransferID = 0x"
00920 << DataHeader.DataTransferID << ". O endereco do vetor com o "
00921 << " cabecalho mais os dados e igual a 0x"
00922 << ( unsigned long ) RawData << dec << ", e serao enviados "
00923 << "um total de " << RawDataSize << " bytes." << endl;
00924 #endif
00925
00926
00927 if( TransferID != NULL )
00928 *TransferID = AuxTransferID;
00929
00930 #ifdef RIO_DEBUG1
00931 INTERFACELOG << "[CNetInterface - SendData] Finish4" << endl;
00932 #endif
00933
00934 return S_OK;
00935 }
00936
00937
00938 #ifdef RIO_DEBUG_FILE
00939 RioResult CNetInterface::Start( struct sockaddr_in *ServerAddress,
00940 unsigned int ServerAddressSize,
00941 time_t ConnectionTimeOut,
00942 unsigned int ReceiveTimeOut,
00943 unsigned int SendTimeOut, const char *LogPath )
00944 #else
00945 RioResult CNetInterface::Start( struct sockaddr_in *ServerAddress,
00946 unsigned int ServerAddressSize,
00947 time_t ConnectionTimeOut,
00948 unsigned int ReceiveTimeOut,
00949 unsigned int SendTimeOut )
00950 #endif
00951 {
00952
00953 RioResult RioStatus;
00954
00955 SNetTcpConfig NetTcpConfig;
00956
00957 SNetUdpConfig NetUdpConfig;
00958
00959
00960
00961 #ifdef RIO_DEBUG_FILE
00962 string file;
00963
00964 const char *LogFileName;
00965 #endif
00966
00967
00968 #ifdef RIO_DEBUG1
00969 RioErr << "[CNetInterface - Start] Start" << endl;
00970 #endif
00971
00972
00973 if( m_Started )
00974 {
00975 #ifdef RIO_DEBUG2
00976 RioErr << "CNetInterface::Start o objeto ja foi inicializado."
00977 << endl;
00978 #endif
00979
00980 #ifdef RIO_DEBUG1
00981 RioErr << "[CNetInterface - Start] Finish1" << endl;
00982 #endif
00983
00984 return ERROR_NETINTERFACE + ERROR_STARTED;
00985 }
00986
00987
00988 #ifdef RIO_DEBUG_FILE
00989 if( LogPath == NULL )
00990 {
00991 char tmpName[256];
00992 char tmpDomain[256];
00993 gethostname( tmpName, 255 );
00994 getdomainname( tmpDomain, 255 );
00995 char tmpPid[ 20 ];
00996
00997
00998 pid_t myPid;
00999 myPid = getpid();
01000 sprintf( tmpPid, "%d", ( int ) myPid );
01001 file = "RIOClientEmul_";
01002 file += tmpPid;
01003 file += "_";
01004 file += tmpName;
01005
01006 if( strstr( tmpName, tmpDomain ) == NULL )
01007 {
01008 file += ".";
01009 file += tmpDomain;
01010 }
01011 file += ".log";
01012
01013 LogFileName = file.c_str();
01014 }
01015 else
01016 LogFileName = LogPath;
01017 #endif
01018
01019
01020 memset( &NetTcpConfig, 0, sizeof( NetTcpConfig ) );
01021
01022
01023 NetTcpConfig.isServer = false;
01024 NetTcpConfig.ReceiveTimeOut = ReceiveTimeOut;
01025 NetTcpConfig.SendTimeOut = SendTimeOut;
01026 NetTcpConfig.ConnectionTimeOut = ConnectionTimeOut;
01027 NetTcpConfig.ServerAddress = ServerAddress;
01028 NetTcpConfig.ServerAddressSize = ServerAddressSize;
01029 #ifdef RIO_DEBUG_FILE
01030 NetTcpConfig.LogFileName = LogFileName;
01031 #endif
01032
01033
01034 RioStatus = FindIP( &NetTcpConfig.HostIP );
01035 if ( FAILED( RioStatus ) )
01036 {
01037 #ifdef RIO_DEBUG2
01038 RioErr << "CNetInterface::Start erro ao executar FindIP." << endl;
01039 #endif
01040
01041 #ifdef RIO_DEBUG1
01042 RioErr << "[CNetInterface - Start] Finish2" << endl;
01043 #endif
01044
01045 return RioStatus;
01046 }
01047
01048
01049 RioStatus = StartTransferInfo();
01050 if( FAILED( RioStatus ) )
01051 {
01052 #ifdef RIO_DEBUG2
01053 RioErr << "CNetInterface::Start erro 0x" << hex << RioStatus << dec
01054 << " (" << GetErrorDescription( RioStatus )
01055 << ") ao executar a funcao StartTransferInfo" << endl;
01056 #endif
01057
01058 #ifdef RIO_DEBUG1
01059 RioErr << "[CNetInterface - Start] Finish3" << endl;
01060 #endif
01061
01062 return RioStatus;
01063 }
01064
01065
01066
01067 RioStatus = StartNetTcp( &NetTcpConfig );
01068 if( FAILED( RioStatus ) )
01069 {
01070 #ifdef RIO_DEBUG2
01071 RioErr << "CNetInterface::Start erro 0x" << hex << RioStatus << dec
01072 << " (" << GetErrorDescription( RioStatus )
01073 << ") ao executar a funcao StartNetTcp" << endl;
01074 #endif
01075
01076 #ifdef RIO_DEBUG1
01077 RioErr << "[CNetInterface - Start] Finish4" << endl;
01078 #endif
01079
01080 return RioStatus;
01081 }
01082
01083
01084
01085 NetUdpConfig.isServer = false;
01086 NetUdpConfig.HostIP = NetTcpConfig.HostIP;
01087 #ifdef RIO_DEBUG_FILE
01088 NetUdpConfig.LogFileName = LogFileName;
01089 #endif
01090
01091
01092
01093 RioStatus = StartNetUdp( &NetUdpConfig );
01094 if( FAILED( RioStatus ) )
01095 {
01096 #ifdef RIO_DEBUG2
01097 RioErr << "CNetInterface::Start erro 0x" << hex << RioStatus << dec
01098 << " (" << GetErrorDescription( RioStatus )
01099 << ") ao executar a funcao StartNetUdp" << endl;
01100 #endif
01101
01102 #ifdef RIO_DEBUG1
01103 RioErr << "[CNetInterface - Start] Finish5" << endl;
01104 #endif
01105
01106 return RioStatus;
01107 }
01108
01109
01110 m_Started = true;
01111
01112 #ifdef RIO_DEBUG2
01113 RioErr << "CNetInterface::Start objeto criado e inicializado com sucesso!"
01114 << endl;
01115 #endif
01116
01117 #ifdef RIO_DEBUG1
01118 RioErr << "[CNetInterface - Start] Finish6" << endl;
01119 #endif
01120
01121 return S_OK;
01122 }
01123
01124
01125 #ifdef RIO_DEBUG_FILE
01126 RioResult CNetInterface::Start( int ServerPort, time_t ConnectionTimeOut,
01127 unsigned int ReceiveTimeOut,
01128 unsigned int SendTimeOut,
01129 CLogRotation *LogRotation, bool isStorage,
01130 const char *LogPath )
01131 #else
01132 RioResult CNetInterface::Start( int ServerPort, time_t ConnectionTimeOut,
01133 unsigned int ReceiveTimeOut,
01134 unsigned int SendTimeOut,
01135 CLogRotation *LogRotation )
01136 #endif
01137 {
01138
01139 RioResult RioStatus;
01140
01141 SNetTcpConfig NetTcpConfig;
01142
01143 SNetUdpConfig NetUdpConfig;
01144
01145
01146
01147 #ifdef RIO_DEBUG_FILE
01148 string file;
01149
01150 const char *LogFileName;
01151 #endif
01152
01153 #ifdef RIO_DEBUG1
01154 RioErr << "[CNetInterface - Start] - Start" << endl;
01155 #endif
01156
01157
01158 if( m_Started )
01159 {
01160 #ifdef RIO_DEBUG2
01161 RioErr << "CNetInterface::Start o objeto ja foi inicializado."
01162 << endl;
01163 #endif
01164
01165 #ifdef RIO_DEBUG1
01166 RioErr << "[CNetInterface - Start] Finish1" << endl;
01167 #endif
01168
01169 return ERROR_NETINTERFACE + ERROR_STARTED;
01170 }
01171
01172
01173 #ifdef RIO_DEBUG_FILE
01174 if( LogPath == NULL )
01175 {
01176 char tmpName[256];
01177 char tmpDomain[256];
01178 gethostname( tmpName, 255 );
01179 getdomainname( tmpDomain, 255 );
01180 string file;
01181
01182 if( isStorage )
01183 file = "RIOStorageEmul_";
01184 else
01185 file = "RIOServerEmul_";
01186
01187 file += tmpName;
01188 if( strstr( tmpName, tmpDomain ) == NULL )
01189 {
01190 file += ".";
01191 file += tmpDomain;
01192 }
01193 file += ".log";
01194
01195 LogFileName = file.c_str();
01196 }
01197 else
01198 LogFileName = LogPath;
01199 #endif
01200
01201
01202 memset( &NetTcpConfig, 0, sizeof( NetTcpConfig ) );
01203
01204
01205 NetTcpConfig.isServer = true;
01206 NetTcpConfig.ReceiveTimeOut = ReceiveTimeOut;
01207 NetTcpConfig.SendTimeOut = SendTimeOut;
01208 NetTcpConfig.ConnectionTimeOut = ConnectionTimeOut;
01209 NetTcpConfig.ServerPort = ServerPort;
01210 NetTcpConfig.LogRotation = LogRotation;
01211 #ifdef RIO_DEBUG_FILE
01212
01213 NetTcpConfig.LogFileName = LogFileName;
01214 #endif
01215
01216
01217 RioStatus = FindIP( &NetTcpConfig.HostIP );
01218 if ( FAILED( RioStatus ) )
01219 {
01220 #ifdef RIO_DEBUG2
01221 RioErr << "CNetInterface::Start erro ao executar FindIP." << endl;
01222 #endif
01223
01224 #ifdef RIO_DEBUG1
01225 RioErr << "[CNetInterface - Start] Finish2" << endl;
01226 #endif
01227
01228 return RioStatus;
01229 }
01230
01231
01232 RioStatus = StartTransferInfo();
01233 if( FAILED( RioStatus ) )
01234 {
01235 #ifdef RIO_DEBUG2
01236 RioErr << "CNetInterface::Start erro 0x" << hex << RioStatus << dec
01237 << " (" << GetErrorDescription( RioStatus )
01238 << ") ao executar a funcao StartTransferInfo" << endl;
01239 #endif
01240
01241 #ifdef RIO_DEBUG1
01242 RioErr << "[CNetInterface - Start] Finish3" << endl;
01243 #endif
01244
01245 return RioStatus;
01246 }
01247
01248
01249
01250 RioStatus = StartNetTcp( &NetTcpConfig );
01251 if( FAILED( RioStatus ) )
01252 {
01253 #ifdef RIO_DEBUG2
01254 RioErr << "CNetInterface::Start erro 0x" << hex << RioStatus << dec
01255 << " (" << GetErrorDescription( RioStatus )
01256 << ") ao executar a funcao StartNet" << endl;
01257 #endif
01258
01259 #ifdef RIO_DEBUG1
01260 RioErr << "[CNetInterface - Start] Finish4" << endl;
01261 #endif
01262
01263 return RioStatus;
01264 }
01265
01266
01267
01268 NetUdpConfig.isServer = true;
01269 NetUdpConfig.LogRotation = LogRotation;
01270 NetUdpConfig.HostIP = NetTcpConfig.HostIP;
01271 #ifdef RIO_DEBUG_FILE
01272 NetUdpConfig.LogFileName = LogFileName;
01273 #endif
01274
01275
01276
01277 RioStatus = StartNetUdp( &NetUdpConfig );
01278 if( FAILED( RioStatus ) )
01279 {
01280 #ifdef RIO_DEBUG2
01281 RioErr << "CNetInterface::Start erro 0x" << hex << RioStatus << dec
01282 << " (" << GetErrorDescription( RioStatus )
01283 << ") ao executar a funcao StartNetUdp" << endl;
01284 #endif
01285
01286 #ifdef RIO_DEBUG1
01287 RioErr << "[CNetInterface - Start] Finish5" << endl;
01288 #endif
01289
01290 return RioStatus;
01291 }
01292
01293
01294 m_Started = true;
01295
01296 #ifdef RIO_DEBUG2
01297 RioErr << "CNetInterface::Start objeto criado e inicializado com sucesso!"
01298 << endl;
01299 #endif
01300
01301 #ifdef RIO_DEBUG1
01302 RioErr << "[CNetInterface - Start] Finish6" << endl;
01303 #endif
01304
01305 return S_OK;
01306 }
01307
01308
01309
01310
01311 RioResult CNetInterface::Stop()
01312 {
01313
01314 RioResult RioStatus;
01315
01316 #ifdef RIO_DEBUG1
01317 INTERFACELOG << "[CNetInterface - Stop] Start" << endl;
01318 #endif
01319
01320 if( !m_Started )
01321 {
01322 #ifdef RIO_DEBUG2
01323 INTERFACELOG << "CNetInterface::Stop o objeto nao foi inicializado."
01324 << endl;
01325 #endif
01326
01327 #ifdef RIO_DEBUG1
01328 INTERFACELOG << "[CNetInterface - Stop] Finish1" << endl;
01329 #endif
01330
01331 return ERROR_NETTCP + ERROR_NOT_STARTED;
01332 }
01333
01334
01335 RioStatus = m_NetTcp->Stop();
01336 if( FAILED( RioStatus ) )
01337 {
01338 #ifdef RIO_DEBUG2
01339 INTERFACELOG << "CNetInterface::Stop erro 0x" << hex << RioStatus << dec
01340 << " (" << GetErrorDescription( RioStatus )
01341 << ") ao executar a funcao Stop do objeto da classe "
01342 << "CNetTcp" << endl;
01343 #endif
01344
01345 #ifdef RIO_DEBUG1
01346 INTERFACELOG << "[CNetInterface - Stop] Finish2" << endl;
01347 #endif
01348
01349 return RioStatus;
01350 }
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375 if( m_TransferInfo != NULL )
01376 delete m_TransferInfo;
01377
01378
01379 if( m_NetTcp != NULL )
01380 delete m_NetTcp;
01381
01382
01383
01384
01385
01386 if( m_NetUdp != NULL )
01387 delete m_NetUdp;
01388
01389
01390 m_Started = false;
01391
01392
01393
01394
01395
01396
01397
01398 #ifdef RIO_DEBUG2
01399 RioErr << "CNetInterface::Stop objeto finalizado com sucesso!" << endl;
01400 #endif
01401
01402 #ifdef RIO_DEBUG1
01403 INTERFACELOG << "[CNetInterface - Stop] Finish3" << endl;
01404 #endif
01405
01406 return S_OK;
01407 }
01408
01409
01410
01411
01412 void CNetInterface::getmyaddrport( int *ipaddr, int *port, int server )
01413 {
01414
01415 RioResult RioStatus;
01416
01417 #ifdef RIO_DEBUG2
01418
01419 in_addr ip;
01420 #endif
01421
01422 #ifdef RIO_DEBUG1
01423 INTERFACELOG << "[CNetInterface - getmyaddrport] Start" << endl;
01424 #endif
01425
01426 if( !m_Started )
01427 {
01428 #ifdef RIO_DEBUG2
01429 INTERFACELOG << "CNetInterface::getmyaddrport o objeto nao foi "
01430 << "inicializado." << endl;
01431 #endif
01432
01433 *ipaddr = 0;
01434 *port = 0;
01435
01436 #ifdef RIO_DEBUG1
01437 INTERFACELOG << "[CNetInterface - getmyaddrport] Finish1" << endl;
01438 #endif
01439
01440 return;
01441 }
01442
01443
01444 RioStatus = m_NetTcp->GetIPAndPort( ipaddr, port, server );
01445 #ifdef RIO_DEBUG2
01446 if( FAILED( RioStatus ) )
01447 {
01448 INTERFACELOG << "CNetInterface::getmyaddrport aviso: erro 0x"
01449 << RioStatus << " (" << GetErrorDescription( RioStatus )
01450 << ") ao executar a funcao GetIPAndPort do objeto da "
01451 << "classe CNet. Erro ignorado porque esta funcao nao "
01452 << "retorna um valor!" << endl;
01453 }
01454
01455 ip.s_addr = *ipaddr;
01456 INTERFACELOG << "CNetInterface::getmyaddrport retornando o IP "
01457 << inet_ntoa( ip ) << " e porta " << ntohs( *port )
01458 << " associado ao servidor com a ID " << server << "."
01459 << endl;
01460 #endif
01461
01462 #ifdef RIO_DEBUG1
01463 INTERFACELOG << "[CNetInterface - getmyaddrport] Finish2" << endl;
01464 #endif
01465 }
01466
01467
01468
01469
01470
01471
01472
01473 void CNetInterface::getalladdrport( int **ipaddr, int **port )
01474 {
01475
01476
01477 RioResult RioStatus;
01478
01479 #ifdef RIO_DEBUG1
01480 INTERFACELOG << "[CNetInterface - getalladdrport] Start" << endl;
01481 #endif
01482
01483 if( !m_Started )
01484 {
01485 #ifdef RIO_DEBUG2
01486 INTERFACELOG << "CNetInterface::getalladdrport o objeto nao foi "
01487 << "inicializado." << endl;
01488 #endif
01489
01490 *ipaddr = NULL;
01491 *port = NULL;
01492
01493 #ifdef RIO_DEBUG1
01494 INTERFACELOG << "[CNetInterface - getalladdrport] Finish1" << endl;
01495 #endif
01496
01497 return;
01498 }
01499
01500
01501 RioStatus = m_NetTcp->GetAllIPsAndPorts( ipaddr, port );
01502 #ifdef RIO_DEBUG2
01503 if( FAILED( RioStatus ) )
01504 {
01505 INTERFACELOG << "CNetInterface::getalladdrport aviso: erro 0x"
01506 << RioStatus << " (" << GetErrorDescription( RioStatus )
01507 << ") ao executar a funcao GetAllIPsAndPorts do objeto da "
01508 << "classe CNet. Erro ignorado porque esta funcao nao "
01509 << "retorna um valor!" << endl;
01510 }
01511
01512 INTERFACELOG << "CNetInterface::getalladdrport retornando todos os pares "
01513 << "IP, porta associados ao objeto da classe." << endl;
01514 #endif
01515
01516 #ifdef RIO_DEBUG1
01517 INTERFACELOG << "[CNetInterface - getalladdrport] Finish2" << endl;
01518 #endif
01519 }
01520
01521
01522
01523
01524 int CNetInterface::getipaddr()
01525 {
01526
01527 RioResult RioStatus;
01528
01529 int IP;
01530
01531 #ifdef RIO_DEBUG2
01532
01533 in_addr ip;
01534 #endif
01535
01536 #ifdef RIO_DEBUG1
01537 INTERFACELOG << "[CNetInterface - getipaddr] Start" << endl;
01538 #endif
01539
01540 if( !m_Started )
01541 {
01542 #ifdef RIO_DEBUG2
01543 INTERFACELOG << "CNetInterface::getipaddr o objeto nao foi "
01544 << "inicializado." << endl;
01545 #endif
01546
01547 #ifdef RIO_DEBUG1
01548 INTERFACELOG << "[CNetInterface - getipaddr] Finish1" << endl;
01549 #endif
01550
01551 return 0;
01552 }
01553
01554
01555 RioStatus = m_NetTcp->GetIPAndPort( &IP, NULL );
01556 #ifdef RIO_DEBUG2
01557 if( FAILED( RioStatus ) )
01558 {
01559 INTERFACELOG << "CNetInterface::getipaddr aviso: erro 0x"
01560 << RioStatus << " (" << GetErrorDescription( RioStatus )
01561 << ") ao executar a funcao GetIPAndPort do objeto da "
01562 << "classe CNet. Erro ignorado porque esta funcao nao "
01563 << "retorna um valor!" << endl;
01564 }
01565
01566 ip.s_addr = IP;
01567 INTERFACELOG << "CNetInterface::getipaddr retornando o IP "
01568 << inet_ntoa( ip ) << " associado ao servidor de despacho."
01569 << endl;
01570 #endif
01571
01572 #ifdef RIO_DEBUG1
01573 INTERFACELOG << "[CNetInterface - getipaddr] Finish2" << endl;
01574 #endif
01575
01576 return IP;
01577 }
01578
01579 void CNetInterface::SendCmd( int ipadr, int port, TTransferID TransferID,
01580 char *cmdp, int cmdl, callback_t callback,
01581 void *callbackparm )
01582 {
01583
01584 RioResult RioStatus;
01585
01586 #ifdef RIO_DEBUG2
01587
01588 in_addr ip;
01589
01590 ip.s_addr = ipadr;
01591 #endif
01592
01593 #ifdef RIO_DEBUG1
01594 INTERFACELOG << "[CNetInterface - SendCmd] Start" << endl;
01595 #endif
01596
01597
01598
01599 RioStatus = SendData( ipadr, port, callback, callbackparm, cmdp, cmdl,
01600 CMDDATA, true, TransferID );
01601
01602 #ifdef RIO_DEBUG2
01603 if( FAILED( RioStatus ) )
01604 {
01605 INTERFACELOG << "CNetInterface::SendCmd erro 0x" << hex << RioStatus
01606 << dec << " (" << GetErrorDescription( RioStatus ) << ") "
01607 << "ao enviar o comando ao endereco IP " << inet_ntoa( ip )
01608 << " e a porta " << ntohs( port ) << "." << endl;
01609 }
01610 #endif
01611
01612 #ifdef RIO_DEBUG2
01613 INTERFACELOG << "CNetInterface::SendCmd enviando um comando ao endereco "
01614 << "endereco IP " << inet_ntoa( ip ) << " e a porta "
01615 << ntohs( port ) << " para a transferencia com a ID "
01616 << TransferID << "." << endl;
01617 #endif
01618
01619 #ifdef RIO_DEBUG1
01620 INTERFACELOG << "[CNetInterface - SendCmd] Finish" << endl;
01621 #endif
01622 }
01623
01624
01625
01626 void CNetInterface::SendResult( int ipadr, int port, TTransferID TransferID,
01627 int result )
01628 {
01629
01630 RioResult RioStatus;
01631
01632 #ifdef RIO_DEBUG2
01633
01634 in_addr ip;
01635
01636 ip.s_addr = ipadr;
01637 #endif
01638
01639 #ifdef RIO_DEBUG1
01640 INTERFACELOG << "[CNetInterface - SendResult] Start" << endl;
01641 #endif
01642
01643
01644
01645 RioStatus = SendData( ipadr, port, NULL, NULL, ( char * ) &result,
01646 sizeof( int ), RESULTDATA, true, TransferID );
01647
01648 #ifdef RIO_DEBUG2
01649 if( FAILED( RioStatus ) )
01650 {
01651 INTERFACELOG << "CNetInterface::SendResult erro 0x" << hex << RioStatus
01652 << dec << " (" << GetErrorDescription( RioStatus ) << ") "
01653 << "ao enviar o resultado " << result << " ao endereco IP "
01654 << inet_ntoa( ip ) << " e a porta " << ntohs( port ) << "."
01655 << endl;
01656 }
01657 #endif
01658
01659 #ifdef RIO_DEBUG2
01660 INTERFACELOG << "CNetInterface::SendResult enviando o resultado " << result
01661 << " ao endereco IP " << inet_ntoa( ip ) << " e a porta "
01662 << ntohs( port ) << " para a transferencia com a ID "
01663 << TransferID << "." << endl;
01664 #endif
01665
01666 #ifdef RIO_DEBUG1
01667 INTERFACELOG << "[CNetInterface - SendResult] Finish" << endl;
01668 #endif
01669 }
01670
01671
01672
01673
01674 void CNetInterface::SendBlock( int ipadr, int port, TTransferID TransferID,
01675 char *bufadr, int buflen, callback_t callback,
01676 void *callbackparm, bool UseTcp,
01677 unsigned int VideoRate )
01678 {
01679
01680 RioResult RioStatus;
01681
01682 #ifdef RIO_DEBUG2
01683
01684 in_addr ip;
01685
01686 ip.s_addr = ipadr;
01687 #endif
01688
01689 #ifdef RIO_DEBUG1
01690 INTERFACELOG << "[CNetInterface - SendBlock] Start" << endl;
01691 #endif
01692
01693
01694
01695
01696 if( UseTcp )
01697 {
01698 #ifdef RIO_DEBUG2
01699 INTERFACELOG << "CNetInterface::SendBlock usando o TCP ao enviar o "
01700 << "bloco ao endereco IP " << inet_ntoa( ip ) << "e a "
01701 << "porta " << ntohs( port ) << " usando a transferencia "
01702 << "com a ID " << TransferID << "." << endl;
01703 #endif
01704 }
01705 else
01706 {
01707 #ifdef RIO_DEBUG2
01708 INTERFACELOG << "CNetInterface::SendBlock usando o UDP ao enviar o "
01709 << "bloco ao endereco IP " << inet_ntoa( ip ) << "e a "
01710 << "porta " << ntohs( port ) << " usando a transferencia "
01711 << "com a ID " << TransferID << "." << endl;
01712 #endif
01713
01714
01715
01716
01717
01718
01719 #ifdef RIO_DEBUG2
01720 INTERFACELOG << "CNetInterface::SendBlock erro ao enviar o bloco ao "
01721 << "endereco IP " << inet_ntoa( ip ) << " e a porta "
01722 << ntohs( port ) << " porque a copia em tempo real via "
01723 << "UDP ainda nao foi implementada. Chamando a callback "
01724 << "com o erro 0x" << hex
01725 << ERROR_NETINTERFACE + ERROR_INVALID_TRAFFIC << dec
01726 << " (" << GetErrorDescription( ERROR_NETINTERFACE +
01727 ERROR_INVALID_TRAFFIC )
01728 << ")." << endl;
01729 #endif
01730
01731
01732
01733
01734 callback( callbackparm, ERROR_NETINTERFACE + ERROR_INVALID_TRAFFIC );
01735
01736 #ifdef RIO_DEBUG1
01737 INTERFACELOG << "[CNetInterface - SendBlock] Finish1" << endl;
01738 #endif
01739
01740 return;
01741 }
01742
01743
01744
01745 RioStatus = SendData( ipadr, port, callback, callbackparm, bufadr, buflen,
01746 BLOCKDATA, UseTcp, TransferID );
01747
01748 #ifdef RIO_DEBUG2
01749 if( FAILED( RioStatus ) )
01750 {
01751 INTERFACELOG << "CNetInterface::SendBlock erro 0x" << hex << RioStatus
01752 << dec << " (" << GetErrorDescription( RioStatus ) << ") "
01753 << "ao enviar o bloco ao endereco IP " << inet_ntoa( ip )
01754 << " e a porta " << ntohs( port ) << "." << endl;
01755 }
01756
01757 INTERFACELOG << "CNetInterface::SendBlock enviado o bloco ao endereco "
01758 << "endereco IP " << inet_ntoa( ip ) << " e a porta "
01759 << ntohs( port ) << " para a transferencia com a ID "
01760 << TransferID << "." << endl;
01761 #endif
01762
01763 #ifdef RIO_DEBUG1
01764 INTERFACELOG << "[CNetInterface - SendBlock] Finish2" << endl;
01765 #endif
01766 }
01767
01768
01769
01770 TTransferID CNetInterface::ExpectBlock( char *buf, int len, callback_t callback,
01771 void *callbackparm, int sendack, int IP,
01772 int Port )
01773 {
01774
01775 RioResult RioStatus;
01776
01777 TTransferID TransferID;
01778
01779 #ifdef RIO_DEBUG2
01780
01781 in_addr ip;
01782
01783 ip.s_addr = IP;
01784 #endif
01785
01786 #ifdef RIO_DEBUG1
01787 INTERFACELOG << "[CNetInterface - ExpectBlock] Start" << endl;
01788 #endif
01789
01790 RioStatus = m_TransferInfo->NewTransfer( IP, Port, callback, callbackparm,
01791 NULL, NULL, buf, len,
01792 RECEIVINGDATA, NULLTRANSFERID,
01793 &TransferID );
01794 if( FAILED( RioStatus ) )
01795 {
01796
01797
01798
01799
01800
01801
01802
01803
01804 callback( callbackparm, ERROR_NETINTERFACE + ERROR_MEMORY );
01805
01806 #ifdef RIO_DEBUG2
01807 INTERFACELOG << "CNetInterface::ExpectBlock erro 0x" << hex << RioStatus
01808 << dec << " (" << GetErrorDescription( RioStatus ) << ") "
01809 << "ao criar uma estrutura para esperar por um bloco "
01810 << "associado ao endereco IP " << inet_ntoa( ip )
01811 << " e a porta " << ntohs( Port ) << "." << endl;
01812 #endif
01813
01814 #ifdef RIO_DEBUG1
01815 INTERFACELOG << "[CNetInterface - ExpectBlock] Finish1" << endl;
01816 #endif
01817
01818 return NULLTRANSFERID;
01819 }
01820
01821 #ifdef RIO_DEBUG2
01822 INTERFACELOG << "CNetInterface::ExpectBlock esperando bloco associado ao "
01823 << "endereco IP " << inet_ntoa( ip ) << " e a porta "
01824 << ntohs( Port ) << " com a ID " << TransferID << "." << endl;
01825 #endif
01826
01827 #ifdef RIO_DEBUG1
01828 INTERFACELOG << "[CNetInterface - ExpectBlock] Finish2" << endl;
01829 #endif
01830
01831 return TransferID;
01832 }
01833
01834 int CNetInterface::CancelExpect( TTransferID TransferID, int result )
01835 {
01836
01837 RioResult RioStatus;
01838
01839
01840
01841
01842
01843 #ifdef RIO_DEBUG1
01844 INTERFACELOG << "[CNetInterface - CancelExpect] Start" << endl;
01845 #endif
01846
01847
01848 RioStatus = m_TransferInfo->RemoveTransfer( TransferID );
01849 if( FAILED( RioStatus ) )
01850 {
01851 #ifdef RIO_DEBUG2
01852 INTERFACELOG << "CNetInterface::CancelExpect erro 0x" << hex
01853 << RioStatus << dec << " ("
01854 << GetErrorDescription( RioStatus ) << ") ao cancelar uma "
01855 << "estrutura com o ID " << TransferID << " para esperar "
01856 << "por dados." << endl;
01857 #endif
01858
01859 #ifdef RIO_DEBUG1
01860 INTERFACELOG << "[CNetInterface - CancelExpect] Finish1" << endl;
01861 #endif
01862
01863 return 0;
01864 }
01865
01866 #ifdef RIO_DEBUG2
01867 INTERFACELOG << "CNetInterface::CancelExpect cancelando a transferencia "
01868 << "com a ID " << TransferID << "." << endl;
01869 #endif
01870
01871 #ifdef RIO_DEBUG1
01872 INTERFACELOG << "[CNetInterface - CancelExpect] Finish2" << endl;
01873 #endif
01874
01875 return 1;
01876 }
01877
01878
01879
01880 void CNetInterface::TellId( int ipadr, int port, TTransferID TransferID,
01881 int tellip, int tellport,
01882 TTransferID tellTTransferID, callback_t callback,
01883 void *callbackparm )
01884 {
01885
01886 u32 TellIdCmd[ CMD_TELLID_SIZE ];
01887
01888 #ifdef RIO_DEBUG2
01889
01890 in_addr ip, ipt;
01891
01892 ip.s_addr = ipadr;
01893 ipt.s_addr = tellip;
01894 #endif
01895
01896 #ifdef RIO_DEBUG1
01897 INTERFACELOG << "[CNetInterface - TellId] Start" << endl;
01898 #endif
01899
01900
01901
01902
01903
01904
01905
01906 TellIdCmd[ 0 ] = htonl( CMD_TELLID );
01907 TellIdCmd[ 1 ] = tellip;
01908 TellIdCmd[ 2 ] = htonl( ntohs( ( short ) tellport ) );
01909 TellIdCmd[ 3 ] = htonl( tellTTransferID );
01910
01911
01912 SendCmd( ipadr, port, TransferID, ( char * ) TellIdCmd, CMD_TELLID_BYTESIZE,
01913 callback, callbackparm );
01914
01915 #ifdef RIO_DEBUG2
01916 INTERFACELOG << "CNetInterface::TellId Informando a ID " << tellTTransferID
01917 << " associada ao endereco IP " << inet_ntoa( ipt ) << " e a "
01918 << "porta " << ntohs( tellport ) << " ao endereco IP "
01919 << inet_ntoa( ip ) << " e a porta " << ntohs( port )
01920 << " usando a ID " << TransferID << "." << endl;
01921 #endif
01922
01923 #ifdef RIO_DEBUG1
01924 INTERFACELOG << "[CNetInterface - TellId] Finish" << endl;
01925 #endif
01926 }
01927
01928
01929 void CNetInterface::GetBlock( int ipadr, int port, TTransferID TransferID,
01930 char *bufadr, int buflen, callback_t callback,
01931 void *callbackparm )
01932 {
01933
01934
01935 TTransferID AuxTransferID;
01936
01937 int ipstor, portstor;
01938
01939 RioResult RioStatus;
01940
01941 #ifdef RIO_DEBUG2
01942
01943 in_addr ip;
01944 #endif
01945
01946 #ifdef RIO_DEBUG1
01947 INTERFACELOG << "[CNetInterface - GetBlock] Start" << endl;
01948 #endif
01949
01950
01951 RioStatus = m_NetTcp->GetIPAndPort( &ipstor, &portstor );
01952 if( FAILED( RioStatus ) )
01953 {
01954
01955
01956
01957
01958
01959
01960 #ifdef RIO_DEBUG2
01961 INTERFACELOG << "CNetInterface::GetBlock erro 0x" << hex << RioStatus
01962 << dec << " (" << GetErrorDescription( RioStatus )
01963 << ") ao obter o par IP, porta do storage." << endl;
01964 #endif
01965
01966 #ifdef RIO_DEBUG1
01967 INTERFACELOG << "[CNetInterface - GetBlock] Finish1" << endl;
01968 #endif
01969
01970 return;
01971
01972 }
01973
01974
01975
01976 AuxTransferID = ExpectBlock( bufadr, buflen, callback, callbackparm, 0,
01977 ipadr, port );
01978 if( AuxTransferID == NULLTRANSFERID )
01979 {
01980
01981
01982
01983 #ifdef RIO_DEBUG2
01984 INTERFACELOG << "CNetInterface::GetBlock erro ao criar uma nova "
01985 << "estrutura de transferencia para esperar por um bloco. "
01986 << endl;
01987 #endif
01988
01989 #ifdef RIO_DEBUG1
01990 INTERFACELOG << "[CNetInterface - GetBlock] Finish2" << endl;
01991 #endif
01992
01993 return;
01994 }
01995
01996 #ifdef RIO_DEBUG2
01997 ip.s_addr = ipadr;
01998 INTERFACELOG << "CNetInterface::GetBlock esperando por um bloco do IP "
01999 << inet_ntoa( ip ) << " e a porta " << ntohs( port )
02000 << " usando a estrutura de transferencia com a ID "
02001 << AuxTransferID << ". Identificacao da transferencia "
02002 << "passada como parametro " << TransferID << "." << endl;
02003
02004 INTERFACELOG << "CNetInterface::GetBlock Antes de executar TellId, usando "
02005 << "os parametros " << inet_ntoa( ip ) << ", " << ntohs( port )
02006 << ", " << TransferID;
02007 fflush( stderr );
02008 ip.s_addr = ipstor;
02009 INTERFACELOG << inet_ntoa( ip ) << ", " << ntohs( portstor ) << ", "
02010 << AuxTransferID << "NULL, NULL." << endl;
02011 #endif
02012
02013
02014 TellId( ipadr, port, TransferID, ipstor, portstor, AuxTransferID, NULL,
02015 NULL );
02016
02017 #ifdef RIO_DEBUG2
02018 INTERFACELOG << "CNetInterface::GetBlock Depois de executar TellId" << endl;
02019 #endif
02020
02021 #ifdef RIO_DEBUG1
02022 INTERFACELOG << "[CNetInterface - GetBlock] Finish2" << endl;
02023 #endif
02024 }
02025
02026
02027
02028
02029 void CNetInterface::ProcessTransferIDs( void *CmdParam, char *Command,
02030 int CommandSize )
02031 {
02032
02033 u32 *TellIdCmd;
02034
02035
02036
02037 TTellIdData *TellIdData;
02038
02039
02040 TellIdData = ( TTellIdData * ) CmdParam;
02041
02042 #ifdef RIO_DEBUG1
02043 STATICINTERFACELOG << "[CNetInterface - ProcessTransferID] Start" << endl;
02044 #endif
02045
02046
02047
02048
02049
02050
02051
02052 if( CommandSize != CMD_TELLID_BYTESIZE )
02053 {
02054
02055
02056
02057
02058
02059
02060
02061
02062
02063 #ifdef RIO_DEBUG2
02064 STATICINTERFACELOG << "CNetInterface::ProcessTransferIDs recebido um "
02065 << "comando com tamanho invalido. Tamanho = "
02066 << CommandSize << "bytes (esperado "
02067 << CMD_TELLID_BYTESIZE << " bytes), comando = "
02068 << hex;
02069 for( int i = 0; i < CommandSize; i++ )
02070 {
02071 STATICINTERFACELOG << "0x" << ( unsigned short ) Command[ i ];
02072 if( i != ( CommandSize - 1 ) )
02073 STATICINTERFACELOG << ",";
02074 else
02075 STATICINTERFACELOG << "." << dec << endl;
02076 }
02077 #endif
02078
02079 #ifdef RIO_DEBUG1
02080 STATICINTERFACELOG << "[CNetInterface - ProcessTransferID] Finish1"
02081 << endl;
02082 #endif
02083 }
02084 else
02085 {
02086
02087
02088
02089 TellIdCmd = ( u32 * ) Command;
02090
02091 TellIdCmd[ 0 ] = ntohl( TellIdCmd[ 0 ] );
02092
02093
02094 TellIdCmd[ 2 ] = htons( ( short ) ( ntohl( TellIdCmd[ 2 ] ) ) );
02095
02096
02097
02098 TellIdCmd[ 3 ] = ntohl( TellIdCmd[ 3 ] );
02099
02100
02101
02102 if( TellIdCmd[ 0 ] == CMD_TELLID )
02103 {
02104 #ifdef RIO_DEBUG2
02105 in_addr ip;
02106 ip.s_addr = TellIdCmd[ 1 ];
02107 STATICINTERFACELOG << "CNetInterface::ProcessTransferIDs tentando "
02108 << "enviar um bloco ao endereco IP "
02109 << inet_ntoa( ip ) << " e a porta "
02110 << ntohs( TellIdCmd[ 2 ] ) << ", usando a "
02111 << "transferencia com o ID " << TellIdCmd[ 3 ]
02112 << ", a partir da transferencia local com a ID "
02113 << TellIdData->SentDataTransfer << "." << endl;
02114 #endif
02115
02116
02117
02118
02119 TellIdData->NetInterface->SendBlock( TellIdCmd[ 1 ], TellIdCmd[ 2 ],
02120 ( TTransferID ) TellIdCmd[ 3 ], TellIdData->Buffer,
02121 TellIdData->BufferSize, NULL, NULL );
02122
02123 #ifdef RIO_DEBUG2
02124 STATICINTERFACELOG << "CNetInterface::ProcessTransferIDs um bloco "
02125 << " foi enviado ao endereco IP "
02126 << inet_ntoa( ip ) << " e a porta "
02127 << ntohs( TellIdCmd[ 2 ] ) << ", e foi "
02128 << "associado a transferencia com o ID "
02129 << TellIdCmd[ 3 ] << ", a partir da "
02130 << "transferencia local com a ID "
02131 << TellIdData->SentDataTransfer << "." << endl;
02132 #endif
02133
02134 #ifdef RIO_DEBUG1
02135 STATICINTERFACELOG << "[CNetInterface - ProcessTransferID] Finish2"
02136 << endl;
02137 #endif
02138
02139 }
02140 else
02141 {
02142
02143
02144
02145
02146
02147
02148
02149
02150
02151 #ifdef RIO_DEBUG2
02152 STATICINTERFACELOG << "CNetInterface::ProcessTransferIDs recebido "
02153 << "um comando com o tipo invalido, apesar de o "
02154 << "tamanho estar correto. Tamanho = "
02155 << CommandSize << "bytes, comando = " << hex;
02156 for( int i = 0; i < CommandSize; i++ )
02157 {
02158 STATICINTERFACELOG << "0x" << ( unsigned short ) Command[ i ];
02159 if( i != ( CommandSize - 1 ) )
02160 STATICINTERFACELOG << ",";
02161 else
02162 STATICINTERFACELOG << "." << dec << endl;
02163 }
02164 #endif
02165
02166 #ifdef RIO_DEBUG1
02167 STATICINTERFACELOG << "[CNetInterface - ProcessTransferID] Finish3"
02168 << endl;
02169 #endif
02170 }
02171 }
02172
02173 #ifdef RIO_DEBUG1
02174 STATICINTERFACELOG << "[CNetInterface - ProcessTransferID] Finish4" << endl;
02175 #endif
02176 }
02177
02178
02179
02180
02181
02182
02183 void CNetInterface::FinalizeSendBlocks( void *Param, int Result )
02184 {
02185
02186
02187
02188 TTellIdData *TellIdData;
02189
02190
02191 TellIdData = ( TTellIdData * ) Param;
02192
02193 #ifdef RIO_DEBUG1
02194 STATICINTERFACELOG << "[CNetInterface - FinalizeSendBlocks] Start" << endl;
02195 #endif
02196
02197 #ifdef RIO_DEBUG2
02198
02199 STATICINTERFACELOG << "CNetInterface::FinalizeSendBlocks callback chamada "
02200 << "com o erro 0x" << hex << Result << dec << " ( "
02201 << GetErrorDescription( Result ) << "), associada a "
02202 << "transferencia com a ID "
02203 << TellIdData->SentDataTransfer << "." << endl;
02204 #endif
02205
02206
02207
02208 #ifdef RIO_DEBUG2
02209 STATICINTERFACELOG << "CNetInterface::FinalizeSendBlocks como o servidor "
02210 << "enviou a mensagem RESULTDATA, entao todos os blocos "
02211 << "foram enviados (pois o servidor somente envia a "
02212 << "mensagem depois de todos os storages confirmarem "
02213 << "o envio dos blocos, mesmo que algum erro ocorra!"
02214 << "Chamando a callback passada a funcao ExpectTellID "
02215 << "com o resultado 0x" << hex << Result << dec
02216 << " (" << GetErrorDescription( Result ) << ")." << endl;
02217 #endif
02218
02219 TellIdData->callback( TellIdData->callbackparm, Result );
02220
02221 #ifdef RIO_DEBUG1
02222 STATICINTERFACELOG << "[CNetInterface - FinalizeSendBlocks] Finish" << endl;
02223 #endif
02224 }
02225
02226
02227
02228
02229 RioResult CNetInterface::FindIP( int *IP )
02230 {
02231 #ifdef RIO_DEBUG1
02232 RioErr << "[CNetInterface - FindIP] Start" << endl;
02233 #endif
02234
02235 struct if_nameindex *iflist = NULL;
02236 struct if_nameindex *listsave = NULL;
02237 struct ifreq ifreq;
02238 int auxsock = 0;
02239 struct sockaddr_in sa;
02240 char localhost_ip[IP_STRING_SIZE] = "";
02241
02242 if( ( auxsock = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
02243 {
02244 *IP = 0;
02245
02246 #ifdef RIO_DEBUG2
02247 RioErr << "CNetInterface::FindIP erro " << errno << " ("
02248 << strerror( errno ) << ") ao criar o socket temporario que "
02249 << "sera usado para obtermos os possiveis enderecos IP da "
02250 << "maquina" << endl;
02251 #endif
02252
02253 #ifdef RIO_DEBUG1
02254 RioErr << "[CNetInterface - FindIP] Finish1" << endl;
02255 #endif
02256
02257 return ERROR_NETINTERFACE + ERROR_SOCKET_CREATE;
02258 }
02259
02260
02261 listsave = if_nameindex();
02262 iflist = listsave;
02263
02264 for( ; *(char *)iflist != 0; iflist++ )
02265 {
02266
02267 strncpy( ifreq.ifr_name, iflist->if_name, IF_NAMESIZE );
02268
02269
02270 if( ioctl( auxsock, SIOCGIFADDR, &ifreq ) < 0 )
02271 {
02272
02273 if( errno != 99 )
02274 {
02275 *IP = 0;
02276
02277 #ifdef RIO_DEBUG2
02278 RioErr << "CNetInterface::FindIP erro " << errno << " ("
02279 << strerror( errno ) << ") ao executar a funcao ioctl "
02280 << "sobre o socket ao obtermos os possiveis enderecos "
02281 << "IP da maquina." << endl;
02282 #endif
02283
02284 #ifdef RIO_DEBUG1
02285 RioErr << "[CNetInterface - FindIP] Finish2" << endl;
02286 #endif
02287
02288 return ERROR_NETINTERFACE + ERROR_IOCTL_FAILED;
02289 }
02290 }
02291
02292
02293 memset( &sa, 0, sizeof( struct sockaddr_in ) );
02294 memcpy( &sa, &ifreq.ifr_addr, sizeof( struct sockaddr_in ) );
02295 strcpy( localhost_ip, inet_ntoa( sa.sin_addr ) );
02296
02297 #ifdef RIO_DEBUG2
02298 RioErr << "CNetInterface::FindIP endereco IP associado a placa: "
02299 << localhost_ip << endl;
02300 #endif
02301
02302 if( strcmp( "127.0.0.1", localhost_ip ) != 0 )
02303 break;
02304 }
02305
02306 if_freenameindex( listsave );
02307 close( auxsock );
02308
02309 *IP = sa.sin_addr.s_addr;
02310
02311 #ifdef RIO_DEBUG2
02312 RioErr << "CNetInterface::FindIP ip achado com sucesso. O IP escolhido "
02313 << "foi o " << inet_ntoa( sa.sin_addr ) << "." << endl;
02314 #endif
02315
02316 #ifdef RIO_DEBUG1
02317 RioErr << "[CNetInterface - FindIP] Finish3" << endl;
02318 #endif
02319
02320 return S_OK;
02321 }
02322
02323
02324
02325
02326
02327 TTransferID CNetInterface::ExpectTellId( char *buf, int len,
02328 callback_t callback,
02329 void *callbackparm, int IP, int Port )
02330 {
02331
02332
02333
02334 TTellIdData *TellIdData;
02335
02336 #ifdef RIO_DEBUG2
02337
02338 in_addr ip;
02339
02340 ip.s_addr = IP;
02341 #endif
02342
02343 #ifdef RIO_DEBUG1
02344 INTERFACELOG << "[CNetInterface - ExpectTellId] Finish3" << endl;
02345 #endif
02346
02347
02348
02349
02350 try
02351 {
02352 TellIdData = new TTellIdData;
02353 }
02354 catch( bad_alloc& ba )
02355 {
02356 #ifdef RIO_DEBUG2
02357 INTERFACELOG << "CNetInterface::ExpectTellId erro de alocacao de "
02358 << "memoria (bad_alloc, error = " << ba.what() << ") ao "
02359 << "criar uma nova estrutura TTellIdData. Retornando "
02360 << "um identificador invalido e chamando a callback com "
02361 << "o erro ERROR_NETINTERFACE + ERROR_MEMORY." << endl;
02362 #endif
02363
02364
02365 callback( callbackparm, ERROR_NETINTERFACE + ERROR_MEMORY );
02366
02367 #ifdef RIO_DEBUG1
02368 INTERFACELOG << "[CNetInterface - ExpectTellId] Finish1" << endl;
02369 #endif
02370
02371 return NULLTRANSFERID;
02372 }
02373
02374
02375
02376 TellIdData->Buffer = buf;
02377 TellIdData->BufferSize = len;
02378 TellIdData->callback = callback;
02379 TellIdData->callbackparm = callbackparm;
02380 TellIdData->NetInterface = this;
02381 TellIdData->SentDataTransfer = ExpectCmd( FinalizeSendBlocks,
02382 ( void * ) TellIdData,
02383 ProcessTransferIDs, IP, Port );
02384 if( TellIdData->SentDataTransfer == NULLTRANSFERID )
02385 {
02386 #ifdef RIO_DEBUG2
02387 INTERFACELOG << "CNetInterface::ExpectTellId erro ao executar a funcao "
02388 << "ExpectCmd. Retornando umum identificador invalido e "
02389 << "chamando a callback com o erro "
02390 << "ERROR_NETINTERFACE + ERROR_MEMORY." << endl;
02391 #endif
02392
02393
02394 delete TellIdData;
02395
02396
02397 callback( callbackparm, ERROR_NETINTERFACE + ERROR_MEMORY );
02398
02399 #ifdef RIO_DEBUG1
02400 INTERFACELOG << "[CNetInterface - ExpectTellId] Finish2" << endl;
02401 #endif
02402
02403 return NULLTRANSFERID;
02404 }
02405
02406 #ifdef RIO_DEBUG2
02407 INTERFACELOG << "CNetInterface::ExpectTellId esperando por uma ID do "
02408 << "endereco IP " << inet_ntoa( ip ) << " e da porta "
02409 << ntohs( Port ) << ", usando a transfencia com a ID "
02410 << TellIdData->SentDataTransfer << "." << endl;
02411 #endif
02412
02413 #ifdef RIO_DEBUG1
02414 INTERFACELOG << "[CNetInterface - ExpectTellId] Finish3" << endl;
02415 #endif
02416
02417 return TellIdData->SentDataTransfer;
02418 }
02419
02420
02421
02422 void CNetInterface::dumppkt( const char *label, char *data, int datal )
02423 {
02424 int off;
02425 int i;
02426 char *s;
02427 char line[100];
02428
02429 RioErr << label;
02430
02431 off = 0;
02432 do
02433 {
02434 s = line;
02435 s += sprintf(s,"%4.4x ", (unsigned) off);
02436
02437 for(i = 0; i < 16 && i < datal; i++, data++)
02438 {
02439 s += sprintf(s, "%2.2x", (unsigned)(*data & 0xff));
02440 if((i & 3) == 3)
02441 {
02442 *s++ = ' ';
02443 }
02444 }
02445
02446 *s++ = '\n';
02447 *s++ = '\0';
02448
02449 if(off > 0x20 && off < 0x5a0)
02450 ;
02451 else
02452 RioErr << line;
02453
02454 datal -= 16;
02455 off += 16;
02456 } while(datal > 0);
02457
02458 RioErr << endl;
02459
02460 }
02461
02462
02463
02464
02465 int CNetInterface::FreeBlock( unsigned int block_id, RioStreamType traffic )
02466 {
02467 #ifdef RIO_DEBUG1
02468 INTERFACELOG << "[CNetInterface - FreeBlock] Start" << endl;
02469 #endif
02470
02471 #ifdef RIO_DEBUG2
02472 INTERFACELOG << "[CNetInterface::FreeBlock] Esta funcao nao esta "
02473 << "implementada! Retornado o valor 0!" << endl;
02474 #endif
02475
02476 #ifdef RIO_DEBUG1
02477 INTERFACELOG << "[CNetInterface - FreeBlock] Finish" << endl;
02478 #endif
02479
02480 return 0;
02481 }
02482
02483
02484
02485
02486 int CNetInterface::FreePendentBlocks( bool useCache, int nBuffers )
02487 {
02488 #ifdef RIO_DEBUG1
02489 INTERFACELOG << "[CNetInterface - FreePendentBlocks] Start" << endl;
02490 #endif
02491
02492 #ifdef RIO_DEBUG2
02493 INTERFACELOG << "[CNetInterface::FreePendentBlocks] Esta funcao nao esta "
02494 << "implementada! Retornado o valor 0!" << endl;
02495 #endif
02496
02497 #ifdef RIO_DEBUG1
02498 INTERFACELOG << "[CNetInterface - FreePendentBlocks] Finish" << endl;
02499 #endif
02500
02501 return 0;
02502 }
02503
02504
02505
02506 bool CNetInterface::thereAreFragments( RioBlock block )
02507 {
02508 #ifdef RIO_DEBUG1
02509 INTERFACELOG << "[CNetInterface - thereAreFragments] Start" << endl;
02510 #endif
02511
02512 #ifdef RIO_DEBUG2
02513 INTERFACELOG << "[CNetInterface::thereAreFragments] Esta funcao nao esta "
02514 << "implementada! Retornado o valor false!" << endl;
02515 #endif
02516
02517 #ifdef RIO_DEBUG1
02518 INTERFACELOG << "[CNetInterface - thereAreFragments] Finish" << endl;
02519 #endif
02520
02521 return false;
02522 }
02523
02524
02525 void CNetInterface::Getmyaddr( int *my_address, int *my_port )
02526 {
02527 #ifdef RIO_DEBUG2
02528
02529 in_addr ip;
02530 #endif
02531
02532 #ifdef RIO_DEBUG1
02533 INTERFACELOG << "[CNetInterface - Getmyaddr] Start" << endl;
02534 #endif
02535
02536 getmyaddrport( my_address, my_port, 0 );
02537
02538 #ifdef RIO_DEBUG2
02539 ip.s_addr = *my_address;
02540 INTERFACELOG << "CNetInterface::Getmyaddr obtido o IP " << inet_ntoa( ip )
02541 << "e a porta " << ntohs( *my_port ) << " associada ao "
02542 << "servidor de despacho." << endl;
02543 #endif
02544
02545 #ifdef RIO_DEBUG1
02546 INTERFACELOG << "[CNetInterface - Getmyaddr] Finish" << endl;
02547 #endif
02548 }
02549
02550
02551
02552 void CNetInterface::setLogRotation( CLogRotation *LogRotation )
02553 {
02554 #ifdef RIO_DEBUG1
02555 INTERFACELOG << "[CNetInterface - setLogRotation] Start" << endl;
02556 #endif
02557
02558 if( !m_Started )
02559 {
02560 #ifdef RIO_DEBUG2
02561 INTERFACELOG << "CNetInterface::setLogRotation o objeto nao foi "
02562 << "inicializado." << endl;
02563 #endif
02564
02565 #ifdef RIO_DEBUG1
02566 INTERFACELOG << "[CNetInterface - setLogRotation] Finish1" << endl;
02567 #endif
02568
02569 return;
02570 }
02571
02572
02573 m_NetTcp->setLogRotation( LogRotation );
02574
02575
02576
02577
02578
02579
02580
02581
02582 #ifdef RIO_DEBUG1
02583 INTERFACELOG << "[CNetInterface - setLogRotation] Finish2" << endl;
02584 #endif
02585 }
02586
02587
02588
02589 TTransferID CNetInterface::ExpectCmd( callback_t callback, void *callbackparm,
02590 cmdcallback_t cmdcallback, int IP,
02591 int Port )
02592 {
02593
02594 RioResult RioStatus;
02595
02596 TTransferID TransferID;
02597
02598 #ifdef RIO_DEBUG2
02599
02600 in_addr ip;
02601
02602 ip.s_addr = IP;
02603 #endif
02604
02605 #ifdef RIO_DEBUG1
02606 INTERFACELOG << "[CNetInterface - ExpectCmd] Start" << endl;
02607 #endif
02608
02609 RioStatus = m_TransferInfo->NewTransfer( IP, Port, callback, callbackparm,
02610 cmdcallback, callbackparm,
02611 NULL, NULL, RECEIVINGDATA,
02612 NULLTRANSFERID, &TransferID );
02613 if( FAILED( RioStatus ) )
02614 {
02615
02616
02617
02618
02619
02620
02621
02622
02623 callback( callbackparm, ERROR_NETINTERFACE + ERROR_MEMORY );
02624
02625 #ifdef RIO_DEBUG2
02626 INTERFACELOG << "CNetInterface::ExpectCmd erro 0x" << hex << RioStatus
02627 << dec << " (" << GetErrorDescription( RioStatus ) << ") "
02628 << "ao criar uma estrutura para esperar por um comando "
02629 << "associado ao endereco IP " << inet_ntoa( ip )
02630 << " e a porta " << ntohs( Port ) << "." << endl;
02631 #endif
02632
02633 #ifdef RIO_DEBUG1
02634 INTERFACELOG << "[CNetInterface - ExpectCmd] Finish1" << endl;
02635 #endif
02636
02637 return NULLTRANSFERID;
02638 }
02639
02640 #ifdef RIO_DEBUG2
02641 INTERFACELOG << "CNetInterface::ExpectCmd Experando um comando do "
02642 << "endereco IP " << inet_ntoa( ip ) << " e da porta "
02643 << ntohs( Port ) << " usando a transferencia com a ID "
02644 << TransferID << "." << endl;
02645 #endif
02646
02647 #ifdef RIO_DEBUG1
02648 INTERFACELOG << "[CNetInterface - ExpectCmd] Finish2" << endl;
02649 #endif
02650
02651 return TransferID;
02652 }
02653
02654
02655
02656
02657
02658
02659 void CNetInterface::SendRst( int ipadr, int port, TTransferID TransferID,
02660 int result )
02661 {
02662
02663 RioResult RioStatus;
02664
02665 #ifdef RIO_DEBUG2
02666
02667 in_addr ip;
02668
02669 ip.s_addr = ipadr;
02670 #endif
02671
02672 #ifdef RIO_DEBUG1
02673 INTERFACELOG << "[CNetInterface - SendRst] Start" << endl;
02674 #endif
02675
02676
02677
02678 RioStatus = SendData( ipadr, port, NULL, NULL, ( char * ) &result,
02679 sizeof( int ), RSTDATA, true, TransferID );
02680
02681 #ifdef RIO_DEBUG2
02682 if( FAILED( RioStatus ) )
02683 {
02684 INTERFACELOG << "CNetInterface::SendRst erro 0x" << hex << RioStatus
02685 << dec << " (" << GetErrorDescription( RioStatus ) << ") "
02686 << "ao enviar o erro " << result << " ao endereco IP "
02687 << inet_ntoa( ip ) << " e a porta " << ntohs( port ) << "."
02688 << endl;
02689 }
02690
02691 INTERFACELOG << "CNetInterface::SendRst enviando o resultado " << result
02692 << " ao endereco IP " << inet_ntoa( ip ) << " e a porta "
02693 << ntohs( port ) << " para a transferencia com a ID "
02694 << TransferID << "." << endl;
02695 #endif
02696
02697
02698 #ifdef RIO_DEBUG1
02699 INTERFACELOG << "[CNetInterface - SendRst] Finish" << endl;
02700 #endif
02701 }
02702
02703
02704
02705 void CNetInterface::ProcessData( int IP, int Port, char *Data,
02706 unsigned int DataSize )
02707 {
02708
02709 RioResult RioStatus;
02710
02711 int IPTransfer;
02712
02713
02714 int PortTransfer;
02715
02716 callback_t FinalizeCallback;
02717
02718
02719 void *FinalizeCallbackParam;
02720
02721
02722
02723 cmdcallback_t ProcessCallback;
02724
02725
02726 void *ProcessCallbackParam;
02727
02728
02729 char *DataTransfer;
02730
02731 unsigned int DataTransferSize;
02732
02733
02734
02735 ETransferType TransferType;
02736
02737
02738
02739 in_addr ip, ipt;
02740
02741 SDataHeader DataHeader;
02742
02743
02744 bool RemoveTrasnfer;
02745
02746
02747 int Result;
02748
02749 #ifdef RIO_DEBUG1
02750 INTERFACELOG << "[CNetInterface - ProcessData] Start" << endl;
02751 #endif
02752
02753
02754 ip.s_addr = IP;
02755
02756
02757
02758 if( DataSize < ( DATAHEADERSIZE + 1 ) )
02759 {
02760
02761
02762
02763
02764 INTERFACELOG << "CNetInterface::ProcessData os dados nao foram "
02765 << "corretamente recebidos para a transferencia associada "
02766 << "ao IP " << inet_ntoa( ip ) << " e a porta "
02767 << ntohs( Port ) << ". Nenhuma callback sera chamada! "
02768 << "Ignorando a mensagem!" << endl;
02769
02770 #ifdef RIO_DEBUG1
02771 INTERFACELOG << "[CNetInterface - ProcessData] Finish1" << endl;
02772 #endif
02773
02774 return;
02775 }
02776
02777
02778 memcpy( &DataHeader, Data, DATAHEADERSIZE );
02779
02780
02781
02782 DataHeader.DataTransferID = ntohl( DataHeader.DataTransferID );
02783 DataHeader.DataType = ntohs( DataHeader.DataType );
02784 DataHeader.SourceTransferID = ntohl( DataHeader.SourceTransferID );
02785
02786
02787
02788
02789 Data = &Data[ DATAHEADERSIZE ];
02790 DataSize = DataSize - DATAHEADERSIZE;
02791
02792 #ifdef RIO_DEBUG2
02793 INTERFACELOG << "CNetInterface::ProcessData Recebida uma mensagem do IP "
02794 << inet_ntoa( ip ) << " e da porta " << ntohs( Port )
02795 << " para a estrutura de trasnferencia com o ID "
02796 << DataHeader.DataTransferID << " com o tipo "
02797 << DataHeader.DataType << " a partir da estrutura de "
02798 << "transferencia origem com a ID "
02799 << DataHeader.SourceTransferID << ". O conteudo da mensagem "
02800 << "possui " << DataSize << " bytes no total." << endl;
02801 #endif
02802
02803 RioStatus = m_TransferInfo->GetTransfer( DataHeader.DataTransferID,
02804 &IPTransfer, &PortTransfer,
02805 &FinalizeCallback,
02806 &FinalizeCallbackParam,
02807 &ProcessCallback,
02808 &ProcessCallbackParam,
02809 &DataTransfer, &DataTransferSize,
02810 &TransferType );
02811 if( FAILED( RioStatus ) )
02812 {
02813 INTERFACELOG << "CNetInterface::ProcessData erro 0x" << hex << RioStatus
02814 << dec << " (" << GetErrorDescription( RioStatus ) << ") "
02815 << "ao executar a funcao GetTransfer. O erro foi gerado "
02816 << "para o par IP " << inet_ntoa( ip ) << ", a porta "
02817 << ntohs( Port ) << ", com o Source ID "
02818 << DataHeader.SourceTransferID << " e o Destination ID "
02819 << DataHeader.DataTransferID << ". O pacote recebido tinha "
02820 << "um tamanho de " << DataSize << " bytes, sendo "
02821 << ", com esperado, " << DATAHEADERSIZE << " bytes usado "
02822 << "pelo cabecalho. Ignorando a mensagem!" << endl;
02823
02824 #ifdef RIO_DEBUG1
02825 INTERFACELOG << "[CNetInterface - ProcessData] Finish2" << endl;
02826 #endif
02827
02828 return;
02829 }
02830
02831
02832 ipt.s_addr = IPTransfer;
02833
02834
02835
02836 if( ( IPTransfer != 0 ) && ( PortTransfer != 0 ) &&
02837 ( ( IP != IPTransfer ) || ( Port != PortTransfer ) ) )
02838 {
02839 INTERFACELOG << "CNetInterface::ProcessData aviso: o IP "
02840 << inet_ntoa( ipt ) << " e a porta "
02841 << ntohs( PortTransfer ) << " associados a transferencia "
02842 << "com o ID " << DataHeader.DataTransferID << " nao sao "
02843 << "iguais ao IP " << inet_ntoa( ip ) << " e a porta "
02844 << ntohs( Port ) << " passados como parametros da funcao! "
02845 << "Ignorando a mensagem!" << endl;
02846
02847 #ifdef RIO_DEBUG1
02848 INTERFACELOG << "[CNetInterface - ProcessData] Finish3" << endl;
02849 #endif
02850
02851 return;
02852 }
02853
02854
02855 if( TransferType != RECEIVINGDATA )
02856 {
02857
02858
02859 INTERFACELOG << "CNetInterface::ProcessData a transferencia "
02860 << "associada ao IP " << inet_ntoa( ipt ) << " e a porta "
02861 << ntohs( PortTransfer ) << " nao e do tipo de "
02862 << "recebimento de dados. Nenhuma callback sera chamada! "
02863 << "Ignorando a mensagem!" << endl;
02864
02865 #ifdef RIO_DEBUG1
02866 INTERFACELOG << "[CNetInterface - ProcessData] Finish4" << endl;
02867 #endif
02868
02869 return;
02870 }
02871
02872
02873
02874
02875
02876
02877
02878
02879
02880
02881 switch( DataHeader.DataType )
02882 {
02883
02884
02885
02886
02887
02888 case RSTDATA:
02889
02890 if( DataSize != sizeof ( int ) )
02891 {
02892 INTERFACELOG << "CNetInterface::ProcessData a mensagem do tipo "
02893 << "RSTDATA recebida do par IP "
02894 << inet_ntoa( ip ) << " e a porta "
02895 << ntohs( Port ) << " estava com o tamanho "
02896 << "incorreto: foram recebidos " << DataSize
02897 << " bytes quando o correto seria "
02898 << sizeof( int ) << " bytes! Ignorando a mensagem!"
02899 << endl;
02900
02901 #ifdef RIO_DEBUG1
02902 INTERFACELOG << "[CNetInterface - ProcessData] Finish5" << endl;
02903 #endif
02904
02905 return;
02906 }
02907
02908
02909 memcpy( &Result, Data, sizeof( int ) );
02910
02911
02912 FinalizeCallback( FinalizeCallbackParam, Result );
02913
02914
02915 RemoveTrasnfer = true;
02916
02917 break;
02918
02919
02920
02921
02922
02923 case CMDDATA:
02924
02925
02926
02927 if( ProcessCallback == NULL )
02928 {
02929 INTERFACELOG << "CNetInterface::ProcessData a mensagem do tipo "
02930 << "CMDDATA recebida do par IP "
02931 << inet_ntoa( ip ) << " e a porta "
02932 << ntohs( Port ) << " foi enviada para uma "
02933 << "estrutura nao associada a um recebimento de "
02934 << "um comando! Ignorando a mensagem!" << endl;
02935
02936 #ifdef RIO_DEBUG1
02937 INTERFACELOG << "[CNetInterface - ProcessData] Finish6" << endl;
02938 #endif
02939
02940 return;
02941 }
02942
02943
02944
02945 ProcessCallback( ProcessCallbackParam, Data, DataSize );
02946
02947
02948 RemoveTrasnfer = false;
02949
02950 break;
02951
02952
02953
02954
02955
02956
02957
02958 case RESULTDATA:
02959
02960 if( DataSize != sizeof ( int ) )
02961 {
02962 INTERFACELOG << "CNetInterface::ProcessData a mensagem do tipo "
02963 << "RESULTDATA recebida do par IP "
02964 << inet_ntoa( ip ) << " e a porta "
02965 << ntohs( Port ) << " estava com o tamanho "
02966 << "incorreto: foram recebidos " << DataSize
02967 << " bytes quando o correto seria "
02968 << sizeof( int ) << " bytes! Ignorando a mensagem!"
02969 << endl;
02970
02971 #ifdef RIO_DEBUG1
02972 INTERFACELOG << "[CNetInterface - ProcessData] Finish7" << endl;
02973 #endif
02974
02975 return;
02976 }
02977
02978
02979 memcpy( &Result, Data, sizeof( int ) );
02980
02981
02982 FinalizeCallback( FinalizeCallbackParam, Result );
02983
02984
02985 RemoveTrasnfer = true;
02986
02987 break;
02988
02989
02990
02991 case BLOCKDATA:
02992
02993
02994
02995
02996
02997 if( DataTransfer == NULL )
02998 {
02999 INTERFACELOG << "CNetInterface::ProcessData a mensagem do tipo "
03000 << "BLOCKDATA recebida do par IP "
03001 << inet_ntoa( ip ) << " e a porta "
03002 << ntohs( Port ) << " para uma estrutura nao "
03003 << "associada ao recebimento de um bloco! "
03004 << "Ignorando a mensagem!" << endl;
03005
03006 #ifdef RIO_DEBUG1
03007 INTERFACELOG << "[CNetInterface - ProcessData] Finish8" << endl;
03008 #endif
03009
03010 return;
03011 }
03012
03013
03014 if( DataSize != DataTransferSize )
03015 {
03016 INTERFACELOG << "CNetInterface::ProcessData a mensagem do tipo "
03017 << "BLOCKDATA recebida do par IP "
03018 << inet_ntoa( ip ) << " e a porta "
03019 << ntohs( Port ) << " tem " << DataSize
03020 << " bytes, mas deveria ter " << DataTransferSize
03021 << " bytes! Ignorando a mensagem!" << endl;
03022
03023 #ifdef RIO_DEBUG1
03024 INTERFACELOG << "[CNetInterface - ProcessData] Finish9"
03025 << endl;
03026 #endif
03027
03028 return;
03029 }
03030
03031
03032 memcpy( DataTransfer, Data, DataSize );
03033
03034
03035
03036 FinalizeCallback( FinalizeCallbackParam, S_OK );
03037
03038
03039 RemoveTrasnfer = true;
03040
03041 break;
03042
03043
03044
03045 default:
03046 INTERFACELOG << "CNetInterface::ProcessData a mensagem de tipo "
03047 << DataHeader.DataType << " invalido recebida do par "
03048 << "IP " << inet_ntoa( ip ) << " e a porta "
03049 << ntohs( Port ) << ". Ignorando a mensagem!" << endl;
03050
03051 #ifdef RIO_DEBUG1
03052 INTERFACELOG << "[CNetInterface - ProcessData] Finish10" << endl;
03053 #endif
03054
03055 return;
03056
03057 break;
03058 }
03059
03060
03061
03062 if( RemoveTrasnfer )
03063 {
03064 RioStatus = m_TransferInfo->RemoveTransfer( DataHeader.DataTransferID );
03065 if( FAILED( RioStatus ) )
03066 {
03067 INTERFACELOG << "CNetInterface::ProcessData erro 0x" << hex
03068 << RioStatus << dec << " ("
03069 << GetErrorDescription( RioStatus ) << ") ao executar "
03070 << "a funcao RemoveTransfer. O erro foi gerado para "
03071 << "o par IP " << inet_ntoa( ip ) << " e a porta "
03072 << ntohs( Port ) << "." << endl;
03073 }
03074 }
03075
03076 #ifdef RIO_DEBUG1
03077 INTERFACELOG << "[CNetInterface - ProcessData] Finish11" << endl;
03078 #endif
03079
03080 return;
03081 }
03082
03083
03084
03085
03086 void CNetInterface::SendDataCompleted( int IP, int Port, void *Param )
03087 {
03088
03089
03090 TTransferID TransferID;
03091
03092 RioResult RioStatus;
03093
03094 int IPTransfer;
03095
03096
03097 int PortTransfer;
03098
03099 callback_t FinalizeCallback;
03100
03101
03102 void *FinalizeCallbackParam;
03103
03104
03105
03106 cmdcallback_t ProcessCallback;
03107
03108
03109 void *ProcessCallbackParam;
03110
03111
03112 char *Data;
03113
03114 unsigned int DataSize;
03115
03116
03117
03118 ETransferType TransferType;
03119
03120
03121
03122 in_addr ip, ipt;
03123
03124 #ifdef RIO_DEBUG1
03125 INTERFACELOG << "[CNetInterface - SendDataCompleted] Start" << endl;
03126 #endif
03127
03128
03129 TransferID = ( TTransferID ) ( TLTransferID ) Param;
03130
03131
03132 ip.s_addr = IP;
03133
03134 #ifdef RIO_DEBUG2
03135 INTERFACELOG << "CNetInterface::SendDataCompleted funcao chamada para "
03136 << "informar que o envio de dados ao IP " << inet_ntoa( ip )
03137 << " e a porta " << ntohs( Port ) << " foi finalizada com "
03138 << " sucesso. O envio estava associado a transferencia com "
03139 << " a ID " << TransferID << "." << endl;
03140 #endif
03141
03142 RioStatus = m_TransferInfo->GetTransfer( TransferID, &IPTransfer,
03143 &PortTransfer, &FinalizeCallback,
03144 &FinalizeCallbackParam,
03145 &ProcessCallback,
03146 &ProcessCallbackParam, &Data,
03147 &DataSize, &TransferType );
03148 if( FAILED( RioStatus ) )
03149 {
03150 INTERFACELOG << "CNetInterface::SendDataCompleted erro 0x" << hex
03151 << RioStatus << dec << " ("
03152 << GetErrorDescription( RioStatus ) << ") ao executar a "
03153 << "funcao GetTransfer. O erro foi gerado para o par IP "
03154 << inet_ntoa( ip ) << " e a porta " << ntohs( Port ) << "."
03155 << endl;
03156
03157 #ifdef RIO_DEBUG1
03158 INTERFACELOG << "[CNetInterface - SendDataCompleted] Finish1" << endl;
03159 #endif
03160
03161 return;
03162 }
03163
03164
03165 ipt.s_addr = IPTransfer;
03166
03167
03168
03169
03170
03171
03172 if( ( IP != IPTransfer ) || ( Port != PortTransfer ) )
03173 {
03174 INTERFACELOG << "CNetInterface::SendDataCompleted aviso: o IP "
03175 << inet_ntoa( ipt ) << " e a porta "
03176 << ntohs( PortTransfer ) << " associados a transferencia "
03177 << "com o ID " << TransferID << " nao sao iguais ao IP "
03178 << inet_ntoa( ip ) << " e a porta " << ntohs( Port )
03179 << " passados como parametros da funcao." << endl;
03180
03181 #ifdef RIO_DEBUG1
03182 INTERFACELOG << "[CNetInterface - SendDataCompleted] Finish2" << endl;
03183 #endif
03184
03185 return;
03186 }
03187
03188 if( TransferType != SENDINGDATA )
03189
03190 {
03191
03192
03193 INTERFACELOG << "CNetInterface::SendDataCompleted a transferencia "
03194 << "associada ao IP " << inet_ntoa( ipt ) << " e a porta "
03195 << ntohs( PortTransfer ) << " nao e do tipo de envio de "
03196 << "dados. A callback nao sera chamada e o buffer Data "
03197 << "nao sera removido!" << endl;
03198
03199 #ifdef RIO_DEBUG1
03200 INTERFACELOG << "[CNetInterface - SendDataCompleted] Finish3" << endl;
03201 #endif
03202
03203 return;
03204 }
03205
03206
03207
03208
03209
03210
03211
03212 if( FinalizeCallback != NULL )
03213 FinalizeCallback( FinalizeCallbackParam, S_OK );
03214
03215
03216 delete[] Data;
03217
03218
03219 RioStatus = m_TransferInfo->RemoveTransfer( TransferID );
03220 if( FAILED( RioStatus ) )
03221 {
03222 INTERFACELOG << "CNetInterface::SendDataCompleted erro 0x" << hex
03223 << RioStatus << dec << " ("
03224 << GetErrorDescription( RioStatus ) << ") ao executar a "
03225 << "funcao RemoveTransfer. O erro foi gerado para o par "
03226 << "IP " << inet_ntoa( ip ) << " e a porta "
03227 << ntohs( Port ) << "." << endl;
03228 }
03229
03230 #ifdef RIO_DEBUG1
03231 INTERFACELOG << "[CNetInterface - SendDataCompleted] Finish4" << endl;
03232 #endif
03233
03234 return;
03235 }
03236
03237
03238 void CNetInterface::ProcessError( int IP, int Port, RioResult RioError,
03239 const char *Msg, EErrorType ErrorType,
03240 void *Param )
03241 {
03242
03243
03244 TTransferID TransferID;
03245
03246 RioResult RioStatus;
03247
03248 int IPTransfer;
03249
03250
03251 int PortTransfer;
03252
03253 callback_t FinalizeCallback;
03254
03255
03256 void *FinalizeCallbackParam;
03257
03258
03259
03260 cmdcallback_t ProcessCallback;
03261
03262
03263 void *ProcessCallbackParam;
03264
03265
03266 char *Data;
03267
03268 unsigned int DataSize;
03269
03270
03271
03272 ETransferType TransferType;
03273
03274
03275 TTransferID DestTransferID;
03276
03277
03278
03279 in_addr ip, ipt;
03280
03281 #ifdef RIO_DEBUG1
03282 INTERFACELOG << "[CNetInterface - ProcessError] Start" << endl;
03283 #endif
03284
03285
03286
03287
03288
03289 TransferID = ( TTransferID ) ( TLTransferID ) Param;
03290
03291
03292 ip.s_addr = IP;
03293
03294 INTERFACELOG << "[ProcessError] " << Msg << ": erro 0x" << hex << RioError
03295 << dec << " (" << GetErrorDescription( RioError ) << ") "
03296 << "recebido do objeto de uma das classes de envio de dados. "
03297 << "O erro foi gerado para o par IP " << inet_ntoa( ip )
03298 << " e a porta " << ntohs( Port ) << "." << endl;
03299
03300
03301 switch( ErrorType )
03302 {
03303
03304
03305
03306
03307 case RECEIVEERROR:
03308 #ifdef RIO_DEBUG2
03309 INTERFACELOG << "CNetInterface::ProcessError Recebido o erro tipo "
03310 << "RECEIVEERROR para o par IP " << inet_ntoa( ip )
03311 << " e a porta " << ntohs( Port ) << ". Fechando "
03312 << "todas as conexoes de leitura!" << endl;
03313 #endif
03314
03315
03316
03317 RioStatus = m_TransferInfo->FinalizeAllTransfers( IP, Port,
03318 RECEIVINGDATA,
03319 RioError );
03320 if( FAILED( RioStatus ) )
03321 {
03322 #ifdef RIO_DEBUG2
03323 INTERFACELOG << "CNetInterface::ProcessError erro 0x" << hex
03324 << RioStatus << dec << " ("
03325 << GetErrorDescription( RioStatus ) << ") ao "
03326 << "executar a funcao FinalizeAllTransfers para o "
03327 << "par IP " << inet_ntoa( ip ) << " e a porta "
03328 << ntohs( Port ) << "." << endl;
03329
03330 #endif
03331
03332 #ifdef RIO_DEBUG1
03333 INTERFACELOG << "[CNetInterface - ProcessError] Finish1"
03334 << endl;
03335 #endif
03336
03337 return;
03338 }
03339 break;
03340
03341
03342
03343
03344
03345 case SENDERROR:
03346
03347
03348 if( TransferID != NULLTRANSFERID )
03349 {
03350 RioStatus = m_TransferInfo->GetTransfer( TransferID,
03351 &IPTransfer,
03352 &PortTransfer,
03353 &FinalizeCallback,
03354 &FinalizeCallbackParam,
03355 &ProcessCallback,
03356 &ProcessCallbackParam,
03357 &Data, &DataSize,
03358 &TransferType,
03359 &DestTransferID );
03360 if( FAILED( RioStatus ) )
03361 {
03362 INTERFACELOG << "CNetInterface::ProcessError erro 0x" << hex
03363 << RioStatus << dec << " ("
03364 << GetErrorDescription( RioStatus ) << ") ao "
03365 << "executar a funcao GetTransfer. O erro foi "
03366 << "gerado para o par IP " << inet_ntoa( ip )
03367 << " e a porta " << ntohs( Port ) << ", e a "
03368 << " transferencia com a ID " << TransferID
03369 << "." << endl;
03370
03371 #ifdef RIO_DEBUG1
03372 INTERFACELOG << "[CNetInterface - ProcessError] Finish2"
03373 << endl;
03374 #endif
03375
03376 return;
03377 }
03378
03379
03380 ipt.s_addr = IPTransfer;
03381
03382
03383
03384
03385 if( ( IP != IPTransfer ) || ( Port != PortTransfer ) )
03386 {
03387 INTERFACELOG << "CNetInterface::ProcessError aviso: o IP "
03388 << inet_ntoa( ipt ) << " e a porta "
03389 << ntohs( PortTransfer ) << " associados a "
03390 << "transferencia com o ID " << TransferID
03391 << " nao sao iguais ao IP " << inet_ntoa( ip )
03392 << " e a porta " << ntohs( Port )
03393 << " passados como parametros da funcao."
03394 << endl;
03395
03396 #ifdef RIO_DEBUG1
03397 INTERFACELOG << "[CNetInterface - ProcessError] Finish3"
03398 << endl;
03399 #endif
03400
03401 return;
03402 }
03403
03404
03405 if( TransferType != SENDINGDATA )
03406 {
03407 INTERFACELOG << "CNetInterface::ProcessError O tipo da "
03408 << "transferencia associada ao par IP "
03409 << inet_ntoa( ip ) << " e a porta "
03410 << ntohs( Port ) << ", com o ID " << TransferID
03411 << " nao e uma transferencia de envio de "
03412 << "dados!" << endl;
03413
03414 #ifdef RIO_DEBUG1
03415 INTERFACELOG << "[CNetInterface - ProcessError] Finish4"
03416 << endl;
03417 #endif
03418
03419 return;
03420 }
03421
03422
03423
03424
03425
03426
03427
03428
03429 SendRst( IP, Port, DestTransferID, RioError );
03430
03431
03432
03433
03434 if( FinalizeCallback != NULL )
03435 FinalizeCallback( FinalizeCallbackParam, RioError );
03436
03437
03438
03439 delete[] Data;
03440
03441
03442
03443 RioStatus = m_TransferInfo->RemoveTransfer( TransferID );
03444 if( FAILED( RioStatus ) )
03445 {
03446 #ifdef RIO_DEBUG2
03447 INTERFACELOG << "CNetInterface::ProcessError erro 0x" << hex
03448 << RioStatus << dec << " ("
03449 << GetErrorDescription( RioStatus ) << ") ao "
03450 << "executar a funcao RemoveTransfer. O erro "
03451 << "foi gerado para o par IP "
03452 << inet_ntoa( ip ) << " e a porta "
03453 << ntohs( Port ) << "." << endl;
03454 #endif
03455
03456 #ifdef RIO_DEBUG1
03457 INTERFACELOG << "[CNetInterface - ProcessError] Finish5"
03458 << endl;
03459 #endif
03460
03461 return;
03462
03463 }
03464 }
03465
03466
03467
03468 case OTHERERROR:
03469 #ifdef RIO_DEBUG2
03470 INTERFACELOG << "CNetInterface::ProcessError Recebido o erro "
03471 << "tipo OTHERERROR para o par IP "
03472 << inet_ntoa( ip ) << " e a porta "
03473 << ntohs( Port ) << ". Nenhuma acao precisa ser "
03474 << "tomada!" << endl;
03475 #endif
03476 break;
03477
03478
03479 default:
03480 #ifdef RIO_DEBUG2
03481 INTERFACELOG << "CNetInterface::ProcessError Foi passado um "
03482 << "tipo de erro " << ErrorType << " invalido ao "
03483 << "chamar a funcao para o par IP "
03484 << inet_ntoa( ip ) << " e a porta "
03485 << ntohs( Port ) << ". Chamada ignorada!" << endl;
03486 #endif
03487 break;
03488 }
03489
03490 #ifdef RIO_DEBUG1
03491 INTERFACELOG << "[CNetInterface - ProcessError] Finish6" << endl;
03492 #endif
03493 }
03494
03495
03496
03497 void CNetInterface::ConnectionClosed( int IP, int Port, EClosedType ClosedType )
03498 {
03499
03500
03501 RioResult RioError;
03502
03503 RioResult RioStatus;
03504
03505 #ifdef RIO_DEBUG1
03506 INTERFACELOG << "[CNetInterface - ConnectionClosed] Start" << endl;
03507 #endif
03508
03509 #ifdef RIO_DEBUG2
03510
03511 in_addr ip;
03512
03513 ip.s_addr = IP;
03514
03515 INTERFACELOG << "CNetInterface::ConnectionClosed Fechando todas as "
03516 << "conexoes ativas para o IP " << inet_ntoa( ip ) << " e a "
03517 << "porta " << ntohs( Port ) << " devido a: ";
03518 #endif
03519
03520 switch( ClosedType )
03521 {
03522
03523 case CONNECTIONCLOSEDLOCALLY:
03524 #ifdef RIO_DEBUG2
03525 INTERFACELOG << "conexao fechada localmente!";
03526 #endif
03527
03528 RioError = ERROR_NETINTERFACE + ERROR_CLOSED_CONNECTION;
03529 break;
03530
03531
03532 case CONNECTIONCLOSEDREMOTELLY:
03533 #ifdef RIO_DEBUG2
03534 INTERFACELOG << "conexao fechada pelo outro lado!";
03535 #endif
03536
03537 RioError = ERROR_NETINTERFACE + ERROR_CLOSED_CONNECTION;
03538 break;
03539
03540
03541 case CONNECTIONTIMEOUT:
03542 #ifdef RIO_DEBUG2
03543 INTERFACELOG << "conexao fechada por timeout!";
03544 #endif
03545
03546 RioError = ERROR_NETINTERFACE + ERROR_SOCKET_CLOSED;
03547 break;
03548
03549
03550
03551 default:
03552 #ifdef RIO_DEBUG2
03553 INTERFACELOG << "nao sei, pois o parametro " << ClosedType
03554 << "passado nao foi definido!";
03555 #endif
03556
03557 RioError = ERROR_NETINTERFACE + ERROR_INVALID_PARAM;
03558 break;
03559
03560 }
03561
03562 #ifdef RIO_DEBUG2
03563 INTERFACELOG << " Fechando todas as conexoes do endereco!" << endl;
03564
03565 if( this == NULL )
03566 INTERFACELOG << "CNetInterface::ConnectionClosed this NULL!"
03567 << endl;
03568
03569 if( m_TransferInfo == NULL )
03570 INTERFACELOG << "CNetInterface::ConnectionClosed m_TransferInfo NULL!"
03571 << endl;
03572 #endif
03573
03574
03575
03576 RioStatus = m_TransferInfo->FinalizeAllTransfers( IP, Port, UNDEFINEDTYPE,
03577 RioError );
03578 #ifdef RIO_DEBUG2
03579 if( FAILED( RioStatus ) )
03580 {
03581 INTERFACELOG << "CNetInterface::ConnectionClosed erro 0x" << hex
03582 << RioStatus << dec << " ("
03583 << GetErrorDescription( RioStatus ) << ") ao executar a "
03584 << "a funcao FinalizeAllTransfers para o par IP "
03585 << inet_ntoa( ip ) << " e a porta " << ntohs( Port ) << "."
03586 << endl;
03587 }
03588 #endif
03589
03590 #ifdef RIO_DEBUG1
03591 INTERFACELOG << "[CNetInterface - ConnectionClosed] Finish" << endl;
03592 #endif
03593 }
03594
03595
03596
03597 bool CNetInterface::FindIPAndPort( int IP, int Port, bool UseTcp )
03598 {
03599
03600
03601
03602 bool isIpAndPortFound;
03603
03604 RioResult RioStatus;
03605
03606 #ifdef RIO_DEBUG1
03607 INTERFACELOG << "[CNetInterface - FindIPAndPort] Start" << endl;
03608 #endif
03609
03610 #ifdef RIO_DEBUG2
03611
03612 in_addr ip;
03613
03614 ip.s_addr = IP;
03615 #endif
03616
03617 #ifdef RIO_DEBUG2
03618 INTERFACELOG << "CNetInterface::FindIPAndPort procurando pelo IP "
03619 << inet_ntoa( ip ) << " e a porta " << ntohs( Port )
03620 << "." << endl;
03621 #endif
03622
03623
03624
03625
03626 if( UseTcp )
03627 {
03628
03629 RioStatus = m_NetTcp->FindIPAndPort( IP, Port, &isIpAndPortFound );
03630 }
03631 else
03632 {
03633
03634
03635
03636
03637
03638
03639
03640
03641 RioStatus = ERROR_NETUDP + ERROR_NOT_IMPLEMENTED;
03642 }
03643
03644 if( FAILED( RioStatus ) )
03645 {
03646 #ifdef RIO_DEBUG2
03647 INTERFACELOG << "CNetInterface::FindIPAndPort erro 0x" << hex
03648 << RioStatus << dec << " ("
03649 << GetErrorDescription( RioStatus ) << ") ao executar a "
03650 << "a funcao FindIPAndPort para o par IP "
03651 << inet_ntoa( ip ) << " e a porta " << ntohs( Port ) << "."
03652 << endl;
03653 #endif
03654
03655 isIpAndPortFound = false;
03656 }
03657
03658 #ifdef RIO_DEBUG2
03659 INTERFACELOG << "CNetInterface::FindIPAndPort o IP " << inet_ntoa( ip )
03660 << " e a porta " << ntohs( Port )
03661 << ( ( isIpAndPortFound ) ? " foram" : " nao foram" )
03662 << " achados!" << endl;
03663 #endif
03664
03665 #ifdef RIO_DEBUG1
03666 INTERFACELOG << "[CNetInterface - FindIPAndPort] Finish" << endl;
03667 #endif
03668
03669 return isIpAndPortFound;
03670 }
03671
03672