Skip to content

Small packet library made for Java SSLSockets. You can use and modify it as you wish. It uses Json so you will need to have json dependencies (Jackson Core).

License

Notifications You must be signed in to change notification settings

FrostedCA/TwoPack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Summary

TwoPack

Small packet library made for Java SSLSockets. You can use and modify it as you wish. It uses Json so you will need to have json dependencies (Jackson Core). For more details contact me on discord: landryman. Version 2.00+ stable.

Example client-side ClassPacket child

With Update v2.11+

public class CPacketRegister extends ClassPacket {

    public CPacketRegister(ISession session) {
        super(session);
    }

    public CPacketRegister(ISession session, RegisterObject registerObject) {
        super(session);
        this.pObject = registerObject;
    }

    @Override
    public void read(JsonNode jsonNode, ObjectMapper objectMapper) throws JsonProcessingException {
        if(!jsonNode.isNull() && !jsonNode.isEmpty()) {
            RegisterObject registerObject = objectMapper.treeToValue(jsonNode, RegisterObject.class);
            App.getSession().getPacketsManager().writePacket(new CPacketLogin(App.getSession(), registerObject));
        }
    }
}

Example server-side ClassPacket child

With Update v2.11+

public class SPacketRegister extends ClassPacket {

    public SPacketRegister(ISession session) {
        super(session);
    }

    public SPacketRegister(ISession session, RegisterObject registerObject) {
        super(session);
        this.pObject = registerObject;
    }

    @Override
    public void read(JsonNode jsonNode, ObjectMapper objectMapper) throws JsonProcessingException {
        boolean result = false;

        RegisterObject registerObject = objectMapper.treeToValue(jsonNode, RegisterObject.class);

        boolean valid = InputValidator.validatePassword(registerObject.getPassword(), registerObject.getConfPassword());
        if(valid) {
            result = DBQueries.registerAccount(registerObject);
        }
        registerObject.setResult(result);
        ((ClientSession) session).getPacketsManager().writePacket(new SPacketRegister(session, registerObject));
    }
}

Register Object Example

public class RegisterObject extends PObject {

    private String email;
    private String password;
    private String confPassword;
    private String firstName;
    private String lastName;

    @JsonProperty("email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @JsonProperty("password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
...
}

How to setup packet manager client and server side

Simply add this code right after where you are starting the handshake between the client and server.

try{
    this.getSslSocket().startHandshake();
    this.packetsManager = new PacketsManager(this.packetList, this);
    this.packetsManager.writePacket(new CPacketHandshake(this));
    while (!this.getSslSocket().isClosed()) {
        this.packetsManager.listenForPackets();
    }
} catch(IOException e) {
// log/code here
}

Example VarPacket

With Update v2.15+

VarPacket or Variable Packet is a new way of creating your packets! As the name implies, you can create a packet inside a variable. Old packets have received there new class ClassPacket.java. The old Packet.java is now protected. Packets now also implements an interface, more onto this on future releases.

public VarPacket varPacket = new VarPacket(session) {
        @Override
        public String getPacketType() {
            return "Example";
        }

        @Override
        public void write(PrintWriter printWriter, ObjectMapper objectMapper) throws JsonProcessingException {
            /**
             * Process your data...
             */

            // Create your object
            this.pObject = new ExampleObject(); // -> (public class ExampleObject extends PObject)
            this.pObject.setPacketType(getPacketType()); // -> Set the packet type.
            printWriter.println(objectMapper.writeValueAsString(pObject)); // -> Write the packet.
            printWriter.flush();
        }

        @Override
        public void read(JsonNode jsonNode, ObjectMapper objectMapper) throws JsonProcessingException {
            /**
             * Process your data... (Same as ClassPacket see Summary inside ReadMe)
             * You can also access your 'session' this.session and cast it to your actual session class if needed (server side especially)
             */
        }
};

Download

Gradle

allprojects {
   repositories {
      ...
      maven { url 'https://www.jitpack.io' }
   }
}

dependencies {
   implementation 'com.github.FrostedCA:TwoPack:VERSION'
}

Maven

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://www.jitpack.io</url>
  </repository>
</repositories>

<dependency>
  <groupId>com.github.FrostedCA</groupId>
  <artifactId>TwoPack</artifactId>
  <version>Tag</version>
</dependency>

About

Small packet library made for Java SSLSockets. You can use and modify it as you wish. It uses Json so you will need to have json dependencies (Jackson Core).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages