| 1 | /* |
|---|
| 2 | PowerDNS Versatile Database Driven Nameserver |
|---|
| 3 | Copyright (C) 2002 PowerDNS.COM BV |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include "utility.hh" |
|---|
| 21 | #include "dynmessenger.hh" |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | #include <errno.h> |
|---|
| 25 | #include <iostream> |
|---|
| 26 | #include <sys/types.h> |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | DynMessenger::DynMessenger(const string &dname, const string &fname) |
|---|
| 30 | { |
|---|
| 31 | string pipename = "\\\\.\\pipe\\" + fname; |
|---|
| 32 | |
|---|
| 33 | m_pipeHandle = CreateFile( pipename.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); |
|---|
| 34 | if ( m_pipeHandle == INVALID_HANDLE_VALUE ) |
|---|
| 35 | throw AhuException( "Could not create named pipe (are you on Windows NT, 2000 or XP? 98 doesn't work!)" ); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | DynMessenger::~DynMessenger() |
|---|
| 39 | { |
|---|
| 40 | CloseHandle( m_pipeHandle ); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | int DynMessenger::send(const string &msg) const |
|---|
| 44 | { |
|---|
| 45 | unsigned long bytesWritten; |
|---|
| 46 | |
|---|
| 47 | if ( !WriteFile( m_pipeHandle, msg.c_str(), msg.length(), &bytesWritten, NULL )) |
|---|
| 48 | return -1; // Could not write. |
|---|
| 49 | |
|---|
| 50 | FlushFileBuffers( m_pipeHandle ); |
|---|
| 51 | |
|---|
| 52 | return 0; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | string DynMessenger::receive() const |
|---|
| 56 | { |
|---|
| 57 | char buffer[1024]; |
|---|
| 58 | |
|---|
| 59 | DWORD bytesRead; |
|---|
| 60 | |
|---|
| 61 | if ( !ReadFile( m_pipeHandle, buffer, sizeof( buffer ) - 1, &bytesRead, NULL )) |
|---|
| 62 | return ""; |
|---|
| 63 | |
|---|
| 64 | buffer[ bytesRead ] = 0; |
|---|
| 65 | |
|---|
| 66 | return buffer; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|