]> granicus.if.org Git - imagemagick/blob - MagickCore/distribute-cache.c
(no commit message)
[imagemagick] / MagickCore / distribute-cache.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %    DDDD    IIIII   SSSSS  TTTTT  RRRR   IIIII  BBBB   U   U  TTTTT  EEEEE   %
6 %    D   D     I     SS       T    R   R    I    B   B  U   U    T    E       %
7 %    D   D     I      SSS     T    RRRR     I    BBBB   U   U    T    EEE     %
8 %    D   D     I        SS    T    R R      I    B   B  U   U    T    E       %
9 %    DDDDA   IIIII   SSSSS    T    R  R   IIIII  BBBB    UUU     T    EEEEE   %
10 %                                                                             %
11 %                      CCCC   AAA    CCCC  H   H  EEEEE                       %
12 %                     C      A   A  C      H   H  E                           %
13 %                     C      AAAAA  C      HHHHH  EEE                         %
14 %                     C      A   A  C      H   H  E                           %
15 %                      CCCC  A   A   CCCC  H   H  EEEEE                       %
16 %                                                                             %
17 %                                                                             %
18 %                 MagickCore Distributed Pixel Cache Methods                  %
19 %                                                                             %
20 %                              Software Design                                %
21 %                                John Cristy                                  %
22 %                                January 2013                                 %
23 %                                                                             %
24 %                                                                             %
25 %  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
26 %  dedicated to making software imaging solutions freely available.           %
27 %                                                                             %
28 %  You may not use this file except in compliance with the License.  You may  %
29 %  obtain a copy of the License at                                            %
30 %                                                                             %
31 %    http://www.imagemagick.org/script/license.php                            %
32 %                                                                             %
33 %  Unless required by applicable law or agreed to in writing, software        %
34 %  distributed under the License is distributed on an "AS IS" BASIS,          %
35 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
36 %  See the License for the specific language governing permissions and        %
37 %  limitations under the License.                                             %
38 %                                                                             %
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %
41 % A distributed pixel cache is an extension of the traditional pixel cache
42 % available on a single host.  The distributed pixel cache may span multiple
43 % servers so that it can grow in size and transactional capacity to support
44 % very large images.  Start up the pixel cache server on one or more machines.
45 % When you read or operate on an image and the local pixel cache resources are
46 % exhausted, ImageMagick contacts one or more of these remote pixel servers to
47 % store or retrieve pixels.
48 %
49 */
50 \f
51 /*
52   Include declarations.
53 */
54 #include "MagickCore/studio.h"
55 #include "MagickCore/cache.h"
56 #include "MagickCore/cache-private.h"
57 #include "MagickCore/distribute-cache.h"
58 #include "MagickCore/distribute-cache-private.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/locale_.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/pixel.h"
67 #include "MagickCore/policy.h"
68 #include "MagickCore/random_.h"
69 #include "MagickCore/registry.h"
70 #include "MagickCore/splay-tree.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/string-private.h"
73 #include "MagickCore/version.h"
74 #if defined(MAGICKCORE_HAVE_SOCKET)
75 #include <netinet/in.h>
76 #include <netdb.h>
77 #include <sys/socket.h>
78 #include <arpa/inet.h>
79 #endif
80 \f
81 /*
82   Define declarations.
83 */
84 #define DPCHostname  "127.0.0.1"
85 #define DPCPendingConnections  10
86 #define DPCPort  6668
87 #define DPCSessionKeyLength  8
88 \f
89 /*
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 %                                                                             %
92 %                                                                             %
93 %                                                                             %
94 +   A c q u i r e D i s t r i b u t e C a c h e I n f o                       %
95 %                                                                             %
96 %                                                                             %
97 %                                                                             %
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 %
100 %  AcquireDistributeCacheInfo() allocates the DistributeCacheInfo structure.
101 %
102 %  The format of the AcquireDistributeCacheInfo method is:
103 %
104 %      DistributeCacheInfo *AcquireDistributeCacheInfo(ExceptionInfo *exception)
105 %
106 %  A description of each parameter follows:
107 %
108 %    o exception: return any errors or warnings in this structure.
109 %
110 */
111
112 static MagickSizeType CRC64(const unsigned char *message,
113   const MagickSizeType length)
114 {
115   MagickSizeType
116     crc;
117
118   register MagickOffsetType
119     i;
120
121   static MagickBooleanType
122     crc_initial = MagickFalse;
123
124   static MagickSizeType
125     crc_xor[256];
126
127   /*
128     Generate a 64-bit cyclic redundancy check for the message.
129   */
130   if (crc_initial == MagickFalse)
131     {
132       MagickSizeType
133         alpha;
134
135       for (i=0; i < 256; i++)
136       {
137         register MagickOffsetType
138           j;
139
140         alpha=(MagickSizeType) i;
141         for (j=0; j < 8; j++)
142         {
143           if ((alpha & 0x01) == 0)
144             alpha>>=1;
145           else
146             alpha=(MagickSizeType) ((alpha >> 1) ^
147               MagickULLConstant(0xd800000000000000));
148         }
149         crc_xor[i]=alpha;
150       }
151       crc_initial=MagickTrue;
152     }
153   crc=0;
154   for (i=0; i < (MagickOffsetType) length; i++)
155     crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
156   return(crc);
157 }
158
159 static inline MagickSizeType MagickMin(const MagickSizeType x,
160   const MagickSizeType y)
161 {
162   if (x < y)
163     return(x);
164   return(y);
165 }
166
167 static inline MagickOffsetType dpc_read(int file,const MagickSizeType length,
168   unsigned char *restrict message)
169 {
170   register MagickOffsetType
171     i;
172
173   ssize_t
174     count;
175
176   count=0;
177   for (i=0; i < (MagickOffsetType) length; i+=count)
178   {
179     count=recv(file,message+i,(size_t) MagickMin(length-i,(MagickSizeType)
180       SSIZE_MAX),0);
181     if (count <= 0)
182       {
183         count=0;
184         if (errno != EINTR)
185           break;
186       }
187   }
188   return(i);
189 }
190
191 static int ConnectPixelCacheServer(const char *hostname,const int port,
192   MagickSizeType *session_key,ExceptionInfo *exception)
193 {
194 #if defined(MAGICKCORE_HAVE_SOCKET)
195   char
196     service[MaxTextExtent];
197
198   const char
199     *shared_secret;
200
201   int
202     client_socket,
203     status;
204
205   MagickOffsetType
206     count;
207
208   register unsigned char
209     *p;
210
211   struct addrinfo
212     hint,
213     *result;
214
215   unsigned char
216     secret[MaxTextExtent],
217     session[2*MaxTextExtent];
218
219   /*
220     Connect to distributed pixel cache and get session key.
221   */
222   *session_key=0;
223   shared_secret=GetPolicyValue("shared-secret");
224   if (shared_secret == (const char *) NULL)
225     {
226       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
227         "DistributedPixelCache","'%s'","shared secret expected");
228       return(-1);
229     }
230   p=session;
231   (void) CopyMagickString((char *) p,shared_secret,MaxTextExtent);
232   p+=strlen(shared_secret);
233   (void) ResetMagickMemory(&hint,0,sizeof(hint));
234   hint.ai_family=AF_INET;
235   hint.ai_socktype=SOCK_STREAM;
236   hint.ai_flags=AI_PASSIVE;
237   (void) FormatLocaleString(service,MaxTextExtent,"%d",port);
238   status=getaddrinfo(hostname,service,&hint,&result);
239   if (status != 0)
240     {
241       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
242         "DistributedPixelCache","'%s'",hostname);
243       return(-1);
244     }
245   client_socket=socket(result->ai_family,result->ai_socktype,
246     result->ai_protocol);
247   if (client_socket == -1)
248     {
249       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
250         "DistributedPixelCache","'%s'",hostname);
251       return(-1);
252     }
253   status=connect(client_socket,result->ai_addr,result->ai_addrlen);
254   if (status == -1)
255     {
256       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
257         "DistributedPixelCache","'%s'",hostname);
258       return(-1);
259     }
260   count=recv(client_socket,secret,MaxTextExtent,0);
261   if (count != -1)
262     {
263       MagickSizeType
264         signature;
265
266       (void) memcpy(p,secret,(size_t) count);
267       p+=count;
268       signature=MagickLibVersion;
269       (void) memcpy(p,&signature,sizeof(signature));
270       p+=sizeof(signature);
271       signature=MAGICKCORE_QUANTUM_DEPTH;
272       (void) memcpy(p,&signature,sizeof(signature));
273       p+=sizeof(signature);
274       signature=MAGICKCORE_HDRI_ENABLE;
275       (void) memcpy(p,&signature,sizeof(signature));
276       p+=sizeof(signature);
277       *session_key=CRC64(session,p-session);
278     }
279   if (*session_key == 0)
280     {
281       close(client_socket);
282       client_socket=(-1);
283     }
284   return(client_socket);
285 #else
286   (void) ThrowMagickException(exception,GetMagickModule(),MissingDelegateError,
287     "DelegateLibrarySupportNotBuiltIn","distributed pixel cache");
288   return(MagickFalse);
289 #endif
290 }
291
292 static char *GetHostname(int *port,ExceptionInfo *exception)
293 {
294   char
295     *host,
296     *hosts,
297     **hostlist;
298
299   int
300     argc;
301
302   register ssize_t
303     i;
304
305   static size_t
306     id = 0;
307
308   /*
309     Parse host list (e.g. 192.168.100.1:6668,192.168.100.2:6668).
310   */
311   hosts=(char *) GetImageRegistry(StringRegistryType,"cache:hosts",
312     exception);
313   if (hosts == (char *) NULL)
314     {
315       *port=DPCPort;
316       return(AcquireString(DPCHostname));
317     }
318   (void) SubstituteString(&hosts,","," ");
319   hostlist=StringToArgv(hosts,&argc);
320   hosts=DestroyString(hosts);
321   if (hostlist == (char **) NULL)
322     {
323       *port=DPCPort;
324       return(AcquireString(DPCHostname));
325     }
326   hosts=AcquireString(hostlist[(id++ % (argc-1))+1]);
327   for (i=0; i < (ssize_t) argc; i++)
328     hostlist[i]=DestroyString(hostlist[i]);
329   hostlist=(char **) RelinquishMagickMemory(hostlist);
330   (void) SubstituteString(&hosts,":"," ");
331   hostlist=StringToArgv(hosts,&argc);
332   if (hostlist == (char **) NULL)
333     {
334       *port=DPCPort;
335       return(AcquireString(DPCHostname));
336     }
337   host=AcquireString(hostlist[1]);
338   if (hostlist[2] == (char *) NULL)
339     *port=DPCPort;
340   else
341     *port=StringToLong(hostlist[2]);
342   for (i=0; i < (ssize_t) argc; i++)
343     hostlist[i]=DestroyString(hostlist[i]);
344   hostlist=(char **) RelinquishMagickMemory(hostlist);
345   return(host);
346 }
347
348 MagickPrivate DistributeCacheInfo *AcquireDistributeCacheInfo(
349   ExceptionInfo *exception)
350 {
351   char
352     *hostname;
353
354   DistributeCacheInfo
355     *server_info;
356
357   MagickSizeType
358     session_key;
359
360   /*
361     Connect to the distributed pixel cache server.
362   */
363   server_info=(DistributeCacheInfo *) AcquireMagickMemory(sizeof(*server_info));
364   if (server_info == (DistributeCacheInfo *) NULL)
365     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
366   (void) ResetMagickMemory(server_info,0,sizeof(*server_info));
367   server_info->signature=MagickSignature;
368   server_info->port=0;
369   hostname=GetHostname(&server_info->port,exception);
370   session_key=0;
371   server_info->file=ConnectPixelCacheServer(hostname,server_info->port,
372     &session_key,exception);
373   server_info->session_key=session_key;
374   (void) CopyMagickString(server_info->hostname,hostname,MaxTextExtent);
375   hostname=DestroyString(hostname);
376   if (server_info->file == -1)
377     server_info=DestroyDistributeCacheInfo(server_info);
378   return(server_info);
379 }
380 \f
381 /*
382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 %                                                                             %
384 %                                                                             %
385 %                                                                             %
386 +   D e s t r o y D i s t r i b u t e C a c h e I n f o                       %
387 %                                                                             %
388 %                                                                             %
389 %                                                                             %
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 %
392 %  DestroyDistributeCacheInfo() deallocates memory associated with an
393 %  DistributeCacheInfo structure.
394 %
395 %  The format of the DestroyDistributeCacheInfo method is:
396 %
397 %      DistributeCacheInfo *DestroyDistributeCacheInfo(
398 %        DistributeCacheInfo *server_info)
399 %
400 %  A description of each parameter follows:
401 %
402 %    o server_info: the distributed cache info.
403 %
404 */
405 MagickPrivate DistributeCacheInfo *DestroyDistributeCacheInfo(
406   DistributeCacheInfo *server_info)
407 {
408   assert(server_info != (DistributeCacheInfo *) NULL);
409   assert(server_info->signature == MagickSignature);
410   if (server_info->file > 0)
411     (void) close(server_info->file);
412   server_info->signature=(~MagickSignature);
413   server_info=(DistributeCacheInfo *) RelinquishMagickMemory(server_info);
414   return(server_info);
415 }
416 \f
417 /*
418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 %                                                                             %
420 %                                                                             %
421 %                                                                             %
422 +   D i s t r i b u t e P i x e l C a c h e S e r v e r                       %
423 %                                                                             %
424 %                                                                             %
425 %                                                                             %
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 %
428 %  DistributePixelCacheServer() waits on the specified port for commands to
429 %  create, read, update, or destroy a pixel cache.
430 %
431 %  The format of the DistributePixelCacheServer() method is:
432 %
433 %      void DistributePixelCacheServer(const int port)
434 %
435 %  A description of each parameter follows:
436 %
437 %    o port: connect the distributed pixel cache at this port.
438 %
439 %    o exception: return any errors or warnings in this structure.
440 %
441 */
442
443 static MagickBooleanType DestroyDistributeCache(SplayTreeInfo *registry,
444   int file,const MagickSizeType session_key)
445 {
446   /*
447     Destroy distributed pixel cache.
448   */
449   return(DeleteNodeFromSplayTree(registry,(const void *) session_key));
450 }
451
452 static inline MagickOffsetType dpc_send(int file,const MagickSizeType length,
453   const unsigned char *restrict message)
454 {
455   MagickOffsetType
456     count;
457
458   register MagickOffsetType
459     i;
460
461   /*
462     Ensure a complete message is sent.
463   */
464   count=0;
465   for (i=0; i < (MagickOffsetType) length; i+=count)
466   {
467     count=(MagickOffsetType) send(file,message+i,(size_t) MagickMin(length-i,
468       (MagickSizeType) SSIZE_MAX),MSG_NOSIGNAL);
469     if (count <= 0)
470       {
471         count=0;
472         if (errno != EINTR)
473           break;
474       }
475   }
476   return(i);
477 }
478
479 static MagickBooleanType OpenDistributeCache(SplayTreeInfo *registry,
480   int file,const MagickSizeType session_key,ExceptionInfo *exception)
481 {
482   Image
483     *image;
484
485   MagickBooleanType
486     status;
487
488   MagickOffsetType
489     count;
490
491   MagickSizeType
492     length;
493
494   register unsigned char
495     *p;
496
497   unsigned char
498     message[MaxTextExtent];
499
500   /*
501     Open distributed pixel cache.
502   */
503   image=AcquireImage((ImageInfo *) NULL,exception);
504   if (image == (Image *) NULL)
505     return(MagickFalse);
506   length=sizeof(image->storage_class)+sizeof(image->colorspace)+
507     sizeof(image->alpha_trait)+sizeof(image->mask)+sizeof(image->columns)+
508     sizeof(image->rows)+sizeof(image->number_channels)+MaxPixelChannels*
509     sizeof(*image->channel_map)+sizeof(image->metacontent_extent);
510   count=dpc_read(file,length,message);
511   if (count != (MagickOffsetType) length)
512     return(MagickFalse);
513   /*
514     Deserialize image attributes.
515   */
516   p=message;
517   (void) memcpy(&image->storage_class,p,sizeof(image->storage_class));
518   p+=sizeof(image->storage_class);
519   (void) memcpy(&image->colorspace,p,sizeof(image->colorspace));
520   p+=sizeof(image->colorspace);
521   (void) memcpy(&image->alpha_trait,p,sizeof(image->alpha_trait));
522   p+=sizeof(image->alpha_trait);
523   (void) memcpy(&image->mask,p,sizeof(image->mask));
524   p+=sizeof(image->mask);
525   (void) memcpy(&image->columns,p,sizeof(image->columns));
526   p+=sizeof(image->columns);
527   (void) memcpy(&image->rows,p,sizeof(image->rows));
528   p+=sizeof(image->rows);
529   (void) memcpy(&image->number_channels,p,sizeof(image->number_channels));
530   p+=sizeof(image->number_channels);
531   (void) memcpy(image->channel_map,p,MaxPixelChannels*
532     sizeof(*image->channel_map));
533   p+=MaxPixelChannels*sizeof(*image->channel_map);
534   (void) memcpy(&image->metacontent_extent,p,sizeof(image->metacontent_extent));
535   p+=sizeof(image->metacontent_extent);
536   status=AddValueToSplayTree(registry,(const void *) session_key,image);
537   return(status);
538 }
539
540 static MagickBooleanType ReadDistributeCacheMetacontent(SplayTreeInfo *registry,
541   int file,const MagickSizeType session_key,ExceptionInfo *exception)
542 {
543   const unsigned char
544     *metacontent;
545
546   Image
547     *image;
548
549   MagickOffsetType
550     count;
551
552   MagickSizeType
553     length;
554
555   RectangleInfo
556     region;
557
558   register const Quantum
559     *p;
560
561   register unsigned char
562     *q;
563
564   unsigned char
565     message[MaxTextExtent];
566
567   /*
568     Read distributed pixel cache metacontent.
569   */
570   image=(Image *) GetValueFromSplayTree(registry,(const void *) session_key);
571   if (image == (Image *) NULL)
572     return(MagickFalse);
573   length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
574     sizeof(region.y)+sizeof(length);
575   count=dpc_read(file,length,message);
576   if (count != (MagickOffsetType) length)
577     return(MagickFalse);
578   q=message;
579   (void) memcpy(&region.width,q,sizeof(region.width));
580   q+=sizeof(region.width);
581   (void) memcpy(&region.height,q,sizeof(region.height));
582   q+=sizeof(region.height);
583   (void) memcpy(&region.x,q,sizeof(region.x));
584   q+=sizeof(region.x);
585   (void) memcpy(&region.y,q,sizeof(region.y));
586   q+=sizeof(region.y);
587   (void) memcpy(&length,q,sizeof(length));
588   q+=sizeof(length);
589   p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
590     exception);
591   if (p == (const Quantum *) NULL)
592     return(MagickFalse);
593   metacontent=GetVirtualMetacontent(image);
594   count=dpc_send(file,length,metacontent);
595   if (count != (MagickOffsetType) length)
596     return(MagickFalse);
597   return(MagickTrue);
598 }
599
600 static MagickBooleanType ReadDistributeCachePixels(SplayTreeInfo *registry,
601   int file,const MagickSizeType session_key,ExceptionInfo *exception)
602 {
603   Image
604     *image;
605
606   MagickOffsetType
607     count;
608
609   MagickSizeType
610     length;
611
612   RectangleInfo
613     region;
614
615   register const Quantum
616     *p;
617
618   register unsigned char
619     *q;
620
621   unsigned char
622     message[MaxTextExtent];
623
624   /*
625     Read distributed pixel cache pixels.
626   */
627   image=(Image *) GetValueFromSplayTree(registry,(const void *) session_key);
628   if (image == (Image *) NULL)
629     return(MagickFalse);
630   length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
631     sizeof(region.y)+sizeof(length);
632   count=dpc_read(file,length,message);
633   if (count != (MagickOffsetType) length)
634     return(MagickFalse);
635   q=message;
636   (void) memcpy(&region.width,q,sizeof(region.width));
637   q+=sizeof(region.width);
638   (void) memcpy(&region.height,q,sizeof(region.height));
639   q+=sizeof(region.height);
640   (void) memcpy(&region.x,q,sizeof(region.x));
641   q+=sizeof(region.x);
642   (void) memcpy(&region.y,q,sizeof(region.y));
643   q+=sizeof(region.y);
644   (void) memcpy(&length,q,sizeof(length));
645   q+=sizeof(length);
646   p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
647     exception);
648   if (p == (const Quantum *) NULL)
649     return(MagickFalse);
650   count=dpc_send(file,length,(unsigned char *) p);
651   if (count != (MagickOffsetType) length)
652     return(MagickFalse);
653   return(MagickTrue);
654 }
655
656 static void *RelinquishImageRegistry(void *image)
657 {
658   return((void *) DestroyImageList((Image *) image));
659 }
660
661 static MagickBooleanType WriteDistributeCacheMetacontent(
662   SplayTreeInfo *registry,int file,const MagickSizeType session_key,
663   ExceptionInfo *exception)
664 {
665   Image
666     *image;
667
668   MagickOffsetType
669     count;
670
671   MagickSizeType
672     length;
673
674   RectangleInfo
675     region;
676
677   register Quantum
678     *q;
679
680   register unsigned char
681     *p;
682
683   unsigned char
684     message[MaxTextExtent],
685     *metacontent;
686
687   /*
688     Write distributed pixel cache metacontent.
689   */
690   image=(Image *) GetValueFromSplayTree(registry,(const void *) session_key);
691   if (image == (Image *) NULL)
692     return(MagickFalse);
693   length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
694     sizeof(region.y)+sizeof(length);
695   count=dpc_read(file,length,message);
696   if (count != (MagickOffsetType) length)
697     return(MagickFalse);
698   p=message;
699   (void) memcpy(&region.width,p,sizeof(region.width));
700   p+=sizeof(region.width);
701   (void) memcpy(&region.height,p,sizeof(region.height));
702   p+=sizeof(region.height);
703   (void) memcpy(&region.x,p,sizeof(region.x));
704   p+=sizeof(region.x);
705   (void) memcpy(&region.y,p,sizeof(region.y));
706   p+=sizeof(region.y);
707   (void) memcpy(&length,p,sizeof(length));
708   p+=sizeof(length);
709   q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
710     exception);
711   if (q == (Quantum *) NULL)
712     return(MagickFalse);
713   metacontent=GetAuthenticMetacontent(image);
714   count=dpc_read(file,length,metacontent);
715   if (count != (MagickOffsetType) length)
716     return(MagickFalse);
717   return(SyncAuthenticPixels(image,exception));
718 }
719
720 static MagickBooleanType WriteDistributeCachePixels(SplayTreeInfo *registry,
721   int file,const MagickSizeType session_key,ExceptionInfo *exception)
722 {
723   Image
724     *image;
725
726   MagickOffsetType
727     count;
728
729   MagickSizeType
730     length;
731
732   RectangleInfo
733     region;
734
735   register Quantum
736     *q;
737
738   register unsigned char
739     *p;
740
741   unsigned char
742     message[MaxTextExtent];
743
744   /*
745     Write distributed pixel cache pixels.
746   */
747   image=(Image *) GetValueFromSplayTree(registry,(const void *) session_key);
748   if (image == (Image *) NULL)
749     return(MagickFalse);
750   length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
751     sizeof(region.y)+sizeof(length);
752   count=dpc_read(file,length,message);
753   if (count != (MagickOffsetType) length)
754     return(MagickFalse);
755   p=message;
756   (void) memcpy(&region.width,p,sizeof(region.width));
757   p+=sizeof(region.width);
758   (void) memcpy(&region.height,p,sizeof(region.height));
759   p+=sizeof(region.height);
760   (void) memcpy(&region.x,p,sizeof(region.x));
761   p+=sizeof(region.x);
762   (void) memcpy(&region.y,p,sizeof(region.y));
763   p+=sizeof(region.y);
764   (void) memcpy(&length,p,sizeof(length));
765   p+=sizeof(length);
766   q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
767     exception);
768   if (q == (Quantum *) NULL)
769     return(MagickFalse);
770   count=dpc_read(file,length,(unsigned char *) q);
771   if (count != (MagickOffsetType) length)
772     return(MagickFalse);
773   return(SyncAuthenticPixels(image,exception));
774 }
775
776 static void *DistributePixelCacheClient(void *socket)
777 {
778   const char
779     *shared_secret;
780
781   ExceptionInfo
782     *exception;
783
784   int
785     client_socket;
786
787   MagickBooleanType
788     status;
789
790   MagickOffsetType
791     count;
792
793   MagickSizeType
794     key,
795     session_key,
796     signature;
797
798   register unsigned char
799     *p;
800
801   RandomInfo
802     *random_info;
803
804   SplayTreeInfo
805     *registry;
806
807   StringInfo
808     *secret;
809
810   unsigned char
811     command,
812     session[2*MaxTextExtent];
813
814   /*
815     Distributed pixel cache client.
816   */
817   shared_secret=GetPolicyValue("shared-secret");
818   if (shared_secret == (const char *) NULL)
819     ThrowFatalException(CacheFatalError,"shared secret expected");
820   p=session;
821   (void) CopyMagickString((char *) p,shared_secret,MaxTextExtent);
822   p+=strlen(shared_secret);
823   random_info=AcquireRandomInfo();
824   secret=GetRandomKey(random_info,DPCSessionKeyLength);
825   (void) memcpy(p,GetStringInfoDatum(secret),DPCSessionKeyLength);
826   p+=DPCSessionKeyLength;
827   signature=MagickLibVersion;
828   (void) memcpy(p,&signature,sizeof(signature));
829   p+=sizeof(signature);
830   signature=MAGICKCORE_QUANTUM_DEPTH;
831   (void) memcpy(p,&signature,sizeof(signature));
832   p+=sizeof(signature);
833   signature=MAGICKCORE_HDRI_ENABLE;
834   (void) memcpy(p,&signature,sizeof(signature));
835   p+=sizeof(signature);
836   session_key=CRC64(session,p-session);
837   random_info=DestroyRandomInfo(random_info);
838   exception=AcquireExceptionInfo();
839   registry=NewSplayTree((int (*)(const void *,const void *)) NULL,
840     (void *(*)(void *)) NULL,RelinquishImageRegistry);
841   client_socket=(*(int *) socket);
842   count=dpc_send(client_socket,DPCSessionKeyLength,GetStringInfoDatum(secret));
843   secret=DestroyStringInfo(secret);
844   for ( ; ; )
845   {
846     count=dpc_read(client_socket,1,(unsigned char *) &command);
847     if (count <= 0)
848       break;
849     count=dpc_read(client_socket,sizeof(key),(unsigned char *) &key);
850     if ((count != (MagickOffsetType) sizeof(key)) || (key != session_key))
851       break;
852     status=MagickFalse;
853     switch (command)
854     {
855       case 'o':
856       {
857         status=OpenDistributeCache(registry,client_socket,session_key,
858           exception);
859         count=dpc_send(client_socket,sizeof(status),(unsigned char *) &status);
860         break;
861       }
862       case 'r':
863       {
864         status=ReadDistributeCachePixels(registry,client_socket,session_key,
865           exception);
866         break;
867       }
868       case 'R':
869       {
870         status=ReadDistributeCacheMetacontent(registry,client_socket,
871           session_key,exception);
872         break;
873       }
874       case 'w':
875       {
876         status=WriteDistributeCachePixels(registry,client_socket,session_key,
877           exception);
878         break;
879       }
880       case 'W':
881       {
882         status=WriteDistributeCacheMetacontent(registry,client_socket,
883           session_key,exception);
884         break;
885       }
886       case 'd':
887       {
888         status=DestroyDistributeCache(registry,client_socket,session_key);
889         break;
890       }
891       default:
892         break;
893     }
894     if (status == MagickFalse)
895       break;
896     if (command == 'd')
897       break;
898   }
899   count=dpc_send(client_socket,sizeof(status),(unsigned char *) &status);
900   (void) close(client_socket);
901   exception=DestroyExceptionInfo(exception);
902   registry=DestroySplayTree(registry);
903   return((void *) NULL);
904 }
905
906 MagickExport void DistributePixelCacheServer(const int port,
907   ExceptionInfo *exception)
908 {
909 #if defined(MAGICKCORE_HAVE_SOCKET) && defined(MAGICKCORE_THREAD_SUPPORT)
910   char
911     service[MaxTextExtent];
912
913   int
914     server_socket,
915     status;
916
917   pthread_attr_t
918     attributes;
919
920   pthread_t
921     threads;
922
923   register struct addrinfo
924     *p;
925
926   struct addrinfo
927     hint,
928     *result;
929
930   struct sockaddr_in
931     address;
932
933   /*
934     Launch distributed pixel cache server.
935   */
936   (void) ResetMagickMemory(&hint,0,sizeof(hint));
937   hint.ai_family=AF_INET;
938   hint.ai_socktype=SOCK_STREAM;
939   hint.ai_flags=AI_PASSIVE;
940   (void) FormatLocaleString(service,MaxTextExtent,"%d",port);
941   status=getaddrinfo((const char *) NULL,service,&hint,&result);
942   if (status != 0)
943     ThrowFatalException(CacheFatalError,"UnableToListen");
944   for (p=result; p != (struct addrinfo *) NULL; p=p->ai_next)
945   {
946     int
947       one;
948
949     server_socket=socket(p->ai_family,p->ai_socktype,p->ai_protocol);
950     if (server_socket == -1)
951       continue;
952     one=1;
953     status=setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&one,(socklen_t)
954       sizeof(one));
955     if (status == -1)
956       continue;
957     status=bind(server_socket,p->ai_addr,p->ai_addrlen);
958     if (status == -1)
959       {
960         (void) close(status);
961         continue;
962       }
963     break;
964   }
965   if (p == (struct addrinfo *) NULL)
966     ThrowFatalException(CacheFatalError,"UnableToBind");
967   freeaddrinfo(result);
968   status=listen(server_socket,DPCPendingConnections);
969   if (status != 0)
970     ThrowFatalException(CacheFatalError,"UnableToListen");
971   pthread_attr_init(&attributes);
972   for ( ; ; )
973   {
974     int
975       client_socket;
976
977     socklen_t
978       length;
979
980     length=(socklen_t) sizeof(address);
981     client_socket=accept(server_socket,(struct sockaddr *) &address,&length);
982     if (client_socket == -1)
983       ThrowFatalException(CacheFatalError,"UnableToEstablishConnection");
984     status=pthread_create(&threads,&attributes,DistributePixelCacheClient,
985       (void *) &client_socket);
986     if (status == -1)
987       ThrowFatalException(CacheFatalError,"UnableToCreateClientThread");
988   }
989   (void) close(server_socket);
990 #else
991   ThrowFatalException(MissingDelegateError,"distributed pixel cache");
992 #endif
993 }
994 \f
995 /*
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997 %                                                                             %
998 %                                                                             %
999 %                                                                             %
1000 +   G e t D i s t r i b u t e C a c h e F i l e                               %
1001 %                                                                             %
1002 %                                                                             %
1003 %                                                                             %
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005 %
1006 %  GetDistributeCacheFile() returns the file associated with this
1007 %  DistributeCacheInfo structure.
1008 %
1009 %  The format of the GetDistributeCacheFile method is:
1010 %
1011 %      int GetDistributeCacheFile(const DistributeCacheInfo *server_info)
1012 %
1013 %  A description of each parameter follows:
1014 %
1015 %    o server_info: the distributed cache info.
1016 %
1017 */
1018 MagickPrivate int GetDistributeCacheFile(const DistributeCacheInfo *server_info)
1019 {
1020   assert(server_info != (DistributeCacheInfo *) NULL);
1021   assert(server_info->signature == MagickSignature);
1022   return(server_info->file);
1023 }
1024 \f
1025 /*
1026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027 %                                                                             %
1028 %                                                                             %
1029 %                                                                             %
1030 +   G e t D i s t r i b u t e C a c h e H o s t n a m e                       %
1031 %                                                                             %
1032 %                                                                             %
1033 %                                                                             %
1034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1035 %
1036 %  GetDistributeCacheHostname() returns the hostname associated with this
1037 %  DistributeCacheInfo structure.
1038 %
1039 %  The format of the GetDistributeCacheHostname method is:
1040 %
1041 %      const char *GetDistributeCacheHostname(
1042 %        const DistributeCacheInfo *server_info)
1043 %
1044 %  A description of each parameter follows:
1045 %
1046 %    o server_info: the distributed cache info.
1047 %
1048 */
1049 MagickPrivate const char *GetDistributeCacheHostname(
1050   const DistributeCacheInfo *server_info)
1051 {
1052   assert(server_info != (DistributeCacheInfo *) NULL);
1053   assert(server_info->signature == MagickSignature);
1054   return(server_info->hostname);
1055 }
1056 \f
1057 /*
1058 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1059 %                                                                             %
1060 %                                                                             %
1061 %                                                                             %
1062 +   G e t D i s t r i b u t e C a c h e P o r t                               %
1063 %                                                                             %
1064 %                                                                             %
1065 %                                                                             %
1066 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1067 %
1068 %  GetDistributeCachePort() returns the port associated with this
1069 %  DistributeCacheInfo structure.
1070 %
1071 %  The format of the GetDistributeCachePort method is:
1072 %
1073 %      int GetDistributeCachePort(const DistributeCacheInfo *server_info)
1074 %
1075 %  A description of each parameter follows:
1076 %
1077 %    o server_info: the distributed cache info.
1078 %
1079 */
1080 MagickPrivate int GetDistributeCachePort(const DistributeCacheInfo *server_info)
1081 {
1082   assert(server_info != (DistributeCacheInfo *) NULL);
1083   assert(server_info->signature == MagickSignature);
1084   return(server_info->port);
1085 }
1086 \f
1087 /*
1088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089 %                                                                             %
1090 %                                                                             %
1091 %                                                                             %
1092 +   O p e n D i s t r i b u t e P i x e l C a c h e                           %
1093 %                                                                             %
1094 %                                                                             %
1095 %                                                                             %
1096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1097 %
1098 %  OpenDistributePixelCache() opens a pixel cache on a remote server.
1099 %
1100 %  The format of the OpenDistributePixelCache method is:
1101 %
1102 %      MagickBooleanType *OpenDistributePixelCache(
1103 %        DistributeCacheInfo *server_info,Image *image)
1104 %
1105 %  A description of each parameter follows:
1106 %
1107 %    o server_info: the distributed cache info.
1108 %
1109 %    o image: the image.
1110 %
1111 */
1112 MagickPrivate MagickBooleanType OpenDistributePixelCache(
1113   DistributeCacheInfo *server_info,Image *image)
1114 {
1115   MagickBooleanType
1116     status;
1117
1118   MagickOffsetType
1119     count;
1120
1121   register unsigned char
1122     *p;
1123
1124   unsigned char
1125     message[MaxTextExtent];
1126
1127   /*
1128     Open distributed pixel cache.
1129   */
1130   assert(server_info != (DistributeCacheInfo *) NULL);
1131   assert(server_info->signature == MagickSignature);
1132   assert(image != (Image *) NULL);
1133   assert(image->signature == MagickSignature);
1134   p=message;
1135   *p++='o';  /* open */
1136   /*
1137     Serialize image attributes (see ValidatePixelCacheMorphology()).
1138   */
1139   (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1140   p+=sizeof(server_info->session_key);
1141   (void) memcpy(p,&image->storage_class,sizeof(image->storage_class));
1142   p+=sizeof(image->storage_class);
1143   (void) memcpy(p,&image->colorspace,sizeof(image->colorspace));
1144   p+=sizeof(image->colorspace);
1145   (void) memcpy(p,&image->alpha_trait,sizeof(image->alpha_trait));
1146   p+=sizeof(image->alpha_trait);
1147   (void) memcpy(p,&image->mask,sizeof(image->mask));
1148   p+=sizeof(image->mask);
1149   (void) memcpy(p,&image->columns,sizeof(image->columns));
1150   p+=sizeof(image->columns);
1151   (void) memcpy(p,&image->rows,sizeof(image->rows));
1152   p+=sizeof(image->rows);
1153   (void) memcpy(p,&image->number_channels,sizeof(image->number_channels));
1154   p+=sizeof(image->number_channels);
1155   (void) memcpy(p,image->channel_map,MaxPixelChannels*
1156     sizeof(*image->channel_map));
1157   p+=MaxPixelChannels*sizeof(*image->channel_map);
1158   (void) memcpy(p,&image->metacontent_extent,sizeof(image->metacontent_extent));
1159   p+=sizeof(image->metacontent_extent);
1160   count=dpc_send(server_info->file,p-message,message);
1161   if (count != (MagickOffsetType) (p-message))
1162     return(MagickFalse);
1163   status=MagickFalse;
1164   count=dpc_read(server_info->file,sizeof(status),(unsigned char *) &status);
1165   if (count != (MagickOffsetType) sizeof(status))
1166     return(MagickFalse);
1167   return(status);
1168 }
1169 \f
1170 /*
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 %                                                                             %
1173 %                                                                             %
1174 %                                                                             %
1175 +   R e a d D i s t r i b u t e P i x e l C a c h e M e t a c o n t e n t     %
1176 %                                                                             %
1177 %                                                                             %
1178 %                                                                             %
1179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180 %
1181 %  ReadDistributePixelCacheMetacontents() reads metacontent from the specified
1182 %  region of the distributed pixel cache.
1183 %
1184 %  The format of the ReadDistributePixelCacheMetacontents method is:
1185 %
1186 %      MagickOffsetType ReadDistributePixelCacheMetacontents(
1187 %        DistributeCacheInfo *server_info,const RectangleInfo *region,
1188 %        const MagickSizeType length,unsigned char *metacontent)
1189 %
1190 %  A description of each parameter follows:
1191 %
1192 %    o server_info: the distributed cache info.
1193 %
1194 %    o image: the image.
1195 %
1196 %    o region: read the metacontent from this region of the image.
1197 %
1198 %    o length: the length in bytes of the metacontent.
1199 %
1200 %    o metacontent: read these metacontent from the pixel cache.
1201 %
1202 */
1203 MagickPrivate MagickOffsetType ReadDistributePixelCacheMetacontent(
1204   DistributeCacheInfo *server_info,const RectangleInfo *region,
1205   const MagickSizeType length,unsigned char *metacontent)
1206 {
1207   MagickOffsetType
1208     count;
1209
1210   register unsigned char
1211     *p;
1212
1213   unsigned char
1214     message[MaxTextExtent];
1215
1216   /*
1217     Read distributed pixel cache metacontent.
1218   */
1219   assert(server_info != (DistributeCacheInfo *) NULL);
1220   assert(server_info->signature == MagickSignature);
1221   assert(region != (RectangleInfo *) NULL);
1222   assert(metacontent != (unsigned char *) NULL);
1223   if (length > (MagickSizeType) SSIZE_MAX)
1224     return(-1);
1225   p=message;
1226   *p++='R';
1227   (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1228   p+=sizeof(server_info->session_key);
1229   (void) memcpy(p,&region->width,sizeof(region->width));
1230   p+=sizeof(region->width);
1231   (void) memcpy(p,&region->height,sizeof(region->height));
1232   p+=sizeof(region->height);
1233   (void) memcpy(p,&region->x,sizeof(region->x));
1234   p+=sizeof(region->x);
1235   (void) memcpy(p,&region->y,sizeof(region->y));
1236   p+=sizeof(region->y);
1237   (void) memcpy(p,&length,sizeof(length));
1238   p+=sizeof(length);
1239   count=dpc_send(server_info->file,p-message,message);
1240   if (count != (MagickOffsetType) (p-message))
1241     return(-1);
1242   return(dpc_read(server_info->file,length,metacontent));
1243 }
1244 \f
1245 /*
1246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1247 %                                                                             %
1248 %                                                                             %
1249 %                                                                             %
1250 +   R e a d D i s t r i b u t e P i x e l C a c h e P i x e l s               %
1251 %                                                                             %
1252 %                                                                             %
1253 %                                                                             %
1254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1255 %
1256 %  ReadDistributePixelCachePixels() reads pixels from the specified region of
1257 %  the distributed pixel cache.
1258 %
1259 %  The format of the ReadDistributePixelCachePixels method is:
1260 %
1261 %      MagickOffsetType ReadDistributePixelCachePixels(
1262 %        DistributeCacheInfo *server_info,const RectangleInfo *region,
1263 %        const MagickSizeType length,unsigned char *pixels)
1264 %
1265 %  A description of each parameter follows:
1266 %
1267 %    o server_info: the distributed cache info.
1268 %
1269 %    o image: the image.
1270 %
1271 %    o region: read the pixels from this region of the image.
1272 %
1273 %    o length: the length in bytes of the pixels.
1274 %
1275 %    o pixels: read these pixels from the pixel cache.
1276 %
1277 */
1278 MagickPrivate MagickOffsetType ReadDistributePixelCachePixels(
1279   DistributeCacheInfo *server_info,const RectangleInfo *region,
1280   const MagickSizeType length,unsigned char *pixels)
1281 {
1282   MagickOffsetType
1283     count;
1284
1285   register unsigned char
1286     *p;
1287
1288   unsigned char
1289     message[MaxTextExtent];
1290
1291   /*
1292     Read distributed pixel cache pixels.
1293   */
1294   assert(server_info != (DistributeCacheInfo *) NULL);
1295   assert(server_info->signature == MagickSignature);
1296   assert(region != (RectangleInfo *) NULL);
1297   assert(pixels != (unsigned char *) NULL);
1298   if (length > (MagickSizeType) SSIZE_MAX)
1299     return(-1);
1300   p=message;
1301   *p++='r';
1302   (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1303   p+=sizeof(server_info->session_key);
1304   (void) memcpy(p,&region->width,sizeof(region->width));
1305   p+=sizeof(region->width);
1306   (void) memcpy(p,&region->height,sizeof(region->height));
1307   p+=sizeof(region->height);
1308   (void) memcpy(p,&region->x,sizeof(region->x));
1309   p+=sizeof(region->x);
1310   (void) memcpy(p,&region->y,sizeof(region->y));
1311   p+=sizeof(region->y);
1312   (void) memcpy(p,&length,sizeof(length));
1313   p+=sizeof(length);
1314   count=dpc_send(server_info->file,p-message,message);
1315   if (count != (MagickOffsetType) (p-message))
1316     return(-1);
1317   return(dpc_read(server_info->file,length,pixels));
1318 }
1319 \f
1320 /*
1321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1322 %                                                                             %
1323 %                                                                             %
1324 %                                                                             %
1325 +   R e l i n q u i s h D i s t r i b u t e P i x e l C a c h e               %
1326 %                                                                             %
1327 %                                                                             %
1328 %                                                                             %
1329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1330 %
1331 %  RelinquishDistributePixelCache() frees resources acquired with
1332 %  OpenDistributePixelCache().
1333 %
1334 %  The format of the RelinquishDistributePixelCache method is:
1335 %
1336 %      MagickBooleanType RelinquishDistributePixelCache(
1337 %        DistributeCacheInfo *server_info)
1338 %
1339 %  A description of each parameter follows:
1340 %
1341 %    o server_info: the distributed cache info.
1342 %
1343 */
1344 MagickPrivate MagickBooleanType RelinquishDistributePixelCache(
1345   DistributeCacheInfo *server_info)
1346 {
1347   MagickBooleanType
1348     status;
1349
1350   MagickOffsetType
1351     count;
1352
1353   register unsigned char
1354     *p;
1355
1356   unsigned char
1357     message[MaxTextExtent];
1358
1359   /*
1360     Delete distributed pixel cache.
1361   */
1362   assert(server_info != (DistributeCacheInfo *) NULL);
1363   assert(server_info->signature == MagickSignature);
1364   p=message;
1365   *p++='d';
1366   (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1367   p+=sizeof(server_info->session_key);
1368   count=dpc_send(server_info->file,p-message,message);
1369   if (count != (MagickOffsetType) (p-message))
1370     return(MagickFalse);
1371   count=dpc_read(server_info->file,sizeof(status),(unsigned char *) &status);
1372   if (count != (MagickOffsetType) sizeof(status))
1373     return(MagickFalse);
1374   return(status);
1375 }
1376 \f
1377 /*
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 %                                                                             %
1380 %                                                                             %
1381 %                                                                             %
1382 +   W r i t e D i s t r i b u t e P i x e l C a c h e M e t a c o n t e n t   %
1383 %                                                                             %
1384 %                                                                             %
1385 %                                                                             %
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 %
1388 %  WriteDistributePixelCacheMetacontents() writes image metacontent to the
1389 %  specified region of the distributed pixel cache.
1390 %
1391 %  The format of the WriteDistributePixelCacheMetacontents method is:
1392 %
1393 %      MagickOffsetType WriteDistributePixelCacheMetacontents(
1394 %        DistributeCacheInfo *server_info,const RectangleInfo *region,
1395 %        const MagickSizeType length,const unsigned char *metacontent)
1396 %
1397 %  A description of each parameter follows:
1398 %
1399 %    o server_info: the distributed cache info.
1400 %
1401 %    o image: the image.
1402 %
1403 %    o region: write the metacontent to this region of the image.
1404 %
1405 %    o length: the length in bytes of the metacontent.
1406 %
1407 %    o metacontent: write these metacontent to the pixel cache.
1408 %
1409 */
1410 MagickPrivate MagickOffsetType WriteDistributePixelCacheMetacontent(
1411   DistributeCacheInfo *server_info,const RectangleInfo *region,
1412   const MagickSizeType length,const unsigned char *metacontent)
1413 {
1414   MagickOffsetType
1415     count;
1416
1417   register unsigned char
1418     *p;
1419
1420   unsigned char
1421     message[MaxTextExtent];
1422
1423   /*
1424     Write distributed pixel cache metacontent.
1425   */
1426   assert(server_info != (DistributeCacheInfo *) NULL);
1427   assert(server_info->signature == MagickSignature);
1428   assert(region != (RectangleInfo *) NULL);
1429   assert(metacontent != (unsigned char *) NULL);
1430   if (length > (MagickSizeType) SSIZE_MAX)
1431     return(-1);
1432   p=message;
1433   *p++='W';
1434   (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1435   p+=sizeof(server_info->session_key);
1436   (void) memcpy(p,&region->width,sizeof(region->width));
1437   p+=sizeof(region->width);
1438   (void) memcpy(p,&region->height,sizeof(region->height));
1439   p+=sizeof(region->height);
1440   (void) memcpy(p,&region->x,sizeof(region->x));
1441   p+=sizeof(region->x);
1442   (void) memcpy(p,&region->y,sizeof(region->y));
1443   p+=sizeof(region->y);
1444   (void) memcpy(p,&length,sizeof(length));
1445   p+=sizeof(length);
1446   count=dpc_send(server_info->file,p-message,message);
1447   if (count != (MagickOffsetType) (p-message))
1448     return(-1);
1449   return(dpc_send(server_info->file,length,metacontent));
1450 }
1451 \f
1452 /*
1453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1454 %                                                                             %
1455 %                                                                             %
1456 %                                                                             %
1457 +   W r i t e D i s t r i b u t e P i x e l C a c h e P i x e l s             %
1458 %                                                                             %
1459 %                                                                             %
1460 %                                                                             %
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 %
1463 %  WriteDistributePixelCachePixels() writes image pixels to the specified
1464 %  region of the distributed pixel cache.
1465 %
1466 %  The format of the WriteDistributePixelCachePixels method is:
1467 %
1468 %      MagickBooleanType WriteDistributePixelCachePixels(
1469 %        DistributeCacheInfo *server_info,const RectangleInfo *region,
1470 %        const MagickSizeType length,const unsigned char *pixels)
1471 %
1472 %  A description of each parameter follows:
1473 %
1474 %    o server_info: the distributed cache info.
1475 %
1476 %    o image: the image.
1477 %
1478 %    o region: write the pixels to this region of the image.
1479 %
1480 %    o length: the length in bytes of the pixels.
1481 %
1482 %    o pixels: write these pixels to the pixel cache.
1483 %
1484 */
1485 MagickPrivate MagickOffsetType WriteDistributePixelCachePixels(
1486   DistributeCacheInfo *server_info,const RectangleInfo *region,
1487   const MagickSizeType length,const unsigned char *pixels)
1488 {
1489   MagickOffsetType
1490     count;
1491
1492   register unsigned char
1493     *p;
1494
1495   unsigned char
1496     message[MaxTextExtent];
1497
1498   /*
1499     Write distributed pixel cache pixels.
1500   */
1501   assert(server_info != (DistributeCacheInfo *) NULL);
1502   assert(server_info->signature == MagickSignature);
1503   assert(region != (RectangleInfo *) NULL);
1504   assert(pixels != (const unsigned char *) NULL);
1505   if (length > (MagickSizeType) SSIZE_MAX)
1506     return(-1);
1507   p=message;
1508   *p++='w';
1509   (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1510   p+=sizeof(server_info->session_key);
1511   (void) memcpy(p,&region->width,sizeof(region->width));
1512   p+=sizeof(region->width);
1513   (void) memcpy(p,&region->height,sizeof(region->height));
1514   p+=sizeof(region->height);
1515   (void) memcpy(p,&region->x,sizeof(region->x));
1516   p+=sizeof(region->x);
1517   (void) memcpy(p,&region->y,sizeof(region->y));
1518   p+=sizeof(region->y);
1519   (void) memcpy(p,&length,sizeof(length));
1520   p+=sizeof(length);
1521   count=dpc_send(server_info->file,p-message,message);
1522   if (count != (MagickOffsetType) (p-message))
1523     return(-1);
1524   return(dpc_send(server_info->file,length,pixels));
1525 }