Skip to content

Commit

Permalink
can now send packets to server
Browse files Browse the repository at this point in the history
  • Loading branch information
xBeastMode committed Jul 9, 2018
1 parent 0d3652a commit bb8abf3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
33 changes: 33 additions & 0 deletions datagram_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ func (db *DatagramBuilder) BuildFromBufferWithLatestSequence(packets []byte) pro
return datagram
}


// this builds a datagram from a packet batch
// and sets the reliability of the encapsulated
// packet to a custom one and the order of the
// datagram to a custom sequence number
func (db *DatagramBuilder) BuildFromBatch(batch *MinecraftPacketBatch, reliability byte, sequenceNumber, orderIndex, messageIndex uint32) protocol.Datagram {
encapsulated := protocol.NewEncapsulatedPacket()
encapsulated.Reliability = reliability
encapsulated.OrderIndex = orderIndex
encapsulated.MessageIndex = messageIndex
encapsulated.Buffer = batch.Buffer
datagram := protocol.NewDatagram()
datagram.SequenceNumber = sequenceNumber
datagram.AddPacket(encapsulated)
datagram.Encode()
return *datagram
}

// this builds a datagram from a slice
// of encapsulated packets and sets the
// reliability of the encapsulated packet
// to a custom one and the order of the
// datagram to a custom sequence number
func (db *DatagramBuilder) BuildFromEncapsulated(packets []*protocol.EncapsulatedPacket, sequenceNumber uint32) protocol.Datagram {
datagram := protocol.NewDatagram()
datagram.SequenceNumber = sequenceNumber
for _, p := range packets {
datagram.AddPacket(p)
}
datagram.Encode()
return *datagram
}

// this builds a datagram from raw bytes
// and sets the reliability of the
// encapsulated packet to a custom one
Expand Down
64 changes: 48 additions & 16 deletions packet_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@ type PacketHandler struct {
// the last datagram received from both hosts
datagram *protocol.Datagram
// a slice of raw packets that will be
// sent out the server next tick, packets are sent
// every tick via the OutboundPacketTicker,
// sent out to the client next tick, packets are sent
// every tick via the packet handler,
// 20 ticks = 1 second, 1 tick ~ 0.05 seconds
OutBoundPackets [][]byte
OutBoundPacketsClient [][]byte
// a slice of raw packets that will be
// sent out to the server next tick, packets are sent
// every tick via the packet handler,
// 20 ticks = 1 second, 1 tick ~ 0.05 seconds
OutBoundPacketsServer [][]byte
// a bool that returns true if the
// Client is ready for packets
Ready bool
Expand All @@ -89,7 +94,8 @@ func NewPacketHandler() *PacketHandler {
h.splits = Indexes{splits: make(map[int16][]*protocol.EncapsulatedPacket), splitCounts: make(map[int16]uint)}
h.DatagramBuilder = NewDatagramBuilder()
h.DatagramBuilder.pkHandler = &h
h.OutBoundPackets = [][]byte{}
h.OutBoundPacketsClient = [][]byte{}
h.OutBoundPacketsServer = [][]byte{}
h.orders = Orders{}
return &h
}
Expand Down Expand Up @@ -130,21 +136,39 @@ func (pkHandler *PacketHandler) FlowDatagram(host Host) {
}

// adds a packet to the slice of
// packets that will be sent out the server next tick,
// packets are sent every tick via the OutboundPacketTicker,
// packets that will be sent out to the client next tick,
// packets are sent every tick via the packet handler,
// 20 ticks = 1 second, 1 tick ~ 0.05 seconds
func (pkHandler *PacketHandler) AddOutboundPacket(packet packets.IPacket) {
func (pkHandler *PacketHandler) AddOutboundPacketClient(packet packets.IPacket) {
packet.EncodeHeader()
packet.Encode()
pkHandler.OutBoundPackets = append(pkHandler.OutBoundPackets, packet.GetBuffer())
pkHandler.OutBoundPacketsClient = append(pkHandler.OutBoundPacketsClient, packet.GetBuffer())
}

// adds a raw packet to the slice of
// packets that will be sent out the server next tick,
// packets are sent every tick via the OutboundPacketTicker,
// packets that will be sent out to the client next tick,
// packets are sent every tick via the packet handler,
// 20 ticks = 1 second, 1 tick ~ 0.05 seconds
func (pkHandler *PacketHandler) AddOutboundRawPacket(packet []byte) {
pkHandler.OutBoundPackets = append(pkHandler.OutBoundPackets, packet)
func (pkHandler *PacketHandler) AddOutboundRawPacketClient(packet []byte) {
pkHandler.OutBoundPacketsClient = append(pkHandler.OutBoundPacketsClient, packet)
}

// adds a packet to the slice of
// packets that will be sent out to the server next tick,
// packets are sent every tick via the packet handler,
// 20 ticks = 1 second, 1 tick ~ 0.05 seconds
func (pkHandler *PacketHandler) AddOutboundPacketServer(packet packets.IPacket) {
packet.EncodeHeader()
packet.Encode()
pkHandler.OutBoundPacketsServer = append(pkHandler.OutBoundPacketsServer, packet.GetBuffer())
}

// adds a raw packet to the slice of
// packets that will be sent out to the server next tick,
// packets are sent every tick via the packet handler,
// 20 ticks = 1 second, 1 tick ~ 0.05 seconds
func (pkHandler *PacketHandler) AddOutboundRawPacketServer(packet []byte) {
pkHandler.OutBoundPacketsServer = append(pkHandler.OutBoundPacketsServer, packet)
}

// handles encapsulated packet
Expand All @@ -167,12 +191,19 @@ func (pkHandler *PacketHandler) HandleEncapsulated(packet *protocol.Encapsulated
}
}

if len(pkHandler.OutBoundPackets) > 0 {
if host.IsServer() {
for _, pk := range pkHandler.OutBoundPackets {
if host.IsServer() {
if toServer := pkHandler.OutBoundPacketsServer; len(toServer) > 0 {
for _, pk := range toServer {
batch2.AddRawPacket(pk)
}
pkHandler.OutBoundPacketsServer = nil
}
}else{
if toClient := pkHandler.OutBoundPacketsClient; len(toClient) > 0 {
for _, pk := range toClient {
batch2.AddRawPacket(pk)
}
pkHandler.OutBoundPackets = nil
pkHandler.OutBoundPacketsClient = nil
}
}

Expand All @@ -188,6 +219,7 @@ func (pkHandler *PacketHandler) HandleEncapsulated(packet *protocol.Encapsulated
dgram.AddPacket(encap)
dgram.SequenceNumber = pkHandler.lastSequenceNumber
dgram.Encode()

host.WritePacket(dgram.Buffer)

handled = true
Expand Down

0 comments on commit bb8abf3

Please sign in to comment.