#include "TCPserver.h"
bool fenge(String &zifuchuan, String &outstring, String fengefu)
{
    int weizhi = 0; //找查的位置

    if (zifuchuan == "")
    {
        // Serial.print("commanesdafasdf");
        return false;

        /* code */
    }

    weizhi = zifuchuan.indexOf(fengefu); //找到位置
    if (weizhi != -1)                    //如果位置不为空
    {
        // Serial.println(zifuchuan);
        outstring = zifuchuan.substring(0, weizhi); //打印取第一个字符
        zifuchuan = zifuchuan.substring(weizhi + fengefu.length(), zifuchuan.length());
        return true;
        //分隔后只取后面一段内容 以方便后面找查
    }
    else
    {
        // Serial.print(zifuchuan);
        outstring = zifuchuan;
        zifuchuan = "";
        return true;
    }
}
TCPserver::TCPserver(int port)
{
    server = new WiFiServer(port);
    myserial = nullptr;
    isPortOutInit = false;

}
#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE 
TCPserver::TCPserver(USBCDC *serial,int port)
{
        server = new WiFiServer(port);
    myserial = serial;
    isPortOutInit = true;

}
#elif ARDUINO_USB_CDC_ON_BOOT && ARDUINO_USB_MODE 
TCPserver::TCPserver(HWCDC *serial,int port)
{
            server = new WiFiServer(port);
    myserial = serial;
    isPortOutInit = true;

}
#else
TCPserver::TCPserver(HardwareSerial *serial, int port /* args */)
{
    server = new WiFiServer(port);
    myserial = serial;
    isPortOutInit = true;

}
#endif
void TCPserver::initme()
{
    server->begin();
    //关闭小包合并包功能，不会延时发送数据
    server->setNoDelay(true);
}
void TCPserver::loop()
{
    uint8_t i;
    //检测是否有新的client请求进来
    //  myserial->print("asdffsd");
    if (server->hasClient())
    {
        for (i = 0; i < MAX_SRV_CLIENTS; i++)
        {
            //释放旧无效或者断开的client
            if (!serverClients[i] || !serverClients[i].connected())
            {
                if (serverClients[i])
                {
                    serverClients[i].stop();
                }
                //分配最新的client
                serverClients[i] = server->available();
                if (isPortOutInit)
                {
                    myserial->print("New client: ");
                    myserial->print(i);
                    /* code */
                }
                break;
            }
        }
        //当达到最大连接数 无法释放无效的client，需要拒绝连接
        if (i == MAX_SRV_CLIENTS)
        {
            WiFiClient serverClient = server->available();
            serverClient.stop();
            if (isPortOutInit)
            {
                myserial->print("Connection rejected ");
            }
        }
    }
    //检测client发过来的数据
    for (i = 0; i < MAX_SRV_CLIENTS; i++)
    {
        if (serverClients[i] && serverClients[i].connected())
        {
            if (serverClients[i].available())
            {
                // get data from the telnet client and push it to the UART
                String str = "";
                while (serverClients[i].available())
                {
                    //发送到串口调试器
                    if (isPortOutInit)
                    {
                        str = str + String((char)serverClients[i].read());
                        // myserial->write(serverClients[i].read());
                        /* code */
                    }
                }

            }
        }
    }
    if (isPortOutInit)
    {

        if (myserial->available())
        {
            //把串口调试器发过来的数据 发送给client
            size_t len = myserial->available();
            uint8_t sbuf[len];
            myserial->readBytes(sbuf, len);
            // push UART data to all connected telnet clients
            for (i = 0; i < MAX_SRV_CLIENTS; i++)
            {
                if (serverClients[i] && serverClients[i].connected())
                {
                    serverClients[i].write(sbuf, len);
                    delay(1);
                }
            }
        }
    }
}
void TCPserver::SendDataToClinet(String str, int clinetid)
{

    // myserial->print("Connection rejected ");
    if (clinetid == -1)
    {
        int i;
        for (i = 0; i < MAX_SRV_CLIENTS; i++)
        {
            if (serverClients[i] && serverClients[i].connected())
            {
                serverClients[i].println(str);
                delay(1);
            }
        }
        /* code */
    }
    else
    {
        if (serverClients[clinetid] && serverClients[clinetid].connected())
        {
            serverClients[clinetid].println(str);
            delay(1);
        }
    }
}
void TCPserver::SendDataToClinet(char *str, int clinetid)
{
    // push UART data to all connected telnet clients
    myserial->print("Connection rejected ");
    if (clinetid == -1)
    {
        int i;
        for (i = 0; i < MAX_SRV_CLIENTS; i++)
        {
            if (serverClients[i] && serverClients[i].connected())
            {
                serverClients[i].write(str);
                delay(1);
            }
        }
        /* code */
    }
    else
    {
        if (serverClients[clinetid] && serverClients[clinetid].connected())
        {
            serverClients[clinetid].write(str);
            delay(1);
        }
    }
}
TCPserver::~TCPserver()
{
    delete server;
}