/*
1Server.c: Implement one TCP/IP server.
           Support one server for TCP port 10000 and 10001.

Compiler: BC++ 3.1, 
          Turbo C++ 1.01(3.01) (free from http://cc.codegear.com/free/cpp)

Compile mode: Large

Project: Server.c
         ..\lib\7186el.lib
         ..\lib\tcp_dm32.lib
         ..\lib\FW_nnnnn.lib
         
Hordware: uPAC-7186EX


[Dec 18, 2008] by Liam
*/

#include<conio.h>
#include<stdio.h>
#include<string.h>
#include "..\lib\7186e.h"
#include "..\lib\Tcpip32.h"
#include "..\lib\MFW.h"

void do_echo( int skt , int mode);
void do_echo2( int skt ,int mode);
#define BUFSIZE 1460        // read/write buffer size

TCP_SERVER TcpServer={
    10000,      // port
    -1,         // socket
    0,          // state
    0,          // connect
    do_echo     // CallBackFun
};

TCP_SERVER TcpServer2={
    10001,      // port
    -1,         // socket
    0,          // state
    0,          // connect
    do_echo2    // CallBackFun
};

char *GetModuleName(void)   // User's program must support this function for UDP search
{
    return "uPAC-7186EX-FW";
}

char *GetAliasName(void)    // User's program must support this function for UDP search
{
    return "test1" ;
} 

unsigned char MyIp[4];
int iProcess;
void XS_UserInit(int argc, char *argv[])
{
    extern int bAcceptBroadcast;
    extern unsigned long ACKDELAY;  /* vaariable in tcp.c */
    extern long MAXTXTOUT;
    void UserLoop(void);
    
    char version[20];
    
    InitLib();
    
    XS_GetVersion(version);
    Print("[X-Server library]: version=%s ", version);
    XS_GetLibDate(version);
    Print("date=%s\r\n", version);
    GetTcpipLibDate(version);
    Print("Tcpip library version:%X, Library Date is %s\r\n", GetTcpipLibVer(), version);
    GetIp(MyIp);
    Print("IP=%d.%d.%d.%d\r\n", MyIp[0], MyIp[1], MyIp[2], MyIp[3]);
    
    bAcceptBroadcast=0;   

    XS_AddSystemLoopFun(UserLoop);
    XS_AddServer(&TcpServer);   //Add a TCP server

    XS_AddSystemLoopFun(XS_SocketLoopFun);
    XS_StartSocket();
    
    EnableWDT();
    ACKDELAY=200;
    MAXTXTOUT=6000UL; 
    iProcess=0;
}

void XS_UserEnd(void)
{
    XS_StopSocket();
    DisableWDT();
}

void UserLoop(void)
{   
    long tmpt;
    int key,err;
    static int iprint=0;
    static int iServer2=0;

    RefreshWDT();
    if(Kbhit()){
        switch((key=Getch())){
            case 27:
                QuitMain=1;
                break;
            case 0x50: //'P'
            case 0x70: //'p'
                if(!iprint)
                    iprint=1;
                else
                    iprint=0;
                break;
            case 0x32: //'2'
                if(!iServer2)
                {
                   iServer2=1;
                   Print("Build Server2 listen 10001\r\n");
                   XS_AddServer(&TcpServer2); //Add a TCP server
                }
                else
                {
                    iServer2=0;
                    Print("Remove Server2 listen 10001\r\n");
                    XS_RemoveServer(&TcpServer2);
                } 
                break;
            default:
                break;
        }
    }
    
    if(iprint)
        Print("Connection1 count=%u, Connection2 =%u\r\n",TcpServer.connect,TcpServer2.connect);  
}

// when client is connected, do echo here
char buf[BUFSIZE],SendBack[BUFSIZE+2];
void do_echo( int skt ,int mode)
{ 
    int i, cc, err, Sendlen;
    char *pit;

    if(!mode){
/*      Sendlen=sprintf(SendBack,"Welcome connect to port %u\r\nThere are %d clients connect to this port\r\n",
                    TcpServer.port,TcpServer.connect);
        goto SendMessage;*/
    }
    else{
        err = cc = readsocket( skt, buf, sizeof(buf)-1);
        if(err<=0){         // error or disconnected by remote side
            XS_CloseSocket(skt);
        }
        else{          
            buf[cc]=0;
            Print(buf);
            for(i=0; i<cc; i++ ){
                SendBack[i]=buf[i];
            }
            Sendlen=cc;
SendMessage:
            err=writesocket(skt,SendBack,Sendlen);
            if(err < 0){        // write error
                Print("echo write error %d\n\r", err);
                XS_CloseSocket(skt);
            }
        }
    }
}

// when client is connected, do echo here
char buf2[BUFSIZE], SendBack2[BUFSIZE+2];
void do_echo2(int skt, int mode)
{ 
    int i, cc, err, Sendlen;

    if(!mode){
        Sendlen=sprintf(SendBack2,"Welcome connect to port %u\r\nThere are %d clients connect to this port\r\n",
                        TcpServer.port,TcpServer.connect);
        goto SendMessage;
    }
    else{
        err=cc=readsocket(skt, buf2, sizeof(buf2)-1);
        if(err<=0){         // error or disconnected by remote side
            XS_CloseSocket(skt);
        }
        else{
            buf2[cc]=0;
            Print(buf2);
            for(i=0; i < cc; i++){
                SendBack2[i]=buf2[i];
            }
            Sendlen=cc;
SendMessage:
            err=writesocket(skt,SendBack2, Sendlen);
            if(err<0){      // write error
                Print("echo write error %d\n\r", err);
                XS_CloseSocket(skt);
            }
        }
    }
}

void main(int argc,char *argv[])
{
    XS_main(argc,argv);     // call the XS library main function
}
