Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

Net.c

Go to the documentation of this file.
00001 /*
00002  *    gui - [gega user interface] the flexible solution for user interface problems
00003  *    Copyright (C) 2002  Gergely Gati
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; version 2 of the License.
00008  *
00009  *    This program is distributed in the hope that it will be useful,
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *    GNU General Public License for more details.
00013  *
00014  *    You should have received a copy of the GNU General Public License
00015  *    along with this program; if not, write to the Free Software
00016  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *    Gergely Gati
00019  *      email:           g.gati@freemail.hu
00020  *      AIM screenname:  GatiGergely
00021  *      ICQ number:      93131690
00022  *
00023  */
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <zlib.h>
00027 
00028 #include "Gadget.h"
00029 #include "debug.h"
00030 #include "Memory.h"
00031 #include "Net.h"
00032 #include "NetCommon.h"
00033 #include "Window.h"
00034 #include "Module.h"
00035 #include "Common.h"
00036 
00037 
00038 struct net_File
00039 {
00040   u8 *data;
00041   int size;
00042   u32 realsize;
00043   u32 type;
00044 };
00045 
00046 
00047 struct net_Files
00048 {
00049   int file_count;
00050   int readed;
00051   u8 *next_data;
00052   u32 data_size;
00053   u8 *data;
00054 };
00055 
00056 
00057 struct net_FileCache
00058 {
00059   u8 *data;
00060   int size;
00061   char *name;
00062   u32 crc;
00063   int expire;
00064   int check_interval;
00065   int last_checked;
00066 };
00067 
00068 
00069 //Declare call_vector[]
00070 static u32 (*call_vector[NET_FUNC_DONE-NET_FUNCBASE+1])(tag *);
00071 
00072 
00073 
00074 static u32 net_GetServerId(void)
00075 {
00076   static u32 curr_id=1;
00077 
00078   debug_Begin();
00079 
00080   curr_id+=2;
00081 
00082   debug_End();
00083 
00084   return(curr_id);
00085 }
00086 
00087 
00088 void net_SerSendApp(u32 conn)
00089 {
00090   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00091   u32 m;
00092 
00093   debug_Begin();
00094 
00095   m=glw_htonl((u32)cuser->app);
00096   glw_SendMsg(conn,REQ_SAPP,net_GetServerId(),(u8 *)&m,4);
00097 
00098   debug_End();
00099 }
00100 
00101 
00102 int net_Server(u32 conn)
00103 {
00104   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00105   Node_t *node;
00106   struct net_FileCache *fcache;
00107 
00108   debug_Begin();
00109 
00110   if(NULL!=(cuser->waitlist=list_CreateList()))
00111   {
00112     if(NULL!=(cuser->file_cache=list_CreateList()))
00113     {
00114       gui_ServerMain(conn);
00115       while(NULL!=(node=list_RemoveNodeHead(cuser->file_cache)))
00116       {
00117         if(NULL!=(fcache=(struct net_FileCache *)list_GetNodeData(node)))
00118         {
00119           if(fcache->data!=NULL) mem_free(fcache->data);
00120           if(fcache->name!=NULL) mem_free(fcache->name);
00121           mem_free(fcache);
00122         }
00123         list_DeleteNode(node);
00124       }
00125       list_DeleteList(cuser->file_cache);
00126       cuser->file_cache=NULL;
00127     }
00128     net_ClearMsgList(conn);
00129   }
00130 
00131   debug_End();
00132 
00133   return(0);
00134 }
00135 
00136 
00137 /*
00138   kliens jelentkezese:
00139     ?GUI      - fix string (4 char)
00140     L/B       - little/bigendian (1 char)
00141     v1.0      - protokoll verzio (max 8 char)
00142     app-name  - app name (max 128 char)
00143   elvalaszto space-k 3 char
00144   max bejelentkezo uzenet-hossz: 144 char + zaro \0
00145   min hossz: 12 + appname (min 1 char) -> 13
00146  */
00147 #define MAX_INIT_LEN  256
00148 
00149 char *net_CheckClient(u32 conn, int *byteorder)
00150 {
00151   int ln;
00152   char buff[MAX_INIT_LEN+1];
00153   char *v,*n,*name=NULL;
00154   int vmajor=0,vminor=0;
00155 
00156   debug_Begin();
00157 
00158   if(12<(ln=glw_Recv(conn,buff,MAX_INIT_LEN)))
00159   {
00160     buff[ln]='\0';
00161     if(strncmp(buff,"?GUI",4)==0)
00162     {
00163       if(buff[5]=='L') *byteorder=0;
00164       else *byteorder=1;
00165       v=&buff[7];
00166       n=strchr(v,' ');
00167       if(n!=NULL)
00168       {
00169         *n++='\0';
00170         sscanf(v,"v%d.%d",&vmajor,&vminor);
00171         if(vmajor==1&&vminor==1)
00172         {
00173           if(NULL!=(name=common_strdup(n)))
00174           {
00175             glw_Send(conn,"OK L " ARCH,7);
00176           }
00177         }
00178         else debug_Warning("Incompatible client version V%d.%d\n",vmajor,vminor);
00179       }
00180     }
00181   }
00182   if(name==NULL) glw_Send(conn,"ER",2);
00183 
00184   debug_End();
00185 
00186   return(name);
00187 }
00188 
00189 
00190 int net_ServerWindowEventHandler(gui_App_t *app, Window_t *win, u32 gadget_id, int event, u32 event_data)
00191 {
00192   u8 msg[5*4];
00193   u8 *m=msg;
00194   u32 id;
00195 
00196   debug_Begin();
00197 
00198   m=net_MarshalPutLong(m,(u32)app);
00199   m=net_MarshalPutLong(m,(u32)win);
00200   m=net_MarshalPutLong(m,(u32)gadget_id);
00201   m=net_MarshalPutLong(m,(u32)event);
00202   m=net_MarshalPutLong(m,(u32)event_data);
00203   id=net_GetServerId();
00204   glw_SendMsg(window_GetConnection(win),REQ_SWINEVENT,id,msg,5*4);
00205 
00206   debug_End();
00207 
00208   return(0);
00209 }
00210 
00211 
00212 int net_SerCreateCheckList(tag *taglist, u8 *msg)
00213 {
00214   int ret=-1,i;
00215   tag *t;
00216   int check_count;
00217 
00218   debug_Begin();
00219 
00220   if(taglist!=NULL)
00221   {
00222     if(NULL!=(t=tag_FindTag(taglist,TAG_WIN_CHECKLIST)))
00223     {
00224       check_count=(int)t->Data;
00225       if(0L!=(t->Data=(u32)mem_malloc((check_count+1)*sizeof(gadget_Check_t))))
00226       {
00227         for(i=0;i<check_count;i++)
00228         {
00229           ((gadget_Check_t *)t->Data)[i].gadget_id=net_MarshalGetLong(&msg);
00230           ((gadget_Check_t *)t->Data)[i].class_name=net_MarshalGetString(&msg);
00231 //          ((gadget_Check_t *)t->Data)[i].class_id=net_MarshalGetLong(&msg);
00232           ((gadget_Check_t *)t->Data)[i].flags=net_MarshalGetLong(&msg);
00233         }
00234         ((gadget_Check_t *)t->Data)[check_count].gadget_id=GADGET_ILLEGAL_ID;
00235         ((gadget_Check_t *)t->Data)[check_count].class_name=NULL;
00236         ((gadget_Check_t *)t->Data)[check_count].flags=0L;
00237         ret=0;
00238       }
00239     }
00240   }
00241 
00242   debug_End();
00243 
00244   return(ret);
00245 }
00246 
00247 
00248 void net_SerDeleteCheckList(tag *taglist)
00249 {
00250   tag *t;
00251   gadget_Check_t *checklist;
00252   int i;
00253 
00254   debug_Begin();
00255 
00256   if(taglist!=NULL&&NULL!=(t=tag_FindTag(taglist,TAG_WIN_CHECKLIST)))
00257   {
00258     checklist=(gadget_Check_t *)t->Data;
00259     for(i=0;checklist[i].gadget_id!=GADGET_ILLEGAL_ID;i++)
00260     {
00261       if(NULL!=checklist[i].class_name) mem_free(checklist[i].class_name);
00262     }
00263     mem_free(checklist);
00264   }
00265 
00266   debug_End();
00267 }
00268 
00269 
00270 int net_SerProcessMessage(u32 conn, u32 userdata, struct glw_SockMsg *msg)
00271 {
00272   int ret=0,stored=0,fromwait=0;
00273   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00274   u8 *bd;
00275   tag *taglist;
00276   u32 retval,gadget_id,method;
00277   Window_t *window;
00278   struct net_File *retfile;
00279 
00280   debug_Begin();
00281 
00282   if(msg==NULL) fromwait=1;
00283   do
00284   {
00285     if(fromwait!=0) msg=net_MsgFromWaitList(conn);
00286     if(msg==NULL) break;
00287 //    printf("%s() socket message arrived. (w=0x%02x) type=0x%02x id=%ld length: %ld\n",__FUNCTION__,cuser->waitfor,msg->type,msg->id,msg->length);
00288     bd=msg->body;
00289     switch(msg->type)
00290     {
00291       case REQ_SAPP|FL_REPLY:
00292         debug_Message("REQ_SAPP|FL_REPLY");
00293       case REQ_SECHO|FL_REPLY:
00294       {
00295         debug_Message("REQ_SECHO|FL_REPLY");
00296         break;
00297       }
00298       case REQ_SLOADFILES|FL_REPLY:
00299       {
00300         debug_Message("REQ_SLOADFILES|FL_REPLY");
00301         if(cuser->waitfor==msg->type&&msg->id==cuser->userdata)
00302         {
00303           if(NULL!=(retfile=mem_malloc(sizeof(struct net_File))))
00304           {
00305             retfile->data=net_MarshalGetBinary(&bd,&retfile->size);
00306             cuser->waitfor=REQ_NONE;
00307             cuser->userdata=(u32)retfile;
00308             ret=1;
00309           }
00310         }
00311         else
00312         {
00313           debug_Error("loadfiles: wrong message order!");
00314         }
00315         break;
00316       }
00317       case REQ_SLOADFILE|FL_REPLY:
00318       {
00319         debug_Message("REQ_SLOADFILE|FL_REPLY");
00320         if(cuser->waitfor==msg->type&&msg->id==cuser->userdata)
00321         {
00322           cuser->userdata=0L;
00323           if(NULL!=(retfile=mem_malloc(sizeof(struct net_File))))
00324           {
00325             retfile->type=net_MarshalGetLong(&bd);
00326             retfile->realsize=net_MarshalGetLong(&bd);
00327             retfile->data=net_MarshalGetBinary(&bd,&retfile->size);
00328             debug_Message("file received...");
00329             cuser->waitfor=REQ_NONE;
00330             cuser->userdata=(u32)retfile;
00331             ret=1;
00332           }
00333         }
00334         else
00335         {
00336           debug_Error("loadfile: wrong message order!");
00337         }
00338         break;
00339       }
00340       case REQ_CWINSLEEP:
00341       {
00342         debug_Message("REQ_CWINSLEEP");
00343         window=(Window_t *)net_MarshalGetLong(&bd);
00344         if(window!=NULL) window->sleep++;
00345         break;
00346       }
00347       case REQ_CWINAWAKE:
00348       {
00349         debug_Message("REQ_CWINAWAKE");
00350         window=(Window_t *)net_MarshalGetLong(&bd);
00351         if(window!=NULL) window->sleep--;
00352         break;
00353       }
00354       case REQ_CECHO:
00355       {
00356         debug_Message("REQ_ECHO");
00357 //        printf("Sending echo\n");
00358         glw_SendMsg(conn,msg->type|FL_REPLY,msg->id,NULL,0);
00359         break;
00360       }
00361       case REQ_SWINEVENT|FL_REPLY:
00362       {
00363         debug_Message("REQ_SWINEVENT|FL_REPLY");
00365         ret=net_MarshalGetLong(&bd);
00366         if(ret!=0L)
00367         {
00368           u32 gadget_id,event;
00369 
00370           window=(Window_t *)net_MarshalGetLong(&bd);
00371           gadget_id=net_MarshalGetLong(&bd);
00372           event=net_MarshalGetLong(&bd);
00373 //          printf("Event discard requested: id=%ld  event=%ld\n",gadget_id,event);
00374           gadget_SetDiscardMask((Gadget_t *)gadget_SearchInWindow(TAG_GADGET_WINDOW,window,TAG_GADGET_GADGET_ID,gadget_id,TAG_DONE),event,0L);
00375         }
00376         //MSG ha a ret!=0 akkor van meg az uzenetben valami.
00377         //MSG gadget_id, event -> ezt felvenni a window deny listajara
00378         //MSG tehat ha a window ettol a gadgettol egy ilyen uzenetet akarna
00379         //MSG kuldeni legkozelebb, az blokkolodik.
00380 //MSG        printf("REPLY: msg id=%ld\n",msg->id);
00381         break;
00382       }
00383       case REQ_CCALLGAD:
00384       {
00385         debug_Message("REQ_CCALLGAD");
00386         if(cuser->waitfor==REQ_NONE)
00387         {
00388           window=(Window_t *)net_MarshalGetLong(&bd);
00389           gadget_id=net_MarshalGetLong(&bd);
00390           method=net_MarshalGetLong(&bd);
00391           taglist=net_MarshalGetTaglist(&bd);
00392           retval=gadget_MethodCall(TAG_GADGET_WINDOW,window,
00393                                              TAG_GADGET_GADGET_ID,gadget_id,
00394                                              TAG_GADGET_METHOD,method,
00395                                              TAG_MORE,taglist);
00396           net_MarshalFreeTaglist(taglist);
00397           retval=glw_htonl(retval);
00398           glw_SendMsg(conn,msg->type|FL_REPLY,msg->id,(u8 *)&retval,4);
00399         }
00400         else
00401         {
00402           stored=net_MsgToWaitList(conn,msg);
00403         }
00404         break;
00405       }
00406       case REQ_CCLOSEWIN:
00407       {
00408         debug_Message("REQ_CCLOSEWIN");
00409         window_CloseWindow(TAG_WIN_OBJECT,(Window_t *)net_MarshalGetLong(&bd),TAG_DONE);
00410         break;
00411       }
00412       case REQ_COPENWIN:
00413       {
00414         char *wname;
00415         u32 app,flags;
00416         u8 reply[4];
00417 
00418         debug_Message("REQ_COPENWIN");
00419         if(cuser->waitfor==REQ_NONE)
00420         {
00421           app=net_MarshalGetLong(&bd);
00422           wname=net_MarshalGetString(&bd);
00423           flags=net_MarshalGetLong(&bd);
00424           taglist=net_MarshalGetTaglist(&bd);
00425           net_SerCreateCheckList(taglist,bd);
00426           retval=(u32)window_OpenWindow(    TAG_WIN_APP,(gui_App_t *)app,
00427                                                                     TAG_WIN_NAME,wname,
00428                                                                     TAG_WIN_EVENT_HANDLER,net_ServerWindowEventHandler,
00429                                                                     TAG_WIN_FLAGS,flags,
00430                                                                     TAG_MORE,taglist);
00431           mem_free(wname);
00432           net_SerDeleteCheckList(taglist);
00433           net_MarshalFreeTaglist(taglist);
00434           net_MarshalPutLong(reply,retval);
00435           glw_SendMsg(conn,msg->type|FL_REPLY,msg->id,reply,sizeof(reply));
00436           window_WindowEvent(((Window_t *)retval),0L,CBK_WINDOW_OPENED,((Window_t *)retval)->userdata);
00437         }
00438         else
00439         {
00440           stored=net_MsgToWaitList(conn,msg);
00441         }
00442         break;
00443       }
00444       case REQ_CCLOSEAPP:
00445       {
00446         debug_Message("REQ_CCLOSEAPP");
00447         cuser->quit=1;
00448         ret=1;
00449         break;
00450       }
00451       case REQ_CNEWSKIN:
00452       {
00453         Window_t *win;
00454         char *skinname;
00455         // win, skinname
00456         win=(Window_t *)net_MarshalGetLong(&bd);
00457         skinname=net_MarshalGetString(&bd);
00458         window_ChangeSkin(TAG_WIN_OBJECT,win,TAG_WIN_SKINNAME,skinname,TAG_DONE);
00459         mem_free(skinname);
00460         break;
00461       }
00462       default:
00463       {
00464         debug_Error("Illegal message type");
00465       }
00466     }
00467     if(stored==0)
00468     {
00469       if(NULL!=msg&&NULL!=msg->body) mem_free(msg->body);
00470       if(NULL!=msg) mem_free(msg);
00471     }
00472     msg=NULL;
00473   }
00474   while(fromwait==0);
00475 
00476   debug_End();
00477 
00478   return(ret);
00479 }
00480 
00481 
00482 struct net_FileCache *net_FindFileCache(u32 conn, char *name)
00483 {
00484   struct net_FileCache *ret=NULL,*file;
00485   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00486   Node_t *node,*head;
00487   int i;
00488 
00489   debug_Begin();
00490 
00491   for(i=0,node=head=list_GetNodeHead(cuser->file_cache);node!=NULL;node=list_GetNodeNext(cuser->file_cache,node),i++)
00492   {
00493     file=(struct net_FileCache *)list_GetNodeData(node);
00494     if(strcmp(file->name,name)==0) break;
00495   }
00496   if(node!=NULL)
00497   {
00498     if(node!=head&&i>3)
00499     {
00500       list_RemoveNode(cuser->file_cache,node);
00501       list_InsertNodeHead(cuser->file_cache,node);
00502     }
00503     ret=file;
00504   }
00505 
00506   debug_End();
00507 
00508   return(ret);
00509 }
00510 
00511 
00512 void net_InsertFileCache(u32 conn, char *name, u8 *data, int size)
00513 {
00514   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00515   Node_t *node;
00516   struct net_FileCache *file;
00517 
00518   debug_Begin();
00519 
00520   if(NULL!=(node=list_CreateNode()))
00521   {
00522     if(NULL!=(file=mem_malloc(sizeof(struct net_FileCache))))
00523     {
00524       if(NULL!=(file->name=mem_malloc(strlen(name)+1)))
00525       {
00526         if(NULL!=(file->data=mem_malloc(size)))
00527         {
00528           strcpy(file->name,name);
00529           memcpy(file->data,data,size);
00530           file->size=size;
00531           list_SetNodeData(node,file);
00532           list_InsertNodeHead(cuser->file_cache,node);
00533         }
00534       }
00535     }
00536   }
00537 
00538   debug_End();
00539 }
00540 
00541 
00542 /*
00543  * valojaban struct net_Files -t var a filelist parameterben
00544  *  ha a megkapott listaban meg vannak file-ok, akkor
00545  *    kiolvassuk a kovetkezot (mint a LoadFile-nal) es visszaadjuk.
00546  *    aktualizaljuk a strukturat
00547  *  ha nincs tobb, vagy ez volt az utolso,
00548  *  akkor felszabaditjuk a data reszet a strukturanak es jelezzuk, hogy ures.
00549  *  ha eredetileg is ureset kaptunk, akkor NULL
00550  */
00556 u32 net_FilelistGetNext(u32 firsttag, ...)
00557 {
00558   return(net_FilelistGetNextTL((tag *)&firsttag));
00559 }
00560 u32 net_FilelistGetNextTL(tag *taglist)
00561 {
00562   u32 conn;
00563   struct net_Files *filelist;
00564   int *len;
00565 
00566   u8 *ret=NULL,*file;
00567   struct net_File retfile;
00568   u32 realsize;
00569 
00570 
00571   debug_Begin();
00572 
00573   conn=(u32 )tag_GetTagData(taglist,TAG_NET_CONN,0L);
00574   filelist=(struct net_Files *)tag_GetTagData(taglist,TAG_NET_FILELIST,0L);
00575   len=(int *)tag_GetTagData(taglist,TAG_NET_LEN,0L);
00576 
00577 
00578   if(filelist!=NULL&&filelist->file_count>filelist->readed)
00579   {
00580     retfile.type=net_MarshalGetLong(&filelist->next_data);
00581     retfile.realsize=net_MarshalGetLong(&filelist->next_data);
00582     retfile.data=net_MarshalGetBinary(&filelist->next_data,&retfile.size);
00583     filelist->readed++;
00584     if(retfile.type==FT_DEFLATED)
00585     {
00586       realsize=retfile.realsize;
00587       if(NULL!=(file=mem_malloc(realsize)))
00588       {
00589         uncompress(file,&realsize,retfile.data,retfile.size);
00590         mem_free(retfile.data);
00591         if(retfile.realsize!=realsize) debug_Error("Corrupt file received!");
00592         retfile.data=file;
00593         retfile.size=realsize;
00594       }
00595       else
00596       {
00597         mem_free(retfile.data);
00598         retfile.data=NULL;
00599         retfile.size=0;
00600         retfile.realsize=0;
00601       }
00602     }
00603     ret=retfile.data;
00604     if(len!=NULL) *len=retfile.size;
00605   }
00606 
00607   debug_End();
00608 
00609   return((u32)ret);
00610 }
00611 
00612 
00613 
00614 /* egy file-listat lehet felszabaditani vele, akar kiolvastuk belole
00615  * a file-okat, akar nem.
00616  */
00621 u32 net_FilelistFree(u32 firsttag, ...)
00622 {
00623   return(net_FilelistFreeTL((tag *)&firsttag));
00624 }
00625 u32 net_FilelistFreeTL(tag *taglist)
00626 {
00627   u32 conn;
00628   struct net_Files *filelist;
00629 
00630 
00631   debug_Begin();
00632 
00633   conn=(u32 )tag_GetTagData(taglist,TAG_NET_CONN,0L);
00634   filelist=(struct net_Files *)tag_GetTagData(taglist,TAG_NET_FILELIST,0L);
00635 
00636   if(filelist!=NULL)
00637   {
00638     if(filelist->data!=NULL) mem_free(filelist->data);
00639     mem_free(filelist);
00640   }
00641 
00642   debug_End();
00643 
00644   return(0L);
00645 }
00646 
00647 /*
00648  * a names tombben szereplo file-okat keri el a klienstol es megvarja a valaszt
00649  *  keres formatuma:
00650  *    u32     file-ok szama
00651  *    string  1. nev
00652  *    string  2. nev
00653  *    string  N. nev
00654  *
00655  *  valasz formatuma:
00656  *    az eddigi file formatumbol a kert mennyiseg
00657  *
00658  *  a valaszt elhelyezzuk egy memoriateruleten, ahol ezenkivul meg a file-ok szamat is
00659  *  taroljuk. struct net_Files -ban.
00660  */
00665 u32 net_FilelistLoad(u32 firsttag, ...)
00666 {
00667   return(net_FilelistLoadTL((tag *)&firsttag));
00668 }
00669 u32 net_FilelistLoadTL(tag *taglist)
00670 {
00671   u32 conn;
00672   char **names;
00673 
00674   struct net_Files *ret=NULL;
00675   struct net_File *retfile;
00676   int i,msglen=0,strcnt;
00677   u8 *msg,*m;
00678   u32 id;
00679 
00680 
00681   debug_Begin();
00682 
00683   conn=(u32 )tag_GetTagData(taglist,TAG_NET_CONN,0L);
00684   names=(char **)tag_GetTagData(taglist,TAG_NET_NAMES,0L);
00685 
00686   if(names!=NULL&&conn!=0L)
00687   {
00688     for(i=0;names[i]!=NULL;i++) msglen+=net_StrMsgLen(names[i]);
00689     strcnt=i;
00690     msglen+=4;
00691     if(NULL!=(msg=mem_malloc(msglen)))
00692     {
00693       if(NULL!=(ret=mem_malloc(sizeof(struct net_Files))))
00694       {
00695         m=msg;
00696         m=net_MarshalPutLong(m,(u32)strcnt);
00697         for(i=0;i<strcnt;i++) m=net_MarshalPutString(m,names[i]);
00698         id=net_GetServerId();
00699         net_SetState(conn,REQ_SLOADFILES|FL_REPLY,id);
00700         glw_SendMsg(conn,REQ_SLOADFILES,id,msg,msglen);
00701         mem_free(msg);
00702         glw_MainLoop(conn,0L,~GLWEV_SOCKET);
00703         retfile=(struct net_File *)net_GetUserData(conn);
00704         ret->file_count=strcnt;
00705         ret->readed=0;
00706         ret->data_size=retfile->size;
00707         ret->data=retfile->data;
00708         ret->next_data=ret->data;
00709         mem_free(retfile);
00710       }
00711     }
00712   }
00713 
00714   debug_End();
00715 
00716   return((u32)ret);
00717 }
00718 
00719 
00725 u32 net_LoadFile(u32 firsttag, ...)
00726 {
00727   return(net_LoadFileTL((tag *)&firsttag));
00728 }
00729 u32 net_LoadFileTL(tag *taglist)
00730 {
00731   u32 conn;
00732   u8 *name;
00733   int *len;
00734 
00735   char *file=NULL;
00736   struct net_FileCache *fcache;
00737 
00738   debug_Begin();
00739 
00740   conn=(u32 )tag_GetTagData(taglist,TAG_NET_CONN,0L);
00741   name=(u8 *)tag_GetTagData(taglist,TAG_NET_NAME,0L);
00742   len=(int *)tag_GetTagData(taglist,TAG_NET_LEN,0L);
00743 
00744   debug_Message("request: '%s'",name);
00745   if(NULL!=(fcache=net_FindFileCache(conn,name)))
00746   {
00747     if(NULL!=(file=mem_malloc(fcache->size)))
00748     {
00749       memcpy(file,fcache->data,fcache->size);
00750       if(len!=NULL) *len=fcache->size;
00751     }
00752   }
00753   else
00754   {
00755     char *name2;
00756     char *msg;
00757     char *f2;
00758     struct net_File *retfile;
00759     int msglen;
00760     u32 id,rs;
00761 
00762     if(NULL!=(name2=mem_malloc(strlen(name)+6+1)))
00763     {
00764       strcpy(name2,"files/");
00765       strcat(name2,name);
00766       if(net_GetWaitfor(conn)!=REQ_NONE) debug_Error("Nested file request! filename: '%s'",name);
00767       msglen=net_StrMsgLen(name2);
00768       if(NULL!=(msg=mem_malloc(msglen)))
00769       {
00770         net_MarshalPutString(msg,name2);
00771         id=net_GetServerId();
00772         net_SetState(conn,REQ_SLOADFILE|FL_REPLY,id);
00773         glw_SendMsg(conn,REQ_SLOADFILE,id,msg,msglen);
00774         mem_free(msg);
00775         glw_MainLoop(conn,0L,~GLWEV_SOCKET);
00776         if(NULL!=(retfile=(struct net_File *)net_GetUserData(conn)))
00777         {
00778           if(retfile->type==FT_DEFLATED)
00779           {
00780             rs=retfile->realsize;
00781             if(NULL!=(f2=mem_malloc(rs)))
00782             {
00783               uncompress(f2,&rs,retfile->data,retfile->size);
00784               mem_free(retfile->data);
00785               if(retfile->realsize!=rs) debug_Error("Corrupt file received!");
00786               retfile->data=f2;
00787               retfile->size=retfile->realsize;
00788             }
00789             else
00790             {
00791               mem_free(retfile->data);
00792               retfile->data=NULL;
00793               retfile->size=0;
00794               retfile->realsize=0;
00795             }
00796           }
00797           debug_Message("FILE INFO: name='%s' size=%d bytes",name,retfile->size);
00798           if(len!=NULL) *len=retfile->size;
00799           if(retfile->size>0)
00800           {
00801             file=retfile->data;
00802             net_InsertFileCache(conn,name,file,retfile->size);
00803           }
00804           else mem_free(retfile->data);
00805           mem_free(retfile);
00806         }
00807       }
00808       mem_free(name2);
00809     }
00810   }
00811 
00812   debug_End();
00813 
00814   return((u32)file);
00815 }
00816 
00817 
00818 
00819 int net_Init(void)
00820 {
00821   int ret=-1;
00822 
00823   debug_Begin();
00824 
00825 //Initialize call vector BEGIN - put this into the init function of this module
00826   call_vector[NET_FILELISTGETNEXT-NET_FUNCBASE]=net_FilelistGetNextTL;
00827   call_vector[NET_FILELISTFREE-NET_FUNCBASE]=net_FilelistFreeTL;
00828   call_vector[NET_FILELISTLOAD-NET_FUNCBASE]=net_FilelistLoadTL;
00829   call_vector[NET_LOADFILE-NET_FUNCBASE]=net_LoadFileTL;
00830   bases_modules.net_Call=net_Call;
00831   bases_modules.net_CallTL=net_CallTL;
00832 //Initialize call vector END
00833 
00834   ret=0;
00835 
00836   debug_End();
00837 
00838   return(ret);
00839 }
00840 
00841 
00842 void net_CleanUp(void)
00843 {
00844   debug_Begin();
00845   debug_End();
00846 }
00847 
00848 
00849 //END//
00850 
00851 u32 net_Call(int function, u32 firsttag, ...)
00852 {
00853   return(net_CallTL(function,(tag *)&firsttag));
00854 }
00855 u32 net_CallTL(int function, tag *taglist)
00856 {
00857   u32 ret=~0L;
00858 
00859   debug_Begin();
00860 
00861   debug_Message("function ID : %d",function-NET_FUNCBASE);
00862 
00863   if(function>=NET_FUNCBASE&&function<NET_FUNC_DONE)
00864   {
00865     ret=call_vector[function-NET_FUNCBASE](taglist);
00866   }
00867   else debug_Warning("%s(): Function code out of range! code=0x%04x",__FUNCTION__,function);
00868 
00869   debug_End();
00870 
00871   return(ret);
00872 }
00873 

Generated on Tue Jan 7 12:11:23 2003 for THEGUI by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002