xref: /haiku/src/kits/app/PortLink.cpp (revision fef6144999c2fa611f59ee6ffe6dd7999501385c)
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 <LinkMsgReader.h>
19 #include <LinkMsgSender.h>
20 #include <PortLink.h>
21 #include <ServerProtocol.h>
22 
23 
24 namespace BPrivate {
25 
26 ServerLink::ServerLink()
27 {
28 }
29 
30 
31 ServerLink::~ServerLink()
32 {
33 }
34 
35 
36 status_t
37 ServerLink::ReadRegion(BRegion *region)
38 {
39 	fReceiver->Read(&region->count, sizeof(long));
40 	fReceiver->Read(&region->bound, sizeof(clipping_rect));
41 	region->set_size(region->count + 1);
42 	return fReceiver->Read(region->data, region->count * sizeof(clipping_rect));
43 }
44 
45 
46 status_t
47 ServerLink::AttachRegion(const BRegion &region)
48 {
49 	fSender->Attach(&region.count, sizeof(long));
50 	fSender->Attach(&region.bound, sizeof(clipping_rect));
51 	return fSender->Attach(region.data, region.count * sizeof(clipping_rect));
52 }
53 
54 
55 status_t
56 ServerLink::ReadShape(BShape *shape)
57 {
58 	int32 opCount, ptCount;
59 	fReceiver->Read(&opCount, sizeof(int32));
60 	fReceiver->Read(&ptCount, sizeof(int32));
61 
62 	uint32 opList[opCount];
63 	fReceiver->Read(opList, opCount * sizeof(uint32));
64 
65 	BPoint ptList[ptCount];
66 	fReceiver->Read(ptList, ptCount * sizeof(BPoint));
67 
68 	shape->SetData(opCount, ptCount, opList, ptList);
69 	return B_OK;
70 }
71 
72 
73 status_t
74 ServerLink::AttachShape(BShape &shape)
75 {
76 	int32 opCount, ptCount;
77 	uint32 *opList;
78 	BPoint *ptList;
79 
80 	shape.GetData(&opCount, &ptCount, &opList, &ptList);
81 
82 	fSender->Attach(&opCount, sizeof(int32));
83 	fSender->Attach(&ptCount, sizeof(int32));
84 	fSender->Attach(opList, opCount * sizeof(uint32));
85 	return fSender->Attach(ptList, ptCount * sizeof(BPoint));
86 }
87 
88 
89 status_t
90 ServerLink::FlushWithReply(int32 &code)
91 {
92 	status_t status = Flush(B_INFINITE_TIMEOUT, true);
93 	if (status < B_OK)
94 		return status;
95 
96 	return GetNextMessage(code);
97 }
98 
99 
100 //	#pragma mark -
101 
102 
103 PortLink::PortLink(port_id send, port_id receive)
104 {
105 	fSender = new LinkSender(send);
106 	fReceiver = new LinkReceiver(receive);
107 }
108 
109 
110 PortLink::~PortLink()
111 {
112 	delete fReceiver;
113 	delete fSender;
114 }
115 
116 
117 void
118 PortLink::SetTo(port_id sender, port_id receiver)
119 {
120 	fSender->SetPort(sender);
121 	fReceiver->SetPort(receiver);
122 }
123 
124 }	// namespace BPrivate
125