Author: pepeto
Date: Thu Dec 18 00:48:24 2014
New Revision: 27337

URL: http://svn.gna.org/viewcvs/freeciv?rev=27337&view=rev
Log:
Remove get_packet_from_connection_helper() and receive_packet_XXX() functions.
Access to the function directly from pointers.

See gna patch #5566

Modified:
    trunk/common/generate_packets.py
    trunk/common/packets.c

Modified: trunk/common/generate_packets.py
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/generate_packets.py?rev=27337&r1=27336&r2=27337&view=diff
==============================================================================
--- trunk/common/generate_packets.py    (original)
+++ trunk/common/generate_packets.py    Thu Dec 18 00:48:24 2014
@@ -1147,7 +1147,6 @@
             self.extra_send_args2=self.extra_send_args2+', force_to_send'
             self.extra_send_args3=self.extra_send_args3+', bool force_to_send'
 
-        self.receive_prototype='struct %(name)s *receive_%(name)s(struct 
connection *pc)'%self.__dict__
         self.send_prototype='int send_%(name)s(struct connection 
*pc%(extra_send_args)s)'%self.__dict__
         if self.want_lsend:
             self.lsend_prototype='void lsend_%(name)s(struct conn_list 
*dest%(extra_send_args)s)'%self.__dict__
@@ -1197,8 +1196,7 @@
     # Returns a code fragement which represents the prototypes of the
     # send and receive functions for the header file.
     def get_prototypes(self):
-        result=(self.receive_prototype+";\n"+
-                self.send_prototype+";\n")
+        result=self.send_prototype+";\n"
         if self.want_lsend:
             result=result+self.lsend_prototype+";\n"
         if self.want_dsend:
@@ -1212,23 +1210,6 @@
         result=self.__dict__.copy()
         result.update(vars)
         return result
-
-    # Returns a code fragment which is the implementation of the
-    # public visible receive function
-    def get_receive(self):
-        return '''%(receive_prototype)s
-{
-  if(!pc->used) {
-    log_error("WARNING: trying to read data from the closed connection %%s",
-              conn_description(pc));
-    return NULL;
-  }
-  fc_assert_ret_val_msg(pc->phs.handlers->receive[%(type)s] != NULL, NULL,
-                        "Handler for %(type)s not installed");
-  return (struct %(name)s *) pc->phs.handlers->receive[%(type)s](pc);
-}
-
-'''%self.get_dict(vars())
 
     def get_send(self):
         if self.no_packet:
@@ -1352,29 +1333,6 @@
 
     for p in packets:
         body=body+p.get_reset_part()
-    return intro+body+extro
-
-# Returns a code fragement which is the implementation of the
-# get_packet_from_connection_helper() function. This function is a big
-# switch case construct which calls the appropriate packet specific
-# receive function.
-def get_get_packet_helper(packets):
-    intro='''void *get_packet_from_connection_helper(struct connection *pc,\n  
  enum packet_type type)
-{
-  switch (type) {
-
-'''
-    body=""
-    for p in packets:
-        body=body+"  case %(type)s:\n    return 
receive_%(name)s(pc);\n\n"%p.__dict__
-    extro='''  default:
-    log_packet("unknown packet type %d received from %s",
-               type, conn_description(pc));
-    return NULL;
-  };
-}
-
-'''
     return intro+body+extro
 
 # Returns a code fragement which is the implementation of the
@@ -1684,7 +1642,6 @@
     output_h.write('''
 void delta_stats_report(void);
 void delta_stats_reset(void);
-void *get_packet_from_connection_helper(struct connection *pc, enum 
packet_type type);
 
 #ifdef __cplusplus
 }
@@ -1750,14 +1707,12 @@
     output_c.write(get_report(packets))
     output_c.write(get_reset(packets))
 
-    output_c.write(get_get_packet_helper(packets))
     output_c.write(get_packet_name(packets))
     output_c.write(get_packet_has_game_info_flag(packets))
 
     # write hash, cmp, send, receive
     for p in packets:
         output_c.write(p.get_variants())
-        output_c.write(p.get_receive())
         output_c.write(p.get_send())
         output_c.write(p.get_lsend())
         output_c.write(p.get_dsend())

Modified: trunk/common/packets.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/packets.c?rev=27337&r1=27336&r2=27337&view=diff
==============================================================================
--- trunk/common/packets.c      (original)
+++ trunk/common/packets.c      Thu Dec 18 00:48:24 2014
@@ -378,6 +378,7 @@
   int header_size = 0;
 #endif
   void *data;
+  void *(*receive_handler)(struct connection *);
 
   if (!pc->used) {
     return NULL;               /* connection was closed, stop reading */
@@ -502,6 +503,16 @@
   dio_get_type(&din, pc->packet_header.type, &utype.itype);
   utype.type = utype.itype;
 
+  if (utype.type < 0
+      || utype.type >= PACKET_LAST
+      || (receive_handler = pc->phs.handlers->receive[utype.type]) == NULL) {
+    log_verbose("Received unsupported packet type %d (%s). The connection "
+                "will be closed now.",
+                utype.type, packet_name(utype.type));
+    connection_close(pc, _("unsupported packet type"));
+    return NULL;
+  }
+
   log_packet("got packet type=(%s)%d len=%d from %s",
              packet_name(utype.type), utype.itype, whole_packet_len,
              is_server() ? pc->username : "server");
@@ -556,7 +567,7 @@
     }
   }
 #endif /* PACKET_SIZE_STATISTICS */
-  data = get_packet_from_connection_helper(pc, utype.type);
+  data = receive_handler(pc);
   if (!data) {
     connection_close(pc, _("incompatible packet contents"));
     return NULL;


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to