diff options
author | kos <kos@dev3> | 2013-12-03 07:20:50 (GMT) |
---|---|---|
committer | kos <kos@dev3> | 2013-12-03 07:20:50 (GMT) |
commit | 7302a1cf53b36472514c668de7ea9b201c1ba6fb (patch) | |
tree | 81ac27c708aed338bd1f4df48977ac5081f2c3bf | |
parent | 418d99918a8cb223d2494a07abf2a77247101272 (diff) |
support multiple transcoding.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | config.h.in | 3 | ||||
-rw-r--r-- | configure.ac | 13 | ||||
-rw-r--r-- | src/Makefile.am | 9 | ||||
-rw-r--r-- | src/Makefile.manual | 9 | ||||
-rw-r--r-- | src/eDemuxPumpThread.cpp | 11 | ||||
-rw-r--r-- | src/eFilePumpThread.cpp | 8 | ||||
-rw-r--r-- | src/eHostInfoMgr.cpp | 235 | ||||
-rw-r--r-- | src/eHostInfoMgr.h | 47 | ||||
-rw-r--r-- | src/eNetworkPumpThread.cpp | 2 | ||||
-rw-r--r-- | src/eParser.cpp | 13 | ||||
-rw-r--r-- | src/eTransCodingDevice.cpp | 37 | ||||
-rw-r--r-- | src/eTransCodingDevice.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 69 | ||||
-rw-r--r-- | src/uPosixSharedMemory.h | 80 | ||||
-rw-r--r-- | src/uStringTool.h | 2 |
16 files changed, 472 insertions, 69 deletions
@@ -23,4 +23,5 @@ depcomp install-sh missing stamp-h1 +config.h.in~ diff --git a/config.h.in b/config.h.in index e8e6468..ef76535 100644 --- a/config.h.in +++ b/config.h.in @@ -6,6 +6,9 @@ /* Define to 1 if you have the `pthread' library (-lpthread). */ #undef HAVE_LIBPTHREAD +/* Define to 1 if you have the `rt' library (-lrt). */ +#undef HAVE_LIBRT + /* Define to 1 if you have the <memory.h> header file. */ #undef HAVE_MEMORY_H diff --git a/configure.ac b/configure.ac index 216ff3d..246f9cc 100644 --- a/configure.ac +++ b/configure.ac @@ -2,8 +2,8 @@ # Process this file with autoconf to produce a configure script. #AC_PREREQ([2.69]) -AC_INIT([transtreamproxy], [2.0.0], [kos@dev3]) -AM_INIT_AUTOMAKE([transtreamproxy], [2.0.0]) +AC_INIT([transtreamproxy], [2.1.0], [kos@dev3]) +AM_INIT_AUTOMAKE([transtreamproxy], [2.1.0]) #AC_CONFIG_SRCDIR([main.cpp]) AC_CONFIG_HEADERS([config.h]) @@ -19,9 +19,16 @@ AC_PROG_CXX AC_CONFIG_MACRO_DIR([m4]) AC_CHECK_LIB(pthread, pthread_create) -PTHREAD_LIBS=-pthread +PTHREAD_LIBS=-lpthread AC_SUBST(PTHREAD_LIBS) +AC_CHECK_LIB(rt, shm_open) +RT_LIBS=-lrt +AC_SUBST(RT_LIBS) + +WARN_CFLAGS=-Wno-unused-result +AC_SUBST(WARN_CFLAGS) + # Checks for libraries. AC_SUBST(PLATFORM) diff --git a/src/Makefile.am b/src/Makefile.am index 03775d7..8bd34c3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,5 @@ ## Process this file with automake to produce Makefile.in + bin_PROGRAMS = transtreamproxy transtreamproxy_SOURCES = \ main.cpp \ @@ -9,8 +10,8 @@ transtreamproxy_SOURCES = \ eTransCodingDevice.cpp \ eFilePumpThread.cpp \ eNetworkPumpThread.cpp \ - eDemuxPumpThread.cpp - -transtreamproxy_LDADD = $(LIBLTDL) @PTHREAD_LIBS@ - + eDemuxPumpThread.cpp \ + eHostInfoMgr.cpp +transtreamproxy_CXXFLAGS = $(LIBSDL_CFLAGS) @WARN_CFLAGS@ +transtreamproxy_LDADD = $(LIBLTDL) @PTHREAD_LIBS@ @RT_LIBS@ diff --git a/src/Makefile.manual b/src/Makefile.manual index 3cfd8ba..0db375f 100644 --- a/src/Makefile.manual +++ b/src/Makefile.manual @@ -8,7 +8,7 @@ include .config -VER = v2.0 +VER = v2.1 REV = $(shell cat .revision) BIN = transtreamproxy @@ -22,12 +22,14 @@ SRC = \ eFilePumpThread.cpp \ eNetworkPumpThread.cpp \ eDemuxPumpThread.cpp \ + eHostInfoMgr.cpp \ CFLAGS ?= LDFLAGS ?= #CFLAGS += -DNORMAL_STREAMPROXY +CFLAGS += -Wno-unused-result ifeq ($(SILENT_BUILD_MODE),y) Q=@ @@ -55,7 +57,7 @@ STRIP= $(CROSS)strip RM= rm -f CFLAGS += -I$(BUILD_TOP)/usr/include -LDFLAGS += -L$(BUILD_TOP)/usr/lib -lpthread +LDFLAGS += -L$(BUILD_TOP)/usr/lib -lpthread -lrt OBJ=$(SRC:.cpp=.o) @@ -86,6 +88,9 @@ install: erase:clean $(Q)$(RM) .revision +autotools: + @cd ../ && aclocal && autoreconf && automake && ./configure && make clean all && make clean + .showinfo: @echo "-----------------------------------------------------" @echo " [ BUILD ENVIRONMENT ] " diff --git a/src/eDemuxPumpThread.cpp b/src/eDemuxPumpThread.cpp index a85ab17..f70777e 100644 --- a/src/eDemuxPumpThread.cpp +++ b/src/eDemuxPumpThread.cpp @@ -57,15 +57,6 @@ void eDemuxPumpThread::Run() pollevt.events = POLLOUT; fd_set demux_readfds; -#ifdef NORMAL_STREAMPROXY - const char *c = "\ -HTTP/1.0 200 OK\r\n\ -Connection: close\r\n\ -Content-Type: video/mpeg\r\n\ -Server: stream_enigma2\r\n\ -\r\n"; - wc = write(1, c, strlen(c)); -#endif mTermFlag = true; #ifdef DEBUG_LOG @@ -95,7 +86,7 @@ Server: stream_enigma2\r\n\ #endif if(wc != rc) { #ifdef DEBUG_LOG - LOG("need retry write.. rc[%d], wc[%d]", rc, wc); + LOG("need rewrite.. rc[%d], wc[%d]", rc, wc); #endif int read_len = rc; fd_set device_writefds; diff --git a/src/eFilePumpThread.cpp b/src/eFilePumpThread.cpp index 8a16248..93a6072 100644 --- a/src/eFilePumpThread.cpp +++ b/src/eFilePumpThread.cpp @@ -83,7 +83,7 @@ void eFilePumpThread::Run() wc = write(mDeviceFd, buffer, rc); if(wc != rc) { #ifdef DEBUG_LOG - LOG("write fail.. rc[%d], wc[%d]", rc, wc); + LOG("need rewrite.. rc[%d], wc[%d]", rc, wc); #endif int read_len = rc; fd_set device_writefds; @@ -95,10 +95,16 @@ void eFilePumpThread::Run() if (select(mDeviceFd + 1, 0, &device_writefds, 0, 0) < 0) break; wc = write(mDeviceFd, buffer + i, read_len - i); +#ifdef DEBUG_LOG + LOG("-> retry write.. rc[%d], wc[%d]", read_len - i, wc); +#endif } } } } +#ifdef DEBUG_LOG + LOG("file pump stoped.", rc); +#endif Close(); mTermFlag = false; } diff --git a/src/eHostInfoMgr.cpp b/src/eHostInfoMgr.cpp new file mode 100644 index 0000000..81f423d --- /dev/null +++ b/src/eHostInfoMgr.cpp @@ -0,0 +1,235 @@ +/* + * eHostInfoMgr.cpp + * + * Created on: 2013. 11. 14. + * Author: kos + */ + +#include <sstream> +#include <fstream> + +#include <stdio.h> +#include <signal.h> +#include <string.h> +#include <dirent.h> +#include <arpa/inet.h> +#include <sys/socket.h> + +#include "eHostInfoMgr.h" +#include "ePreDefine.h" + +#define KILLPROC(PID) { kill(PID, SIGINT); } + +using namespace std; +//------------------------------------------------------------------------------- + +#ifdef DEBUG_LOG +//#undef LOG +//#define LOG(X,...) { do{}while(0); } +#endif +//------------------------------------------------------------------------------- + +eHostInfoMgr::eHostInfoMgr(std::string aName, int aInfoCount) + : mInfoCount(aInfoCount) +{ + mSemId = 0; + mSemName = "/" + aName + "_sem"; + + mShmFd = 0; + mShmSize = sizeof(eHostInfo) * aInfoCount; + mShmName = "/" + aName + "_shm"; + + mShmData = 0; +} +//------------------------------------------------------------------------------- + +eHostInfoMgr::~eHostInfoMgr() +{ +} +//------------------------------------------------------------------------------- + +bool eHostInfoMgr::IsTerminated(std::vector<int>& aList, int aPid) +{ + for (int i = 0; i < aList.size(); ++i) { + if (aList[i] == aPid) { + return false; + } + } + return true; +} +//------------------------------------------------------------------------------- + +bool eHostInfoMgr::Init() +{ + if (Open() == false) { + return false; + } +#ifdef DEBUG_LOG + LOG("shm-info : fd [%d], name [%s], size [%d], data [%p]", mShmFd, mShmName.c_str(), mShmSize, mShmData); + LOG("sem-info : id [%p], name [%s]", mSemId, mSemName.c_str()); +#endif + std::vector<int> pidlist = FindPid("transtreamproxy", 0); +#ifdef DEBUG_LOG + Dump("before init."); +#endif + Wait(); + for (int i = 0; i < mInfoCount; i++) { + if (mShmData[i].pid != 0) { + int pid = mShmData[i].pid; + if(IsTerminated(pidlist, pid)) { + Erease(pid); + } + } + } + Post(); + return true; +} +//------------------------------------------------------------------------------- + +bool eHostInfoMgr::Register(std::string aIpAddr, int aPid) +{ + bool result = false; + + Wait(); + for (int i = 0; i < mInfoCount; i++) { + if (mShmData[i].pid == 0) { + result = true; + mShmData[i].pid = aPid; + strcpy(mShmData[i].ip, aIpAddr.c_str()); + break; + } + } + Post(); +#ifdef DEBUG_LOG + Dump("after register."); +#endif + return result; +} +//------------------------------------------------------------------------------- + +void eHostInfoMgr::Unregister(std::string aIpAddr) +{ + Wait(); + for (int i = 0; i < mInfoCount; i++) { + if (strcmp(mShmData[i].ip, aIpAddr.c_str()) == 0) { + memset(mShmData[i].ip, 0, 16); + mShmData[i].pid = 0; + break; + } + } + Post(); +#ifdef DEBUG_LOG + Dump("after unregister."); +#endif +} +//------------------------------------------------------------------------------- + +void eHostInfoMgr::Erease(int aPid) +{ + for (int i = 0; i < mInfoCount; i++) { + if (mShmData[i].pid == aPid) { +#ifdef DEBUG_LOG + LOG("erase.. %s : %d", mShmData[i].ip, mShmData[i].pid); +#endif + memset(mShmData[i].ip, 0, 16); + mShmData[i].pid = 0; + break; + } + } +} +//------------------------------------------------------------------------------- + +void eHostInfoMgr::Update(std::string aIpAddr, int aPid) +{ +#ifdef DEBUG_LOG + Dump("before update."); +#endif + Wait(); + for (int i = 0; i < mInfoCount; i++) { + if (strcmp(mShmData[i].ip, aIpAddr.c_str()) == 0) { + KILLPROC(mShmData[i].pid); + memset(mShmData[i].ip, 0, 16); + mShmData[i].pid = 0; + } + } + Post(); + Register(aIpAddr, aPid); +} +//------------------------------------------------------------------------------- + +int eHostInfoMgr::IsExist(std::string aIpAddr) +{ + int existCount = 0; + + Wait(); + for (int i = 0; i < mInfoCount; i++) { + if (strcmp(mShmData[i].ip, aIpAddr.c_str()) == 0) { + existCount++; + } + } + Post(); + + return existCount; +} +//------------------------------------------------------------------------------- + +#ifdef DEBUG_LOG +void eHostInfoMgr::Dump(const char* aMessage) +{ + LOG(" >> %s", aMessage); + LOG("-------- [ DUMP HOST INFO ] ---------"); + for (int i = 0; i < mInfoCount; i++) { + LOG("%d : ip [%s], pid [%d]", i, mShmData[i].ip, mShmData[i].pid); + } + LOG("-------------------------------------"); + Post(); +} +#endif +//------------------------------------------------------------------------------- + +std::string eHostInfoMgr::GetHostAddr() +{ + std::stringstream ss; + struct sockaddr_in addr; + socklen_t addrlen = sizeof(addr); + + getpeername(0, (struct sockaddr*)&addr, &addrlen); + ss << inet_ntoa(addr.sin_addr);// << ":" << addr.sin_port; + + return ss.str(); +} +//------------------------------------------------------------------------------- + +std::vector<int> eHostInfoMgr::FindPid(std::string aProcName, int aMyPid) +{ + std::vector<int> pidlist; + char cmdlinepath[256] = {0}; + DIR* d = opendir("/proc"); + if (d != 0) { + struct dirent* de; + while ((de = readdir(d)) != 0) { + int pid = atoi(de->d_name); + if (pid > 0) { + sprintf(cmdlinepath, "/proc/%s/cmdline", de->d_name); + + std::string cmdline; + std::ifstream cmdlinefile(cmdlinepath); + std::getline(cmdlinefile, cmdline); + if (!cmdline.empty()) { + size_t pos = cmdline.find('\0'); + if (pos != string::npos) + cmdline = cmdline.substr(0, pos); + pos = cmdline.rfind('/'); + if (pos != string::npos) + cmdline = cmdline.substr(pos + 1); + if ((aProcName == cmdline) && ((aMyPid != pid) || (aMyPid == 0))) { + pidlist.push_back(pid); + } + } + } + } + closedir(d); + } + return pidlist; +} +//------------------------------------------------------------------------------- diff --git a/src/eHostInfoMgr.h b/src/eHostInfoMgr.h new file mode 100644 index 0000000..01603ba --- /dev/null +++ b/src/eHostInfoMgr.h @@ -0,0 +1,47 @@ +/* + * eHostInfoMgr.h + * + * Created on: 2013. 11. 14. + * Author: kos + */ + +#ifndef EHOSTINFOMGR_H_ +#define EHOSTINFOMGR_H_ + +#include <vector> +#include <string> + +#include "uPosixSharedMemory.h" +//------------------------------------------------------------------------------- + +typedef struct host_info_t { + int pid; + char ip[16]; +} eHostInfo; +//------------------------------------------------------------------------------- + +class eHostInfoMgr : public uPosixSharedMemory<eHostInfo> +{ +private: + int mInfoCount; +protected: + void Erease(int aPid); + bool IsTerminated(std::vector<int>& aList, int aPid); +public: + eHostInfoMgr(std::string aName, int aInfoCount); + virtual ~eHostInfoMgr(); + bool Init(); + bool Register(std::string aIpAddr, int aPid); + void Unregister(std::string aIpAddr); + void Update(std::string aIpAddr, int aPid); + int IsExist(std::string aIpAddr); +#ifdef DEBUG_LOG + void Dump(const char* aMessage); +#endif + + static std::string GetHostAddr(); + std::vector<int> FindPid(std::string aProcName, int aMyPid); +}; +//------------------------------------------------------------------------------- + +#endif /* EHOSTINFOMGR_H_ */ diff --git a/src/eNetworkPumpThread.cpp b/src/eNetworkPumpThread.cpp index 73b9299..842b434 100644 --- a/src/eNetworkPumpThread.cpp +++ b/src/eNetworkPumpThread.cpp @@ -69,7 +69,7 @@ Server: stream_enigma2\r\n\ wc = write(1, buffer, rc); #ifdef DEBUG_LOG if(wc != rc) { - LOG("need retry write.. rc[%d], wc[%d]", rc, wc); + LOG("need rewrite.. rc[%d], wc[%d]", rc, wc); } #endif } else if (pollevt.revents & POLLHUP) { diff --git a/src/eParser.cpp b/src/eParser.cpp index e3fefcf..153a128 100644 --- a/src/eParser.cpp +++ b/src/eParser.cpp @@ -212,7 +212,20 @@ bool eParser::LiveStreamPid(std::string aData, std::vector<unsigned long>& aPidL } } break; +#ifdef NORMAL_STREAMPROXY + case 2: { + const char *c = "\ +HTTP/1.0 200 OK\r\n\ +Connection: close\r\n\ +Content-Type: video/mpeg\r\n\ +Server: stream_enigma2\r\n\ +\r\n"; + wc = write(1, c, strlen(c)); + state = 3; + } +#else case 2: +#endif case 3: if(line.length() > 0 && line.at(0) == '+') { /*+0:0:pat,17d4:pmt,17de:video,17e8:audio,17e9:audio,17eb:audio,17ea:audio,17f3:subtitle,17de:pcr,17f2:text*/ diff --git a/src/eTransCodingDevice.cpp b/src/eTransCodingDevice.cpp index 406fe15..bf8629e 100644 --- a/src/eTransCodingDevice.cpp +++ b/src/eTransCodingDevice.cpp @@ -6,8 +6,10 @@ */ #include <stdio.h> -#include <unistd.h> #include <fcntl.h> +#include <unistd.h> +#include <dirent.h> +#include <string.h> #include <sys/ioctl.h> #include "ePreDefine.h" @@ -25,6 +27,28 @@ #define IOCTL_OPCODE_START_TRANSCODING 100 #define IOCTL_OPCODE_STOP_TRANSCODING 200 +static int gMaxDeviceCount = 0; +//------------------------------------------------------------------------------- + +int eTransCodingDevice::GetMaxDeviceCount() +{ + if(gMaxDeviceCount == 0) { + char cmdlinepath[256] = {0}; + DIR* d = opendir("/dev"); + if (d != 0) { + struct dirent* de; + while ((de = readdir(d)) != 0) { + if (strncmp("bcm_enc", de->d_name, 7) == 0) { + gMaxDeviceCount++; + } + } + closedir(d); + } + } + return gMaxDeviceCount; +} +//------------------------------------------------------------------------------- + eTransCodingDevice::eTransCodingDevice() : mDeviceFd(0) { @@ -39,7 +63,14 @@ eTransCodingDevice::~eTransCodingDevice() bool eTransCodingDevice::Open() { - mDeviceFd = open("/dev/bcm_enc0", O_RDWR); + char path[16] = {0}; + int maxcnt = eTransCodingDevice::GetMaxDeviceCount(); + for (int i = 0; i < maxcnt; ++i) { + sprintf(path, "/dev/bcm_enc%d", i); + mDeviceFd = open(path, O_RDWR); + if(mDeviceFd > 0) break; + } + //mDeviceFd = open("/dev/bcm_enc0", O_RDWR); if(mDeviceFd <= 0) { mDeviceFd = 0; #ifdef DEBUG_LOG @@ -48,7 +79,7 @@ bool eTransCodingDevice::Open() return false; } #ifdef DEBUG_LOG - LOG("transcoding device open ok."); + LOG("transcoding device open ok. [%s]", path); #endif return true; } diff --git a/src/eTransCodingDevice.h b/src/eTransCodingDevice.h index 45b059d..845939c 100644 --- a/src/eTransCodingDevice.h +++ b/src/eTransCodingDevice.h @@ -26,6 +26,8 @@ public: bool StartTranscoding(); void StopTranscoding(); + + static int GetMaxDeviceCount(); }; //------------------------------------------------------------------------------- diff --git a/src/main.cpp b/src/main.cpp index 0bd64f2..897b58e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,18 +15,18 @@ #include <sys/ioctl.h> #include <stdint.h> #include <sys/types.h> -#include <dirent.h> #include <vector> #include <string> #include <iterator> -#include <fstream> + +#include "uStringTool.h" #include "ePreDefine.h" #include "eParser.h" #include "eUpstreamSocket.h" #include "eTransCodingDevice.h" -#include "uStringTool.h" +#include "eHostInfoMgr.h" #include "eFilePumpThread.h" #include "eDemuxPumpThread.h" @@ -48,7 +48,6 @@ eNetworkPumpThread* hNetworkPumpThread = 0; eTransCodingDevice* hTranscodingDevice = 0; void SigHandler(int aSigNo); -std::vector<int> FindPid(std::string aProcName, int aMyPid); //------------------------------------------------------------------------------- /* @@ -71,13 +70,20 @@ int main(int argc, char** argv) fpLog = fopen("/tmp/transtreamproxy.log", "a+"); #endif - std::vector<int> pidlist = FindPid(argv[0], getpid()); - for(int i = 0; i < pidlist.size(); ++i) { + std::string ipaddr = eHostInfoMgr::GetHostAddr(); #ifdef DEBUG_LOG - LOG("send sigint to %d", pidlist[i]); + LOG("client info : %s, device count : %d", ipaddr.c_str(), eTransCodingDevice::GetMaxDeviceCount()); #endif - kill(pidlist[i], SIGINT); + eHostInfoMgr hostmgr("tsp", eTransCodingDevice::GetMaxDeviceCount()); + if (hostmgr.Init() == false) { + return 1; + } + if (hostmgr.IsExist(ipaddr) > 0) { + hostmgr.Update(ipaddr, getpid()); + } else { + hostmgr.Register(ipaddr, getpid()); } + signal(SIGINT, SigHandler); if (!ReadRequest(request)) { @@ -238,13 +244,14 @@ int main(int argc, char** argv) filepump.Start(); hFilePumpThread = &filepump; + sleep(1); + filepump.SeekOffset(0); if(transcoding.StartTranscoding() == false) { #ifdef DEBUG_LOG LOG("Transcoding start failed."); #endif return 1;//RETURN_ERR_502("Transcoding start failed."); } - filepump.SeekOffset(0); networkpump.Start(); networkpump.Join(); @@ -266,47 +273,21 @@ char* ReadRequest(char* aRequest) void SigHandler(int aSigNo) { +#ifdef DEBUG_LOG + LOG("%d", aSigNo); +#endif switch(aSigNo) { case SIGINT: #ifdef DEBUG_LOG LOG("SIGINT detected."); #endif +// if(hDemuxPumpThread) { +// hDemuxPumpThread->Close(); +// } +// if(hTranscodingDevice) { +// hTranscodingDevice->close(); +// } exit(0); } } //------------------------------------------------------------------------------- - -std::vector<int> FindPid(std::string aProcName, int aMyPid) -{ - std::vector<int> pidlist; - char cmdlinepath[256] = {0}; - DIR* d = opendir("/proc"); - if (d != 0) { - struct dirent* de; - while ((de = readdir(d)) != 0) { - int pid = atoi(de->d_name); - if (pid > 0) { - sprintf(cmdlinepath, "/proc/%s/cmdline", de->d_name); - - std::string cmdline; - std::ifstream cmdlinefile(cmdlinepath); - std::getline(cmdlinefile, cmdline); - if (!cmdline.empty()) { - size_t pos = cmdline.find('\0'); - if (pos != string::npos) - cmdline = cmdline.substr(0, pos); - pos = cmdline.rfind('/'); - if (pos != string::npos) - cmdline = cmdline.substr(pos + 1); - if ((aProcName == cmdline) && (aMyPid != pid)) { - pidlist.push_back(pid); - } - } - } - } - closedir(d); - } - return pidlist; -} -//------------------------------------------------------------------------------- - diff --git a/src/uPosixSharedMemory.h b/src/uPosixSharedMemory.h new file mode 100644 index 0000000..f3fb29d --- /dev/null +++ b/src/uPosixSharedMemory.h @@ -0,0 +1,80 @@ +/* + * uPosixSharedMemory.h + * + * Created on: 2013. 11. 14. + * Author: kos + */ + +#ifndef UPOSIXSHAREDMEMORY_H_ +#define UPOSIXSHAREDMEMORY_H_ + +#include <string> + +#include <fcntl.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/mman.h> + +using namespace std; +//------------------------------------------------------------------------------- + +template <class T> +class uPosixSharedMemory +{ +protected: + sem_t* mSemId; + std::string mSemName; + + int mShmFd; + int mShmSize; + std::string mShmName; + + T* mShmData; + +protected: + void Close() + { + if (mShmData > 0) { + munmap(mShmData, mShmSize); + } + mShmData = 0; + if (mShmFd > 0) { + close(mShmFd); + //shm_unlink(mShmName.c_str()); + } + mShmFd = 0; + if (mSemId > 0) { + sem_close(mSemId); + sem_unlink(mSemName.c_str()); + } + mSemId = 0; + } + + bool Open() + { + mShmFd = shm_open(mShmName.c_str(), O_CREAT | O_RDWR, S_IRWXU | S_IRWXG); + if (mShmFd < 0) { + return false; + } + ftruncate(mShmFd, mShmSize); + + mShmData = (T*) mmap(NULL, mShmSize, PROT_READ | PROT_WRITE, MAP_SHARED, mShmFd, 0); + if (mShmData == 0) { + return false; + } + mSemId = sem_open(mSemName.c_str(), O_CREAT, S_IRUSR | S_IWUSR, 1); + return true; + } + + void Wait() { sem_wait(mSemId); } + void Post() { sem_post(mSemId); } + +public: + ~uPosixSharedMemory() + { + Close(); + } +}; +//------------------------------------------------------------------------------- + +#endif /* UPOSIXSHAREDMEMORY_H_ */ diff --git a/src/uStringTool.h b/src/uStringTool.h index 5dd0f5e..d863184 100644 --- a/src/uStringTool.h +++ b/src/uStringTool.h @@ -45,7 +45,7 @@ namespace uStringTool return out; } - inline int Split(std::string data, const char delimiter, std::vector<string>& tokens) + inline int Split(std::string data, const char delimiter, std::vector<std::string>& tokens) { std::stringstream data_stream(data); for(std::string token; std::getline(data_stream, token, delimiter); tokens.push_back(Trim(token))); |