Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

NetCommon.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 
00027 #include "debug.h"
00028 #include "Memory.h"
00029 #include "xml_Gui.h"
00030 #include "NetCommon.h"
00031 
00032 
00033 
00034 /*
00035 static char *msgnames[1024];
00036 static char *msgtypes[]=
00037 {
00038   " -  ***ILLEGAL",
00039   " - S NONE",
00040   " - C NONE",
00041   " - S ECHO",
00042   " - C ECHO",
00043   " - S LOADFILE",
00044   " - S WINEVENT",
00045   " - S APP",
00046   " - C CALLGAD",
00047   " - C CLOSEWIN",
00048   " - C QUIT",
00049   " - C OPENWIN",
00050   "R- S ECHO",
00051   "R- C ECHO",
00052   "R- S LOADFILE",
00053   "R- S WINEVENT",
00054   "R- S APP",
00055   "R- C CALLGAD",
00056   "R- C CLOSEWIN",
00057   "R- C QUIT",
00058   "R- C OPENWIN",
00059   NULL
00060 };
00061 */
00062 
00063 
00064 
00065 /*
00066 char *net_GetMsgName(u8 type)
00067 {
00068   char *ret;
00069 
00070   if(msgnames[0]==NULL)
00071   {
00072     int i;
00073     for(i=0;i<255;i++) msgnames[i]=msgtypes[0];
00074     msgnames[REQ_NONE]=msgtypes[1];
00075     msgnames[REQ_NONE+FL_CLI]=msgtypes[2];
00076     msgnames[REQ_SECHO]=msgtypes[3];
00077     msgnames[REQ_CECHO]=msgtypes[4];
00078     msgnames[REQ_SLOADFILE]=msgtypes[5];
00079     msgnames[REQ_SWINEVENT]=msgtypes[6];
00080     msgnames[REQ_SAPP]=msgtypes[7];
00081     msgnames[REQ_CCALLGAD]=msgtypes[8];
00082     msgnames[REQ_CCLOSEWIN]=msgtypes[9];
00083     msgnames[REQ_CCLOSEAPP]=msgtypes[10];
00084     msgnames[REQ_COPENWIN]=msgtypes[11];
00085 
00086     msgnames[REQ_SECHO|FL_REPLY]=msgtypes[12];
00087     msgnames[REQ_CECHO|FL_REPLY]=msgtypes[13];
00088     msgnames[REQ_SLOADFILE|FL_REPLY]=msgtypes[14];
00089     msgnames[REQ_SWINEVENT|FL_REPLY]=msgtypes[15];
00090     msgnames[REQ_SAPP|FL_REPLY]=msgtypes[16];
00091     msgnames[REQ_CCALLGAD|FL_REPLY]=msgtypes[17];
00092     msgnames[REQ_CCLOSEWIN|FL_REPLY]=msgtypes[18];
00093     msgnames[REQ_CCLOSEAPP|FL_REPLY]=msgtypes[19];
00094     msgnames[REQ_COPENWIN|FL_REPLY]=msgtypes[20];
00095   }
00096   ret=msgnames[type];
00097 printf("req type: 0x%02x = %s\n",type,msgnames[type]);
00098   return(ret);
00099 }
00100 */
00101 
00102 int net_GetPort(void)
00103 {
00104   return(9394);
00105 }
00106 
00107 
00108 u8 *net_MarshalPutLong(u8 *msg, u32 value)
00109 {
00110   debug_Begin();
00111 
00112   *((u32 *)msg)=glw_htonl(value);
00113 
00114   debug_End();
00115 
00116   return(msg+4);
00117 }
00118 
00119 
00120 u8 *net_MarshalPutString(u8 *msg, char *value)
00121 {
00122   u8 *ret;
00123   int len;
00124 
00125   debug_Begin();
00126 
00127   len=strlen(value)+1;
00128   ret=msg+len+4;
00129   *((u32 *)msg)=glw_htonl(len);
00130   strcpy(msg+4,value);
00131 
00132   debug_End();
00133 
00134   return(ret);
00135 }
00136 
00137 
00138 u8 *net_MarshalPutBinary(u8 *msg, u8 *value, int len)
00139 {
00140   u8 *ret;
00141 
00142   debug_Begin();
00143 
00144   ret=msg+len+4;
00145   *((u32 *)msg)=glw_htonl(len);
00146   if(len>0) memcpy(msg+4,value,len);
00147 
00148   debug_End();
00149 
00150   return(ret);
00151 }
00152 
00153 
00154 u8 *net_MarshalPutTaglist(u8 *msg, tag *taglist)
00155 {
00156   tag *tags;
00157   u8 *st;
00158   int tag_count;
00159 
00160   debug_Begin();
00161 
00162   st=msg;
00163   for(msg+=4,tag_count=0,tags=taglist;tags->Name!=TAG_DONE;tags=tag_GetNextTag(tags),tag_count++)
00164   {
00165     msg=net_MarshalPutLong(msg,tags->Name);
00166     if((tags->Name&TAGT_STRING)!=0)
00167     {
00168       msg=net_MarshalPutString(msg,(char *)tags->Data);
00169     }
00170     else msg=net_MarshalPutLong(msg,tags->Data);
00171   }
00172   net_MarshalPutLong(st,tag_count);
00173 
00174   debug_End();
00175 
00176   return(msg);
00177 }
00178 
00179 
00180 u32 net_MarshalGetLong(u8 **msg)
00181 {
00182   u32 ret;
00183 
00184   debug_Begin();
00185 
00186   ret=glw_ntohl(*((u32 *)*msg));
00187   *msg+=4;
00188 
00189   debug_End();
00190 
00191   return(ret);
00192 }
00193 
00194 
00195 char *net_MarshalGetString(u8 **msg)
00196 {
00197   char *str;
00198   int len;
00199 
00200   debug_Begin();
00201 
00202   len=glw_ntohl(*((u32 *)(*msg)));
00203   if(NULL!=(str=mem_malloc(len)))
00204   {
00205     memcpy(str,(*msg)+4,len);
00206     str[len-1]='\0';
00207   }
00208   *msg+=len+4;
00209 
00210   debug_End();
00211 
00212   return(str);
00213 }
00214 
00215 
00216 u8 *net_MarshalGetBinary(u8 **msg, int *len)
00217 {
00218   u8 *data;
00219   int blen;
00220 
00221   debug_Begin();
00222 
00223   blen=glw_ntohl(*((u32 *)*msg));
00224   if(NULL!=(data=mem_malloc(blen)))
00225   {
00226     memcpy(data,(*msg)+4,blen);
00227     if(len!=NULL) *len=blen;
00228   }
00229   *msg+=blen+4;
00230 
00231   debug_End();
00232 
00233   return(data);
00234 }
00235 
00236 
00237 int net_MarshalGetBinaryLen(u8 **msg)
00238 {
00239   int blen;
00240 
00241   blen=glw_ntohl(*((u32 *)*msg));
00242 
00243   return(blen);
00244 }
00245 
00246 
00247 
00248 tag *net_MarshalGetTaglist(u8 **msg)
00249 {
00250   tag *ret,*tags;
00251   int tag_count,i;
00252 
00253   debug_Begin();
00254 
00255   tag_count=(int)net_MarshalGetLong(msg);
00256   if(NULL!=(ret=mem_malloc((tag_count+1)*sizeof(tag))))
00257   {
00258     for(tags=ret,i=0;i<tag_count;i++,tags++)
00259     {
00260       tags->Name=net_MarshalGetLong(msg);
00261       if((tags->Name&TAGT_STRING)!=0)
00262       {
00263         tags->Data=(u32)net_MarshalGetString(msg);
00264       }
00265       else tags->Data=net_MarshalGetLong(msg);
00266     }
00267     tags->Name=TAG_DONE;
00268   }
00269 
00270   debug_End();
00271 
00272   return(ret);
00273 }
00274 
00275 
00276 void net_MarshalFreeTaglist(tag *taglist)
00277 {
00278   tag *tags;
00279 
00280   if(taglist!=NULL)
00281   {
00282     for(tags=taglist;tags->Name!=TAG_DONE;tags=tag_GetNextTag(tags))
00283     {
00284       if((tags->Name&TAGT_STRING)!=0) mem_free((void *)tags->Data);
00285     }
00286     mem_free(taglist);
00287   }
00288 }
00289 
00290 
00291 int net_SetState(u32 conn, int waitfor, u32 userdata)
00292 {
00293   int ret=-1;
00294   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00295 
00296   debug_Begin();
00297 
00298   ret=cuser->waitfor;
00299   cuser->waitfor=waitfor;
00300   cuser->userdata=userdata;
00301 
00302   debug_End();
00303 
00304   return(ret);
00305 }
00306 
00307 
00308 int net_MsgToWaitList(u32 conn, struct glw_SockMsg *msg)
00309 {
00310   int ret=0;
00311   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00312   Node_t *node;
00313 
00314   debug_Begin();
00315 
00316   if(cuser!=NULL&&msg!=NULL)
00317   {
00318     if(NULL!=(node=list_CreateNode()))
00319     {
00320       list_SetNodeData(node,msg);
00321       list_InsertNodeTail(cuser->waitlist,node);
00322       ret=1;
00323     }
00324   }
00325 
00326   debug_End();
00327 
00328   return(ret);
00329 }
00330 
00331 
00332 struct glw_SockMsg *net_MsgFromWaitList(u32 conn)
00333 {
00334   struct glw_SockMsg *ret=NULL;
00335   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00336   Node_t *node;
00337 
00338   debug_Begin();
00339 
00340   if(cuser!=NULL&&cuser->waitfor==REQ_NONE&&NULL!=(node=list_RemoveNodeHead(cuser->waitlist)))
00341   {
00342     ret=(struct glw_SockMsg *)list_GetNodeData(node);
00343     list_DeleteNode(node);
00344   }
00345 
00346   debug_End();
00347 
00348   return(ret);
00349 }
00350 
00351 
00352 void net_ClearMsgList(u32 conn)
00353 {
00354   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00355   struct glw_SockMsg *msg;
00356   Node_t *node;
00357 
00358   debug_Begin();
00359 
00360   if(NULL!=cuser&&cuser->waitlist!=NULL)
00361   {
00362     while(NULL!=(node=list_RemoveNodeHead(cuser->waitlist)))
00363     {
00364       msg=list_GetNodeData(node);
00365       list_DeleteNode(node);
00366       if(NULL!=msg)
00367       {
00368         if(NULL!=msg->body) mem_free(msg->body);
00369         mem_free(msg);
00370       }
00371     }
00372     list_DeleteList(cuser->waitlist);
00373     cuser->waitlist=NULL;
00374   }
00375 
00376   debug_End();
00377 }
00378 
00379 
00380 u32 net_GetUserData(u32 conn)
00381 {
00382   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00383   return(cuser->userdata);
00384 }
00385 
00386 
00387 int net_GetWaitfor(u32 conn)
00388 {
00389   struct glw_ConnUser *cuser=(struct glw_ConnUser *)conn;
00390   return(cuser->waitfor);
00391 }
00392 
00393 
00394 int net_StrMsgLen(char *str)
00395 {
00396   return(strlen(str)+1+4);
00397 }
00398 
00399 

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