Skip to content

Commit 9542f05

Browse files
authored
Merge pull request #35 from SharkooMaster/main
Added bases for client-server architecture
2 parents 813cc09 + 98f576b commit 9542f05

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

.idea/libraries/gson_2_9_0.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaGameEngine.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<orderEntry type="sourceFolder" forTests="false" />
1515
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
1616
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-script-runtime:1.6.10" level="project" />
17+
<orderEntry type="library" name="gson-2.9.0" level="project" />
1718
</component>
1819
</module>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package javagameengine.networking;
2+
3+
import javagameengine.msc.Vector2;
4+
5+
import java.util.LinkedList;
6+
7+
class Header
8+
{
9+
String clientId;
10+
String custom; // Pack data into a string.
11+
}
12+
13+
class Body
14+
{
15+
LinkedList<obVec> deltaPos = new LinkedList<obVec>();
16+
}
17+
18+
class obVec
19+
{
20+
String ID;
21+
Vector2 vec;
22+
}
23+
24+
public class BufferData {
25+
Header h;
26+
Body b;
27+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package javagameengine.networking;
2+
3+
import com.google.gson.Gson;
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.net.ServerSocket;
10+
import java.net.Socket;
11+
12+
class socket_handler implements Runnable {
13+
public Thread t;
14+
public ServerSocket socket; // Socket to listen on
15+
public Socket i_socket; // Start listening on socket
16+
17+
@Override
18+
public void run() {
19+
20+
}
21+
22+
public socket_handler(String _lineup_id)
23+
{
24+
t = new Thread(this, "t_client_" + _lineup_id);
25+
t.start();
26+
try {
27+
socket = new ServerSocket(8074);
28+
i_socket = socket.accept();
29+
} catch (IOException e) {
30+
throw new RuntimeException(e);
31+
}
32+
}
33+
34+
public InputStream input;
35+
public BufferedReader reader;
36+
37+
public BufferData read_client()
38+
{
39+
String data;
40+
BufferData parsed = new BufferData();
41+
try {
42+
input = i_socket.getInputStream();
43+
44+
reader = new BufferedReader(new InputStreamReader(input));
45+
data = reader.readLine();
46+
47+
Gson json_parser = new Gson();
48+
49+
parsed = json_parser.fromJson(data, BufferData.class);
50+
} catch (IOException e) {
51+
throw new RuntimeException(e);
52+
}
53+
return parsed;
54+
}
55+
}
56+
57+
public class Tcp_server extends Thread {
58+
59+
public Tcp_server()
60+
{
61+
Tcp_server thread = new Tcp_server();
62+
thread.start();
63+
}
64+
65+
// Connected lineup ** List of connected clients **
66+
public int lineup_id = 0;
67+
68+
public String client_joined()
69+
{
70+
lineup_id = lineup_id++;
71+
return Integer.toString(lineup_id);
72+
}
73+
74+
public void client_left()
75+
{
76+
// We don't handle any queues. Meaning it doesn't matter what the ID is as long as they don't conflict.
77+
// So this is just a security measure to make sure that it doesn't happen.
78+
lineup_id = lineup_id++;
79+
}
80+
81+
public void listenFor_lineup()
82+
{
83+
84+
}
85+
86+
public void read_lineup()
87+
{
88+
}
89+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
############################################
3+
MULTIPLAYER
4+
############################################
5+
6+
[Client - Server]
7+
8+
-> Start server (create socket)
9+
-> Create buffer listener on socket
10+
11+
**********
12+
{READER}
13+
-> Read string from client
14+
-> Convert from JSON to class
15+
{{{ (Aforementioned class):
16+
{
17+
Header: {
18+
ClientID: ...
19+
Custom: /* Anything the developer would like to know which is separate from the game events */
20+
}
21+
Body: {
22+
deltaPos: [{objectID, vector2}] /* To include changes in objects position (Update) */
23+
}
24+
}
25+
}}}
26+
// The aforementioned class is the same one used for sending data

0 commit comments

Comments
 (0)