gpt4 book ai didi

java - LibGDX 服务器线程

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:00 25 4
gpt4 key购买 nike

我目前正在使用 libGDX 开发一款类似 MOBA 的游戏,但我在服务器方面遇到了困难。目前,我为每个连接都有一个线程,并有一个存储数据的关键区域。我的问题是,存在线程,它们同时写入临界区域并从中读取。有人可以帮我吗?

这是我的类(class):客户端线程:

package at.mobaserver;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import at.motm.model.GameObject;
import at.motm.model.Projectile;

import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;

public class ClientConnection implements Runnable
{
private ObjectInputStream ois;
private ObjectOutputStream oos;
private Socket socket;
private GameServer server;
private GameObject receivedObject;
private Rectangle r;
private Projectile projectile;
private boolean found;

public ClientConnection(Socket socket, GameServer server)
{
try
{
this.socket = socket;
this.oos = new ObjectOutputStream(socket.getOutputStream());
this.ois = new ObjectInputStream(socket.getInputStream());
this.server = server;
this.receivedObject = null;
this.projectile = null;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public void run()
{
try
{
this.oos.writeLong(Thread.currentThread().getId());
this.oos.flush();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true)
{
try
{
// Add GameObjects to list
// this.server.getMutex().lock();
this.oos.reset();

// Hitdetection
if (this.projectile != null)
{
projectileHitDetection();
}

// Write Objects to client
this.oos.writeObject(this.server.getSendingList());
this.oos.flush();

// Add Objects to Severobject Arraylist
// this.server.getMutex().lock();
addObjectsToList();

// Add Projectiles to Serverobject Arraylist
readProjectiles();
// this.server.getMutex().unlock();
// this.server.getMutex().unlock();

}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
break;
}
}
}

private void readProjectiles() throws ClassNotFoundException, IOException
{
synchronized (this.server.getProjectiles())
{
this.projectile = (Projectile) this.ois.readObject();

if (this.projectile != null)
{
this.found = false;
for (int i = 0; i < this.server.getProjectiles().size(); i++)
{
if (this.server.getProjectiles().get(i).getThreadID() == Thread
.currentThread().getId())
{
this.server.getProjectiles().set(i, this.projectile);
this.found = true;
break;
}
}
if (!this.found)
{
this.server.getProjectiles().add(this.projectile);
}
}
}

}

private void addObjectsToList() throws ClassNotFoundException, IOException
{
synchronized (this.server.getObjects())
{
this.receivedObject = (GameObject) this.ois.readObject();

this.found = false;
for (int i = 0; i < this.server.getObjects().size(); i++)
{
if (this.server.getObjects().get(i).getThreadID() == Thread
.currentThread().getId())
{
this.server.getObjects().set(i, this.receivedObject);
this.found = true;

break;
}
}
if (!this.found)
{
this.server.addObject(this.receivedObject);
}
}

}

private void projectileHitDetection()
{
synchronized (this.server.getObjects())
{
this.r = new Rectangle(this.projectile.getPosition().x,
this.projectile.getPosition().y,
this.projectile.getWidth(), this.projectile.getHeight());

for (int i = 0; i < this.server.getObjects().size(); i++)
{
if (this.server.getObjects().get(0).getTeam() != this.projectile
.getTeam())
{
Rectangle r1 = new Rectangle(this.server.getObjects()
.get(0).getPosition().x, this.server.getObjects()
.get(0).getPosition().y, this.server.getObjects()
.get(0).getWidth(), this.server.getObjects().get(0)
.getHeight());
if (Intersector.overlaps(this.r, r1))
{
System.out.println("test");
this.server.getProjectiles().remove(this.projectile);
break;
}
}
}
}

}
}

关键区域: 包at.mobaserver;

import java.util.ArrayList;

import at.motm.model.GameObject;
import at.motm.model.Projectile;

public class GameServer
{
private ArrayList<GameObject> objects;
private ArrayList<Projectile> projectiles;
private ArrayList<GameObject> sendList;

public GameServer()
{
this.objects = new ArrayList<GameObject>();
this.projectiles = new ArrayList<Projectile>();
this.sendList = new ArrayList<GameObject>();
}

public ArrayList<GameObject> getSendingList()
{

synchronized (this.sendList)
{
this.sendList.clear();
for (GameObject o : this.objects)
{
if (o.isAlive())
{
this.sendList.add(o);
}
}
this.sendList.addAll(this.projectiles);
return this.sendList;
}

}

public ArrayList<GameObject> getObjects()
{
synchronized (this.objects)
{
return this.objects;
}
}

public void setObjects(ArrayList<GameObject> objects)
{
synchronized (objects)
{
this.objects = objects;
}

}

public void addObject(GameObject o)
{
synchronized (this.sendList)
{
this.objects.add(o);
}

}

public ArrayList<Projectile> getProjectiles()
{
synchronized (this.projectiles)
{
return this.projectiles;
}
}

public void setProjectiles(ArrayList<Projectile> projectiles)
{
synchronized (this.projectiles)
{
this.projectiles = projectiles;
}

}

public void addProjectile(Projectile o)
{
synchronized (this.sendList)
{
this.projectiles.add(o);
}

}
}

最佳答案

我已经解决了我的问题。我使用 CopyAndWriteArrayList 而不是普通的 ArrayList。

关于java - LibGDX 服务器线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37778483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com