delphi - Convert From indy udp To Tcp -
i have been using indy udp , want move on indy tcp
but dont know how convert code work in same way indy tcp
my project work send stream chat room here udp code
procedure tform1.recorderdata(sender: tobject; const buffer: pointer; buffersize: cardinal; var freeit: boolean); begin freeit :=true; if idudpclient.active idudpclient.sendbuffer(rawtobytes(buffer^, buffersize)) else stop.caption := 'error'; end;
and server on read event
procedure tform1.udpreceiverudpread(athread: tidudplistenerthread; const adata: tidbytes; abinding: tidsockethandle); var audiodatasize: integer; audiodata : pointer; begin try entercriticalsection(section); try audiodatasize := length(adata); if audiodatasize > 10 begin try if not player.active begin player.active := true; player.waitforstart; end; except end; if blockalign > 1 dec(audiodatasize, audiodatasize mod blockalign); audiodata := audiobuffer.beginupdate(audiodatasize); try bytestoraw(adata, audiodata^, audiodatasize); audiobuffer.endupdate; end; end else begin player.active := false; player.waitforstop; end; leavecriticalsection(section); end; except end; end;
how make them work in same way in indy tcp ?
indy's udp , tcp components use different interface architectures. not enough port code, have re-design communication protocol accordingly, may require re-write core code logic. remember udp message-based, tcp not, have design own message framing.
you have take account tidudpserver
, tidtcpserver
multi-threaded. unlike tidudpserver
, tidtcpserver
not have threadedevent
property, have provide own synchronizing when accessing other threads, main ui thread.
based on simple example, try this:
procedure tform1.recorderdata(sender: tobject; const buffer: pointer; buffersize: cardinal; var freeit: boolean); begin freeit := true; try if idtcpclient1.connected begin idtcpclient1.iohandler.write(buffersize); idtcpclient1.iohandler.write(rawtobytes(buffer^, buffersize)); exit; end; except end; stop.caption := 'error'; end;
procedure tform1.idtcpserver1execute(acontext: tidcontext); var audiodatasize: integer; audiodatabytes: tidbytes; audiodata: pointer; begin audiodatasize := acontext.connection.iohandler.readlongint(); acontext.connection.iohandler.readbytes(audiodatabytes, audiodatasize); entercriticalsection(section); try if audiodatasize > 10 begin try if not player.active begin player.active := true; player.waitforstart; end; except end; if blockalign > 1 dec(audiodatasize, audiodatasize mod blockalign); audiodata := audiobuffer.beginupdate(audiodatasize); try bytestoraw(audiodatabytes, audiodata^, audiodatasize); audiobuffer.endupdate; end; end else begin player.active := false; player.waitforstop; end; leavecriticalsection(section); end; end;
Comments
Post a Comment