📄 ipcclient.cpp
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/// IpcClient.cpp: implementation of the CIpcClient class.//////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "IpcClient.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////#include <sys/types.h>#include <unistd.h>//#include <sys/socket.h>//#include <netinet/in.h>#include <arpa/inet.h>#include "../../Utility/TcpHead.h"#include "../../Utility/StringHelper.h"CIpcClient::CIpcClient(): m_dwProcId(0), m_serverSockData(0), m_serverSockEvent(0){ InitializeCriticalSection(&m_csData); m_sockData = INVALID_SOCKET; m_sockEvent = INVALID_SOCKET; m_dwProcId =getpid(); }CIpcClient::~CIpcClient(){ printf("client exiting ...............\n"); DisConnect(); s_bStopListenEvent=true; DeleteCriticalSection(&m_csData);}void CIpcClient::DisConnect(){ if(m_sockData != INVALID_SOCKET) { vector<string> vectParam,vectRtn; IpcCall(ID_TYPE_EXIT,NULL,vectParam,vectRtn); close(m_sockData); } if(m_sockEvent != INVALID_SOCKET) close(m_sockEvent); m_sockData = INVALID_SOCKET;}DWORD SockEventProc(LPVOID IpParam){ SOCKET sockAcept = (SOCKET)IpParam; CTcpFromClient tcpFromClt; CTcpFromServer tcpFromSvr; printf("i have come listening event....\n"); while(!s_bStopListenEvent) { printf("listening event....\n"); //IntelMobileChar vectParam[NAMELENGH]; //vector<string> vectParam, vectRtn; //vector<string> vectRtn; //CTcpFromServer m_tcpFromSvr; //int nRtn = recv(sockAcept, vectParam, NAMELENGH, 0); //int nRtn = tcpFromClt.Recv(sockAcept, vectParam); IntelMobileChar vectParam[NAMELENGH]; vector<string> vectRtn; CTcpFromServer m_tcpFromSvr; int nRtn = m_tcpFromSvr.Recv( sockAcept, vectRtn); if(nRtn <= 0) { printf("error\n"); break; } /* printf("Event is %s\n", vectParam); HANDLE handle; string handleStr(vectParam); CStringHelper::Str2Param(handleStr, handle); try { if (!SetEvent(handle)) { cout << "SetEvnet Fail!" << endl; } else { cout << "SetEvent Success!" << endl; } } */ if( strcmp(vectRtn[0].c_str(), EVENTHEAD) ==0 ) { long lType; void* pObj; CStringHelper::Str2Param(vectRtn[1], pObj); CStringHelper::Str2Param(vectRtn[2], (long &)lType); cout << " SockEventProc " << " pSvrObj " << pObj << " lType " << lType << endl; try { /* CClientImplEventManager &eventMgr = CClientImplEventManager::GetClientImplEventManagerInst(); if(eventMgr.FireEvent(eType, pSvrObj)) { cout << "FireEvent Sucess" << endl; } else { cout << "FireEvent Fail" << endl; } */ time_t t = ::time(NULL); EventClientImpl *pEventSubject = reinterpret_cast<EventClientImpl*>(pObj); bool bRet = pEventSubject->EventFired(lType, t); if(bRet) { cout << "FireEvent Sucess" << endl; } else { cout << "FireEvent Fail" << endl; } } catch(...) { printf("fire event has exception\n"); } } } // return 0; DWORD retval = 0; ExitThread(retval);}void CIpcClient::Connect(){ CreateConnect( string(DATASOCKET) ); CreateConnect( string(EVENTSOCKET) );}LONG CIpcClient::IpcCall(int nDataType, void *pSvrObj, vector<string> &vectParam, vector<string> &vectRtn) //CTcpFromClient &tcpClt, vector<string> &vectParam, CTcpFromServer &tcpSvr, vector<string> &vectRtn){ EnterCriticalSection(&m_csData); LONG exceptionCode; m_tcpFromClt.m_nDataType = nDataType; m_tcpFromClt.m_pSvrObj = pSvrObj; /* IPC DEBUG Information */ /* printf("IpcCall DataType %d, SvrObj %x Parameters = ", nDataType, pSvrObj); vector<string>::const_iterator ittr; for ( ittr = vectParam.begin(); ittr < vectParam.end(); ++ittr ) { printf(" %s",(*ittr).c_str()); } printf("\r\n"); */ // if (NULL!=vectParam[0].c_str())printf("param is %s\n", vectParam[0].c_str()); //nhu: measure the time struct timeval tv; gettimeofday(&tv, NULL); //cout << "before Send: " << tv.tv_sec << "." << tv.tv_usec << endl; m_tcpFromClt.Send(m_sockData, vectParam); gettimeofday(&tv, NULL); //cout << "after Send:" << tv.tv_sec << "." << tv.tv_usec << endl; //printf("%s\t%s\n",m_tcpFromClt.m_nDataType,m_tcpFromClt.m_pSvrObj); gettimeofday(&tv, NULL); //cout << "before Recv" << tv.tv_sec << "." << tv.tv_usec << endl; m_tcpFromSvr.SetExceptionCode(0); int nRtn = m_tcpFromSvr.Recv(m_sockData, vectRtn); gettimeofday(&tv, NULL); //cout << "after Recv" << tv.tv_sec << "." << tv.tv_usec << endl; exceptionCode = m_tcpFromSvr.GetExceptionCode(); LeaveCriticalSection(&m_csData); if(nRtn <= 0) //error { //XXX: should return IPC error return -1; }// printf("IPC CALL exceptionCode %x\n", exceptionCode); return (LONG)exceptionCode;}void CIpcClient::CreateConnect(string socketType){ EnterCriticalSection(&m_csData); printf("socket type %s\n", socketType.c_str()); SOCKET *pSocket; int *pServerSocket; string nameSocket; if( strcmp( socketType.c_str(), DATASOCKET)==0 ) { pSocket = &m_sockData; pServerSocket = &m_serverSockData; nameSocket = DATASOCKET; } else if( strcmp(socketType.c_str(), EVENTSOCKET) ==0 ) { pSocket = &m_sockEvent; pServerSocket = &m_serverSockEvent; nameSocket = EVENTSOCKET; } else { LeaveCriticalSection(&m_csData); return; } m_tcpFromClt.m_nDataType = 0; m_tcpFromClt.m_pSvrObj = NULL; vector<string> vectParam, vectRtn; *pSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //nhu: trun off the Nagle Algo. on this socket. int val = 0; int len = sizeof(val); int ret; ret = getsockopt(*pSocket, IPPROTO_TCP, TCP_NODELAY, (void*) &val, &len); if(ret < 0) printf("getsocketopt error\n"); else printf("TCP_NODELAY: %d\n", val); const int ival = 1; len = sizeof(ival); ret = setsockopt(*pSocket, IPPROTO_TCP, TCP_NODELAY, (const void*) &ival, len); if(ret < 0) printf("setsocketopt error\n"); val = 0; len = sizeof(val); ret = getsockopt(*pSocket, IPPROTO_TCP, TCP_NODELAY, (void*) &val, &len); if(ret < 0) printf("getsocketopt error\n"); else printf("TCP_NODELAY: %d\n", val); char szHost[MAX_PATH]; memset(szHost, 0, MAX_PATH); int nRtn = gethostname(szHost, MAX_PATH); hostent *pHost = gethostbyname(szHost); if(NULL == pHost) //nRtn = WSAGetLastError(); nRtn =-1; sockaddr_in sa; sa.sin_family = AF_INET;// memcpy((void *)&(sa.sin_addr), pHost->h_addr_list[0], pHost->h_length);// i519 will sengment in this.... sa.sin_addr.s_addr = inet_addr("127.0.0.1"); // for Ubuntu Linux// sa.sin_addr.s_addr = inet_addr("10.239.34.216"); //for shxpwfli-WinXP server sa.sin_port = htons(nPortListenFunc); int SOCKET_ERROR=-1; //while(SOCKET_ERROR == connect( *pSocket, (sockaddr *)&sa, sizeof(sockaddr))) if(SOCKET_ERROR == connect( *pSocket, (sockaddr *)&sa, sizeof(sockaddr))) { printf("client try to setup server\n"); pid_t pd=fork(); printf("%d\n",pd); if( pd==0) { printf("i want find server program and set up "); //execve("IntelMobile_Server",NULL,NULL); int ret=execlp("./IntelMobile_Server",(char*)0); printf("return %d\n", ret); } while(SOCKET_ERROR == connect( *pSocket, (sockaddr *)&sa, sizeof(sockaddr))) { nRtn =-1; sleep(1); printf("connecting........\n"); } } printf("server has setup\n"); //IntelMobileChar socketDataName[NAMELENGH], socketEventName[NAMELENGH]; m_tcpFromSvr.Recv( *pSocket, vectRtn); //recv(m_sockData, socketDataName,NAMELENGH,0); CStringHelper::Str2Param(vectRtn[0], *pServerSocket); printf("datasocket= %d\n",*pServerSocket); vectParam.push_back( CStringHelper::Param2Str( m_dwProcId ) ); vectParam.push_back( CStringHelper::Param2Str( nameSocket ) ); printf("send id %s\n",vectParam[0].c_str()); printf("send name %s\n",vectParam[1].c_str()); m_tcpFromClt.Send(*pSocket, vectParam); //send(m_sockData, "socketData", NAMELENGH,0); if( strcmp(socketType.c_str(), EVENTSOCKET) ==0 ) { DWORD dwThread=0; HANDLE hThread = CreateThread(NULL, 0, SockEventProc, (void *)(*pSocket), CREATE_DETACHED, &dwThread); if(hThread) { // ::WaitForSingleObject( hThread, INFINITE ); // ::CloseHandle(hThread); } else CloseHandle(hThread); } LeaveCriticalSection(&m_csData);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -