From 42e5077688dc8bea7ab848113b3243737fd16071 Mon Sep 17 00:00:00 2001 From: Kaori Hagihara Date: Tue, 10 May 2011 14:42:00 +0000 Subject: [PATCH] Modification of opj_dec_server to be portable to windows --- applications/jpip/CHANGES | 3 + .../jpip/opj_client/opj_dec_server/Makefile | 2 +- .../opj_dec_server/imgsock_manager.c | 77 +++++++++--------- .../opj_dec_server/imgsock_manager.h | 32 ++++---- .../opj_dec_server/opj_dec_server.c | 78 ++++++++++++------- 5 files changed, 113 insertions(+), 79 deletions(-) diff --git a/applications/jpip/CHANGES b/applications/jpip/CHANGES index 3c10897f..8744dcf8 100644 --- a/applications/jpip/CHANGES +++ b/applications/jpip/CHANGES @@ -5,6 +5,9 @@ What's New for OpenJPIP ! : 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 diff --git a/applications/jpip/opj_client/opj_dec_server/Makefile b/applications/jpip/opj_client/opj_dec_server/Makefile index ba992545..0c077283 100644 --- a/applications/jpip/opj_client/opj_dec_server/Makefile +++ b/applications/jpip/opj_client/opj_dec_server/Makefile @@ -2,7 +2,7 @@ LIBDIR = ../../libopenjpip LIBFNAME = $(LIBDIR)/libopenjpip_local.a CFLAGS = -O3 -Wall -I$(LIBDIR) LDFLAGS = -L$(LIBDIR) -lm -lopenjpeg -lopenjpip_local - +#-lws2_32 ALL = opj_dec_server diff --git a/applications/jpip/opj_client/opj_dec_server/imgsock_manager.c b/applications/jpip/opj_client/opj_dec_server/imgsock_manager.c index e92b8745..a7471a11 100644 --- a/applications/jpip/opj_client/opj_dec_server/imgsock_manager.c +++ b/applications/jpip/opj_client/opj_dec_server/imgsock_manager.c @@ -1,5 +1,5 @@ /* - * $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 @@ -33,26 +33,26 @@ #define strcasecmp _stricmp #else #include +#include +#include +#include +#include +#include +#include +#include #endif #include #include #include -#include -#include -#include -#include #include -#include -#include -#include #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; @@ -69,17 +69,20 @@ int open_listeningsocket() 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); @@ -87,18 +90,18 @@ int open_listeningsocket() 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 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; @@ -164,9 +167,9 @@ Byte_t * receive_JPTstream( int connected_socket, char *target, char *cid, int * 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]; @@ -180,7 +183,7 @@ void send_XMLstream( int connected_socket, Byte_t *xmlstream, int length) 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]; @@ -193,7 +196,7 @@ void send_CIDstream( int connected_socket, char *cid, int cidlen) 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]; @@ -212,13 +215,13 @@ void send_PNMstream( int connected_socket, Byte_t *pnmstream, unsigned int width 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; @@ -228,14 +231,14 @@ void send_stream( int connected_socket, void *stream, int length) } } -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; @@ -248,12 +251,12 @@ int read_line(int socket, char *p) *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; @@ -262,6 +265,6 @@ void response_signal( int connected_socket, bool succeed) else code = 0; - if( write( connected_socket, &code, 1) != 1) + if( send( connected_socket, &code, 1, 0) != 1) fprintf( stderr, "Response signalling error\n"); } diff --git a/applications/jpip/opj_client/opj_dec_server/imgsock_manager.h b/applications/jpip/opj_client/opj_dec_server/imgsock_manager.h index a4cb0d1f..6032392f 100644 --- a/applications/jpip/opj_client/opj_dec_server/imgsock_manager.h +++ b/applications/jpip/opj_client/opj_dec_server/imgsock_manager.h @@ -1,5 +1,5 @@ /* - * $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 @@ -34,16 +34,22 @@ #include "bool.h" #include "byte_manager.h" +#ifdef _WIN32 +#include +#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 @@ -51,7 +57,7 @@ typedef enum eMSGTYPE{ JPTSTREAM, PNMREQ, XMLREQ, CIDREQ, CIDDST, JP2SAVE, QUIT, * @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 @@ -62,7 +68,7 @@ msgtype_t identify_clientmsg( int connected_socket); * @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 @@ -74,7 +80,7 @@ Byte_t * receive_JPTstream( int connected_socket, char *target, char *cid, int * * @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 @@ -83,7 +89,7 @@ void send_PNMstream( int connected_socket, Byte_t *pnmstream, unsigned int width * @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 @@ -92,7 +98,7 @@ void send_XMLstream( int connected_socket, Byte_t *xmlstream, int length); * @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 @@ -100,17 +106,17 @@ void send_CIDstream( int connected_socket, char *cid, int cidlen); * @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 diff --git a/applications/jpip/opj_client/opj_dec_server/opj_dec_server.c b/applications/jpip/opj_client/opj_dec_server/opj_dec_server.c index b00e8ccc..3d95ceec 100644 --- a/applications/jpip/opj_client/opj_dec_server/opj_dec_server.c +++ b/applications/jpip/opj_client/opj_dec_server/opj_dec_server.c @@ -1,5 +1,5 @@ /* - * $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 @@ -47,9 +47,6 @@ #include #include #include -#include -#include -#include #include #include "byte_manager.h" #include "msgqueue_manager.h" @@ -58,6 +55,14 @@ #include "jptstream_manager.h" #include "cache_manager.h" +#ifdef _WIN32 +WSADATA initialisation_win32; +#else +#include +#include +#include +#endif //_WIN32 + //! maximum length of target name #define MAX_LENOFTARGET 128 @@ -73,7 +78,7 @@ * @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 @@ -83,7 +88,7 @@ void handle_JPTstreamMSG( int connected_socket, cachelist_param_t *cachelist, By * @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 @@ -92,7 +97,7 @@ void handle_PNMreqMSG( int connected_socket, Byte_t *jptstream, msgqueue_param_t * @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 @@ -100,7 +105,7 @@ void handle_XMLreqMSG( int connected_socket, Byte_t *jptstream, cachelist_param_ * @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 @@ -108,7 +113,7 @@ void handle_CIDreqMSG( int connected_socket, cachelist_param_t *cachelist); * @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 @@ -118,19 +123,28 @@ void handle_dstCIDreqMSG( int connected_socket, cachelist_param_t *cachelist); * @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(); @@ -165,19 +179,19 @@ int main(int argc, char *argv[]){ 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; } @@ -190,10 +204,18 @@ int main(int argc, char *argv[]){ 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; @@ -231,7 +253,7 @@ void handle_JPTstreamMSG( int connected_socket, cachelist_param_t *cachelist, 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; @@ -239,14 +261,14 @@ void handle_PNMreqMSG( int connected_socket, Byte_t *jptstream, msgqueue_param_t 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); @@ -256,12 +278,12 @@ void handle_PNMreqMSG( int connected_socket, Byte_t *jptstream, msgqueue_param_t 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; @@ -272,13 +294,13 @@ void handle_XMLreqMSG( int connected_socket, Byte_t *jptstream, cachelist_param_ 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){ @@ -290,23 +312,23 @@ void handle_CIDreqMSG( int connected_socket, cachelist_param_t *cachelist) 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;