! : changed
+ : added
+May 10, 2011
+! [kaori] Modification of opj_dec_server to be portable to windows
+
May 9, 2011
* [kaori] Removal of c99 from the compile option (to be compatible to win platform) and bool definition in libopenjpip/bool.h
LIBFNAME = $(LIBDIR)/libopenjpip_local.a
CFLAGS = -O3 -Wall -I$(LIBDIR)
LDFLAGS = -L$(LIBDIR) -lm -lopenjpeg -lopenjpip_local
-
+#-lws2_32
ALL = opj_dec_server
/*
- * $Id: imgsock_manager.c 53 2011-05-09 16:55:39Z kaori $
+ * $Id: imgsock_manager.c 54 2011-05-10 13:22:47Z kaori $
*
* Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
* Copyright (c) 2002-2011, Professor Benoit Macq
#define strcasecmp _stricmp
#else
#include <strings.h>
-#endif
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
-#include <unistd.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <netdb.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
#include "imgsock_manager.h"
#define BUF_LEN 256
-int open_listeningsocket()
+SOCKET open_listeningsocket()
{
- int listening_socket;
+ SOCKET listening_socket;
struct sockaddr_in sin;
int sock_optval = 1;
int port = 5000;
exit(1);
}
+ memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if ( bind(listening_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0 ){
perror("bind");
+ closesocket(listening_socket);
exit(1);
}
if( listen(listening_socket, SOMAXCONN) == -1){
perror("listen");
+ closesocket(listening_socket);
exit(1);
}
printf("port %d is listened\n", port);
return listening_socket;
}
-msgtype_t identify_clientmsg( int connected_socket)
+msgtype_t identify_clientmsg( SOCKET connected_socket)
{
- int read_size;
+ int receive_size;
char buf[BUF_LEN];
char *magicid[] = { "JPT-stream", "PNM request", "XML request", "CID request", "CID destroy", "JP2 save", "QUIT"};
int i;
- read_size = read_line( connected_socket, buf);
+ receive_size = receive_line( connected_socket, buf);
- if( read_size == 0){
- fprintf( stderr, "Error to read the header of client message\n");
- return ERROR;
+ if( receive_size == 0){
+ fprintf( stderr, "Error to receive the header of client message\n");
+ return MSGERROR;
}
for( i=0; i<NUM_OF_MSGTYPES; i++){
}
fprintf( stderr, "Cannot identify client message type\n");
- return ERROR;
+ return MSGERROR;
}
-Byte_t * receive_JPTstream( int connected_socket, char *target, char *cid, int *streamlen)
+Byte_t * receive_JPTstream( SOCKET connected_socket, char *target, char *cid, int *streamlen)
{
Byte_t *jptstream=NULL, *ptr;
char buf[BUF_LEN], versionstring[] = "version 1.0";
target[0] = 0;
cid[0] = 0;
- if((linelen = read_line( connected_socket, buf)) == 0)
+ if((linelen = receive_line( connected_socket, buf)) == 0)
return NULL;
if( strncmp( versionstring, buf, strlen(versionstring))!=0){
fprintf( stderr, "Wrong format\n");
return NULL;
}
- if((linelen = read_line( connected_socket, buf)) == 0)
+ if((linelen = receive_line( connected_socket, buf)) == 0)
return NULL;
if( strstr( buf, "jp2")){
// register cid option
strcpy( target, buf);
- if((linelen = read_line( connected_socket, buf)) == 0)
+ if((linelen = receive_line( connected_socket, buf)) == 0)
return NULL;
strcpy( cid, buf);
- if((linelen = read_line( connected_socket, buf)) == 0)
+ if((linelen = receive_line( connected_socket, buf)) == 0)
return NULL;
}
*streamlen = atoi( buf);
- fprintf( stderr, "Reading Data length: %d\n", *streamlen);
+ fprintf( stderr, "Receiveing Data length: %d\n", *streamlen);
jptstream = (unsigned char *)malloc( (*streamlen));
ptr = jptstream;
remlen = (*streamlen);
while( remlen > 0){
- redlen = read( connected_socket, ptr, remlen);
+ redlen = recv( connected_socket, ptr, remlen, 0);
if( redlen == -1){
- fprintf( stderr, "read jptstream error\n");
+ fprintf( stderr, "receive jptstream error\n");
break;
}
remlen -= redlen;
return jptstream;
}
-void send_stream( int connected_socket, void *stream, int length);
+void send_stream( SOCKET connected_socket, void *stream, int length);
-void send_XMLstream( int connected_socket, Byte_t *xmlstream, int length)
+void send_XMLstream( SOCKET connected_socket, Byte_t *xmlstream, int length)
{
Byte_t header[5];
send_stream( connected_socket, xmlstream, length);
}
-void send_CIDstream( int connected_socket, char *cid, int cidlen)
+void send_CIDstream( SOCKET connected_socket, char *cid, int cidlen)
{
Byte_t header[4];
send_stream( connected_socket, cid, cidlen);
}
-void send_PNMstream( int connected_socket, Byte_t *pnmstream, unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval)
+void send_PNMstream( SOCKET connected_socket, Byte_t *pnmstream, unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval)
{
int pnmlen = 0;
Byte_t header[7];
send_stream( connected_socket, pnmstream, pnmlen);
}
-void send_stream( int connected_socket, void *stream, int length)
+void send_stream( SOCKET connected_socket, void *stream, int length)
{
void *ptr = stream;
int remlen = length;
while( remlen > 0){
- int sentlen = write( connected_socket, ptr, remlen);
+ int sentlen = send( connected_socket, ptr, remlen, 0);
if( sentlen == -1){
fprintf( stderr, "sending stream error\n");
break;
}
}
-int read_line(int socket, char *p)
+int receive_line(SOCKET connected_socket, char *p)
{
int len = 0;
while (1){
int ret;
- ret = read(socket, p, 1);
+ ret = recv( connected_socket, p, 1, 0);
if ( ret == -1 ){
- perror("read");
+ perror("receive");
exit(1);
} else if ( ret == 0 ){
break;
*p = '\0';
if( len == 0)
- fprintf( stderr, "Header read error\n");
+ fprintf( stderr, "Header receive error\n");
return len;
}
-void response_signal( int connected_socket, bool succeed)
+void response_signal( SOCKET connected_socket, bool succeed)
{
Byte_t code;
else
code = 0;
- if( write( connected_socket, &code, 1) != 1)
+ if( send( connected_socket, &code, 1, 0) != 1)
fprintf( stderr, "Response signalling error\n");
}
/*
- * $Id: imgsock_manager.h 53 2011-05-09 16:55:39Z kaori $
+ * $Id: imgsock_manager.h 54 2011-05-10 13:22:47Z kaori $
*
* Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
* Copyright (c) 2002-2011, Professor Benoit Macq
#include "bool.h"
#include "byte_manager.h"
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+typedef int SOCKET;
+#define closesocket close
+#endif //_WIN32
+
/**
* open listening socket
*
- * @return file descriptor for the new socket
+ * @return new socket
*/
-int open_listeningsocket();
+SOCKET open_listeningsocket();
#define NUM_OF_MSGTYPES 7
-typedef enum eMSGTYPE{ JPTSTREAM, PNMREQ, XMLREQ, CIDREQ, CIDDST, JP2SAVE, QUIT, ERROR} msgtype_t;
-
+typedef enum eMSGTYPE{ JPTSTREAM, PNMREQ, XMLREQ, CIDREQ, CIDDST, JP2SAVE, QUIT, MSGERROR} msgtype_t;
/**
* indeitify client message type
* @param [in] connected_socket file descriptor of the connected socket
* @return message type
*/
-msgtype_t identify_clientmsg( int connected_socket);
+msgtype_t identify_clientmsg( SOCKET connected_socket);
/**
* receive JPT-stream from client
* @param [out] streamlen length of the received codestream
* @return codestream
*/
-Byte_t * receive_JPTstream( int connected_socket, char *target, char *cid, int *streamlen);
+Byte_t * receive_JPTstream( SOCKET connected_socket, char *target, char *cid, int *streamlen);
/**
* send PGM/PPM image stream to the client
* @param [in] numofcomp number of components of the image
* @param [in] maxval maximum value of the image (only 255 supported)
*/
-void send_PNMstream( int connected_socket, Byte_t *pnmstream, unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval);
+void send_PNMstream( SOCKET connected_socket, Byte_t *pnmstream, unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval);
/**
* send XML data stream to the client
* @param [in] xmlstream xml data stream
* @param [in] length legnth of the xml data stream
*/
-void send_XMLstream( int connected_socket, Byte_t *xmlstream, int length);
+void send_XMLstream( SOCKET connected_socket, Byte_t *xmlstream, int length);
/**
* send CID data stream to the client
* @param [in] cid cid string
* @param [in] cidlen legnth of the cid string
*/
-void send_CIDstream( int connected_socket, char *cid, int cidlen);
+void send_CIDstream( SOCKET connected_socket, char *cid, int cidlen);
/**
* send response signal to the client
* @param [in] connected_socket file descriptor of the connected socket
* @param [in] succeed whether if the requested process succeeded
*/
-void response_signal( int connected_socket, bool succeed);
+void response_signal( SOCKET connected_socket, bool succeed);
/**
- * read a string line (ending with '\n') from client
+ * receive a string line (ending with '\n') from client
*
* @param [in] connected_socket file descriptor of the connected socket
* @param [out] buf string to be stored
* @return red size
*/
-int read_line(int connected_socket, char *buf);
+int receive_line(SOCKET connected_socket, char *buf);
#endif /* !IMGSOCK_MANAGER_H_ */
/*! \file
/*
- * $Id: opj_dec_server.c 46 2011-02-17 14:50:55Z kaori $
+ * $Id: opj_dec_server.c 54 2011-05-10 13:22:47Z kaori $
*
* Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
* Copyright (c) 2002-2011, Professor Benoit Macq
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
#include <unistd.h>
#include "byte_manager.h"
#include "msgqueue_manager.h"
#include "jptstream_manager.h"
#include "cache_manager.h"
+#ifdef _WIN32
+WSADATA initialisation_win32;
+#else
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#endif //_WIN32
+
//! maximum length of target name
#define MAX_LENOFTARGET 128
* @param[in,out] jptlen address of jptstream length
* @param[in,out] msgqueue message queue pointer
*/
-void handle_JPTstreamMSG( int connected_socket, cachelist_param_t *cachelist, Byte_t **jptstream, int *jptlen, msgqueue_param_t *msgqueue);
+void handle_JPTstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist, Byte_t **jptstream, int *jptlen, msgqueue_param_t *msgqueue);
/**
* handle PNM request message
* @param[in] msgqueue message queue pointer
* @param[in] cachelist cache list pointer
*/
-void handle_PNMreqMSG( int connected_socket, Byte_t *jptstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist);
+void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jptstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist);
/**
* handle XML request message
* @param[in] jptstream address of caching jptstream pointer
* @param[in] cachelist cache list pointer
*/
-void handle_XMLreqMSG( int connected_socket, Byte_t *jptstream, cachelist_param_t *cachelist);
+void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jptstream, cachelist_param_t *cachelist);
/**
* handle ChannelID request message
* @param[in] connected_socket socket descriptor
* @param[in] cachelist cache list pointer
*/
-void handle_CIDreqMSG( int connected_socket, cachelist_param_t *cachelist);
+void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist);
/**
* handle distroy ChannelID message
* @param[in] connected_socket socket descriptor
* @param[in,out] cachelist cache list pointer
*/
-void handle_dstCIDreqMSG( int connected_socket, cachelist_param_t *cachelist);
+void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist);
/**
* handle saving JP2 file request message
* @param[in] msgqueue message queue pointer
* @param[in] jptstream address of caching jptstream pointer
*/
-void handle_JP2saveMSG( int connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jptstream);
+void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jptstream);
int main(int argc, char *argv[]){
- int connected_socket;
+ SOCKET connected_socket;
struct sockaddr_in peer_sin;
Byte_t *jptstream = NULL;
int jptlen = 0;
msgqueue_param_t *msgqueue = gene_msgqueue( true, NULL);
bool quit = false;
+
+#ifdef _WIN32
+ int erreur = WSAStartup(MAKEWORD(2,2),&initialisation_win32);
+ if( erreur!=0)
+ fprintf( stderr, "Erreur initialisation Winsock error : %d %d\n",erreur,WSAGetLastError());
+ else
+ printf( "Initialisation Winsock\n");
+#endif //_WIN32
int listening_socket = open_listeningsocket();
- socklen_t addrlen = sizeof(peer_sin);
+
+ int addrlen = sizeof(peer_sin);
cachelist_param_t *cachelist = gene_cachelist();
case QUIT:
quit = true;
break;
- case ERROR:
+ case MSGERROR:
break;
}
printf("cut the connection. listening to port\n");
- if( close(connected_socket) == -1 ){
+ if( closesocket(connected_socket) != 0){
perror("close");
return -1;
}
if( quit)
break;
}
- if( close(listening_socket) == -1 ){
+ if( closesocket(listening_socket) != 0){
perror("close");
return -1;
}
save_codestream( jptstream, jptlen, "jpt");
free( jptstream);
+#ifdef _WIN32
+ if( WSACleanup() != 0){
+ printf("\nError in WSACleanup : %d %d",erreur,WSAGetLastError());
+ }else{
+ printf("\nWSACleanup OK\n");
+ }
+#endif
+
return 0;
}
-void handle_JPTstreamMSG( int connected_socket, cachelist_param_t *cachelist,
+void handle_JPTstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist,
Byte_t **jptstream, int *jptlen, msgqueue_param_t *msgqueue)
{
Byte_t *newjptstream;
response_signal( connected_socket, true);
}
-void handle_PNMreqMSG( int connected_socket, Byte_t *jptstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
+void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jptstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
{
Byte_t *pnmstream;
ihdrbox_param_t *ihdrbox;
cache_param_t *cache;
int fw, fh;
- read_line( connected_socket, cid);
+ receive_line( connected_socket, cid);
if(!(cache = search_cacheBycid( cid, cachelist)))
return;
- read_line( connected_socket, tmp);
+ receive_line( connected_socket, tmp);
fw = atoi( tmp);
- read_line( connected_socket, tmp);
+ receive_line( connected_socket, tmp);
fh = atoi( tmp);
pnmstream = jpt_to_pnm( jptstream, msgqueue, cache->csn, fw, fh, &cache->ihdrbox);
free( pnmstream);
}
-void handle_XMLreqMSG( int connected_socket, Byte_t *jptstream, cachelist_param_t *cachelist)
+void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jptstream, cachelist_param_t *cachelist)
{
char cid[MAX_LENOFCID];
cache_param_t *cache;
- read_line( connected_socket, cid);
+ receive_line( connected_socket, cid);
if(!(cache = search_cacheBycid( cid, cachelist)))
return;
free( xmlstream);
}
-void handle_CIDreqMSG( int connected_socket, cachelist_param_t *cachelist)
+void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
{
char target[MAX_LENOFTARGET], *cid = NULL;
cache_param_t *cache;
int cidlen = 0;
- read_line( connected_socket, target);
+ receive_line( connected_socket, target);
cache = search_cache( target, cachelist);
if( cache){
send_CIDstream( connected_socket, cid, cidlen);
}
-void handle_dstCIDreqMSG( int connected_socket, cachelist_param_t *cachelist)
+void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
{
char cid[MAX_LENOFCID];
- read_line( connected_socket, cid);
+ receive_line( connected_socket, cid);
remove_cachecid( cid, cachelist);
response_signal( connected_socket, true);
}
-void handle_JP2saveMSG( int connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jptstream)
+void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jptstream)
{
char cid[MAX_LENOFCID];
cache_param_t *cache;
Byte_t *jp2stream;
Byte8_t jp2len;
- read_line( connected_socket, cid);
+ receive_line( connected_socket, cid);
if(!(cache = search_cacheBycid( cid, cachelist)))
return;