/*
 * Copyright (c) 1994, 2021 Alex Leigh, All Rights Reserved
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ALEX LEIGH The copyright
 * notice above does not evidence any actual or intended publication of such
 * source code.
 *
 * Disclosure, copying, distribution, or transmission of this code without
 * express permission is prohibited.
*/

#ifndef MECHA_NET_H
#define MECHA_NET_H

#define M_NET_FLAG_FREEPT (1<<0)
#define M_NET_FLAG_FREEUD (1<<1)

#include <stdint.h>

struct mSnet_con;

#ifndef _WIN32
#include "net_posix.h"
#else
#include "net_win.h"
#endif // _WIN32

union mUnet_ud {
    int ud_int;
    void *ud_ptr;
#ifdef _WIN32
    SOCKET wsock;
#endif
};

struct mSnet_con {
    struct mSnet_pt *pt;
    uint8_t flags;
    union mUnet_ud ud;
};

struct mSnet_pt {
    void (*close)(struct mSnet_con *sock);

    int (*write)(struct mSnet_con *con, const void *buf, size_t len, size_t *wrote_len);

    int (*read)(struct mSnet_con *con, void *buf, size_t len, size_t *read_len);
};

void mFnet_close(struct mSnet_con *con);

void mFnet_pt_set(struct mSnet_con *con, struct mSnet_pt *pt, int should_free);

int mFnet_write(struct mSnet_con *con, const void *buf, size_t len, size_t *wrote_len);

int mFnet_read(struct mSnet_con *con, void *buf, size_t len, size_t *read_len);

int mFnet_sock_init();

int mFnet_sock_connect(struct mSnet_con *con, const struct mSurl *url);

#endif //MECHA_NET_H