localaddr:string;\r
localport:string;\r
proto:string;\r
- udp:boolean;\r
+ udp,dgram:boolean;\r
listenqueue:integer;\r
{$ifdef secondlistener}\r
secondlistener:tlsocket;\r
procedure secondaccepthandler(sender:tobject;error:word);\r
procedure internalclose(error:word);override;\r
{$endif}\r
- procedure connectionfailedhandler(error:word);\r
- procedure connecttimeouthandler(sender:tobject);\r
- procedure connectsuccesshandler;\r
function getaddrsize:integer;\r
procedure connect; virtual;\r
procedure realconnect;\r
function getXport:string; virtual;\r
function getpeerport:string; virtual;\r
constructor Create(AOwner: TComponent); override;\r
+\r
+ //this one has to be kept public for now because lcorewsaasyncselect calls it\r
+ procedure connectionfailedhandler(error:word);\r
+ private\r
+ procedure taskcallconnectionfailedhandler(wparam,lparam : longint);\r
+\r
+ procedure connecttimeouthandler(sender:tobject);\r
+ procedure connectsuccesshandler;\r
{$ifdef win32}\r
procedure myfdclose(fd : integer); override;\r
function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;\r
\r
procedure tlsocket.realconnect;\r
var\r
- a:integer;\r
-\r
+ a,b:integer;\r
begin\r
-// writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);\r
+ //writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);\r
makeinaddrv(biniplist_get(biniplist,currentip),port,inaddr);\r
inc(currentip);\r
if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;\r
- udp := uppercase(proto) = 'UDP';\r
- if udp then a := SOCK_DGRAM else a := SOCK_STREAM;\r
- a := Socket(inaddr.inaddr.family,a,0);\r
+\r
+ udp := false;\r
+ if (uppercase(proto) = 'UDP') then begin\r
+ b := IPPROTO_UDP;\r
+ a := SOCK_DGRAM;\r
+ udp := true;\r
+ dgram := true;\r
+ end else if (uppercase(proto) = 'TCP') or (uppercase(proto) = '') then begin\r
+ b := IPPROTO_TCP;\r
+ a := SOCK_STREAM;\r
+ dgram := false;\r
+ end else if (uppercase(proto) = 'ICMP') or (strtointdef(proto,256) < 256) then begin\r
+ b := strtointdef(proto,IPPROTO_ICMP);\r
+ a := SOCK_RAW;\r
+ dgram := true;\r
+ end else begin\r
+ raise ESocketException.create('unrecognised protocol');\r
+ end;\r
+\r
+ a := Socket(inaddr.inaddr.family,a,b);\r
//writeln(ord(inaddr.inaddr.family));\r
if a = -1 then begin\r
- lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
- raise esocketexception.create('unable to create socket');\r
+ //unable to create socket, fire an error event (better to use an error event\r
+ //to avoid poor interaction with multilistener stuff.\r
+ //a socket value of -2 is a special value to say there is no socket but\r
+ //we want internalclose to act as if there was\r
+ fdhandlein := -2;\r
+ fdhandleout := -2;\r
+ tltask.create(taskcallconnectionfailedhandler,self,{$ifdef win32}wsagetlasterror{$else}socketerror{$endif},0);\r
+ exit;\r
end;\r
try\r
dup(a);\r
bindsocket;\r
- if udp then begin\r
+ if dgram then begin\r
{$ifndef win32}\r
SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
+ {$else}\r
+ SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
{$endif}\r
state := wsconnected;\r
if assigned(onsessionconnected) then onsessionconnected(self,0);\r
currentip := 0;\r
if not assigned(connecttimeout) then begin\r
connecttimeout := tltimer.create(self);\r
- connecttimeout.Tag := integer(self);\r
connecttimeout.ontimer := connecttimeouthandler;\r
connecttimeout.interval := 2500;\r
connecttimeout.enabled := false;\r
\r
procedure tlsocket.sendstr(const str : string);\r
begin\r
- if udp then begin\r
+ if dgram then begin\r
send(@str[1],length(str))\r
end else begin\r
inherited sendstr(str);\r
\r
function tlsocket.send(data:pointer;len:integer):integer;\r
begin\r
- if udp then begin\r
+ if dgram then begin\r
// writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');\r
result := sendto(inaddr,getaddrsize,data,len);\r
\r
\r
function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;\r
begin\r
- if udp then begin\r
+ if dgram then begin\r
{$ifdef secondlistener}\r
if lastsessionfromsecond then begin\r
result := secondlistener.receive(buf,bufsize);\r
localaddr := '0.0.0.0';\r
end;\r
//gethostbyname(localaddr,host);\r
-\r
inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);\r
\r
If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin\r
begin\r
if state <> wsclosed then close;\r
udp := uppercase(proto) = 'UDP';\r
- if udp then socktype := SOCK_DGRAM else socktype := SOCK_STREAM;\r
+ if udp then begin\r
+ socktype := SOCK_DGRAM;\r
+ dgram := true;\r
+ end else socktype := SOCK_STREAM;\r
origaddr := addr;\r
\r
if addr = '' then begin\r
end;\r
{$endif}\r
\r
- if fdhandlein = -1 then raise ESocketException.create('unable to create socket');\r
+ if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' error='+inttostr(wsagetlasterror){$endif});\r
dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things\r
//eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup\r
state := wsclosed; // then set this back as it was an undesired side effect of dup\r
if not udp then begin\r
{!!! allow custom queue length? default 5}\r
if listenqueue = 0 then listenqueue := 5;\r
- If {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise esocketexception.create('unable to listen');\r
+ If {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise\r
+esocketexception.create('unable to listen');\r
state := wsListening;\r
end else begin\r
{$ifndef win32}\r
SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
+ {$else}\r
+ SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
{$endif}\r
state := wsconnected;\r
end;\r
srclen := tempsrclen;\r
end;\r
\r
+procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint);\r
+begin\r
+ connectionfailedhandler(wparam);\r
+end;\r
+\r
procedure tlsocket.connectionfailedhandler(error:word);\r
begin\r
if trymoreips then begin\r
eventcore.rmasterclr(fdhandlein);\r
if assigned(onsessionAvailable) then onsessionAvailable(self,0);\r
end;\r
- if udp and readtrigger then begin\r
+ if dgram and readtrigger then begin\r
if assigned(ondataAvailable) then ondataAvailable(self,0);\r
{!!!test}\r
exit;\r