00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "JackWinEvent.h"
00021 #include "JackTools.h"
00022 #include "JackError.h"
00023 #include <assert.h>
00024
00025
00026
00027
00028 namespace Jack
00029 {
00030
00031 void JackWinEvent::BuildName(const char* name, const char* server_name, char* res)
00032 {
00033 sprintf(res, "jack_pipe.%s_%s", server_name, name);
00034 }
00035
00036 bool JackWinEvent::Signal()
00037 {
00038 BOOL res;
00039 assert(fEvent);
00040
00041 if (fFlush)
00042 return true;
00043
00044 if (!(res = SetEvent(fEvent))) {
00045 jack_error("JackWinEvent::Signal name = %s err = %ld", fName, GetLastError());
00046 }
00047
00048 return res;
00049 }
00050
00051 bool JackWinEvent::SignalAll()
00052 {
00053 BOOL res;
00054 assert(fEvent);
00055
00056 if (fFlush)
00057 return true;
00058
00059 if (!(res = SetEvent(fEvent))) {
00060 jack_error("JackWinEvent::SignalAll name = %s err = %ld", fName, GetLastError());
00061 }
00062
00063 return res;
00064 }
00065
00066 bool JackWinEvent::Wait()
00067 {
00068 DWORD res;
00069
00070 if ((res = WaitForSingleObject(fEvent, INFINITE)) == WAIT_TIMEOUT) {
00071 jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
00072 }
00073
00074 return (res == WAIT_OBJECT_0);
00075 }
00076
00077 bool JackWinEvent::TimedWait(long usec)
00078 {
00079 DWORD res;
00080
00081 if ((res = WaitForSingleObject(fEvent, usec / 1000)) == WAIT_TIMEOUT) {
00082 jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
00083 }
00084
00085 return (res == WAIT_OBJECT_0);
00086 }
00087
00088
00089 bool JackWinEvent::ConnectInput(const char* name, const char* server_name)
00090 {
00091 BuildName(name, server_name, fName);
00092 jack_log("JackWinEvent::Connect %s", fName);
00093
00094
00095 if (fEvent) {
00096 jack_log("Already connected name = %s", name);
00097 return true;
00098 }
00099
00100 if ((fEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, fName)) == NULL) {
00101 jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
00102 return false;
00103 } else {
00104 return true;
00105 }
00106 }
00107
00108 bool JackWinEvent::Connect(const char* name, const char* server_name)
00109 {
00110 return ConnectInput(name, server_name);
00111 }
00112
00113 bool JackWinEvent::ConnectOutput(const char* name, const char* server_name)
00114 {
00115 return ConnectInput(name, server_name);
00116 }
00117
00118 bool JackWinEvent::Disconnect()
00119 {
00120 if (fEvent) {
00121 jack_log("JackWinEvent::Disconnect %s", fName);
00122 CloseHandle(fEvent);
00123 fEvent = NULL;
00124 return true;
00125 } else {
00126 return false;
00127 }
00128 }
00129
00130 bool JackWinEvent::Allocate(const char* name, const char* server_name, int value)
00131 {
00132 BuildName(name, server_name, fName);
00133 jack_log("JackWinEvent::Allocate name = %s val = %ld", fName, value);
00134
00135
00136 if ((fEvent = CreateEvent(NULL, FALSE, FALSE, fName)) == NULL) {
00137 jack_error("Allocate: can't check in named event name = %s err = %ld", fName, GetLastError());
00138 return false;
00139 } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
00140 jack_error("Allocate: named event already exist name = %s", fName);
00141 CloseHandle(fEvent);
00142 fEvent = NULL;
00143 return false;
00144 } else {
00145 return true;
00146 }
00147 }
00148
00149 void JackWinEvent::Destroy()
00150 {
00151 if (fEvent != NULL) {
00152 jack_log("JackWinEvent::Destroy %s", fName);
00153 CloseHandle(fEvent);
00154 fEvent = NULL;
00155 } else {
00156 jack_error("JackWinEvent::Destroy synchro == NULL");
00157 }
00158 }
00159
00160
00161 }
00162