Skip to content

Commit ea7fe80

Browse files
committed
refactor(channel): Simply how packets are handled
fix(packet): Fix misreporting of packet sizes in one of the write functions
1 parent 0869d28 commit ea7fe80

30 files changed

+1333
-1759
lines changed

.github/workflows/maven-publish.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ jobs:
1414
runs-on: ubuntu-latest
1515

1616
steps:
17-
- uses: actions/checkout@v2
18-
- name: Set up JDK 1.8
19-
uses: actions/setup-java@v1
17+
- uses: actions/checkout@v4
18+
- name: Set up JDK 17
19+
uses: actions/setup-java@v4
2020
with:
21-
java-version: 1.8
21+
distribution: 'temurin'
22+
java-version: 17
2223
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
2324
settings-path: ${{ github.workspace }} # location for the settings.xml file
2425

.github/workflows/maven-test.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v2
19-
- name: Set up JDK 1.8
20-
uses: actions/setup-java@v1
18+
- uses: actions/checkout@v4
19+
- name: Set up JDK 17
20+
uses: actions/setup-java@v4
2121
with:
22-
java-version: 1.8
22+
distribution: 'temurin'
23+
java-version: 17
2324
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
2425
settings-path: ${{ github.workspace }} # location for the settings.xml file
2526

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public class Main
2323
CoolSocket coolSocket = new CoolSocket(port)
2424
{
2525
@Override
26-
public void onConnected(ActiveConnection activeConnection)
26+
public void onConnected(ActiveConnection channel)
2727
{
2828
try {
29-
activeConnection.reply("Hello!");
30-
System.out.println(activeConnection.receive().getAsString()); // Merhaba!
29+
channel.reply("Hello!");
30+
System.out.println(channel.receive().getAsString()); // Merhaba!
3131
} catch (IOException e) {
3232
e.printStackTrace();
3333
}
@@ -36,9 +36,9 @@ public class Main
3636

3737
coolSocket.start();
3838

39-
try (ActiveConnection activeConnection = ActiveConnection.connect(new InetSocketAddress(port), 0)) {
40-
System.out.println(activeConnection.receive().getAsString()); // "Hello!"
41-
activeConnection.reply("Merhaba!");
39+
try (ActiveConnection channel = ActiveConnection.connect(new InetSocketAddress(port), 0)) {
40+
System.out.println(channel.receive().getAsString()); // "Hello!"
41+
channel.reply("Merhaba!");
4242
} finally {
4343
coolSocket.stop();
4444
}

pom.xml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
<url>https://github.com/libcoolsocket/java</url>
3131
</scm>
3232
<properties>
33-
<maven.compiler.source>1.8</maven.compiler.source>
34-
<maven.compiler.target>1.8</maven.compiler.target>
33+
<maven.compiler.source>17</maven.compiler.source>
34+
<maven.compiler.target>17</maven.compiler.target>
3535
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3636
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
3737
</properties>
@@ -40,7 +40,7 @@
4040
<plugin>
4141
<groupId>org.apache.maven.plugins</groupId>
4242
<artifactId>maven-jar-plugin</artifactId>
43-
<version>3.2.0</version>
43+
<version>3.4.2</version>
4444
<executions>
4545
<execution>
4646
<goals>
@@ -52,18 +52,18 @@
5252
<plugin>
5353
<groupId>org.apache.maven.plugins</groupId>
5454
<artifactId>maven-compiler-plugin</artifactId>
55-
<version>3.8.1</version>
55+
<version>3.13.0</version>
5656
<configuration>
57-
<source>1.8</source>
58-
<target>1.8</target>
57+
<source>17</source>
58+
<target>17</target>
5959
</configuration>
6060
</plugin>
6161
<plugin>
6262
<groupId>org.apache.maven.plugins</groupId>
6363
<artifactId>maven-javadoc-plugin</artifactId>
64-
<version>3.1.1</version>
64+
<version>3.8.0</version>
6565
<configuration>
66-
<source>8</source>
66+
<source>17</source>
6767
<show>private</show>
6868
<nohelp>true</nohelp>
6969
<javadocExecutable>/usr/bin/javadoc</javadocExecutable>
@@ -80,7 +80,7 @@
8080
<plugin>
8181
<groupId>org.apache.maven.plugins</groupId>
8282
<artifactId>maven-source-plugin</artifactId>
83-
<version>3.0.1</version>
83+
<version>3.3.1</version>
8484
<executions>
8585
<execution>
8686
<id>attach-sources</id>
@@ -93,7 +93,7 @@
9393
<plugin>
9494
<groupId>org.apache.maven.plugins</groupId>
9595
<artifactId>maven-release-plugin</artifactId>
96-
<version>2.5.2</version>
96+
<version>3.1.1</version>
9797
<configuration>
9898
<releaseProfiles>release</releaseProfiles>
9999
</configuration>
@@ -121,7 +121,7 @@
121121
<plugin>
122122
<groupId>org.apache.maven.plugins</groupId>
123123
<artifactId>maven-gpg-plugin</artifactId>
124-
<version>1.6</version>
124+
<version>3.2.3</version>
125125
<executions>
126126
<execution>
127127
<id>sign-artifacts</id>
@@ -135,7 +135,7 @@
135135
<plugin>
136136
<groupId>org.sonatype.plugins</groupId>
137137
<artifactId>nexus-staging-maven-plugin</artifactId>
138-
<version>1.6.7</version>
138+
<version>1.7.0</version>
139139
<extensions>true</extensions>
140140
<configuration>
141141
<serverId>ossrh</serverId>
@@ -157,11 +157,6 @@
157157
</profile>
158158
</profiles>
159159
<dependencies>
160-
<dependency>
161-
<groupId>org.json</groupId>
162-
<artifactId>json</artifactId>
163-
<version>20230618</version>
164-
</dependency>
165160
<dependency>
166161
<groupId>junit</groupId>
167162
<artifactId>junit</artifactId>

src/main/java/org/monora/coolsocket/core/client/ClientHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.monora.coolsocket.core.client;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.monora.coolsocket.core.session.ActiveConnection;
4+
import org.monora.coolsocket.core.session.Channel;
55

66
/**
77
* This class handles the last sequence of a client connection which is to answer to it.
@@ -13,9 +13,9 @@ public interface ClientHandler
1313
/**
1414
* When a client is connected, this method will be called.
1515
*
16-
* @param activeConnection The connection object that represents the client.
16+
* @param channel The connection object that represents the client.
1717
*/
18-
default void onConnected(@NotNull ActiveConnection activeConnection)
18+
default void onConnected(@NotNull Channel channel)
1919
{
2020

2121
}

src/main/java/org/monora/coolsocket/core/config/Config.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ public final class Config
1515
*/
1616
public static final int DEFAULT_BUFFER_SIZE = 8192;
1717

18-
/**
19-
* The default internal cache size spared for polling requests in memory.
20-
*/
21-
public static final int DEFAULT_INTERNAL_CACHE_SIZE = 0x100000;
22-
2318
/**
2419
* The default inverse exchange point number.
2520
* <p>

src/main/java/org/monora/coolsocket/core/config/ConfigFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.monora.coolsocket.core.config;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.monora.coolsocket.core.session.ActiveConnection;
4+
import org.monora.coolsocket.core.session.Channel;
55

66
import java.io.IOException;
77
import java.io.InputStream;
@@ -32,14 +32,14 @@ public interface ConfigFactory
3232
ServerSocket createServer() throws IOException;
3333

3434
/**
35-
* Configure the socket connection to a client before its actual usage and produce an {@link ActiveConnection}
35+
* Configure the socket connection to a client before its actual usage and produce an {@link Channel}
3636
* instance. The configuration may be different from that of {@link ServerSocket#accept()} assigns.
3737
*
3838
* @param client To configure.
39-
* @return The configured socket encapsulated in a {@link ActiveConnection}.
39+
* @return The configured socket encapsulated in a {@link Channel}.
4040
* @throws SocketException When an unrecoverable error occurs due to misconfiguration.
4141
*/
42-
ActiveConnection configureClient(@NotNull Socket client) throws SocketException;
42+
Channel configureClient(@NotNull Socket client) throws IOException;
4343

4444
/**
4545
* The address that the upcoming products will be assigned to. This does not necessarily reflect the address

src/main/java/org/monora/coolsocket/core/config/DefaultConfigFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.monora.coolsocket.core.config;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.monora.coolsocket.core.session.ActiveConnection;
4+
import org.monora.coolsocket.core.session.Channel;
55

66
import java.io.IOException;
77
import java.net.ServerSocket;
@@ -30,9 +30,10 @@ public void configureServer(@NotNull ServerSocket serverSocket) throws IOExcepti
3030
}
3131

3232
@Override
33-
public @NotNull ActiveConnection configureClient(@NotNull Socket socket) throws SocketException
33+
public @NotNull Channel configureClient(@NotNull Socket socket) throws IOException
3434
{
35-
return new ActiveConnection(socket, readTimeout);
35+
socket.setSoTimeout(readTimeout);
36+
return Channel.wrap(socket);
3637
}
3738

3839
@Override

src/main/java/org/monora/coolsocket/core/response/Flags.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class Flags
99
{
1010
/**
11-
* Bit order: 0
11+
* The bit order: 0
1212
* <p>
1313
* Tells that the read is inconclusive and another read is needed. If the data is not chunked, then, the data will
1414
* be read as many as its length.
Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,54 @@
11
package org.monora.coolsocket.core.response;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.jetbrains.annotations.Nullable;
5-
import org.json.JSONObject;
6-
import org.monora.coolsocket.core.session.ActiveConnection;
4+
import org.monora.coolsocket.core.session.Channel;
75

86
import java.io.ByteArrayOutputStream;
9-
import java.io.OutputStream;
107
import java.io.UnsupportedEncodingException;
118
import java.net.SocketAddress;
129

1310
/**
1411
* This class represents the response received from the remote client that the CoolSocket is connected to.
1512
*
16-
* @see ActiveConnection#receive(OutputStream)
13+
* @see Channel#readAll
1714
*/
18-
public class Response
19-
{
15+
public class Response {
2016
/**
2117
* The remote that sent the response.
2218
*/
2319
public final @NotNull SocketAddress remote;
2420

2521
/**
26-
* The feature flags set for this part.
22+
* The flags set for the response.
2723
*/
2824
public final @NotNull Flags flags;
2925

3026
/**
31-
* The length of the data received for this part. This will also be the total length of a chunked data.
27+
* The total length of the data.
3228
*/
3329
public final long length;
3430

3531
/**
36-
* If the received data is small in size, and is written to a byte buffer (e.g., {@link ByteArrayOutputStream}),
37-
* it will be included in this field.
32+
* The data.
3833
*/
39-
public final @Nullable ByteArrayOutputStream data;
34+
public final @NotNull ByteArrayOutputStream data;
4035

4136
/**
4237
* Creates a new Response instance.
4338
*
4439
* @param remote Where the remote is located at.
4540
* @param flags The feature flags for this response.
4641
* @param length The total length of the data.
47-
* @param data The byte data stored in the heap.
42+
* @param data The data.
4843
*/
4944
public Response(@NotNull SocketAddress remote, @NotNull Flags flags, long length,
50-
@Nullable ByteArrayOutputStream data)
51-
{
45+
@NotNull ByteArrayOutputStream data) {
5246
this.remote = remote;
5347
this.flags = flags;
5448
this.length = length;
5549
this.data = data;
5650
}
5751

58-
/**
59-
* Check whether the data contains actual data.
60-
*
61-
* @return True if the data is actually an object.
62-
*/
63-
public boolean containsData()
64-
{
65-
return data != null;
66-
}
67-
68-
public @NotNull JSONObject getAsJson()
69-
{
70-
return new JSONObject(getAsString());
71-
}
72-
73-
/**
74-
* Get the data as a JSON object.
75-
*
76-
* @param charsetName The name of the charset to use to decode the data.
77-
* @return The encapsulated JSON data.
78-
* @throws UnsupportedEncodingException If the supplied charset name is not available.
79-
*/
80-
public @NotNull JSONObject getAsJson(@NotNull String charsetName) throws UnsupportedEncodingException
81-
{
82-
return new JSONObject(getAsString(charsetName));
83-
}
84-
8552
/**
8653
* Return the data as a string.
8754
*
@@ -90,11 +57,7 @@ public boolean containsData()
9057
* @throws UnsupportedEncodingException If the supplied charset name is not available.
9158
* @see #getAsString()
9259
*/
93-
public @NotNull String getAsString(@NotNull String charsetName) throws UnsupportedEncodingException
94-
{
95-
if (data == null) {
96-
throw new IllegalStateException("Trying to read data from a response whose data point is separate.");
97-
}
60+
public @NotNull String getAsString(@NotNull String charsetName) throws UnsupportedEncodingException {
9861
return data.toString(charsetName);
9962
}
10063

@@ -104,11 +67,16 @@ public boolean containsData()
10467
* @return The string representation of the {@link #data}.
10568
* @see #getAsString(String)
10669
*/
107-
public @NotNull String getAsString()
108-
{
109-
if (data == null) {
110-
throw new IllegalStateException("Trying to read data from a response whose data point is separate.");
111-
}
70+
public @NotNull String getAsString() {
11271
return data.toString();
11372
}
73+
74+
/**
75+
* Get the raw data.
76+
*
77+
* @return The raw data.
78+
*/
79+
public @NotNull byte[] getBytes() {
80+
return data.toByteArray();
81+
}
11482
}

0 commit comments

Comments
 (0)