public ServerResponseType HandleMessage(Backend.Backend backend, ServerMessageType message, string session, BinaryReader datareader, BinaryWriter datawriter) { if (backend.IsMultiplayer) { return(ServerResponseType.REQ_ERROR); } var ipaddr = datareader.ReadUInt32(); ushort port = 80; if (datareader.BaseStream.Length - datareader.BaseStream.Position >= 2) { port = datareader.ReadUInt16(); } if (_ip.GrabEntity(ipaddr) == null) { return(ServerResponseType.REQ_ERROR); } var entity = _entityManager.GetPlayerEntityId(session); var playerIp = _ip.FetchAllIPs(entity); if (playerIp.Length == 0) { return(ServerResponseType.REQ_ERROR); } try { _ip.MakeConnection(ipaddr, playerIp[0].Address, port); return(ServerResponseType.REQ_SUCCESS); } catch { return(ServerResponseType.REQ_ERROR); } }
internal void StartLocalServer() { _localBackend = new Backend.Backend(3252, false, Path.Combine(_appdata.GamePath, "world")); _localBackend.Listen(); _localBackend.ServerReady.WaitOne(); Logger.Log("Starting internal single-player server."); var result = _server.Connect("localhost:3252"); result.Wait(); Logger.Log("*** Connected To Internal Server ***", System.ConsoleColor.Green); if (result.Result.Result != ConnectionResultType.Success) { throw new Exception("An error has occurred starting the internal server."); } EnsureProperEnvironment(); }
public BroadcastingStream(Backend backend, string player) { _backend = backend; _player = player; }
/// <inheritdoc/> public ServerResponseType HandleMessage(Backend backend, ServerMessageType message, string session, BinaryReader datareader, BinaryWriter datawriter) { datawriter.Write(backend.IsMultiplayer); return(ServerResponseType.REQ_SUCCESS); }
public ServerResponseType HandleMessage(Backend backend, ServerMessageType message, string session, BinaryReader datareader, BinaryWriter datawriter) { // The easy part: authorising. var id = datareader.ReadInt32(); var fail = false; fail = id < 0 || id >= holder.info.Count; // Stream ID out of range Tuple <Stream, string> s = null; if (!fail) { s = holder.info[id]; fail = s == null; // Stream ID nulled (closed already) } if (!fail) { fail = s.Item2 != session; // Stream doesn't belong to accessing user } if (fail) { Logger.Log("failed"); // We give the same error in all cases so the user can't figure anything out // by making deliberately dodgy requests datawriter.Write($"Could not get a handle on stream {id} - nonexistent or not authorised."); return(ServerResponseType.REQ_ERROR); } var opi = datareader.ReadByte(); if (!Enum.IsDefined(typeof(StreamOp), opi)) // Make sure the byte is in range of the enum { Logger.Log("invalid stream operation"); datawriter.Write($"Invalid stream operation {opi}."); return(ServerResponseType.REQ_ERROR); } var op = (StreamOp)opi; // We know it's valid now, so get the enum entry MethodInfo fun; dynamic ret; try { switch (op) // special cases { case StreamOp.Close: s.Item1.Dispose(); holder.info[id] = null; return(ServerResponseType.REQ_SUCCESS); case StreamOp.ReadByte: datawriter.Write(s.Item1.ReadByte()); return(ServerResponseType.REQ_SUCCESS); case StreamOp.WriteByte: s.Item1.WriteByte((byte)datareader.ReadInt32()); return(ServerResponseType.REQ_SUCCESS); case StreamOp.Read: var r = new byte[datareader.ReadInt32()]; datawriter.Write(s.Item1.Read(r, 0, r.Length)); datawriter.Write(r.Length); datawriter.Write(r); return(ServerResponseType.REQ_SUCCESS); case StreamOp.Write: var w = datareader.ReadBytes(datareader.ReadInt32()); s.Item1.Write(w, 0, w.Length); return(ServerResponseType.REQ_SUCCESS); } // Look up the method from its enum entry. fun = typeof(Stream).GetMethod(op.ToString()); // Call it and get the result. // The Select finds a BinaryReader method that reads each argument type. ret = fun.Invoke(s.Item1, fun.GetParameters().Select(p => typeof(BinaryReader).GetMethods().First(m => m.Name.Length > 4 && m.Name.StartsWith("Read", StringComparison.InvariantCulture) && m.ReturnType == (p.ParameterType.IsEnum ? Enum.GetUnderlyingType(p.ParameterType) : p.ParameterType)).Invoke(datareader, null)).ToArray()); } catch (Exception ex) { // Not sending back the exception, who knows what's in it? Logger.Log(ex.ToString()); datawriter.Write($"Internal error."); return(ServerResponseType.REQ_ERROR); } if (fun.ReturnType != typeof(void)) { ((dynamic)datawriter).Write(ret); } return(ServerResponseType.REQ_SUCCESS); }
public ServerResponseType HandleMessage(Backend backend, ServerMessageType message, string session, BinaryReader datareader, BinaryWriter datawriter) { datawriter.Write(rstreams.Create(new MemoryStream(Encoding.UTF8.GetBytes("SOMETHING SOMETHING IS A TESTAMENT TO WHY THE WAYS OF THE BLIND WILL NEVER GET")), session)); return(ServerResponseType.REQ_SUCCESS); }