Refactor to export structs chnk_stream and message

This commit is contained in:
Muaz Ahmad 2023-08-10 13:58:57 +05:00
parent 48029e0ee6
commit fabcecae43

View file

@ -6,7 +6,7 @@ import (
"encoding/binary" "encoding/binary"
) )
type chnk_stream struct { type ChunkStream struct {
timestamp uint32 timestamp uint32
last_msg_strm_id uint32 last_msg_strm_id uint32
last_msg_len uint32 last_msg_len uint32
@ -14,7 +14,7 @@ type chnk_stream struct {
timedelta uint32 timedelta uint32
} }
type message struct { type Message struct {
data []byte data []byte
curr_bytes_read uint32 curr_bytes_read uint32
timestamp uint32 timestamp uint32
@ -43,9 +43,9 @@ func new_chunk_bufs() (*chunk_bufs) {
return &new_chunk_bufs return &new_chunk_bufs
} }
func OpenStreamsMapInit() (map[uint32]*chnk_stream, map[uint32]*message, *chunk_bufs) { func OpenStreamsMapInit() (map[uint32]*ChunkStream, map[uint32]*Message, *chunk_bufs) {
open_chnkstrms := make(map[uint32]*chnk_stream) open_chnkstrms := make(map[uint32]*ChunkStream)
open_msgs := make(map[uint32]*message) open_msgs := make(map[uint32]*Message)
chunk_bufs_ptr := new_chunk_bufs() chunk_bufs_ptr := new_chunk_bufs()
return open_chnkstrms, open_msgs, chunk_bufs_ptr return open_chnkstrms, open_msgs, chunk_bufs_ptr
} }
@ -116,7 +116,7 @@ func read_time_extd(conn net.Conn, chunk_bufs_ptr *chunk_bufs) (time uint32, err
return return
} }
func read_message_header_0(conn net.Conn, chnk_stream_ptr *chnk_stream, msg_ptr *message, chunk_bufs_ptr *chunk_bufs) (error) { func read_message_header_0(conn net.Conn, chnk_stream_ptr *ChunkStream, msg_ptr *Message, chunk_bufs_ptr *chunk_bufs) (error) {
var extended_time bool var extended_time bool
var err error var err error
chnk_stream_ptr.timestamp, extended_time, err = read_time(conn, chunk_bufs_ptr) chnk_stream_ptr.timestamp, extended_time, err = read_time(conn, chunk_bufs_ptr)
@ -153,7 +153,7 @@ func read_message_header_0(conn net.Conn, chnk_stream_ptr *chnk_stream, msg_ptr
return nil return nil
} }
func read_message_header_1(conn net.Conn, chnk_stream_ptr *chnk_stream, msg_ptr *message, chunk_bufs_ptr *chunk_bufs) (error) { func read_message_header_1(conn net.Conn, chnk_stream_ptr *ChunkStream, msg_ptr *Message, chunk_bufs_ptr *chunk_bufs) (error) {
var extended_time bool var extended_time bool
var err error var err error
@ -186,7 +186,7 @@ func read_message_header_1(conn net.Conn, chnk_stream_ptr *chnk_stream, msg_ptr
return nil return nil
} }
func read_message_header_2(conn net.Conn, chnk_stream_ptr *chnk_stream, msg_ptr *message, chunk_bufs_ptr *chunk_bufs) (error) { func read_message_header_2(conn net.Conn, chnk_stream_ptr *ChunkStream, msg_ptr *Message, chunk_bufs_ptr *chunk_bufs) (error) {
var extended_time bool var extended_time bool
var err error var err error
@ -212,7 +212,7 @@ func read_message_header_2(conn net.Conn, chnk_stream_ptr *chnk_stream, msg_ptr
return nil return nil
} }
func read_chunk_data(conn net.Conn, msg_ptr *message, chnk_size uint32) (error) { func read_chunk_data(conn net.Conn, msg_ptr *Message, chnk_size uint32) (error) {
bytes_left := msg_ptr.msg_len - msg_ptr.curr_bytes_read bytes_left := msg_ptr.msg_len - msg_ptr.curr_bytes_read
var buffer_end uint32 var buffer_end uint32
if bytes_left < chnk_size { if bytes_left < chnk_size {
@ -229,11 +229,11 @@ func read_chunk_data(conn net.Conn, msg_ptr *message, chnk_size uint32) (error)
} }
func ReadChunk(conn net.Conn, open_chnkstrms map[uint32]*chnk_stream, open_msgs map[uint32]*message, chnk_size uint32, chunk_bufs_ptr *chunk_bufs) (*message, error){ func ReadChunk(conn net.Conn, open_chnkstrms map[uint32]*ChunkStream, open_msgs map[uint32]*Message, chnk_size uint32, chunk_bufs_ptr *chunk_bufs) (*Message, error){
conn.SetDeadline(time.Now().Add(10 * time.Second)) conn.SetDeadline(time.Now().Add(10 * time.Second))
var chnkstream_ptr *chnk_stream var chnkstream_ptr *ChunkStream
var msg_ptr *message var msg_ptr *Message
format, csid, err := read_basic_header(conn, chunk_bufs_ptr) format, csid, err := read_basic_header(conn, chunk_bufs_ptr)
if err != nil { if err != nil {
@ -242,23 +242,23 @@ func ReadChunk(conn net.Conn, open_chnkstrms map[uint32]*chnk_stream, open_msgs
switch format { switch format {
case 0: case 0:
chnkstream_ptr = new(chnk_stream) chnkstream_ptr = new(ChunkStream)
open_chnkstrms[csid] = chnkstream_ptr open_chnkstrms[csid] = chnkstream_ptr
msg_ptr = new(message) msg_ptr = new(Message)
if err := read_message_header_0(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil { if err := read_message_header_0(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil {
return nil, err return nil, err
} }
case 1: case 1:
chnkstream_ptr = open_chnkstrms[csid] chnkstream_ptr = open_chnkstrms[csid]
msg_ptr = new(message) msg_ptr = new(Message)
if err := read_message_header_1(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil { if err := read_message_header_1(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil {
return nil, err return nil, err
} }
case 2: case 2:
chnkstream_ptr = open_chnkstrms[csid] chnkstream_ptr = open_chnkstrms[csid]
msg_ptr = new(message) msg_ptr = new(Message)
if err := read_message_header_2(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil { if err := read_message_header_2(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil {
return nil, err return nil, err