xref: /haiku/src/kits/app/ServerLink.cpp (revision b30304acc8c37e678a1bf66976d15bdab103f931)
1 /*
2  * Copyright 2001-2005, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Pahtz <pahtz@yahoo.com.au>
7  *		Axel Dörfler, axeld@pinc-software.de
8  */
9 
10 /** Class for low-overhead port-based messaging */
11 
12 #include <stdlib.h>
13 #include <string.h>
14 #include <new>
15 #include <Region.h>
16 #include <Shape.h>
17 
18 #include <ServerLink.h>
19 #include <ServerProtocol.h>
20 
21 
22 namespace BPrivate {
23 
24 ServerLink::ServerLink()
25 {
26 }
27 
28 
29 ServerLink::~ServerLink()
30 {
31 }
32 
33 
34 status_t
35 ServerLink::ReadRegion(BRegion *region)
36 {
37 	fReceiver->Read(&region->fCount, sizeof(long));
38 	if (region->fCount > 0) {
39 		fReceiver->Read(&region->fBounds, sizeof(clipping_rect));
40 		if (!region->_SetSize(region->fCount))
41 			return B_NO_MEMORY;
42 		return fReceiver->Read(region->fData,
43 			region->fCount * sizeof(clipping_rect));
44 	} else {
45 		return fReceiver->Read(&region->fBounds, sizeof(clipping_rect));
46 	}
47 }
48 
49 
50 status_t
51 ServerLink::AttachRegion(const BRegion &region)
52 {
53 	fSender->Attach(&region.fCount, sizeof(long));
54 	if (region.fCount > 0) {
55 		fSender->Attach(&region.fBounds, sizeof(clipping_rect));
56 		return fSender->Attach(region.fData,
57 			region.fCount * sizeof(clipping_rect));
58 	} else {
59 		return fSender->Attach(&region.fBounds, sizeof(clipping_rect));
60 	}
61 }
62 
63 
64 status_t
65 ServerLink::ReadShape(BShape *shape)
66 {
67 	int32 opCount, ptCount;
68 	fReceiver->Read(&opCount, sizeof(int32));
69 	fReceiver->Read(&ptCount, sizeof(int32));
70 
71 	uint32 opList[opCount];
72 	if (opCount > 0)
73 		fReceiver->Read(opList, opCount * sizeof(uint32));
74 
75 	BPoint ptList[ptCount];
76 	if (ptCount > 0)
77 		fReceiver->Read(ptList, ptCount * sizeof(BPoint));
78 
79 	shape->SetData(opCount, ptCount, opList, ptList);
80 	return B_OK;
81 }
82 
83 
84 status_t
85 ServerLink::AttachShape(BShape &shape)
86 {
87 	int32 opCount, ptCount;
88 	uint32 *opList;
89 	BPoint *ptList;
90 
91 	shape.GetData(&opCount, &ptCount, &opList, &ptList);
92 
93 	fSender->Attach(&opCount, sizeof(int32));
94 	fSender->Attach(&ptCount, sizeof(int32));
95 	if (opCount > 0)
96 		fSender->Attach(opList, opCount * sizeof(uint32));
97 	if (ptCount > 0)
98 		fSender->Attach(ptList, ptCount * sizeof(BPoint));
99 	return B_OK;
100 }
101 
102 
103 status_t
104 ServerLink::FlushWithReply(int32 &code)
105 {
106 	status_t status = Flush(B_INFINITE_TIMEOUT, true);
107 	if (status < B_OK)
108 		return status;
109 
110 	return GetNextMessage(code);
111 }
112 
113 }	// namespace BPrivate
114