void Connection::serve(u16 port)
{
dout_con<<getDesc()<<" serving at port "<<port<<std::endl;
- m_socket.Bind(port);
- m_peer_id = PEER_ID_SERVER;
+ try{
+ m_socket.Bind(port);
+ m_peer_id = PEER_ID_SERVER;
+ }
+ catch(SocketException &e){
+ // Create event
+ ConnectionEvent e;
+ e.bindFailed();
+ putEvent(e);
+ }
}
void Connection::connect(Address address)
if(m_bc_peerhandler)
m_bc_peerhandler->deletingPeer(&tmp, e.timeout);
continue; }
+ case CONNEVENT_BIND_FAILED:
+ throw ConnectionBindFailed("Failed to bind socket "
+ "(port already in use?)");
}
}
throw NoIncomingDataException("No incoming data");
{}
};
+class ConnectionBindFailed : public BaseException
+{
+public:
+ ConnectionBindFailed(const char *s):
+ BaseException(s)
+ {}
+};
+
/*class ThrottlingException : public BaseException
{
public:
CONNEVENT_DATA_RECEIVED,
CONNEVENT_PEER_ADDED,
CONNEVENT_PEER_REMOVED,
+ CONNEVENT_BIND_FAILED,
};
struct ConnectionEvent
return "CONNEVENT_PEER_ADDED";
case CONNEVENT_PEER_REMOVED:
return "CONNEVENT_PEER_REMOVED";
+ case CONNEVENT_BIND_FAILED:
+ return "CONNEVENT_BIND_FAILED";
}
return "Invalid ConnectionEvent";
}
timeout = timeout_;
address = address_;
}
+ void bindFailed()
+ {
+ type = CONNEVENT_BIND_FAILED;
+ }
};
enum ConnectionCommandType{
{
infostream<<"Server: PeerNotFoundException"<<std::endl;
}
+ catch(con::ConnectionBindFailed &e)
+ {
+ m_server->setAsyncFatalError(e.what());
+ }
}
END_DEBUG_EXCEPTION_HANDLER(errorstream)
m_path_world(path_world),
m_path_config(path_config),
m_gamespec(gamespec),
+ m_async_fatal_error(""),
m_env(NULL),
m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
m_authmanager(path_world+DIR_DELIM+"auth.txt"),
m_step_dtime_mutex.Init();
m_step_dtime = 0.0;
+ if(path_world == "")
+ throw ServerError("Supplied empty world path");
+
if(!gamespec.isValid())
throw ServerError("Supplied invalid gamespec");
void Server::start(unsigned short port)
{
DSTACK(__FUNCTION_NAME);
+ infostream<<"Starting server on port "<<port<<"..."<<std::endl;
+
// Stop thread if already running
m_thread.stop();
JMutexAutoLock lock(m_step_dtime_mutex);
m_step_dtime += dtime;
}
+ // Throw if fatal error occurred in thread
+ std::string async_err = m_async_fatal_error.get();
+ if(async_err != ""){
+ throw ServerError(async_err);
+ }
}
void Server::AsyncRunStep()
std::string getWorldPath(){ return m_path_world; }
+ void setAsyncFatalError(const std::string &error)
+ {
+ m_async_fatal_error.set(error);
+ }
+
private:
// con::PeerHandler implementation.
// Equivalent of /usr/share/minetest/server
std::string m_path_share;
+ // Thread can set; step() will throw as ServerError
+ MutexedVariable<std::string> m_async_fatal_error;
+
// Some timers
float m_liquid_transform_timer;
float m_print_info_timer;