aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/ServerConnection.cpp
blob: 07616aa4704c62f9fd2d5cdc34c9088cfffe19fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "stdafx.h"
#include "Options.h"
#include "ServerConnection.h"

#include <memory>
#include "PendingConnection.h"
#include "PlayerConnection.h"
#include "ServerPlayer.h"
#include "..\Minecraft.World\net.minecraft.network.h"
#include "..\Minecraft.World\Socket.h"
#include "..\Minecraft.World\net.minecraft.world.level.h"
#include "MultiPlayerLevel.h"

ServerConnection::ServerConnection(MinecraftServer *server)
{
	// 4J - added initialiser
	connectionCounter = 0;
	InitializeCriticalSection(&pending_cs);

	this->server = server;
}

ServerConnection::~ServerConnection()
{
	DeleteCriticalSection(&pending_cs);
}

// 4J - added to handle incoming connections, to replace thread that original used to have
void ServerConnection::NewIncomingSocket(Socket *socket)
{
	shared_ptr<PendingConnection> unconnectedClient = std::make_shared<PendingConnection>(server, socket, L"Connection #" + std::to_wstring(connectionCounter++));
	handleConnection(unconnectedClient);
}

void ServerConnection::addPlayerConnection(shared_ptr<PlayerConnection> uc)
{
	players.push_back(uc);
}

void ServerConnection::handleConnection(shared_ptr<PendingConnection> uc)
{
	EnterCriticalSection(&pending_cs);
	pending.push_back(uc);
	LeaveCriticalSection(&pending_cs);
}

void ServerConnection::stop()
{
	EnterCriticalSection(&pending_cs);
    for (unsigned int i = 0; i < pending.size(); i++)
	{
        shared_ptr<PendingConnection> uc = pending[i];
        uc->connection->close(DisconnectPacket::eDisconnect_Closed);
    }
	LeaveCriticalSection(&pending_cs);

    for (unsigned int i = 0; i < players.size(); i++)
	{
        shared_ptr<PlayerConnection> player = players[i];
        player->connection->close(DisconnectPacket::eDisconnect_Closed);
    }
}

void ServerConnection::tick()
{
	{
		// MGH - changed this so that the the CS lock doesn't cover the tick (was causing a lockup when 2 players tried to join)
		EnterCriticalSection(&pending_cs);
		vector< shared_ptr<PendingConnection> > tempPending = pending;
		LeaveCriticalSection(&pending_cs);

		for (unsigned int i = 0; i < tempPending.size(); i++)
		{
			shared_ptr<PendingConnection> uc = tempPending[i];
	//        try {	// 4J - removed try/catch
				uc->tick();
	//        } catch (Exception e) {
	//            uc.disconnect("Internal server error");
	//            logger.log(Level.WARNING, "Failed to handle packet: " + e, e);
	//        }
			if(uc->connection != NULL) uc->connection->flush();
		}
	}

	// now remove from the pending list
	EnterCriticalSection(&pending_cs);
	for (unsigned int i = 0; i < pending.size(); i++)
	if (pending[i]->done)
	{
		pending.erase(pending.begin()+i);
		i--;
	}
	LeaveCriticalSection(&pending_cs);

    for (unsigned int i = 0; i < players.size(); i++)
	{
        shared_ptr<PlayerConnection> player = players[i];
		shared_ptr<ServerPlayer> serverPlayer = player->getPlayer();
		if( serverPlayer )
		{
			serverPlayer->updateFrameTick();
			serverPlayer->doChunkSendingTick(false);
		}
        player->tick();
        if (player->done)
		{
            players.erase(players.begin()+i);
			i--;
        }
        player->connection->flush();
    }

}

bool ServerConnection::addPendingTextureRequest(const wstring &textureName)
{
    auto it = find(m_pendingTextureRequests.begin(), m_pendingTextureRequests.end(), textureName);
    if( it == m_pendingTextureRequests.end() )
	{
		m_pendingTextureRequests.push_back(textureName);
		return true;
	}

	// 4J Stu - We want to request this texture from everyone, if we have a duplicate it's most likely because the first person we asked for it didn't have it
	// eg They selected a skin then deleted the skin pack. The side effect of this change is that in certain cases we can send a few more requests, and receive
	// a few more responses if people join with the same skin in a short space of time
	return true;
}

void ServerConnection::handleTextureReceived(const wstring &textureName)
{
    auto it = find(m_pendingTextureRequests.begin(), m_pendingTextureRequests.end(), textureName);
    if( it != m_pendingTextureRequests.end() )
	{
		m_pendingTextureRequests.erase(it);
	}
	for (auto& player : players)
	{
        if (!player->done)
		{
			player->handleTextureReceived(textureName);
        }
    }
}

void ServerConnection::handleTextureAndGeometryReceived(const wstring &textureName)
{
    auto it = find(m_pendingTextureRequests.begin(), m_pendingTextureRequests.end(), textureName);
    if( it != m_pendingTextureRequests.end() )
	{
		m_pendingTextureRequests.erase(it);
	}
	for (auto& player : players)
	{
		if (!player->done)
		{
			player->handleTextureAndGeometryReceived(textureName);
		}
	}
}

void ServerConnection::handleServerSettingsChanged(shared_ptr<ServerSettingsChangedPacket> packet)
{
	Minecraft *pMinecraft = Minecraft::GetInstance();

	if(packet->action==ServerSettingsChangedPacket::HOST_DIFFICULTY)
	{
		for(unsigned int i = 0; i < pMinecraft->levels.length; ++i)
		{
			if( pMinecraft->levels[i] != NULL )
			{
				app.DebugPrintf("ClientConnection::handleServerSettingsChanged - Difficulty = %d",packet->data);
				pMinecraft->levels[i]->difficulty = packet->data;
			}
		}
	}
// 	else if(packet->action==ServerSettingsChangedPacket::HOST_IN_GAME_SETTINGS)// options
// 	{
// 		app.SetGameHostOption(eGameHostOption_All,packet->m_serverSettings)
// 	}
// 	else
// 	{
// 		unsigned char ucData=(unsigned char)packet->data;
// 		if(ucData&1)
// 		{
// 			// hide gamertags
// 			pMinecraft->options->SetGamertagSetting(true);
// 		}
// 		else
// 		{
// 			pMinecraft->options->SetGamertagSetting(false);
// 		}
//
// 		for (unsigned int i = 0; i < players.size(); i++)
// 		{
// 			shared_ptr<PlayerConnection> playerconnection = players[i];
// 			playerconnection->setShowOnMaps(pMinecraft->options->GetGamertagSetting());
// 		}
// 	}
}

vector< shared_ptr<PlayerConnection> >  * ServerConnection::getPlayers()
{
	return &players;
}