Darkover
Angelic
- Jul 29, 2021
- 4,631
trying to do some coding even tho i've got a brain injury its hard to remember everything going to code a simple chat program with a server and clients to send messages for windows 10
this is what i've managed to achive so far using chat gpt in the euphoria programming lanaguage
i used chat gpt to create a circular buffer for me and built from there
circular buffer
server code
client code
this is what i've managed to achive so far using chat gpt in the euphoria programming lanaguage
i used chat gpt to create a circular buffer for me and built from there
circular buffer
C:
-- Define the size of the circular buffer
-- Define the size of the circular buffer
constant BUFFER_SIZE = 100
-- Define the circular buffer and the read and write indices
sequence buffer
buffer= repeat(0, BUFFER_SIZE)
integer read_index,write_index,read_size
read_index=0
write_index = 0
read_size=0
-- Function to write a packet to the circular buffer
global procedure write_packet(sequence packet)
-- Check if there is enough space in the buffer for the packet
if length(packet) > BUFFER_SIZE - write_index then
-- Not enough space, move the write index to the beginning of the buffer
read_size-= write_index-read_index
write_index = 0
end if
-- Copy the packet to the buffer
for i = 1 to length(packet) do
buffer[write_index + i] = packet[i]
end for
-- Update the write index
write_index += length(packet)
read_size+=length(packet)
end procedure
-- Function to write a packet to the circular buffer
global procedure write_byte(integer packet)
-- Check if there is enough space in the buffer for the packet
if 1 > BUFFER_SIZE - write_index then
-- Not enough space, move the write index to the beginning of the buffer
read_size-= write_index-read_index
write_index = 0
end if
-- Copy the packet to the buffer
buffer[write_index +1] = packet
-- Update the write index
write_index += 1
read_size+=1
end procedure
global procedure writeutf(sequence v)
sequence s
s=int_to_bytes(length(v))
write_packet(s[2]&s[1]&v)
end procedure
global procedure writeint(atom v)
sequence s
s=int_to_bytes(v)
write_packet(s[4]&s[3]&s[2]&s[1])
end procedure
global procedure writebool(integer b)
write_packet(b)
end procedure
global procedure writeshort( atom v)
sequence s
s=int_to_bytes(v)
write_packet(s[2]&s[1])
end procedure
global procedure writebyte(integer v)
write_byte(v)
end procedure
-- Function to read a packet from the circular buffer
global function read_packet(integer size)
sequence packet
packet = {}
-- Check if there is enough data in the buffer to read a packet
if size > BUFFER_SIZE - read_index then
-- Not enough data, move the read index to the beginning of the buffer
read_index = 0
end if
-- Read the packet from the buffer
for i = 1 to size do
packet&= buffer[read_index + i]
if read_index+i> BUFFER_SIZE then
-- Not enough data, move the read index to the beginning of the buffer
read_index = 0
end if
end for
-- Update the read index
read_index += size
read_size-=size
return packet
end function
global function read_byte()
-- Check if there is enough data in the buffer to read a packet
if 1 > BUFFER_SIZE - read_index then
-- Not enough data, move the read index to the beginning of the buffer
read_index = 0
end if
-- Read the packet from the buffer
if read_index+1> BUFFER_SIZE then
-- Not enough data, move the read index to the beginning of the buffer
read_index = 0
end if
-- Update the read index
read_index += 1
read_size-=1
return buffer[read_index ]
end function
global function input_stream_readutf()
integer len
len=bytes_to_int( reverse(read_packet(2))&{0,0} )
return read_packet(len)
end function
global function input_stream_readint()
atom v
v=bytes_to_int( reverse(read_packet(4)) )
return v
end function
global function input_stream_readfloat()
atom v
v=float32_to_atom( reverse(read_packet(4)) )
return v
end function
global function input_stream_readshort()
atom v
v=bytes_to_int( reverse(read_packet(2))&{0,0} )
return v
end function
global function input_stream_readbyte()
return read_byte()
end function
global function input_stream_readbool()
return and_bits(read_packet(1),#1)
end function
global function buffer_size()
return read_size
end function
server code
C:
-- code generated by Win32Lib IDE v0.21.1 Build Sept-29-2006
constant TheProgramType="exw"
include Win32Lib.ew
without warning
--------------------------------------------------------------------------------
-- Window Window1
constant Window1 = createEx( Window, "server", 0, Default, Default, 400, 300, 0, 0 )
constant PushButton2 = createEx( PushButton, "PushButton2", Window1, 96, 136, 88, 28, 0, 0 )
---------------------------------------------------------
--------------------------------------------------------------------------------
include SrvSckIP.ew
include buffer.ew
atom hWndMainWindow,Server_sock
global constant SM_ASYNC = #FFFF
integer hello_packet
hello_packet =1
sequence client_list
client_list={}
writeutf("testing")
writeint(1234)
writeshort(999)
writebyte(245)
writebyte(246)
puts(1, input_stream_readutf())
puts(1, sprint(input_stream_readint()))
puts(1, sprint(input_stream_readshort()))
puts(1, sprint(input_stream_readbyte()))
puts(1, sprint(input_stream_readbyte()))
puts(1,"buffer size \n")
?buffer_size()
procedure processServerMessage(atom hWnd, atom arg1, atom arg2)
atom index, newsock
integer action, sock, iwork
object owork,junk
sequence newIP,swork,rx,tx
sequence data,temp
action = lo_word(arg2)
sock = arg1
puts(1,"process server messafe\n")
if action=FD_ACCEPT then
puts(1,"SERVER ACCEPT\n")
newsock = WsockAccept(Server_sock)
client_list&=newsock
elsif action=FD_CONNECT then
puts(1,"SERVER connect\n")
elsif action = FD_WRITE then
puts(1,"fd-write")
?sock
?WsockSendData(sock,repeat(1,1024))
elsif action = FD_READ then
puts(1,"server packet recived\n")
rx = ""
owork = WsockReadData_1024(sock,1024)
while sequence(owork) do
rx = rx & owork
owork = WsockReadData_1024(sock,1024)
end while
if length(rx) > 0 then
end if
elsif action = FD_CLOSE then
puts(1,"server socket closed\n")
for i=1 to length(client_list) do
end for
WsockCloseSocket(sock)
end if
end procedure
--------------------------------------------------------------------------------
procedure Window1_onEvent (integer self, integer event, sequence params)--params is ( int iMsg, atom wParm, atom lParm )
if params[1] = SM_ASYNC then
processServerMessage(hWndMainWindow,params[2],params[3])
end if
end procedure
setHandler( Window1, w32HEvent, routine_id("Window1_onEvent"))
--------------------------------------------------------------------------------
procedure Window1_onOpen (integer self, integer event, sequence params)--params is ()
integer iwork, port
sequence swork,ip
atom Server
port = 2050
iwork = WsockInit()
if iwork = -1 then -- WinSock failed
iwork = message_box("WSockInit() failed!","Error",MB_TASKMODAL + MB_ICONSTOP + MB_OK)
abort(99)
end if
hWndMainWindow = getHandle(Window1)
Server_sock = WsockListenAsync(port, hWndMainWindow, SM_ASYNC,or_all({FD_READ,FD_ACCEPT,FD_CLOSE,FD_CONNECT}))
if Server_sock = SOCKET_ERROR then -- WinSock failed
iwork = message_box("WsockListenAsync() failed!","Error",MB_TASKMODAL+MB_ICONSTOP+MB_OK)
abort(99)
end if
end procedure
setHandler( Window1, w32HOpen, routine_id("Window1_onOpen"))
--------------------------------------------------------------------------------
procedure PushButton2_onClick (integer self, integer event, sequence params)--params is ()
for i=1 to length(client_list) do
?WsockSendData(client_list[i],repeat(1,1024))
?client_list[i]
end for
end procedure
setHandler( PushButton2, w32HClick, routine_id("PushButton2_onClick"))
WinMain( Window1,Normal )
--this program has 133 lines without including this line. If there is a discrepancy please send this file zipped to Judith.
client code
C:
-- code generated by Win32Lib IDE v0.21.1 Build Sept-29-2006
constant TheProgramType="exw"
include Win32Lib.ew
without warning
--------------------------------------------------------------------------------
-- Window Window1
constant Window1 = createEx( Window, "client", 0, Default, Default, 400, 300, 0, 0 )
constant PushButton2 = createEx( PushButton, "PushButton2", Window1, 92, 124, 88, 28, 0, 0 )
constant PushButton3 = createEx( PushButton, "PushButton3", Window1, 244, 136, 88, 28, 0, 0 )
---------------------------------------------------------
--------------------------------------------------------------------------------
include SrvSckIP.ew
include buffer.ew
atom hWndMainWindow,Server_sock
global constant SM_ASYNC = #FFFF
constant port=2050
constant ip="127.0.0.1"
integer hello_packet
hello_packet =1
procedure connect_to_server()
integer iwork
sequence swork
iwork = WsockInit()
if iwork = -1 then -- WinSock failed
iwork = message_box("WSockInit() failed!","Error",MB_TASKMODAL + MB_ICONSTOP + MB_OK)
abort(99)
end if
hWndMainWindow = getHandle(Window1)
Server_sock=Wsock_new(ip, port , hWndMainWindow, SM_ASYNC,or_all({FD_READ,FD_CLOSE,FD_CONNECT}))
end procedure
procedure processServerMessage(atom hWnd, atom arg1, atom arg2)
atom index, newsock
integer action, sock, iwork
object owork,junk
sequence newIP,swork,rx,tx
sequence data,temp
action = lo_word(arg2)
sock = arg1
if action=FD_CONNECT then
puts(1,"client connected\n")
?sock
?Server_sock
elsif action = FD_READ then
puts(1,"packet recived\n")
rx = ""
owork = WsockReadData_1024(sock,1024)
while sequence(owork) do
rx = rx & owork
owork = WsockReadData_1024(sock,1024)
end while
if length(rx) > 0 then
end if
elsif action = FD_CLOSE then
puts(1,"client closed\n")
WsockCloseSocket(sock)
end if
end procedure
--------------------------------------------------------------------------------
procedure Window1_onEvent (integer self, integer event, sequence params)--params is ( int iMsg, atom wParm, atom lParm )
if params[1] = SM_ASYNC then
processServerMessage(hWndMainWindow,params[2],params[3])
end if
end procedure
setHandler( Window1, w32HEvent, routine_id("Window1_onEvent"))
--------------------------------------------------------------------------------
procedure PushButton2_onClick (integer self, integer event, sequence params)--params is ()
connect_to_server()
end procedure
setHandler( PushButton2, w32HClick, routine_id("PushButton2_onClick"))
--------------------------------------------------------------------------------
procedure PushButton3_onClick (integer self, integer event, sequence params)--params is ()
?WsockSendData(Server_sock,repeat(1,1024))
?Server_sock
end procedure
setHandler( PushButton3, w32HClick, routine_id("PushButton3_onClick"))
WinMain( Window1,Normal )
--this program has 103 lines without including this line. If there is a discrepancy please send this file zipped to Judith.
Last edited: