1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
19 | */ |
---|
20 | #define DEBUG_MODULE_NETWORK |
---|
21 | |
---|
22 | #include "write_sync.h" |
---|
23 | |
---|
24 | #include "debug.h" |
---|
25 | |
---|
26 | |
---|
27 | /** |
---|
28 | * default constructor |
---|
29 | */ |
---|
30 | WriteSync::WriteSync(const char* name) |
---|
31 | : Synchronizeable(name) |
---|
32 | { |
---|
33 | /* define the local buffer size */ |
---|
34 | this->outLength = 10; |
---|
35 | this->recLength = 0; |
---|
36 | this->inLength = 40; |
---|
37 | this->outData = new byte[this->outLength]; |
---|
38 | this->inData = new byte[this->inLength]; |
---|
39 | |
---|
40 | /* init the buffer data */ |
---|
41 | for( int i = 0; i < this->outLength; i++) |
---|
42 | { |
---|
43 | this->outData[i] = i; |
---|
44 | } |
---|
45 | for( int i = 0; i < this->inLength; i++) |
---|
46 | { |
---|
47 | this->inData[i] = 0; |
---|
48 | } |
---|
49 | |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | /** |
---|
54 | * default destructor deletes all unneded stuff |
---|
55 | */ |
---|
56 | WriteSync::~WriteSync() |
---|
57 | { |
---|
58 | if( this->outData) |
---|
59 | delete[] this->outData; |
---|
60 | if( this->inData) |
---|
61 | delete[] this->inData; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | /** |
---|
66 | * write data to Synchronizeable |
---|
67 | */ |
---|
68 | void WriteSync::writeBytes(const byte* data, int length) |
---|
69 | { |
---|
70 | PRINTF(0)("WriteSync: got %i bytes of data\n", length); |
---|
71 | this->recLength = length; |
---|
72 | if(this->inLength < length) |
---|
73 | PRINTF(0)("ERROR: local buffer is smaller than the data to receive.\n"); |
---|
74 | |
---|
75 | /* copy the data localy */ |
---|
76 | for( int i = 0; i < length; i++) |
---|
77 | { |
---|
78 | this->inData[i] = data[i]; |
---|
79 | } |
---|
80 | /* and debug output */ |
---|
81 | this->writeDebug(); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /** |
---|
86 | * read data from Synchronizeable |
---|
87 | */ |
---|
88 | int WriteSync::readBytes(byte* data) |
---|
89 | { |
---|
90 | PRINTF(0)("WriteSync: sent %i bytes of data\n", this->outLength); |
---|
91 | |
---|
92 | /* debug msg */ |
---|
93 | this->readDebug(); |
---|
94 | |
---|
95 | /* write the test message */ |
---|
96 | for( int i = 0; i < this->outLength; i++) |
---|
97 | data[i] = this->outData[i]; |
---|
98 | |
---|
99 | /* return the length of the test */ |
---|
100 | return this->outLength; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | void WriteSync::writeDebug() const |
---|
105 | { |
---|
106 | PRINTF(0)("Write in bytes: \t(0 <-) |"); |
---|
107 | for(int i = 0; i < this->recLength; i++) |
---|
108 | { |
---|
109 | PRINT(0)(" [%u] ",this->inData[i]); |
---|
110 | } |
---|
111 | PRINT(0)("|\n"); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void WriteSync::readDebug() const |
---|
116 | { |
---|
117 | PRINTF(0)("Read out bytes: \t(0 ->) |"); |
---|
118 | for(int i = 0; i < this->outLength; i++) |
---|
119 | { |
---|
120 | PRINT(0)(" [%u] ",this->outData[i]); |
---|
121 | } |
---|
122 | PRINT(0)("|\n"); |
---|
123 | } |
---|