NEW
PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF SBIN/HTTPD
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/LIBTCPIP.I
*--------------------------------------
TIMEOUT.MAX		.EQ 30					30 sec.
CONN.MAX		.EQ 16
*--------------------------------------
ZPIPCfgPtr		.EQ ZPBIN
ZPSktPtr		.EQ ZPBIN+2
ZPLinePtr		.EQ ZPBIN+4
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA 0
				.DA CS.END-CS.START		CS
				.DA DS.END-DS.START		DS
				.DA #64					SS
				.DA #6					ZP
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------				
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.LIBTCPIP		.DA LIBTCPIP
L.Socket		.DA Socket
L.MSG.TCPWAIT	.DA MSG.TCPWAIT
L.MSG.INITOK	.DA MSG.INITOK
L.MSG.TCPIPERR	.DA MSG.TCPIPERR
L.MSG.SKTERR	.DA MSG.SKTERR
L.MSG.ABORT		.DA MSG.ABORT
L.MSG.INCOMING	.DA MSG.INCOMING
L.HTTP.200OK	.DA HTTP.200OK
				.DA 0				
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				sta hLIBTCPIP

				lda (pPs)
				ora #S.PS.F.EVENT		Now accept events
				sta (pPs)
				clc
				rts
*--------------------------------------
CS.RUN			jsr Init.Timeout

				>LDYA L.MSG.TCPWAIT
				>SYSCALL printf
				
.1				>LIBCALL hLIBTCPIP,LIBTCPIP.GET.IPCFG	is TCPIP loaded ?
				bcs .99
				>STYA ZPIPCfgPtr
				
				lda (ZPIPCfgPtr)		Configured ?
				bmi CS.RUN.INIT
				
				>SLEEP
				jsr Wait.Timeout
				bcc .1
				>SYSCALL GetChar
				bcs .1
				cmp #$03
				bne .1
				
				>LDYA L.MSG.ABORT
				>SYSCALL printf
				lda #0
				clc
				rts
				
.99				>LDYA L.MSG.TCPIPERR
				>SYSCALL printf
				lda #E.SYN
				sec	
CS.RUN.RTS		rts
				
CS.RUN.INIT		ldx #3
				ldy #S.IPCFG.IP+3
				
.1				lda (ZPIPCfgPtr),y
				sta Socket.Src.Addr,x
				dey
				dex
				bpl .1
				
				>PUSHW L.Socket
				>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.New
				bcs CS.RUN.RTS

				>STA.G hSrvSocket
				
				>LDYAI 256
				>SYSCALL getmem
				bcs CS.RUN.RTS
				
				>STYA ZPLinePtr
				txa
				>STA.G hLineBuf
				
				>LDYA L.MSG.INITOK
				>SYSCALL printf

CS.RUN.LOOP		>SLEEP
				jsr CS.RUN.SERVER
				bcs CS.RUN.ERR
				
				ldy #SktIndex
				lda (pData),y
				tay
				lda (pData),y			hSockets
				
				beq .3
				
				jsr CS.RUN.CLIENT
				
.3				ldy #SktIndex
				lda (pData),y
				inc
				cmp #CONN.MAX
				bne .4
				lda #0
				
.4				sta (pData),y		
				
				bra CS.RUN.LOOP
				
.9				>LDYA L.MSG.SKTERR
				>SYSCALL printf
				lda #E.SYN
				sec	
				rts
				
CS.RUN.ERR		sec	
				rts
*--------------------------------------
CS.RUN.SERVER	ldy #hSrvSocket
				lda (pData),y
				
				>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.AcceptA
				bcs .8
				pha
				
				ldy #hSockets
				ldx #CONN.MAX
				
.1				lda (pData),y
				beq .2
				iny
				dex
				bne .1
				
				pla
				>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.CloseA
				bra .8
				
.2				pla
				sta (pData),y
				sta .4+1
				
				>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.GetA
				>STYA ZPSktPtr

				ldx #3
				ldy #S.SOCKET.DST.ADDR+3
				
.3				>PUSHB (ZPSktPtr),y
				dey
				dex
				bpl .3
				
.4				lda #$ff
				>PUSHA
				
				>LDYA L.MSG.INCOMING
				>SYSCALL printf
.8				clc
.9				rts
*--------------------------------------
CS.RUN.CLIENT	sta hClntSocket

				>PUSHA					hSocket
				>PUSHW ZPLinePtr
				>PUSHWI 256
				>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.GetS
				bcs .1
				
				>PUSHW ZPLinePtr
				>SYSCALL printf
				
				>PUSHB hClntSocket
				>PUSHW L.HTTP.200OK
				>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.PutS
				
.1				clc
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			ldx #CONN.MAX
				ldy #hSockets
				
.1				lda (pData),y
				beq .2
				
				phx
				phy
				>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.CLOSEA
				ply
				plx
				
.2				iny
				dex
				bne .1
				
				ldy #hSrvSocket
				lda (pData),y
				>LIBCALL hLIBTCPIP,LIBTCPIP.SKT.CLOSEA
				
				>LDA.G hLineBuf
				beq .3
				
				>SYSCALL FreeMem
				
.3				lda hLIBTCPIP
				beq .8
				>SYSCALL UnloadLib
				
.8				clc
				rts
*--------------------------------------
Init.Timeout	ldy #TimeOut
				lda #TIMEOUT.MAX
				sta (pData),y
				rts
*--------------------------------------
Wait.TimeOut	sec
				ldy #TimeOut
				lda (pData),y
				beq .9
				clc
.9				rts
*--------------------------------------
CS.END
*--------------------------------------
LIBTCPIP		.AZ "libtcpip.o"
MSG.TCPWAIT		.AZ "HTTPD:Waiting for TCP/IP initializing...\r\n"
MSG.INITOK		.AZ "HTTPD:Init Ok, Listening.\r\n"
MSG.TCPIPERR	.AZ "HTTPD:TCP/IP Not initialized properly\r\n"
MSG.SKTERR		.AZ "HTTPD:Listen Error\r\n"
MSG.ABORT		.AZ "HTTPD:User Aborted\r\n"
MSG.INCOMING	.AZ "HTTPD:Incoming Connection [SKT=%h] From : %d.%d.%d.%d\r\n"
hLIBTCPIP		.BS 1
hClntSocket		.BS 1
*--------------------------------------
HTTP.200OK		.AZ "HTTP/1.1 200 OK\r\n"
*--------------------------------------
Socket			.DA #S.SOCKET.T.STREAM
				.DA #S.SOCKET.O.ACCEPTCONN
				.DA #S.SOCKET.TCP.S.LISTEN
				.BS 1
Socket.Src.Addr	.BS 4
Socket.Src.Port	.DA 80
Socket.Dst.Addr	.BS 4
Socket.Dst.Port	.BS 2
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
hSockets		.BS	CONN.MAX			pData
hLineBuf		.BS 1
SktIndex		.BS 1
hSrvSocket		.BS 1
TimeOut			.BS 1
DS.END
				.ED
*--------------------------------------
MAN
SAVE USR/SRC/SBIN/HTTPD.S
ASM
