public void async_connect(string ip, int port, functional.func <socket_result> cb) { try { _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.BeginConnect(IPAddress.Parse(ip), port, delegate(IAsyncResult ar) { socket_result res = new socket_result(); try { _socket.EndConnect(ar); _socket.NoDelay = true; res.ok = true; } catch (System.Exception ec) { res.message = ec.Message; } cb(res); }, null); } catch (System.Exception) { close(); cb(new socket_result()); } }
void _async_write(int index, int currTotal, IList <ArraySegment <byte> > buffs, functional.func <socket_result> cb) { async_write(buffs[index], delegate(socket_result tempRes) { if (tempRes.ok) { index++; currTotal += tempRes.s; if (index == buffs.Count) { socket_result result = new socket_result(); result.s = currTotal; result.ok = true; cb(result); } else { _async_write(index, currTotal, buffs, cb); } } else { socket_result res = new socket_result(); res.s = currTotal; res.message = tempRes.message; cb(res); } }); }
public void async_accept(socket_tcp sck, functional.func <socket_result> cb) { try { _socket.BeginAccept(delegate(IAsyncResult ar) { socket_result res = new socket_result(); try { sck._socket = _socket.EndAccept(ar); sck._socket.NoDelay = true; res.ok = true; } catch (System.Exception ec) { res.message = ec.Message; } cb(res); }, null); } catch (System.Exception) { close(); cb(new socket_result()); } }
void _async_write(int currTotal, ArraySegment <byte> buff, functional.func <socket_result> cb) { async_write_same(buff, delegate(socket_result tempRes) { if (tempRes.ok) { currTotal += tempRes.s; if (buff.Count == currTotal) { socket_result result = new socket_result(); result.s = buff.Count; result.ok = true; cb(result); } else { _async_write(currTotal, new ArraySegment <byte>(buff.Array, buff.Offset + tempRes.s, buff.Count - tempRes.s), cb); } } else { socket_result res = new socket_result(); res.s = currTotal; res.message = tempRes.message; cb(res); } }); }
public void async_read_same(IList <ArraySegment <byte> > buffs, functional.func <socket_result> cb) { try { _socket.BeginReceive(buffs, 0, delegate(IAsyncResult ar) { socket_result res = new socket_result(); try { res.s = _socket.EndReceive(ar); res.ok = true; } catch (System.Exception ec) { res.message = ec.Message; } cb(res); }, null); } catch (System.Exception) { cb(new socket_result()); } }