2 * $Id: imgsock_manager.c 54 2011-05-10 13:22:47Z kaori $
4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #include "imgsock_manager.h"
36 #define strncasecmp _strnicmp
39 msgtype_t identify_clientmsg( SOCKET connected_socket)
41 OPJ_SIZE_T receive_size;
43 static const char *magicid[] = { "JPIP-stream", "PNM request", "XML request",
44 "TID request", "CID request", "CID destroy", "SIZ request", "JP2 save",
48 receive_size = receive_line( connected_socket, buf);
50 if( receive_size == 0){
51 fprintf( stderr, "Error to receive the header of client message\n");
55 for( i=0; i<NUM_OF_MSGTYPES; i++){
56 if( strncasecmp( magicid[i], buf, strlen(magicid[i])) == 0){
57 fprintf( stderr, "%s\n", magicid[i]);
62 fprintf( stderr, "Cannot identify client message type %s\n", buf);
66 Byte_t * receive_JPIPstream( SOCKET connected_socket, char **target, char **tid, char **cid, OPJ_SIZE_T *streamlen)
69 const char versionstring[] = "version 1.2";
71 OPJ_SIZE_T linelen, datalen;
74 *target = *cid = *tid = NULL;
76 if((linelen = receive_line( connected_socket, buf)) == 0)
78 if( strncmp( versionstring, buf, strlen(versionstring))!=0){
79 fprintf( stderr, "Wrong format\n");
83 if((linelen = receive_line( connected_socket, buf)) == 0)
86 if( strstr( buf, "jp2")){
87 /* register cid option*/
88 *target = strdup( buf);
90 if((linelen = receive_line( connected_socket, buf)) == 0)
92 if( strcmp( buf, "0") != 0)
95 if((linelen = receive_line( connected_socket, buf)) == 0)
97 if( strcmp( buf, "0") != 0)
100 if((linelen = receive_line( connected_socket, buf)) == 0)
104 idatalen = atoi( buf);
107 fprintf( stderr, "Receive Data: %d Bytes\n", idatalen);
110 datalen = (OPJ_SIZE_T)idatalen;
111 fprintf( stdout, "Receive Data: %lu Bytes\n", datalen);
113 jpipstream = receive_stream( connected_socket, datalen);
116 if( jpipstream[datalen-3] == 0x00 && ( jpipstream[datalen-2] == 0x01 || jpipstream[datalen-2] == 0x02))
117 *streamlen = datalen -3;
119 *streamlen = datalen;
124 void send_XMLstream( SOCKET connected_socket, Byte_t *xmlstream, OPJ_SIZE_T length)
131 header[3] = (Byte_t)((length >> 8) & 0xff);
132 header[4] = (Byte_t)(length & 0xff);
134 send_stream( connected_socket, header, 5);
135 send_stream( connected_socket, xmlstream, length);
138 void send_IDstream( SOCKET connected_socket, const char *id, OPJ_SIZE_T idlen, const char *label);
140 void send_CIDstream( SOCKET connected_socket, const char *cid, OPJ_SIZE_T cidlen)
142 send_IDstream( connected_socket, cid, cidlen, "CID");
145 void send_TIDstream( SOCKET connected_socket, const char *tid, OPJ_SIZE_T tidlen)
147 send_IDstream( connected_socket, tid, tidlen, "TID");
150 void send_IDstream( SOCKET connected_socket, const char *id, OPJ_SIZE_T idlen, const char *label)
154 header[0] = label[0];
155 header[1] = label[1];
156 header[2] = label[2];
157 header[3] = (char)(idlen & 0xff);
159 send_stream( connected_socket, header, 4);
160 send_stream( connected_socket, id, idlen);
163 void send_PNMstream( SOCKET connected_socket, Byte_t *pnmstream, unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval)
165 OPJ_SIZE_T pnmlen = 0;
168 pnmlen = width*height*numofcomp;
171 header[1] = numofcomp==3 ? 6:5;
172 header[2] = (width >> 8) & 0xff;
173 header[3] = width & 0xff;
174 header[4] = (height >> 8) & 0xff;
175 header[5] = height & 0xff;
178 send_stream( connected_socket, header, 7);
179 send_stream( connected_socket, pnmstream, pnmlen);
182 void send_SIZstream( SOCKET connected_socket, unsigned int width, unsigned int height)
189 responce[3] = (width >> 16) & 0xff;
190 responce[4] = (width >> 8) & 0xff;
191 responce[5] = width & 0xff;
192 responce[6] = (height >> 16) & 0xff;
193 responce[7] = (height >> 8) & 0xff;
194 responce[8] = height & 0xff;
196 send_stream( connected_socket, responce, 9);
199 void response_signal( SOCKET connected_socket, bool succeed)
208 send_stream( connected_socket, &code, 1);