* fix the build by moving a typedef to public scope, and avoid returning a value in the destructor

* some clean up


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31025 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2009-06-13 10:42:11 +00:00
parent bcc2ed0722
commit 581708b997
2 changed files with 38 additions and 41 deletions
+36 -39
View File
@@ -14,16 +14,10 @@ template <
size_t MAX_MESSAGE_DEEP = 16, size_t MAX_MESSAGE_DEEP = 16,
uint32 PRIORITY = B_URGENT_DISPLAY_PRIORITY> uint32 PRIORITY = B_URGENT_DISPLAY_PRIORITY>
class PortListener { class PortListener {
typedef status_t (*port_listener_func)(TYPE*, int32, size_t);
struct PortListenerInfo {
port_id* port;
port_listener_func func;
} fInformation;
public: public:
PortListener(const char* name, port_listener_func handler) typedef status_t (*port_listener_func)(TYPE*, int32, size_t);
PortListener(const char* name, port_listener_func handler)
{ {
fInformation.func = handler; fInformation.func = handler;
fInformation.port = &fPort; fInformation.port = &fPort;
@@ -35,8 +29,8 @@ class PortListener {
} }
~PortListener() { ~PortListener()
{
status_t status; status_t status;
close_port(fPort); close_port(fPort);
@@ -45,27 +39,24 @@ class PortListener {
delete fThreadName; delete fThreadName;
delete fPortName; delete fPortName;
return status;
} }
status_t TriggerCode(int32 code) { status_t TriggerCode(int32 code)
{
return write_port(fPort, code, NULL, 0); return write_port(fPort, code, NULL, 0);
}
}
status_t InitCheck() { status_t InitCheck()
{
// Create Port // Create Port
fPort = find_port(fPortName); fPort = find_port(fPortName);
if (fPort == B_NAME_NOT_FOUND) { if (fPort == B_NAME_NOT_FOUND) {
fPort = create_port(MAX_MESSAGE_DEEP, fPortName); fPort = create_port(MAX_MESSAGE_DEEP, fPortName);
} }
if (fPort < B_OK) if (fPort < B_OK)
return fPort; return fPort;
// Create Thread // Create Thread
@@ -73,43 +64,49 @@ class PortListener {
fThread = find_thread(fThreadName); fThread = find_thread(fThreadName);
if (fThread < B_OK) { if (fThread < B_OK) {
#ifdef KERNEL_LAND #ifdef KERNEL_LAND
fThread = spawn_kernel_thread((thread_func)&PortListener<TYPE,MAX_MESSAGE_SIZE, fThread = spawn_kernel_thread((thread_func)&PortListener<TYPE,MAX_MESSAGE_SIZE,
MAX_MESSAGE_DEEP, PRIORITY>::threadFunction, fThreadName, PRIORITY, &fInformation); MAX_MESSAGE_DEEP, PRIORITY>::threadFunction, fThreadName, PRIORITY, &fInformation);
#else #else
fThread = spawn_thread((thread_func)&PortListener<TYPE,MAX_MESSAGE_SIZE, fThread = spawn_thread((thread_func)&PortListener<TYPE,MAX_MESSAGE_SIZE,
MAX_MESSAGE_DEEP, PRIORITY>::threadFunction, fThreadName, PRIORITY, &fInformation); MAX_MESSAGE_DEEP, PRIORITY>::threadFunction, fThreadName, PRIORITY, &fInformation);
#endif #endif
} }
if (fThread < B_OK) if (fThread < B_OK)
return fThread; return fThread;
return B_OK; return B_OK;
} }
status_t Launch() { status_t Launch()
{
status_t check = InitCheck(); status_t check = InitCheck();
if (check < B_OK) if (check < B_OK)
return check; return check;
return resume_thread(fThread); return resume_thread(fThread);
} }
status_t Stop() { status_t Stop()
{
status_t status; status_t status;
wait_for_thread(fThread, &status); wait_for_thread(fThread, &status);
return status; return status;
} }
private:
port_id fPort;
private:
struct PortListenerInfo {
port_id* port;
port_listener_func func;
} fInformation;
port_id fPort;
thread_id fThread; thread_id fThread;
char* fThreadName; char* fThreadName;
char* fPortName; char* fPortName;
@@ -127,9 +124,9 @@ class PortListener {
TYPE* buffer = (TYPE*)malloc(MAX_MESSAGE_SIZE); TYPE* buffer = (TYPE*)malloc(MAX_MESSAGE_SIZE);
while ((ssizePort = port_buffer_size(*port)) != B_BAD_PORT_ID) { while ((ssizePort = port_buffer_size(*port)) != B_BAD_PORT_ID) {
if (ssizePort <= 0) { if (ssizePort <= 0) {
snooze(500*1000); snooze(500*1000);
continue; continue;
} }
@@ -139,7 +136,7 @@ class PortListener {
continue; continue;
} }
ssizeRead = read_port(*port, &code, (void*)buffer, ssizePort); ssizeRead = read_port(*port, &code, (void*)buffer, ssizePort);
if (ssizeRead != ssizePort) if (ssizeRead != ssizePort)
continue; continue;
@@ -152,7 +149,7 @@ class PortListener {
free(buffer); free(buffer);
if (ssizePort == B_BAD_PORT_ID) // the port dissapeared if (ssizePort == B_BAD_PORT_ID) // the port disappeared
return ssizePort; return ssizePort;
return status; return status;
@@ -160,5 +157,5 @@ class PortListener {
}; // PortListener }; // PortListener
#endif // PORTLISTENER_H_ #endif // PORTLISTENER_H_
+2 -2
View File
@@ -63,8 +63,8 @@ BluetoothServer::BluetoothServer() : BApplication(BLUETOOTH_SIGNATURE)
fDeviceManager = new DeviceManager(); fDeviceManager = new DeviceManager();
fLocalDevicesList.MakeEmpty(); fLocalDevicesList.MakeEmpty();
fEventListener2 = new BluetoothPortListener(BT_USERLAND_PORT_NAME, fEventListener2 = new BluetoothPortListener(BT_USERLAND_PORT_NAME,
(BluetoothPortListener::port_listener_func)&DispatchEvent); (BluetoothPortListener::port_listener_func)&DispatchEvent);
} }