Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. The components of embodiments of the present invention generally described and illustrated in the figures herein may be arranged and designed in a wide variety of different configurations. Thus, the following detailed description of the embodiments of the present invention, presented in the figures, is not intended to limit the scope of the invention, as claimed, but is merely representative of selected embodiments of the invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments of the present invention without making any creative effort, shall fall within the protection scope of the present invention.
It should be noted that: like reference numbers and letters refer to like items in the following figures, and thus, once an item is defined in one figure, it need not be further defined and explained in subsequent figures. Meanwhile, in the description of the present invention, the terms "first", "second", and the like are used only for distinguishing the description, and are not to be construed as indicating or implying relative importance.
Referring to fig. 1, fig. 1 is a block diagram illustrating a terminal device 100 according to an embodiment of the present invention. The terminal device 100 may be, but is not limited to, a smart phone, a Personal Computer (PC), a tablet PC, a Personal Digital Assistant (PDA), a Mobile Internet Device (MID), a web server, and the like. The terminal device 100 includes a data transmission apparatus 200, a memory 101, a memory controller 102, and a processor 103.
The memory 101, the memory controller 102 and the processor 103 are electrically connected to each other directly or indirectly to realize data transmission or interaction. For example, the components may be electrically connected to each other via one or more communication buses or signal lines. The data transmission device 200 includes at least one software functional module which can be stored in the memory 101 in the form of software or firmware (firmware) or is fixed in an Operating System (OS) of the terminal device 100. The processor 103 is used for executing executable modules stored in the memory 101, such as software functional modules or computer programs included in the data transmission device 200.
The Memory 101 may be, but is not limited to, a Random Access Memory (RAM), a Read Only Memory (ROM), a Programmable Read-Only Memory (PROM), an Erasable Read-Only Memory (EPROM), an electrically Erasable Read-Only Memory (EEPROM), and the like. The memory 101 is configured to store a program, and the processor 103 executes the program after receiving an execution instruction, and the method executed by the server defined by the process disclosed in any embodiment of the present invention may be applied to the processor 103, or implemented by the processor 103.
The processor 103 may be an integrated circuit chip having signal processing capabilities. The processor 103 may be a general-purpose processor, and includes a Central Processing Unit (CPU), a Network Processor (NP), a voice processor, a video processor, and the like; but may also be a digital signal processor, an application specific integrated circuit, a field programmable gate array or other programmable logic device, discrete gate or transistor logic, discrete hardware components. The various methods, steps and logic blocks disclosed in the embodiments of the present invention may be implemented or performed. A general purpose processor may be a microprocessor or the processor 103 may be any conventional processor or the like.
First embodiment
Referring to fig. 2, fig. 2 is a flowchart illustrating a data transmission method according to an embodiment of the present invention. The data transmission method comprises the following steps:
step S101, a first transmission object to be transmitted is obtained.
In the embodiment of the present invention, the first transport object may be any one transport object in a network communication transport protocol, and the network communication transport protocol may be a data packet, where the data packet includes a plurality of transport objects. As an embodiment, the first transport object may include an address field and a name field, for example, the first transport object may be represented by the following code:
the Message is a first transmission object, id is an address field, and name is a name field, and as can be seen from the above codes, the Message occupies 400 bytes of memory in total, but the actual memory size of the address field id is 100 bytes, and the actual memory size of the name field name is 100 bytes, that is, the Message has 200 empty bytes.
Step S102, writing the data of the first transmission object into a second transmission object, where the memory size of the second transmission object is consistent with the data size of the first transmission object.
In the embodiment of the present invention, the second transmission object may be a transmission object that stores data of the first transmission object. The method for writing the data of the first transmission object into the second transmission object can be as follows: firstly, a second transmission object can be defined as a Pickle, and a memory of the Pickle is preset to be 4 KB; and then writing the address field and the name field of the first transmission object into a second transmission object, so that the memory size of the second transmission object is consistent with the data size of the first transmission object. In one embodiment, during the process of writing the data of the first transfer object into the second transfer object, the realloc function may be called to automatically increase the memory of the Pickle, and the size of the memory may always be consistent with the size of the data written into the Pickle by the Message.
Referring to fig. 3, step S102 may include the following sub-steps:
in the sub-step S1021, the address field and the name field of the first transmission object are obtained.
In the embodiment of the present invention, the address field and the name field of the first transmission object are obtained, and the actual memory sizes of the address field id and the name field name are obtained, that is, the obtained address field of the first transmission object is 100 bytes, and the obtained name field of the first transmission object is 100 bytes. As an implementation, a strlen function may be called to obtain the actual memory size of the address field id and the name field name.
In sub-step S1022, the address field is written into the second transport object.
In the embodiment of the present invention, after the address field is obtained, the address field may be written into a first storage area of the second transfer object, where the first storage area may include a first byte number storage area and an address field storage area, and addresses of the first byte number storage area and the address field storage area are consecutive. And writing the address field into the second transmission object, namely writing the byte number of the address field into the first byte number storage area, and writing all the bytes of the address field into the address field storage area.
As an embodiment, the method for writing the address field into the second transmission object may be: firstly, calling a strlen function to acquire the byte number of an address field, wherein the byte number can be 100; secondly, calling a WriteInt function, writing byte number of an address field in a first byte number storage area, wherein the length of the first byte number storage area can be 4 bytes, namely, writing a digital variable 100 in the first byte number storage area with the length of 4 bytes from the first address of a second transmission object, and calling a realloc function to increase the memory offset size _ t of the Pickle by 4; then, calling bits, namely aligning all bytes of an address field according to a preset number for aligning all bytes of a name field and optimizing memory arrangement, wherein the preset number can be 4 bytes or 8 bytes; finally, calling the WriteData function, writing the aligned address field in the address field storage area, that is, after the first byte storage area with the length of 4 bytes, starting to write the aligned address field with 100 bytes, and calling the realloc function to increase the memory offset size _ t of the Pickle by 100. Therefore, after writing the address field into the second transfer object, the memory size of the second transfer object is 104 bytes.
Referring to fig. 4, the sub-step S1022 may include the following sub-steps:
and a substep S10221 of obtaining the byte number of the address field.
In the embodiment of the present invention, a strlen function is called to obtain the number of bytes of an address field, and the number of bytes of the address field may be 100.
In the substep S10222, the byte number of the address field is written in the first byte number storage area.
In the embodiment of the present invention, the length of the first byte storage area may be 4 bytes, the WriteInt function may be called to write the digital variable 100 from the first address of the second transfer object to the first byte storage area with a length of 4 bytes, and the realloc function is called to increase the memory offset size _ t of the Pickle by 4.
The sub-step S10223 aligns all bytes of the address field by a preset number.
In the embodiment of the invention, a bits function is called, all bytes of an address field are aligned according to a preset number, the preset number can be 4 bytes or 8 bytes, and the alignment is used for aligning all bytes of a name field, so that the memory arrangement is optimized.
In the substep S10224, the aligned address field is written in the address field storage area.
In the embodiment of the invention, the WriteData function is called, after the first byte storage area with the length of 4 bytes, the aligned address field with 100 bytes is written, and simultaneously, the realloc function is called to increase the memory offset size _ t of the Pickle by 100.
And a substep S1023 of writing a name field into the second transfer object, wherein the address field occupies a first storage area of the second transfer object, the name field occupies a second storage area of the second transfer object, and addresses of the first storage area and the second storage area are continuous.
In the embodiment of the present invention, after the name field is acquired, the name field may be written into a second storage area of the second transmission object, where addresses of the second storage area and the first storage area are consecutive. The second storage area may include a second byte count storage area and a name field storage area, and addresses of the second byte count storage area and the name field storage area are consecutive. And writing the name field into the second transmission object, namely writing the byte number of the name field into a second byte number storage area, and writing all the bytes of the name field into the name field storage area.
As an embodiment, the method for writing the name field into the second transport object may be: firstly, calling a strlen function to acquire the number of bytes of a name field, wherein the number of bytes can be 100; secondly, calling a WriteInt function, writing the byte number of a name field in a second byte storage area, wherein the length of the second byte storage area can be 4 bytes, namely, writing a digital variable 100 in the second byte storage area with the length of 4 bytes behind an address field storage area with the length of 100 bytes, and simultaneously calling a realloc function to increase the memory offset size _ t of the Pickle by 4; then, calling bits, namely aligning all bytes of the name field according to a preset number, aligning all bytes of the name field with all bytes of the address field, and optimizing memory arrangement, wherein the preset number can be 4 bytes or 8 bytes; finally, calling the WriteData function, writing the aligned address field in the name field storage area, that is, after the second byte storage area with the length of 4 bytes, starting to write the aligned address field with 100 bytes, and calling the realloc function to increase the memory offset size _ t of the Pickle by 100. Thus, after writing the name field to the second transfer object, the memory size of the second transfer object is 208 bytes.
Referring to fig. 5, the sub-step S1023 may include the following sub-steps:
and a substep S10231 of obtaining the byte number of the name field.
In the embodiment of the present invention, a strlen function is called to obtain the number of bytes of a name field, and the number of bytes of the name field may be 100.
And a substep S10232, writing the byte number of the name field in the second byte number storage area.
In the embodiment of the present invention, the length of the second byte storage area may be 4 bytes, a WriteInt function may be called, the digital variable 100 is written in the second byte storage area with the length of 4 bytes after the address field storage area with 100 bytes, and a realloc function is called to increase the memory offset size _ t of the Pickle by 4.
And a sub-step S10233 of aligning all bytes of the name field by a preset number.
In the embodiment of the invention, a bits function is called, all bytes of a name field are aligned according to a preset number, the alignment is used for aligning all bytes of an address field, and the memory arrangement is optimized, wherein the preset number can be 4 bytes or 8 bytes.
In sub-step S10234, the aligned name field is written in the name field storage area.
In the embodiment of the present invention, the WriteData function is called, after the second byte storage area with the length of 4 bytes, the address field of 100 bytes after alignment starts to be written, the realloc function is called to increase the memory offset size _ t of Pickle by 100 at the same time, and after the address field and the name field are all written into the second transfer object, the memory size of the second transfer object is 208 bytes.
Step S103, sending the data of the second transmission object to other terminal devices.
In the embodiment of the invention, after the data of the first transmission object is written into the second transmission object, namely after the address field and the name field of the first transmission object are written into the second transmission object, firstly, a data function is called to return a memory pointer of the second transmission object, and a size function is called to return the memory size of the second transmission object; secondly, acquiring a second transmission object according to the memory pointer and the size of the memory; and finally, calling a network transmission interface, and sending the data of the second transmission object to other terminal equipment through a network. Other terminal devices may be, but are not limited to, smart phones, Personal Computers (PCs), tablet PCs, Personal Digital Assistants (PDAs), Mobile Internet Devices (MIDs), web servers, and the like.
Referring to fig. 6, step S103 may include the following sub-steps:
and a substep S1031, obtaining a memory pointer of the second transmission object.
In the embodiment of the invention, a data function can be called to return the memory pointer of the second transmission object Pickle.
In the sub-step S1032, the memory size of the second transmission object is obtained.
In the embodiment of the invention, a size function can be called to return the memory size of the second transmission object Pickle.
And a substep S1033 of obtaining the second transmission object according to the memory pointer and the memory size.
In the embodiment of the invention, the second transmission object Pickle is found according to the memory pointer returned by the data function and the memory size returned by the size function.
And a substep S1034, calling a network transmission interface, and sending the data of the second transmission object to other terminal devices.
In the embodiment of the present invention, a network transmission interface is called, and the data of the second transmission object is sent to other terminal devices such as a smart phone, a Personal Computer (PC), a tablet PC, a Personal Digital Assistant (PDA), a Mobile Internet Device (MID), a web server, and the like through a network.
In the embodiment of the invention, the data of the first transmission object to be transmitted is written into the second transmission object, so that the size of the memory of the second transmission object is consistent with the size of the data of the first transmission object, namely, the memory of the first transmission object is reduced by nearly 50%, and the memory occupied by the data of the transmission object is effectively reduced. And the data of the second transmission object is sent to other terminal equipment, so that the network communication transmission speed is obviously improved, and the method has good practicability.
Second embodiment
Referring to fig. 7, fig. 7 is a block diagram illustrating a data transmission apparatus 200 according to an embodiment of the invention. The data transmission device 200 includes an obtaining module 201, a data writing module 202, and a data sending module 203.
The obtaining module 201 is configured to obtain a first transmission object to be transmitted.
In this embodiment of the present invention, the obtaining module 201 may be configured to execute step S101.
The data writing module 202 is configured to write data of the first transmission object into a second transmission object, where a memory size of the second transmission object is consistent with a data size of the first transmission object.
In this embodiment of the present invention, the data writing module 202 may be configured to execute step S102.
Referring to fig. 8, fig. 8 is a block diagram illustrating a data writing module 202 in the data transmission apparatus 200 shown in fig. 7. The data writing module 202 includes a field acquiring unit 2021, an address field writing unit 2022, and a name field writing unit 2023.
A field obtaining unit 2021, configured to obtain an address field and a name field of the first transport object.
In an embodiment of the present invention, the field obtaining unit 2021 may be configured to perform the sub-step S1021.
An address field writing unit 2022, configured to write the address field into the second transfer object.
In an embodiment of the present invention, the address field writing unit 2022 may be configured to perform sub-step S1022.
Referring to fig. 9, fig. 9 is a block diagram illustrating an address field writing unit 2022 in the data writing module 202 shown in fig. 8. The address field writing unit 2022 includes a first byte count obtaining unit 20221, a first byte count writing unit 20222, a first execution unit 20223, and a second execution unit 20224.
A first byte count obtaining unit 20221, configured to obtain the byte count of the address field.
In an embodiment of the present invention, the first byte count obtaining unit 20221 may be configured to perform the sub-step S10221.
A first byte count writing unit 20222, for writing the byte count of the address field in the first byte count storage area.
In an embodiment of the present invention, the first byte count write unit 20222 may be configured to perform the substep S10222.
A first execution unit 20223, configured to align all bytes of the address field according to a preset number.
In an embodiment of the present invention, the first performing unit 20223 may be configured to perform the sub-step S10223.
A second execution unit 20224, configured to write the aligned address field in the address field storage area.
In an embodiment of the present invention, the second performing unit 20224 may be configured to perform the substep S10224.
A name field writing unit 2023, configured to write a name field into the second transfer object, where the address field occupies a first storage area of the second transfer object, the name field occupies a second storage area of the second transfer object, and addresses of the first storage area and the second storage area are consecutive.
In an embodiment of the present invention, the name field writing unit 2023 may be configured to perform the sub-step S1023.
Referring to fig. 10, fig. 10 is a block diagram illustrating the name field writing unit 2023 in the data writing module 202 shown in fig. 8. The name field writing unit 2023 includes a second byte count obtaining unit 20231, a second byte count writing unit 20232, a third execution unit 20233, and a fourth execution unit 20234.
A second byte count acquisition unit 20231, configured to acquire the byte count of the name field.
In this embodiment of the present invention, the second byte count obtaining unit 20231 may be configured to perform the sub-step S10231.
A second byte count writing unit 20232, configured to write the byte count of the name field in the second byte count storage area.
In the embodiment of the present invention, the second byte count writing unit 20232 may be configured to perform the sub-step S10232.
A third performing unit 20233, configured to align all bytes of the name field according to a preset number.
In an embodiment of the present invention, the third performing unit 20233 may be configured to perform the sub-step S10233.
A fourth execution unit 20234, configured to write the aligned name field in the name field storage area.
In an embodiment of the present invention, the fourth performing unit 20234 may be configured to perform the sub-step S10234.
And a data sending module 203, configured to send the data of the second transmission object to other terminal devices.
In this embodiment of the present invention, the data sending module 203 may be configured to execute step S103.
Referring to fig. 11, fig. 11 is a block diagram illustrating a data sending module 203 in the data transmission apparatus 200 shown in fig. 7. The data transmission module 203 includes a memory pointer obtaining unit 2031, a memory size obtaining unit 2032, a transmission object obtaining unit 2033, and a transmission unit 2034.
A memory pointer obtaining unit 2031, configured to obtain a memory pointer of the second transmission object.
In this embodiment of the present invention, the memory pointer obtaining unit 2031 may be configured to perform the substep S1031.
A memory size obtaining unit 2032, configured to obtain a memory size of the second transmission object.
In this embodiment of the present invention, the memory size obtaining unit 2032 may be configured to perform the sub-step S1032.
A transmission object obtaining unit 2033, configured to obtain the second transmission object according to the memory pointer and the memory size.
In an embodiment of the present invention, the transmission object acquiring unit 2033 may be configured to perform the substep S1033.
A sending unit 2034, configured to invoke the network transmission interface, and send the data of the second transmission object to the other terminal device.
In this embodiment of the present invention, the sending unit 2034 may be configured to perform sub-step S1034.
In summary, the data transmission method, apparatus and terminal device provided by the present invention include: acquiring a first transmission object to be transmitted; writing the data of the first transmission object into a second transmission object, wherein the memory size of the second transmission object is consistent with the data size of the first transmission object; and sending the data of the second transmission object to other terminal equipment. According to the invention, the data of the first transmission object to be transmitted is written into the second transmission object, so that the size of the memory of the second transmission object is consistent with the size of the data of the first transmission object, namely, the memory of the first transmission object is reduced by nearly 50%, the memory occupied by the data of the transmission object is effectively reduced, and then the data of the second transmission object is sent to other terminal equipment, so that the network communication transmission speed is obviously improved, and the method has good practicability.
In the embodiments provided in the present application, it should be understood that the disclosed apparatus and method can be implemented in other ways. The apparatus embodiments described above are merely illustrative, and for example, the flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
In addition, the functional modules in the embodiments of the present invention may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.
The functions, if implemented in the form of software functional modules and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes. It is noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention, and various modifications and changes may be made by those skilled in the art. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention. It should be noted that: like reference numbers and letters refer to like items in the following figures, and thus, once an item is defined in one figure, it need not be further defined and explained in subsequent figures.