]> 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/locale_.h"
64 #include "MagickCore/memory_.h"
65 #include "MagickCore/policy.h"
66 #include "MagickCore/random_.h"
67 #include "MagickCore/registry.h"
68 #include "MagickCore/splay-tree.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/string-private.h"
71 #if defined(MAGICKCORE_HAVE_SOCKET)
72 #include <netinet/in.h>
73 #include <netdb.h>
74 #include <sys/socket.h>
75 #include <arpa/inet.h>
76 #endif
77 \f
78 /*
79   Define declarations.
80 */
81 #define DPCHostname  "127.0.0.1"
82 #define DPCPort  6668
83 #define DPCSessionKeyLength  8
84 \f
85 /*
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %                                                                             %
88 %                                                                             %
89 %                                                                             %
90 +   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                       %
91 %                                                                             %
92 %                                                                             %
93 %                                                                             %
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 %
96 %  AcquireDistributeCacheInfo() allocates the DistributeCacheInfo structure.
97 %
98 %  The format of the AcquireDistributeCacheInfo method is:
99 %
100 %      DistributeCacheInfo *AcquireDistributeCacheInfo(ExceptionInfo *exception)
101 %
102 %  A description of each parameter follows:
103 %
104 %    o exception: return any errors or warnings in this structure.
105 %
106 */
107
108 static MagickSizeType CRC64(const unsigned char *message,const size_t length)
109 {
110   MagickSizeType
111     crc;
112
113   register ssize_t
114     i;
115
116   static MagickBooleanType
117     crc_initial = MagickFalse;
118
119   static MagickSizeType
120     crc_xor[256];
121
122   if (crc_initial == MagickFalse)
123     {
124       MagickSizeType
125         alpha;
126
127       for (i=0; i < 256; i++)
128       {
129         register ssize_t
130           j;
131
132         alpha=(MagickSizeType) i;
133         for (j=0; j < 8; j++)
134         {
135           if ((alpha & 0x01) == 0)
136             alpha>>=1;
137           else
138             alpha=(MagickSizeType) ((alpha >> 1) ^
139               MagickULLConstant(0xd800000000000000));
140         }
141         crc_xor[i]=alpha;
142       }
143       crc_initial=MagickTrue;
144     }
145   crc=0;
146   for (i=0; i < (ssize_t) length; i++)
147     crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
148   return(crc);
149 }
150
151 static int ConnectPixelCacheServer(const char *hostname,const int port,
152   MagickSizeType *session_key,ExceptionInfo *exception)
153 {
154 #if defined(MAGICKCORE_HAVE_SOCKET)
155   char
156     secret[MaxTextExtent];
157
158   const char
159     *shared_secret;
160
161   int
162     client_socket,
163     status;
164
165   ssize_t
166     count;
167
168   struct hostent
169     *host;
170
171   struct sockaddr_in
172     address;
173
174   unsigned char
175     session[MaxTextExtent];
176
177   /*
178     Connect to distributed pixel cache and get session key.
179   */
180   *session_key=0;
181   shared_secret=GetPolicyValue("shared-secret");
182   if (shared_secret == (const char *) NULL)
183     {
184       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
185         "DistributedPixelCache","'%s'","shared secret expected");
186       return(-1);
187     }
188   (void) CopyMagickString((char *) session,shared_secret,MaxTextExtent-
189     DPCSessionKeyLength);
190   host=gethostbyname(hostname);
191   client_socket=socket(AF_INET,SOCK_STREAM,0);
192   if (client_socket == -1)
193     {
194       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
195         "DistributedPixelCache","'%s'",hostname);
196       return(-1);
197     }
198   (void) ResetMagickMemory(&address,0,sizeof(address));
199   address.sin_family=AF_INET;
200   address.sin_port=htons((uint16_t) port);
201   address.sin_addr=(*((struct in_addr *) host->h_addr));
202   status=connect(client_socket,(struct sockaddr *) &address,(socklen_t)
203     sizeof(struct sockaddr));
204   if (status == -1)
205     {
206       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
207         "DistributedPixelCache","'%s'",hostname);
208       return(-1);
209     }
210   count=read(client_socket,secret,MaxTextExtent);
211   if (count != -1)
212     {
213       (void) memcpy(session+strlen(shared_secret),secret,(size_t) count);
214       *session_key=CRC64(session,strlen(shared_secret)+count);
215     }
216   if (*session_key == 0)
217     {
218       close(client_socket);
219       client_socket=(-1);
220     }
221   return(client_socket);
222 #else
223   (void) ThrowMagickException(exception,GetMagickModule(),MissingDelegateError,
224     "DelegateLibrarySupportNotBuiltIn","distributed pixel cache");
225   return(MagickFalse);
226 #endif
227 }
228
229 static char *GetHostname(int *port,ExceptionInfo *exception)
230 {
231   char
232     *host,
233     *hosts,
234     **hostlist;
235
236   int
237     argc;
238
239   register ssize_t
240     i;
241
242   static size_t
243     id = 0;
244
245   /*
246     Parse host list (e.g. 192.168.100.1:6668,192.168.100.2:6668).
247   */
248   hosts=(char *) GetImageRegistry(StringRegistryType,"cache:hosts",
249     exception);
250   if (hosts == (char *) NULL)
251     {
252       *port=DPCPort;
253       return(AcquireString(DPCHostname));
254     }
255   (void) SubstituteString(&hosts,","," ");
256   hostlist=StringToArgv(hosts,&argc);
257   hosts=DestroyString(hosts);
258   if (hostlist == (char **) NULL)
259     {
260       *port=DPCPort;
261       return(AcquireString(DPCHostname));
262     }
263   hosts=AcquireString(hostlist[(id++ % (argc-1))+1]);
264   for (i=0; i < (ssize_t) argc; i++)
265     hostlist[i]=DestroyString(hostlist[i]);
266   hostlist=(char **) RelinquishMagickMemory(hostlist);
267   (void) SubstituteString(&hosts,":"," ");
268   hostlist=StringToArgv(hosts,&argc);
269   if (hostlist == (char **) NULL)
270     {
271       *port=DPCPort;
272       return(AcquireString(DPCHostname));
273     }
274   host=AcquireString(hostlist[1]);
275   if (hostlist[2] == (char *) NULL)
276     *port=DPCPort;
277   else
278     *port=StringToLong(hostlist[2]);
279   for (i=0; i < (ssize_t) argc; i++)
280     hostlist[i]=DestroyString(hostlist[i]);
281   hostlist=(char **) RelinquishMagickMemory(hostlist);
282   return(host);
283 }
284
285 MagickPrivate DistributeCacheInfo *AcquireDistributeCacheInfo(
286   ExceptionInfo *exception)
287 {
288   char
289     *hostname;
290
291   DistributeCacheInfo
292     *distribute_cache_info;
293
294   MagickSizeType
295     session_key;
296
297   distribute_cache_info=(DistributeCacheInfo *) AcquireMagickMemory(
298     sizeof(*distribute_cache_info));
299   if (distribute_cache_info == (DistributeCacheInfo *) NULL)
300     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
301   (void) ResetMagickMemory(distribute_cache_info,0,
302     sizeof(*distribute_cache_info));
303   distribute_cache_info->signature=MagickSignature;
304   /*
305     Contact pixel cache server.
306   */
307   distribute_cache_info->port=0;
308   hostname=GetHostname(&distribute_cache_info->port,exception);
309   session_key=0;
310   distribute_cache_info->file=ConnectPixelCacheServer(hostname,
311     distribute_cache_info->port,&session_key,exception);
312   distribute_cache_info->session_key=session_key;
313   (void) CopyMagickString(distribute_cache_info->hostname,hostname,
314     MaxTextExtent);
315   hostname=DestroyString(hostname);
316   if (distribute_cache_info->file == -1)
317     distribute_cache_info=DestroyDistributeCacheInfo(distribute_cache_info);
318   return(distribute_cache_info);
319 }
320 \f
321 /*
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 %                                                                             %
324 %                                                                             %
325 %                                                                             %
326 +   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                       %
327 %                                                                             %
328 %                                                                             %
329 %                                                                             %
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 %
332 %  DestroyDistributeCacheInfo() deallocates memory associated with an
333 %  DistributeCacheInfo structure.
334 %
335 %  The format of the DestroyDistributeCacheInfo method is:
336 %
337 %      DistributeCacheInfo *DestroyDistributeCacheInfo(
338 %        DistributeCacheInfo *distribute_cache_info)
339 %
340 %  A description of each parameter follows:
341 %
342 %    o distribute_cache_info: the distributed cache info.
343 %
344 */
345 MagickPrivate DistributeCacheInfo *DestroyDistributeCacheInfo(
346   DistributeCacheInfo *distribute_cache_info)
347 {
348   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
349   assert(distribute_cache_info->signature == MagickSignature);
350   distribute_cache_info->signature=(~MagickSignature);
351   distribute_cache_info=(DistributeCacheInfo *) RelinquishMagickMemory(
352     distribute_cache_info);
353   return(distribute_cache_info);
354 }
355 \f
356 /*
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 %                                                                             %
359 %                                                                             %
360 %                                                                             %
361 +   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                       %
362 %                                                                             %
363 %                                                                             %
364 %                                                                             %
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 %
367 %  DistributePixelCacheServer() waits on the specified port for commands to
368 %  create, read, update, or destroy a pixel cache.
369 %
370 %  The format of the DistributePixelCacheServer() method is:
371 %
372 %      void DistributePixelCacheServer(const size_t port)
373 %
374 %  A description of each parameter follows:
375 %
376 %    o port: connect the distributed pixel cache at this port.
377 %
378 %    o exception: return any errors or warnings in this structure.
379 %
380 */
381
382 static MagickBooleanType CreateDistributeCache(SplayTreeInfo *image_registry,
383   int file,const MagickSizeType session_key)
384 {
385   ExceptionInfo
386     *exception;
387
388   Image
389     *image;
390
391   MagickBooleanType
392     status;
393
394   register unsigned char
395     *p;
396
397   size_t
398     length;
399
400   ssize_t
401     count;
402
403   unsigned char
404     buffer[MaxTextExtent];
405
406   exception=AcquireExceptionInfo();
407   image=AcquireImage((ImageInfo *) NULL,exception);
408   exception=DestroyExceptionInfo(exception);
409   length=sizeof(image->columns)+sizeof(image->rows)+
410     sizeof(image->number_channels);
411   count=read(file,buffer,length);
412   if (count != (ssize_t) length)
413     return(MagickFalse);
414   p=buffer;
415   (void) memcpy(&image->columns,p,sizeof(image->columns));
416   p+=sizeof(image->columns);
417   (void) memcpy(&image->rows,p,sizeof(image->rows));
418   p+=sizeof(image->rows);
419   (void) memcpy(&image->number_channels,p,sizeof(image->number_channels));
420   p+=sizeof(image->number_channels);
421   status=AddValueToSplayTree(image_registry,(const void *) session_key,image);
422   return(status);
423 }
424
425 static MagickBooleanType DestroyDistributeCache(SplayTreeInfo *image_registry,
426   int file,const MagickSizeType session_key)
427 {
428   return(DeleteNodeFromSplayTree(image_registry,(const void *) session_key));
429 }
430
431 static MagickBooleanType ReadDistributeCacheMetacontent(
432   SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
433 {
434   ExceptionInfo
435     *exception;
436
437   Image
438     *image;
439
440   RectangleInfo
441     region;
442
443   register const Quantum
444     *p;
445
446   register unsigned char
447     *q;
448
449   size_t
450     length;
451
452   ssize_t
453     count;
454
455   unsigned char
456     buffer[MaxTextExtent];
457
458   image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
459     session_key);
460   if (image == (Image *) NULL)
461     return(MagickFalse);
462   length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
463     sizeof(region.y)+sizeof(length);
464   count=read(file,buffer,length);
465   if (count != (ssize_t) length)
466     return(MagickFalse);
467   q=buffer;
468   (void) memcpy(&region.width,q,sizeof(region.width));
469   q+=sizeof(region.width);
470   (void) memcpy(&region.height,q,sizeof(region.height));
471   q+=sizeof(region.width);
472   (void) memcpy(&region.x,q,sizeof(region.x));
473   q+=sizeof(region.width);
474   (void) memcpy(&region.y,q,sizeof(region.y));
475   q+=sizeof(region.width);
476   (void) memcpy(&length,q,sizeof(length));
477   q+=sizeof(length);
478   exception=AcquireExceptionInfo();
479   p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
480     exception);
481   exception=DestroyExceptionInfo(exception);
482   if (p == (const Quantum *) NULL)
483     return(MagickFalse);
484   count=write(file,p,length);
485   if (count != (ssize_t) length)
486     return(MagickFalse);
487   return(MagickTrue);
488 }
489
490 static MagickBooleanType ReadDistributeCachePixels(
491   SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
492 {
493   ExceptionInfo
494     *exception;
495
496   Image
497     *image;
498
499   RectangleInfo
500     region;
501
502   register const Quantum
503     *p;
504
505   register unsigned char
506     *q;
507
508   size_t
509     length;
510
511   ssize_t
512     count;
513
514   unsigned char
515     buffer[MaxTextExtent];
516
517   image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
518     session_key);
519   if (image == (Image *) NULL)
520     return(MagickFalse);
521   length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
522     sizeof(region.y)+sizeof(length);
523   count=read(file,buffer,length);
524   if (count != (ssize_t) length)
525     return(MagickFalse);
526   q=buffer;
527   (void) memcpy(&region.width,q,sizeof(region.width));
528   q+=sizeof(region.width);
529   (void) memcpy(&region.height,q,sizeof(region.height));
530   q+=sizeof(region.width);
531   (void) memcpy(&region.x,q,sizeof(region.x));
532   q+=sizeof(region.width);
533   (void) memcpy(&region.y,q,sizeof(region.y));
534   q+=sizeof(region.width);
535   (void) memcpy(&length,q,sizeof(length));
536   q+=sizeof(length);
537   exception=AcquireExceptionInfo();
538   p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
539     exception);
540   exception=DestroyExceptionInfo(exception);
541   if (p == (const Quantum *) NULL)
542     return(MagickFalse);
543   count=write(file,p,length);
544   if (count != (ssize_t) length)
545     return(MagickFalse);
546   return(MagickTrue);
547 }
548
549 static MagickBooleanType WriteDistributeCacheMetacontent(
550   SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
551 {
552   ExceptionInfo
553     *exception;
554
555   Image
556     *image;
557
558   MagickBooleanType
559     status;
560
561   RectangleInfo
562     region;
563
564   register Quantum
565     *q;
566
567   register unsigned char
568     *p;
569
570   size_t
571     length;
572
573   ssize_t
574     count;
575
576   unsigned char
577     buffer[MaxTextExtent];
578
579   image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
580     session_key);
581   if (image == (Image *) NULL)
582     return(MagickFalse);
583   length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
584     sizeof(region.y)+sizeof(length);
585   count=read(file,buffer,length);
586   if (count != (ssize_t) length)
587     return(MagickFalse);
588   p=buffer;
589   (void) memcpy(&region.width,p,sizeof(region.width));
590   p+=sizeof(region.width);
591   (void) memcpy(&region.height,p,sizeof(region.height));
592   p+=sizeof(region.width);
593   (void) memcpy(&region.x,p,sizeof(region.x));
594   p+=sizeof(region.width);
595   (void) memcpy(&region.y,p,sizeof(region.y));
596   p+=sizeof(region.width);
597   (void) memcpy(&length,p,sizeof(length));
598   p+=sizeof(length);
599   exception=AcquireExceptionInfo();
600   q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
601     exception);
602   exception=DestroyExceptionInfo(exception);
603   if (q == (Quantum *) NULL)
604     return(MagickFalse);
605   count=read(file,q,length);
606   if (count != (ssize_t) length)
607     return(MagickFalse);
608   status=SyncAuthenticPixels(image,exception);
609   return(status);
610 }
611
612 static MagickBooleanType WriteDistributeCachePixels(
613   SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
614 {
615   ExceptionInfo
616     *exception;
617
618   Image
619     *image;
620
621   MagickBooleanType
622     status;
623
624   RectangleInfo
625     region;
626
627   register Quantum
628     *q;
629
630   register unsigned char
631     *p;
632
633   size_t
634     length;
635
636   ssize_t
637     count;
638
639   unsigned char
640     buffer[MaxTextExtent];
641
642   image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
643     session_key);
644   if (image == (Image *) NULL)
645     return(MagickFalse);
646   length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
647     sizeof(region.y)+sizeof(length);
648   count=read(file,buffer,length);
649   if (count != (ssize_t) length)
650     return(MagickFalse);
651   p=buffer;
652   (void) memcpy(&region.width,p,sizeof(region.width));
653   p+=sizeof(region.width);
654   (void) memcpy(&region.height,p,sizeof(region.height));
655   p+=sizeof(region.width);
656   (void) memcpy(&region.x,p,sizeof(region.x));
657   p+=sizeof(region.width);
658   (void) memcpy(&region.y,p,sizeof(region.y));
659   p+=sizeof(region.width);
660   (void) memcpy(&length,p,sizeof(length));
661   p+=sizeof(length);
662   exception=AcquireExceptionInfo();
663   q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
664     exception);
665   exception=DestroyExceptionInfo(exception);
666   if (q == (Quantum *) NULL)
667     return(MagickFalse);
668   count=read(file,q,length);
669   if (count != (ssize_t) length)
670     return(MagickFalse);
671   status=SyncAuthenticPixels(image,exception);
672   return(status);
673 }
674
675 static void *DistributePixelCacheClient(void *socket)
676 {
677   const char
678     *shared_secret;
679
680   int
681     client_socket;
682
683   MagickBooleanType
684     status;
685
686   MagickSizeType
687     key,
688     session_key;
689
690   RandomInfo
691     *random_info;
692
693   SplayTreeInfo
694     *image_registry;
695
696   ssize_t
697     count;
698
699   StringInfo
700     *secret;
701
702   unsigned char
703     command,
704     session[MaxTextExtent];
705
706   /*
707     Generate session key.
708   */
709   shared_secret=GetPolicyValue("shared-secret");
710   if (shared_secret == (const char *) NULL)
711     ThrowFatalException(CacheFatalError,"shared secret expected");
712   (void) CopyMagickString((char *) session,shared_secret,MaxTextExtent-
713     DPCSessionKeyLength);
714   random_info=AcquireRandomInfo();
715   secret=GetRandomKey(random_info,DPCSessionKeyLength);
716   (void) memcpy(session+strlen(shared_secret),GetStringInfoDatum(secret),
717     DPCSessionKeyLength);
718   session_key=CRC64(session,strlen(shared_secret)+DPCSessionKeyLength);
719   random_info=DestroyRandomInfo(random_info);
720   image_registry=NewSplayTree((int (*)(const void *,const void *)) NULL,
721     (void *(*)(void *)) NULL,(void *(*)(void *)) NULL);
722   client_socket=(*(int *) socket);
723   count=write(client_socket,GetStringInfoDatum(secret),DPCSessionKeyLength);
724   secret=DestroyStringInfo(secret);
725   for ( ; ; )
726   {
727     count=read(client_socket,&command,1);
728     if (count <= 0)
729       break;
730     count=read(client_socket,&key,sizeof(key));
731     if ((count != (ssize_t) sizeof(key)) && (key != session_key))
732       break;
733     status=MagickFalse;
734     switch (command)
735     {
736       case 'c':
737       {
738         status=CreateDistributeCache(image_registry,client_socket,session_key);
739         break;
740       }
741       case 'r':
742       {
743         status=ReadDistributeCachePixels(image_registry,client_socket,
744           session_key);
745         break;
746       }
747       case 'u':
748       {
749         status=WriteDistributeCachePixels(image_registry,client_socket,
750           session_key);
751         break;
752       }
753       case 'd':
754       {
755         status=DestroyDistributeCache(image_registry,client_socket,session_key);
756         break;
757       }
758       case 'm':
759       {
760         status=ReadDistributeCacheMetacontent(image_registry,client_socket,
761           session_key);
762         break;
763       }
764       case 'M':
765       {
766         status=WriteDistributeCacheMetacontent(image_registry,client_socket,
767           session_key);
768         break;
769       }
770       default:
771         break;
772     }
773     count=write(client_socket,&status,sizeof(status));
774     if (count != (ssize_t) sizeof(status))
775       break;
776   }
777   return((void *) NULL);
778 }
779
780 MagickExport void DistributePixelCacheServer(const size_t port,
781   ExceptionInfo *exception)
782 {
783 #if defined(MAGICKCORE_HAVE_SOCKET) && defined(MAGICKCORE_THREAD_SUPPORT)
784   int
785     server_socket,
786     status;
787
788   pthread_attr_t
789     attributes;
790
791   pthread_t
792     threads;
793
794   struct sockaddr_in
795     address;
796
797   /*
798     Launch distributed pixel cache server.
799   */
800   server_socket=socket(AF_INET,SOCK_STREAM,0);
801   address.sin_family=AF_INET;
802   address.sin_port=htons(port);
803   address.sin_addr.s_addr=htonl(INADDR_ANY);
804   status=bind(server_socket,(struct sockaddr *) &address,(socklen_t)
805     sizeof(address));
806   if (status != 0)
807     ThrowFatalException(CacheFatalError,"UnableToBind");
808   status=listen(server_socket,32);
809   if (status != 0)
810     ThrowFatalException(CacheFatalError,"UnableToListen");
811   pthread_attr_init(&attributes);
812   for ( ; ; )
813   {
814     int
815       client_socket;
816
817     socklen_t
818       length;
819
820     length=(socklen_t) sizeof(address);
821     client_socket=accept(server_socket,(struct sockaddr *) &address,&length);
822     if (client_socket == -1)
823       ThrowFatalException(CacheFatalError,"UnableToEstablishConnection");
824     status=pthread_create(&threads,&attributes,DistributePixelCacheClient,
825       (void *) &client_socket);
826     if (status == -1)
827       ThrowFatalException(CacheFatalError,"UnableToCreateClientThread");
828   }
829   (void) close(server_socket);
830 #else
831   ThrowFatalException(MissingDelegateError,"distributed pixel cache");
832 #endif
833 }
834 \f
835 /*
836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 %                                                                             %
838 %                                                                             %
839 %                                                                             %
840 +   G e t D i s t r i b u t e C a c h e F i l e                               %
841 %                                                                             %
842 %                                                                             %
843 %                                                                             %
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845 %
846 %  GetDistributeCacheFile() returns the file associated with this
847 %  DistributeCacheInfo structure.
848 %
849 %  The format of the GetDistributeCacheFile method is:
850 %
851 %      int GetDistributeCacheFile(
852 %        const DistributeCacheInfo *distribute_cache_info)
853 %
854 %  A description of each parameter follows:
855 %
856 %    o distribute_cache_info: the distributed cache info.
857 %
858 */
859 MagickPrivate int GetDistributeCacheFile(
860   const DistributeCacheInfo *distribute_cache_info)
861 {
862   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
863   assert(distribute_cache_info->signature == MagickSignature);
864   return(distribute_cache_info->file);
865 }
866 \f
867 /*
868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869 %                                                                             %
870 %                                                                             %
871 %                                                                             %
872 +   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                       %
873 %                                                                             %
874 %                                                                             %
875 %                                                                             %
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877 %
878 %  GetDistributeCacheHostname() returns the hostname associated with this
879 %  DistributeCacheInfo structure.
880 %
881 %  The format of the GetDistributeCacheHostname method is:
882 %
883 %      const char *GetDistributeCacheHostname(
884 %        const DistributeCacheInfo *distribute_cache_info)
885 %
886 %  A description of each parameter follows:
887 %
888 %    o distribute_cache_info: the distributed cache info.
889 %
890 */
891 MagickPrivate const char *GetDistributeCacheHostname(
892   const DistributeCacheInfo *distribute_cache_info)
893 {
894   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
895   assert(distribute_cache_info->signature == MagickSignature);
896   return(distribute_cache_info->hostname);
897 }
898 \f
899 /*
900 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 %                                                                             %
902 %                                                                             %
903 %                                                                             %
904 +   G e t D i s t r i b u t e C a c h e P o r t                               %
905 %                                                                             %
906 %                                                                             %
907 %                                                                             %
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909 %
910 %  GetDistributeCachePort() returns the port associated with this
911 %  DistributeCacheInfo structure.
912 %
913 %  The format of the GetDistributeCachePort method is:
914 %
915 %      int GetDistributeCachePort(
916 %        const DistributeCacheInfo *distribute_cache_info)
917 %
918 %  A description of each parameter follows:
919 %
920 %    o distribute_cache_info: the distributed cache info.
921 %
922 */
923 MagickPrivate int GetDistributeCachePort(
924   const DistributeCacheInfo *distribute_cache_info)
925 {
926   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
927   assert(distribute_cache_info->signature == MagickSignature);
928   return(distribute_cache_info->port);
929 }
930 \f
931 /*
932 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933 %                                                                             %
934 %                                                                             %
935 %                                                                             %
936 +   O p e n D i s t r i b u t e P i x e l C a c h e                           %
937 %                                                                             %
938 %                                                                             %
939 %                                                                             %
940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941 %
942 %  OpenDistributePixelCache() opens a pixel cache on a remote server.
943 %
944 %  The format of the OpenDistributePixelCache method is:
945 %
946 %      MagickBooleanType *OpenDistributePixelCache(
947 %        DistributeCacheInfo *distribute_cache_info,Image *image)
948 %
949 %  A description of each parameter follows:
950 %
951 %    o distribute_cache_info: the distributed cache info.
952 %
953 %    o image: the image.
954 %
955 */
956 MagickPrivate MagickBooleanType OpenDistributePixelCache(
957   DistributeCacheInfo *distribute_cache_info,Image *image)
958 {
959   MagickBooleanType
960     status;
961
962   register unsigned char
963     *p;
964
965   ssize_t
966     count;
967
968   unsigned char
969     buffer[MaxTextExtent];
970
971   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
972   assert(distribute_cache_info->signature == MagickSignature);
973   assert(image != (Image *) NULL);
974   assert(image->signature == MagickSignature);
975   p=buffer;
976   *p++='c';  /* create */
977   (void) memcpy(p,&distribute_cache_info->session_key,
978     sizeof(distribute_cache_info->session_key));
979   p+=sizeof(distribute_cache_info->session_key);
980   (void) memcpy(p,&image->columns,sizeof(image->columns));
981   p+=sizeof(image->columns);
982   (void) memcpy(p,&image->rows,sizeof(image->rows));
983   p+=sizeof(image->rows);
984   (void) memcpy(p,&image->number_channels,sizeof(image->number_channels));
985   p+=sizeof(image->number_channels);
986   count=write(distribute_cache_info->file,buffer,p-buffer);
987   if (count != (ssize_t) (p-buffer))
988     return(MagickFalse);
989   count=read(distribute_cache_info->file,&status,sizeof(status));
990   if (count != (ssize_t) sizeof(status))
991     return(MagickFalse);
992   return(MagickTrue);
993 }
994 \f
995 /*
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997 %                                                                             %
998 %                                                                             %
999 %                                                                             %
1000 +   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     %
1001 %                                                                             %
1002 %                                                                             %
1003 %                                                                             %
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005 %
1006 %  ReadDistributePixelCacheMetacontents() reads metacontent from the specified
1007 %  region of the distributed pixel cache.
1008 %
1009 %  The format of the ReadDistributePixelCacheMetacontents method is:
1010 %
1011 %      MagickBooleanType *ReadDistributePixelCacheMetacontents(
1012 %        DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1013 %        const MagickSizeType length,unsigned char *metacontent)
1014 %
1015 %  A description of each parameter follows:
1016 %
1017 %    o distribute_cache_info: the distributed cache info.
1018 %
1019 %    o image: the image.
1020 %
1021 %    o region: read the metacontent from this region of the image.
1022 %
1023 %    o length: write the metacontent to this region of the image.
1024 %
1025 %    o metacontent: read these metacontent from the pixel cache.
1026 %
1027 */
1028 MagickPrivate MagickBooleanType ReadDistributePixelCacheMetacontent(
1029   DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1030   const MagickSizeType length,unsigned char *metacontent)
1031 {
1032   MagickBooleanType
1033     status;
1034
1035   register unsigned char
1036     *p;
1037
1038   ssize_t
1039     count;
1040
1041   unsigned char
1042     buffer[MaxTextExtent];
1043
1044   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1045   assert(distribute_cache_info->signature == MagickSignature);
1046   assert(region != (RectangleInfo *) NULL);
1047   assert(metacontent != (unsigned char *) NULL);
1048   assert(length == ((size_t) length));
1049   p=buffer;
1050   *p++='m';  /* read */
1051   (void) memcpy(p,&distribute_cache_info->session_key,
1052     sizeof(distribute_cache_info->session_key));
1053   p+=sizeof(distribute_cache_info->session_key);
1054   (void) memcpy(p,&region->width,sizeof(region->width));
1055   p+=sizeof(region->width);
1056   (void) memcpy(p,&region->height,sizeof(region->height));
1057   p+=sizeof(region->height);
1058   (void) memcpy(p,&region->x,sizeof(region->x));
1059   p+=sizeof(region->x);
1060   (void) memcpy(p,&region->y,sizeof(region->y));
1061   p+=sizeof(region->y);
1062   (void) memcpy(p,&length,sizeof(length));
1063   p+=sizeof(length);
1064   count=write(distribute_cache_info->file,buffer,p-buffer);
1065   if (count != (ssize_t) (p-buffer))
1066     return(MagickFalse);
1067   count=read(distribute_cache_info->file,(unsigned char *) metacontent,(size_t)
1068     length);
1069   if (count != (ssize_t) length)
1070     return(MagickFalse);
1071   count=read(distribute_cache_info->file,&status,sizeof(status));
1072   if (count != (ssize_t) sizeof(status))
1073     return(MagickFalse);
1074   return(status != 0 ? MagickTrue : MagickFalse);
1075 }
1076 \f
1077 /*
1078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079 %                                                                             %
1080 %                                                                             %
1081 %                                                                             %
1082 +   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               %
1083 %                                                                             %
1084 %                                                                             %
1085 %                                                                             %
1086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 %
1088 %  ReadDistributePixelCachePixels() reads pixels from the specified region of
1089 %  the distributed pixel cache.
1090 %
1091 %  The format of the ReadDistributePixelCachePixels method is:
1092 %
1093 %      MagickBooleanType *ReadDistributePixelCachePixels(
1094 %        DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1095 %        const MagickSizeType length,unsigned char *pixels)
1096 %
1097 %  A description of each parameter follows:
1098 %
1099 %    o distribute_cache_info: the distributed cache info.
1100 %
1101 %    o image: the image.
1102 %
1103 %    o region: read the pixels from this region of the image.
1104 %
1105 %    o length: write the pixels to this region of the image.
1106 %
1107 %    o pixels: read these pixels from the pixel cache.
1108 %
1109 */
1110 MagickPrivate MagickBooleanType ReadDistributePixelCachePixels(
1111   DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1112   const MagickSizeType length,unsigned char *pixels)
1113 {
1114   MagickBooleanType
1115     status;
1116
1117   register unsigned char
1118     *p;
1119
1120   ssize_t
1121     count;
1122
1123   unsigned char
1124     buffer[MaxTextExtent];
1125
1126   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1127   assert(distribute_cache_info->signature == MagickSignature);
1128   assert(region != (RectangleInfo *) NULL);
1129   assert(pixels != (unsigned char *) NULL);
1130   assert(length == ((size_t) length));
1131   p=buffer;
1132   *p++='r';  /* read */
1133   (void) memcpy(p,&distribute_cache_info->session_key,
1134     sizeof(distribute_cache_info->session_key));
1135   p+=sizeof(distribute_cache_info->session_key);
1136   (void) memcpy(p,&region->width,sizeof(region->width));
1137   p+=sizeof(region->width);
1138   (void) memcpy(p,&region->height,sizeof(region->height));
1139   p+=sizeof(region->height);
1140   (void) memcpy(p,&region->x,sizeof(region->x));
1141   p+=sizeof(region->x);
1142   (void) memcpy(p,&region->y,sizeof(region->y));
1143   p+=sizeof(region->y);
1144   (void) memcpy(p,&length,sizeof(length));
1145   p+=sizeof(length);
1146   count=write(distribute_cache_info->file,buffer,p-buffer);
1147   if (count != (ssize_t) (p-buffer))
1148     return(MagickFalse);
1149   count=read(distribute_cache_info->file,(unsigned char *) pixels,(size_t)
1150     length);
1151   if (count != (ssize_t) length)
1152     return(MagickFalse);
1153   count=read(distribute_cache_info->file,&status,sizeof(status));
1154   if (count != (ssize_t) sizeof(status))
1155     return(MagickFalse);
1156   return(status != 0 ? MagickTrue : MagickFalse);
1157 }
1158 \f
1159 /*
1160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1161 %                                                                             %
1162 %                                                                             %
1163 %                                                                             %
1164 +   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               %
1165 %                                                                             %
1166 %                                                                             %
1167 %                                                                             %
1168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1169 %
1170 %  RelinquishDistributePixelCache() frees resources acquired with
1171 %  OpenDistributePixelCache().
1172 %
1173 %  The format of the RelinquishDistributePixelCache method is:
1174 %
1175 %      MagickBooleanType RelinquishDistributePixelCache(
1176 %        DistributeCacheInfo *distribute_cache_info)
1177 %
1178 %  A description of each parameter follows:
1179 %
1180 %    o distribute_cache_info: the distributed cache info.
1181 %
1182 */
1183 MagickPrivate MagickBooleanType RelinquishDistributePixelCache(
1184   DistributeCacheInfo *distribute_cache_info)
1185 {
1186   register unsigned char
1187     *p;
1188
1189   ssize_t
1190     count;
1191
1192   unsigned char
1193     buffer[MaxTextExtent];
1194
1195   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1196   assert(distribute_cache_info->signature == MagickSignature);
1197   p=buffer;
1198   *p++='d';  /* delete */
1199   (void) memcpy(p,&distribute_cache_info->session_key,
1200     sizeof(distribute_cache_info->session_key));
1201   p+=sizeof(distribute_cache_info->session_key);
1202   count=write(distribute_cache_info->file,buffer,p-buffer);
1203   if (count != (ssize_t) (p-buffer))
1204     return(MagickFalse);
1205   return(MagickTrue);
1206 }
1207 \f
1208 /*
1209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1210 %                                                                             %
1211 %                                                                             %
1212 %                                                                             %
1213 +   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   %
1214 %                                                                             %
1215 %                                                                             %
1216 %                                                                             %
1217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1218 %
1219 %  WriteDistributePixelCacheMetacontents() writes image metacontent to the
1220 %  specified region of the distributed pixel cache.
1221 %
1222 %
1223 %  The format of the WriteDistributePixelCacheMetacontents method is:
1224 %
1225 %      MagickBooleanType *WriteDistributePixelCacheMetacontents(
1226 %        DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1227 %        const MagickSizeType length,const unsigned char *metacontent)
1228 %
1229 %  A description of each parameter follows:
1230 %
1231 %    o distribute_cache_info: the distributed cache info.
1232 %
1233 %    o image: the image.
1234 %
1235 %    o region: write the metacontent to this region of the image.
1236 %
1237 %    o length: write the metacontent to this region of the image.
1238 %
1239 %    o metacontent: write these metacontent to the pixel cache.
1240 %
1241 */
1242 MagickPrivate MagickBooleanType WriteDistributePixelCacheMetacontent(
1243   DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1244   const MagickSizeType length,const unsigned char *metacontent)
1245 {
1246   MagickBooleanType
1247     status;
1248
1249   register unsigned char
1250     *p;
1251
1252   ssize_t
1253     count;
1254
1255   unsigned char
1256     buffer[MaxTextExtent];
1257
1258   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1259   assert(distribute_cache_info->signature == MagickSignature);
1260   assert(region != (RectangleInfo *) NULL);
1261   assert(metacontent != (unsigned char *) NULL);
1262   assert(length == ((size_t) length));
1263   p=buffer;
1264   *p++='M';  /* update */
1265   (void) memcpy(p,&distribute_cache_info->session_key,
1266     sizeof(distribute_cache_info->session_key));
1267   p+=sizeof(distribute_cache_info->session_key);
1268   (void) memcpy(p,&region->width,sizeof(region->width));
1269   p+=sizeof(region->width);
1270   (void) memcpy(p,&region->height,sizeof(region->height));
1271   p+=sizeof(region->height);
1272   (void) memcpy(p,&region->x,sizeof(region->x));
1273   p+=sizeof(region->x);
1274   (void) memcpy(p,&region->y,sizeof(region->y));
1275   p+=sizeof(region->y);
1276   (void) memcpy(p,&length,sizeof(length));
1277   p+=sizeof(length);
1278   count=write(distribute_cache_info->file,buffer,p-buffer);
1279   if (count != (ssize_t) (p-buffer))
1280     return(MagickFalse);
1281   count=write(distribute_cache_info->file,(unsigned char *) metacontent,(size_t)
1282     length);
1283   if (count != (ssize_t) length)
1284     return(MagickFalse);
1285   count=read(distribute_cache_info->file,&status,sizeof(status));
1286   if (count != (ssize_t) sizeof(status))
1287     return(MagickFalse);
1288   return(status != 0 ? MagickTrue : MagickFalse);
1289 }
1290 \f
1291 /*
1292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1293 %                                                                             %
1294 %                                                                             %
1295 %                                                                             %
1296 +   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             %
1297 %                                                                             %
1298 %                                                                             %
1299 %                                                                             %
1300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 %
1302 %  WriteDistributePixelCachePixels() writes image pixels to the specified
1303 %  region of the distributed pixel cache.
1304 %
1305 %
1306 %  The format of the WriteDistributePixelCachePixels method is:
1307 %
1308 %      MagickBooleanType *WriteDistributePixelCachePixels(
1309 %        DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1310 %        const MagickSizeType length,const unsigned char *pixels)
1311 %
1312 %  A description of each parameter follows:
1313 %
1314 %    o distribute_cache_info: the distributed cache info.
1315 %
1316 %    o image: the image.
1317 %
1318 %    o region: write the pixels to this region of the image.
1319 %
1320 %    o length: write the pixels to this region of the image.
1321 %
1322 %    o pixels: write these pixels to the pixel cache.
1323 %
1324 */
1325 MagickPrivate MagickBooleanType WriteDistributePixelCachePixels(
1326   DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1327   const MagickSizeType length,const unsigned char *pixels)
1328 {
1329   MagickBooleanType
1330     status;
1331
1332   register unsigned char
1333     *p;
1334
1335   ssize_t
1336     count;
1337
1338   unsigned char
1339     buffer[MaxTextExtent];
1340
1341   assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1342   assert(distribute_cache_info->signature == MagickSignature);
1343   assert(region != (RectangleInfo *) NULL);
1344   assert(pixels != (const unsigned char *) NULL);
1345   assert(length == ((size_t) length));
1346   p=buffer;
1347   *p++='u';  /* update */
1348   (void) memcpy(p,&distribute_cache_info->session_key,
1349     sizeof(distribute_cache_info->session_key));
1350   p+=sizeof(distribute_cache_info->session_key);
1351   (void) memcpy(p,&region->width,sizeof(region->width));
1352   p+=sizeof(region->width);
1353   (void) memcpy(p,&region->height,sizeof(region->height));
1354   p+=sizeof(region->height);
1355   (void) memcpy(p,&region->x,sizeof(region->x));
1356   p+=sizeof(region->x);
1357   (void) memcpy(p,&region->y,sizeof(region->y));
1358   p+=sizeof(region->y);
1359   (void) memcpy(p,&length,sizeof(length));
1360   p+=sizeof(length);
1361   count=write(distribute_cache_info->file,buffer,p-buffer);
1362   if (count != (ssize_t) (p-buffer))
1363     return(MagickFalse);
1364   count=write(distribute_cache_info->file,(unsigned char *) pixels,(size_t)
1365     length);
1366   if (count != (ssize_t) length)
1367     return(MagickFalse);
1368   count=read(distribute_cache_info->file,&status,sizeof(status));
1369   if (count != (ssize_t) sizeof(status))
1370     return(MagickFalse);
1371   return(status != 0 ? MagickTrue : MagickFalse);
1372 }