00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <iosfwd>
00023 #include <queue>
00024 #include <enet/enet.h>
00025
00026 #include "bandwidth.hpp"
00027 #include "messageout.hpp"
00028 #include "netcomputer.hpp"
00029
00030 #include "../defines.h"
00031
00032 #include "../utils/logger.h"
00033 #include "../utils/processorutils.hpp"
00034
00035 NetComputer::NetComputer(ENetPeer *peer):
00036 mPeer(peer)
00037 {
00038 }
00039
00040 bool NetComputer::isConnected()
00041 {
00042 return (mPeer->state == ENET_PEER_STATE_CONNECTED);
00043 }
00044
00045 void NetComputer::disconnect(const MessageOut &msg)
00046 {
00047 if (isConnected())
00048 {
00049
00050
00051
00052
00053 send(msg, ENET_PACKET_FLAG_RELIABLE, 0xFF);
00054
00055
00056
00057
00058 enet_peer_disconnect(mPeer, 0);
00059 }
00060 }
00061
00062 void NetComputer::send(const MessageOut &msg, bool reliable,
00063 unsigned int channel)
00064 {
00065 LOG_DEBUG("Sending message " << msg << " to " << *this);
00066
00067 gBandwidth->increaseClientOutput(this, msg.getLength());
00068
00069 ENetPacket *packet;
00070 packet = enet_packet_create(msg.getData(),
00071 msg.getLength(),
00072 reliable ? ENET_PACKET_FLAG_RELIABLE : 0);
00073
00074 if (packet)
00075 {
00076 enet_peer_send(mPeer, channel, packet);
00077 }
00078 else
00079 {
00080 LOG_ERROR("Failure to create packet!");
00081 }
00082 }
00083
00084 std::ostream &operator <<(std::ostream &os, const NetComputer &comp)
00085 {
00086
00087 if (utils::processor::isLittleEndian)
00088 os << ( comp.mPeer->address.host & 0x000000ff) << "."
00089 << ((comp.mPeer->address.host & 0x0000ff00) >> 8) << "."
00090 << ((comp.mPeer->address.host & 0x00ff0000) >> 16) << "."
00091 << ((comp.mPeer->address.host & 0xff000000) >> 24);
00092 else
00093
00094
00095 os << ((comp.mPeer->address.host & 0xff000000) >> 24) << "."
00096 << ((comp.mPeer->address.host & 0x00ff0000) >> 16) << "."
00097 << ((comp.mPeer->address.host & 0x0000ff00) >> 8) << "."
00098 << ((comp.mPeer->address.host & 0x000000ff));
00099
00100 return os;
00101 }
00102
00103 int NetComputer::getIP() const
00104 {
00105 return mPeer->address.host;
00106 }