diff options
author | Yorhel <git@yorhel.nl> | 2011-01-03 18:59:28 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2011-01-03 18:59:28 +0100 |
commit | ab6e3ead83707a7029f9bccc276d31d6f423a8e2 (patch) | |
tree | da3aa06585acc3031ef8f027692e9c616f1b0ea1 | |
parent | 2fec430001eee2a6c803876f643286080b10d6fd (diff) |
VndbApi: Check for remote disconnect when sending/receiving data
-rw-r--r-- | vndbApi.ml | 26 |
1 files changed, 19 insertions, 7 deletions
@@ -97,18 +97,22 @@ class connection in let rec dorecv () = let cnt = recv s buf 0 (String.length buf) [] in - reply := !reply ^ (String.sub buf 0 cnt); - match String.contains !reply '\004' with - | true -> finish () - | false -> pollfunc s true dorecv + if cnt == 0 then self#disconnect else ( + reply := !reply ^ (String.sub buf 0 cnt); + match String.contains !reply '\004' with + | true -> finish () + | false -> pollfunc s true dorecv + ) in let dosend () = (* assumes the command can be sent with a single call *) - let _ = send s + let cnt = send s (cmd ^ (String.make 1 (char_of_int 4))) 0 (String.length cmd + 1) [] in - match linefunc with None -> () | Some f -> f true cmd; - pollfunc s true dorecv + if cnt == 0 then self#disconnect else ( + match linefunc with None -> () | Some f -> f true cmd; + pollfunc s true dorecv + ) in pollfunc s false dosend ) @@ -154,6 +158,14 @@ class connection method disconnect = + (* we'll have to clear the comamnd queue when disconnected, since we need to + * be able to issue the login command the first thing after a reconnect... + * It might be an idea to either special-case the login command or call all + * registered callback functions with an error. + * Behaviour is currently undefined when disconnect is called while a + * command is being handled. *) + Queue.clear cmd_queue; + cmd_running <- false; match s_sock with None -> () | Some s -> close s; s_sock <- None; match disconnectfunc with None -> () | Some f -> f () |