Mana
Loading...
Searching...
No Matches
connection.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "log.h"
25
28
29#include <string>
30
31namespace ManaServ
32{
33
34Connection::Connection(ENetHost *client):
35 mClient(client)
36{
38}
39
44
45bool Connection::connect(const std::string &address, enet_uint16 port)
46{
47 Log::info("Net::Connection::connect(%s, %i)", address.c_str(), port);
48 if (mConnection)
49 disconnect();
50
51 if (address.empty())
52 {
53 Log::info("Net::Connection::connect() got empty address!");
55 return false;
56 }
57
58 ENetAddress enetAddress;
59
60 enet_address_set_host(&enetAddress, address.c_str());
61 enetAddress.port = port;
62
63 // Initiate the connection, allocating channel 0.
64 mConnection = enet_host_connect(mClient, &enetAddress, 1, 0);
65
66 if (!mConnection)
67 {
68 Log::info("Unable to initiate connection to the server.");
70 return false;
71 }
72
73 mPort = port;
74
75 return true;
76}
77
79{
80 if (!mConnection)
81 return;
82
83 enet_peer_disconnect(mConnection, 0);
84 enet_host_flush(mClient);
85 enet_peer_reset(mConnection);
86
87 mConnection = nullptr;
88}
89
91{
92 return mConnection && mConnection->state == ENET_PEER_STATE_CONNECTED;
93}
94
96{
97 if (!isConnected())
98 {
99 Log::warn("Cannot send message to not connected server!");
100 return;
101 }
102
103 ENetPacket *packet = enet_packet_create(msg.getData(),
104 msg.getDataSize(),
105 ENET_PACKET_FLAG_RELIABLE);
106 enet_peer_send(mConnection, 0, packet);
107}
108
109} // namespace ManaServ
bool isConnected()
Returns whether the server is connected.
void send(const ManaServ::MessageOut &msg)
Sends a message.
bool connect(const std::string &address, enet_uint16 port)
Connects to the given server with the specified address and port.
ENetPeer * mConnection
Definition connection.h:73
Connection(ENetHost *client)
void disconnect()
Disconnects from the given server.
Used for building an outgoing message to manaserv.
Definition messageout.h:37
char * getData() const
Returns the content of the message.
Definition messageout.h:67
unsigned int getDataSize() const
Returns the length of the data.
Definition messageout.h:72
void warn(const char *log_text,...) LOG_PRINTF_ATTR
void info(const char *log_text,...) LOG_PRINTF_ATTR
int connections
Definition internal.cpp:26