[9293] | 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 | */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | #include "synchronizeable_ip.h" |
---|
| 17 | #include "converter.h" |
---|
[9386] | 18 | #include "netdefs.h" |
---|
| 19 | #include "ip.h" |
---|
| 20 | #include <cassert> |
---|
[9293] | 21 | |
---|
| 22 | /** |
---|
| 23 | * standard constructor |
---|
| 24 | * @todo this constructor is not jet implemented - do it |
---|
| 25 | */ |
---|
[9334] | 26 | SynchronizeableIP::SynchronizeableIP( IP * ptrIn, IP * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 2*INTSIZE, permission, priority ) |
---|
[9293] | 27 | { |
---|
| 28 | this->vPtrIn = ptrIn; |
---|
| 29 | this->vPtrOut = ptrOut; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * standard deconstructor |
---|
| 35 | */ |
---|
| 36 | SynchronizeableIP::~SynchronizeableIP () |
---|
| 37 | { |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * write var data to byte buffer |
---|
| 42 | * @param buf pointer to write to |
---|
| 43 | * @param maxLength writeToBuf will not write more than maxLength bytes |
---|
| 44 | * @return number bytes written |
---|
| 45 | */ |
---|
| 46 | int SynchronizeableIP::writeToBuf( byte * buf, int maxLength ) |
---|
| 47 | { |
---|
| 48 | int n = 0; |
---|
| 49 | int res; |
---|
| 50 | |
---|
[9344] | 51 | res = Converter::intToByteArray( (int)vPtrIn->host(), buf, maxLength ); |
---|
[9294] | 52 | assert(res > 0); |
---|
| 53 | n += res; |
---|
[9293] | 54 | |
---|
[9360] | 55 | |
---|
[9363] | 56 | res = Converter::intToByteArray( (int)vPtrIn->port(), buf + n, maxLength); |
---|
[9294] | 57 | assert(res > 0); |
---|
| 58 | n += res; |
---|
| 59 | |
---|
| 60 | assert( 2 * INTSIZE == n); |
---|
| 61 | |
---|
[9293] | 62 | return n; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * read var data from byte buffer |
---|
| 67 | * @param buf pointer to read from |
---|
| 68 | * @param maxLength readFromBuf will not read more than maxLength bytes |
---|
| 69 | * @return number bytes read |
---|
| 70 | */ |
---|
| 71 | int SynchronizeableIP::readFromBuf( byte * buf, int maxLength ) |
---|
| 72 | { |
---|
[9294] | 73 | assert( maxLength >= 2 * INTSIZE); |
---|
[9293] | 74 | |
---|
[9294] | 75 | int host, port; |
---|
[9293] | 76 | |
---|
| 77 | int res; |
---|
| 78 | int n = 0; |
---|
| 79 | |
---|
[9344] | 80 | IP oldVal = *vPtrOut; |
---|
[9294] | 81 | |
---|
| 82 | res = Converter::byteArrayToInt( buf + n, &host); |
---|
| 83 | assert( res > 0); |
---|
| 84 | n += res; |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | res = Converter::byteArrayToInt( buf + n, &port); |
---|
| 88 | assert( res > 0); |
---|
| 89 | n += res; |
---|
| 90 | |
---|
[9334] | 91 | *this->vPtrOut = IP(host, port); |
---|
[9294] | 92 | |
---|
[9360] | 93 | this->setHasChanged( *this->vPtrOut != oldVal); |
---|
[9294] | 94 | |
---|
[9365] | 95 | assert( n == 2 * INTSIZE); |
---|
[9294] | 96 | |
---|
[9293] | 97 | return n; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * print out variable value |
---|
| 103 | */ |
---|
| 104 | void SynchronizeableIP::SynchronizeableIP::debug( ) |
---|
| 105 | { |
---|
[9344] | 106 | printf("SYNCHRONIZEABLE_VAR: %s IN: %i, %i OUT: %i, %i\n", name.c_str(), vPtrIn->host(), vPtrIn->port(), vPtrOut->host(), vPtrOut->port()); |
---|
[9293] | 107 | } |
---|
| 108 | |
---|