Encoding Decoding Golang problem: gob: duplicate type received

81 Views Asked by At

I'm trying to build a simulator of an exchange. The server builds the Order_Book struct with the orders sent from the Clients.
I want the server to send the Order_Book struct to the Client after his request. Unfortunately after the first Client decoding of the Order_Book I have the error

gob: duplicate type received

But (only) the second error say:

gob: decoding into local type *int, received remote type User = struct { Name string; }

Server

decoder := gob.NewDecoder(conn)
        // Read the message type
        var messageType int
        err := decoder.Decode(&messageType)
        if err != nil {
            fmt.Printf("\nError decoding message type: %v\n", err)
            return
        }

        switch messageType {
            case 3:// Oeder book
                

                var user_info common.User
                err := decoder.Decode(&user_info)
                if err != nil {
                    fmt.Println("Error decoding User for OB:", err)
                    return
                }
                fmt.Printf("Received User info for OB: %+v\n", user_info)
                
                encoder := gob.NewEncoder(conn)
                // Encode the message type
                
                err_bi := encoder.Encode(4)
                if err_bi != nil {
                    fmt.Printf("error encoding message type: %v", err_bi)
                }

                // Encode and send the order book to the client
                err_mm := encoder.Encode(OB)
                if err_mm != nil {
                    fmt.Println("Error encoding order book:", err_mm)
                    //return
                }

                fmt.Println("Order book sent to client")


        }

Client

var exi string="n"
    for{
        
        fmt.Print("\nWanna: \n (q) Quit\n (t) Trade\n (v) See OrderBook\n\n")
        inp, _ := reader.ReadString('\n')
        exi = strings.TrimSpace(inp)
        
        
        if exi== "q"{                            
            fmt.Print("\n\n$$$$$$$$$$$$$$$$$$$$\n$       BYE!       $\n$$$$$$$$$$$$$$$$$$$$\n\n")
            break

        } else if exi== "t" {
            inOrd := Type_Order()

            fmt.Printf("Address of inord= %v:\t%p\n", inOrd, &inOrd)
            
            err = common.SendMessage(conn, 1, inOrd)
            if err != nil {
                fmt.Println("Error sending MessageOne:", err)
                return
            }
            fmt.Printf("\nOrder sent: %v\n",inOrd)

        } else if exi== "v" {

            encoder := gob.NewEncoder(conn)

            // Encode the message type
            err := encoder.Encode(3)
            if err != nil {
                continue //fmt.Errorf("error encoding message type: %v", err)
            }
            
            // Encode and send the actual data
            err = encoder.Encode(User_info) 
            if err != nil {
                continue//fmt.Errorf("error encoding data: %v", err)
            }
            var code int
            var OB common.Order_Book

        
            decoder := gob.NewDecoder(conn)

            err__f := decoder.Decode(&code)
            if err__f != nil {
                fmt.Println("Error decoding order book type message:", err__f)
                continue
            }
            
            err_m := decoder.Decode(&OB)
            if err_m != nil {
                fmt.Println("Error decoding order book data:", err_m)
                continue
            }
            fmt.Printf("Received Order Book: %+v\n", OB)
        
            // Print the received order book
            fmt.Println("Received Order Book:")
            common.Order_Book_print(OB, common.ORDER_BOOK_LENGTH, false)

         }

Client Output

Wanna: 
 (q) Quit
 (t) Trade
 (v) See OrderBook

v
Error decoding order book type message: gob: duplicate type received

Wanna: 
 (q) Quit
 (t) Trade
 (v) See OrderBook

v
Error decoding order book type message: gob: decoding into local type *int, received remote type User = struct { Name string; }

Wanna:
 (q) Quit
 (t) Trade
 (v) See OrderBook

v
Error decoding order book type message: gob: duplicate type received 

I tried to rearage the decoder and encoders, the SendMessage is a function with the the encoders

func SendMessage(conn net.Conn, messageType int, data interface{}) error {
    // Create a new encoder for writing messages
    encoder := gob.NewEncoder(conn)

    // Encode the message type
    err := encoder.Encode(messageType)
    if err != nil {
        return fmt.Errorf("error encoding message type: %v", err)
    }

    // Encode and send the actual data
    err = encoder.Encode(data)
    if err != nil {
        return fmt.Errorf("error encoding data: %v", err)
    }

    return nil
}

But I still have the same problem. I don't undestand the problem, I send different messages from the client to the server, but if the server sends the Order_Book the Client can't decode it

0

There are 0 best solutions below