Create a basic Connection class

master
Lonami Exo 2018-07-23 21:59:28 +02:00
parent 957e29b4db
commit 3960febf3c
4 changed files with 127 additions and 1 deletions

View File

@ -1,6 +1,14 @@
package io.github.lonamiwebs.overgram;
import io.github.lonamiwebs.overgram.network.connection.Connection;
import io.github.lonamiwebs.overgram.network.connection.TcpFull;
import java.io.IOException;
public class Overgram {
public static void main(final String... args) {
public static void main(final String... args) throws IOException {
final Connection connection = new TcpFull();
connection.connect("149.154.167.91", 443);
connection.disconnect();
}
}

View File

@ -0,0 +1,46 @@
package io.github.lonamiwebs.overgram.network;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TcpClient {
private Socket socket;
public void connect(final String ipAddress, final int port) throws IOException {
socket = new Socket(ipAddress, port);
}
public void close() {
try {
socket.close();
} catch (IOException ignored) {
}
}
public byte[] read(final int length) throws IOException {
final InputStream in = socket.getInputStream();
final byte[] result = new byte[length];
int index = 0;
while (true) {
final int count = in.read(result, index, result.length - index);
if (count == -1) {
throw new IOException("Connection closed");
}
index += count;
if (index == result.length) {
return result;
}
}
}
public void write(final byte[] bytes) throws IOException {
final OutputStream out = socket.getOutputStream();
out.write(bytes);
out.flush();
}
}

View File

@ -0,0 +1,13 @@
package io.github.lonamiwebs.overgram.network.connection;
import java.io.IOException;
public abstract class Connection {
public abstract void connect(final String ipAddress, final int port) throws IOException;
public abstract void disconnect();
public abstract void send(final byte[] data) throws IOException;
public abstract byte[] recv() throws IOException;
}

View File

@ -0,0 +1,59 @@
package io.github.lonamiwebs.overgram.network.connection;
import io.github.lonamiwebs.overgram.network.TcpClient;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.CRC32;
public class TcpFull extends Connection {
private final TcpClient client;
private int counter;
public TcpFull() {
client = new TcpClient();
}
@Override
public void connect(final String ipAddress, final int port) throws IOException {
client.connect(ipAddress, port);
counter = 0;
}
@Override
public void disconnect() {
client.close();
}
@Override
public void send(final byte[] data) throws IOException {
final int length = data.length + 12;
final ByteBuffer buffer = ByteBuffer.allocate(length);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(length);
buffer.putInt(counter);
buffer.put(data);
final CRC32 crc32 = new CRC32();
crc32.update(buffer.array(), 0, length - 4);
buffer.putInt((int) crc32.getValue());
client.write(buffer.array());
++counter;
}
@Override
public byte[] recv() throws IOException {
final ByteBuffer buffer = ByteBuffer.wrap(client.read(8));
final int length = buffer.getInt();
final int seq = buffer.getInt();
final byte[] body = client.read(length - 12);
final int crc = ByteBuffer.wrap(client.read(4)).getInt();
// TODO Check crc
return body;
}
}