rme_net.cpp

main 190 lines · 4.5 KB Raw
Alex Alex Commit Map Editor 18/10/2026 23:40
1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#include "main.h"
19
20#if 0
21
22 #include "rmenet.h"
23
24const wxEventType EVT_RMENET_CONNECTION_ESTABLISHED = wxNewEventType();
25const wxEventType EVT_RMENET_CONNECTION_LOST = wxNewEventType();
26const wxEventType EVT_RMENET_SHOW_SERVLIST = wxNewEventType();
27
28
29RMENet::RMENet(wxEvtHandler* event_dump) :
30 event_dump(event_dump)
31{
32}
33
34RMENet::~RMENet()
35{
36}
37
38bool RMENet::Connect()
39{
40 wxIPV4address ipaddr;
41
42 ipaddr.Hostname("127.0.0.1");//90.230.54.138"));
43 ipaddr.Service(31312);
44
45 socket = newd wxSocketClient(wxSOCKET_NOWAIT);
46
47 connection = newd NetworkConnection(this, socket);
48 socket->SetClientData(connection);
49
50 socket->SetEventHandler(*this, wxID_ANY);
51 socket->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG | wxSOCKET_LOST_FLAG);
52 socket->Notify(true);
53
54 wxEvtHandler::Connect(wxID_ANY, wxEVT_SOCKET, wxSocketEventHandler(RMENet::HandleEvent));
55
56 socket->Connect(ipaddr, false);
57 if(!socket || !socket->WaitOnConnect(5, 0) ||
58 !socket || !socket->IsConnected())
59 {
60 if(socket)
61 socket->Destroy();
62 socket = nullptr;
63 delete connection;
64 connection = nullptr;
65 return false;
66 }
67
68 NetworkMessage* nmsg = AllocMessage();
69 nmsg->AddByte(0x00); // Hello!
70 nmsg->AddU32(__LIVE_NET_VERSION__);
71 nmsg->AddString(g_settings.getString(Config::LIVE_USERNAME));
72 nmsg->AddString(g_settings.getString(Config::LIVE_PASSWORD));
73 connection->Send(nmsg);
74
75 return true;
76}
77
78void RMENet::Close()
79{
80 delete connection;
81 connection = nullptr;
82 if(socket) {
83 socket->Destroy();
84 socket = nullptr;
85 }
86
87 // Delayed destruction: the socket will be deleted during the next
88 // idle loop iteration. This ensures that all pending events have
89 // been processed.
90 //m_beingDeleted = true;
91
92 // schedule this object for deletion
93 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : nullptr;
94 if( traits ) {
95 // let the traits object decide what to do with us
96 traits->ScheduleForDestroy(this);
97 } else { // no app or no traits
98 // in wxBase we might have no app object at all, don't leak memory
99 delete this;
100 }
101}
102
103void RMENet::HandleEvent(wxSocketEvent& evt)
104{
105 NetworkConnection* connection = reinterpret_cast<NetworkConnection*>(evt.GetClientData());
106 switch(evt.GetSocketEvent())
107 {
108 case wxSOCKET_LOST:
109 // Connection was lost, either client disconnecting, or our socket breaking
110 {
111 // Generate event
112 wxCommandEvent event(EVT_RMENET_CONNECTION_LOST);
113 event.SetInt(0);
114 event.SetString("GOOOFU");
115 event_dump->AddPendingEvent(event);
116 } break;
117 case wxSOCKET_OUTPUT:
118 // We're ready to write to a socket
119 {
120 // Send waiting data
121 connection->Send();
122 } break;
123 case wxSOCKET_INPUT:
124 // We got some data to be read.
125 {
126 NetworkMessage* nmsg = connection->Receive();
127 if(nmsg)
128 {
129 try
130 {
131 OnParsePacket(nmsg);
132 FreeMessage(nmsg);
133 }
134 catch(std::runtime_error&)
135 {
136 FreeMessage(nmsg);
137 Close();
138 }
139 }
140 } break;
141 }
142}
143
144
145
146void RMENet::OnParsePacket(NetworkMessage* nmsg)
147{
148 switch(nmsg->ReadByte()) {
149 case 0x00: // welcome
150 {
151 OnReceiveWelcome(nmsg);
152 } break;
153 case 0x01: // Disconnect
154 {
155 OnReceiveDisconnect(nmsg);
156 } break;
157 case 0x02: // Ping
158 {
159 OnReceivePing(nmsg);
160 } break;
161 }
162}
163
164
165void RMENet::OnReceiveWelcome(NetworkMessage* nmsg)
166{
167 can_host = nmsg->ReadU8() == 1;
168
169 wxCommandEvent event(EVT_RMENET_CONNECTION_ESTABLISHED);
170 event_dump->AddPendingEvent(event);
171}
172
173void RMENet::OnReceiveDisconnect(NetworkMessage *nmsg)
174{
175 wxCommandEvent event(EVT_RMENET_CONNECTION_LOST, wxID_ANY);
176 event.SetString(wxstr(nmsg->ReadString()));
177 event.SetInt(1);
178 event_dump->AddPendingEvent(event);
179}
180
181void RMENet::OnReceivePing(NetworkMessage *nmsg)
182{
183 // Send pong
184 NetworkMessage* omsg = AllocMessage();
185 omsg->AddByte(0x02);
186 connection->Send(omsg);
187}
188
189#endif
190
Top