public bool delSocket(Socket s) { lock (socks) socks.Remove(s.FD); s.Dispose(); return(true); }
public bool addSocket(Socket s, SocketUpdateFunc update_func, TcpTransport trans) { s.Info = new SocketInfo { sock = s.FD, func = update_func, transport = trans }; lock (socks) socks.Add(s.FD, s); return true; }
public bool delSocket(Socket s) { lock (socks) socks.Remove(s.FD); s.Dispose(); return true; }
public void close() { DisconnectFunc disconnect_cb = null; lock (close_mutex) { if (!closed) { closed = true; if (poll_set != null) { poll_set.delSocket(sock); } if (sock.Connected) { sock.Shutdown(SocketShutdown.Both); } sock.Close(); sock = null; disconnect_cb = this.disconnect_cb; this.disconnect_cb = null; read_cb = null; write_cb = null; accept_cb = null; } } if (disconnect_cb != null) { disconnect_cb(this); } }
public bool delEvents(Socket s, int events) { if (s != null && s.Info != null) { s.Info.events &= ~events; } return(true); }
public bool addEvents(Socket s, int events) { if (s != null && s.Info != null) { s.Info.events |= events; } return(true); }
public bool addSocket(Socket s, SocketUpdateFunc update_func, TcpTransport trans) { s.Info = new SocketInfo { sock = s.FD, func = update_func, transport = trans }; lock (socks) socks.Add(s.FD, s); return(true); }
public PollSet() { localpipeevents[0] = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); localpipeevents[0].Bind(new IPEndPoint(IPAddress.Loopback, 0)); localpipeevents[1] = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); localpipeevents[1].Connect(localpipeevents[0].LocalEndPoint); localpipeevents[0].Connect(localpipeevents[1].LocalEndPoint); localpipeevents[0].Blocking = false; localpipeevents[1].Blocking = false; addSocket(localpipeevents[0], onLocalPipeEvents); addEvents(localpipeevents[0].FD, POLLIN); }
public bool connect(string host, int port) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connected_host = host; connected_port = port; setNonBlocking(); IPAddress IPA = null; if (!IPAddress.TryParse(host, out IPA)) { foreach (IPAddress ipa in Dns.GetHostAddresses(host).Where(ipa => !ipa.ToString().Contains(":"))) { IPA = ipa; break; } if (IPA == null) { close(); EDB.WriteLine("Couldn't resolve host name [{0}]", host); return(false); } } if (IPA == null) { return(false); } IPEndPoint ipep = new IPEndPoint(IPA, port); LocalEndPoint = ipep; if (!sock.ConnectAsync(new SocketAsyncEventArgs { RemoteEndPoint = ipep })) { return(false); } cached_remote_host = "" + host + ":" + port + " on socket " + sock; while (ROS.ok && !sock.Connected) { } if (!ROS.ok || !initializeSocket()) { return(false); } return(true); }
private bool setKeepAlive(Socket sock, ulong time, ulong interval, ulong count) { try { // resulting structure byte[] SIO_KEEPALIVE_VALS = new byte[3 * bytesperlong]; // array to hold input values ulong[] input = new ulong[4]; // put input arguments in input array if (time == 0 || interval == 0) // enable disable keep-alive { input[0] = (0UL); // off } else { input[0] = (1UL); // on } input[1] = (time); // time millis input[2] = (interval); // interval millis input[3] = count; // pack input into byte struct for (int i = 0; i < input.Length; i++) { SIO_KEEPALIVE_VALS[i * bytesperlong + 3] = (byte)(input[i] >> ((bytesperlong - 1) * bitsperbyte) & 0xff); SIO_KEEPALIVE_VALS[i * bytesperlong + 2] = (byte)(input[i] >> ((bytesperlong - 2) * bitsperbyte) & 0xff); SIO_KEEPALIVE_VALS[i * bytesperlong + 1] = (byte)(input[i] >> ((bytesperlong - 3) * bitsperbyte) & 0xff); SIO_KEEPALIVE_VALS[i * bytesperlong + 0] = (byte)(input[i] >> ((bytesperlong - 4) * bitsperbyte) & 0xff); } // create bytestruct for result (bytes pending on server socket) byte[] result = BitConverter.GetBytes(0); // write SIO_VALS to Socket IOControl sock.IOControl(IOControlCode.KeepAliveValues, SIO_KEEPALIVE_VALS, result); ByteDump(result); } catch (Exception) { return(false); } return(true); }
public bool addSocket(Socket s, SocketUpdateFunc update_func, TcpTransport trans) { SocketInfo info = new SocketInfo { sock = s.FD, func = update_func, transport = trans }; lock (socket_info_mutex) { if (socket_info.ContainsKey(info.sock)) { return(false); } socket_info.Add(info.sock, info); sockets_changed = true; } signal(); return(true); }
public bool delSocket(Socket s) { lock (socket_info_mutex) { uint fd = s.FD; if (!socket_info.ContainsKey(fd)) { return(false); } socket_info.Remove(fd); lock (just_deleted_mutex) { just_deleted.Add(s); } sockets_changed = true; } signal(); return(true); }
public bool listen(int port, int backlog, AcceptCallback accept_cb) { is_server = true; this.accept_cb = accept_cb; sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); setNonBlocking(); sock.Bind(new IPEndPoint(IPAddress.Any, port)); server_port = ((IPEndPoint)sock.LocalEndPoint).Port; sock.Listen(backlog); if (!initializeSocket()) { return(false); } if ((flags & (int)Flags.SYNCHRONOUS) == 0) { enableRead(); } return(true); }
public TcpTransport accept() { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); if (sock == null || !sock.AcceptAsync(args)) { return(null); } if (args.AcceptSocket == null) { EDB.WriteLine("NOTHING TO ACCEPT SO RETURNING NULL!"); return(null); } Socket acc = new Socket(args.AcceptSocket); TcpTransport transport = new TcpTransport(poll_set, flags); if (!transport.setSocket(acc)) { throw new Exception("FAILED TO ADD SOCKET TO TRANSPORT ZOMG!"); } return(transport); }
private bool setSocket(Socket s) { sock = s; return initializeSocket(); }
public TcpTransport accept() { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); if (sock == null || !sock.AcceptAsync(args)) return null; if (args.AcceptSocket == null) { EDB.WriteLine("NOTHING TO ACCEPT SO RETURNING NULL!"); return null; } Socket acc = new Socket(args.AcceptSocket); TcpTransport transport = new TcpTransport(poll_set, flags); if (!transport.setSocket(acc)) { throw new Exception("FAILED TO ADD SOCKET TO TRANSPORT ZOMG!"); } return transport; }
public bool listen(int port, int backlog, AcceptCallback accept_cb) { is_server = true; this.accept_cb = accept_cb; sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); setNonBlocking(); sock.Bind(new IPEndPoint(IPAddress.Any, port)); server_port = ((IPEndPoint) sock.LocalEndPoint).Port; sock.Listen(backlog); if (!initializeSocket()) return false; if ((flags & (int)Flags.SYNCHRONOUS) == 0) enableRead(); return true; }
private bool setKeepAlive(Socket sock, ulong time, ulong interval, ulong count) { try { // resulting structure byte[] SIO_KEEPALIVE_VALS = new byte[3*bytesperlong]; // array to hold input values ulong[] input = new ulong[4]; // put input arguments in input array if (time == 0 || interval == 0) // enable disable keep-alive input[0] = (0UL); // off else input[0] = (1UL); // on input[1] = (time); // time millis input[2] = (interval); // interval millis input[3] = count; // pack input into byte struct for (int i = 0; i < input.Length; i++) { SIO_KEEPALIVE_VALS[i*bytesperlong + 3] = (byte) (input[i] >> ((bytesperlong - 1)*bitsperbyte) & 0xff); SIO_KEEPALIVE_VALS[i*bytesperlong + 2] = (byte) (input[i] >> ((bytesperlong - 2)*bitsperbyte) & 0xff); SIO_KEEPALIVE_VALS[i*bytesperlong + 1] = (byte) (input[i] >> ((bytesperlong - 3)*bitsperbyte) & 0xff); SIO_KEEPALIVE_VALS[i*bytesperlong + 0] = (byte) (input[i] >> ((bytesperlong - 4)*bitsperbyte) & 0xff); } // create bytestruct for result (bytes pending on server socket) byte[] result = BitConverter.GetBytes(0); // write SIO_VALS to Socket IOControl sock.IOControl(IOControlCode.KeepAliveValues, SIO_KEEPALIVE_VALS, result); ByteDump(result); } catch (Exception) { return false; } return true; }
public void update(int poll_timeout) { createNativePollSet(); int udfscount = ufds.Count; //int ret = 0; for (int i = 0; i < ufds.Count; i++) { Socket sock = Socket.Get(ufds[i].sock); if (sock == null || !sock.Connected) { ufds[i].revents |= POLLHUP; } else if (sock == null || sock.SafePoll(poll_timeout, SelectMode.SelectError)) { ufds[i].revents |= POLLERR; } else { if (sock != null && sock.SafePoll(poll_timeout, SelectMode.SelectWrite)) { ufds[i].revents |= POLLOUT; } if (sock != null && sock.SafePoll(poll_timeout, SelectMode.SelectRead)) { ufds[i].revents |= POLLIN; } } } if (udfscount == 0) { return; } for (int i = 0; i < udfscount; i++) { if (ufds[i].revents == 0) { continue; } SocketUpdateFunc func = null; int events = 0; lock (socket_info_mutex) { if (!socket_info.ContainsKey(ufds[i].sock)) { continue; } SocketInfo info = socket_info[ufds[i].sock]; func = info.func; events = info.events; } int revents = ufds[i].revents; if (func != null && ((events & revents) != 0 || (revents & POLLERR) != 0 || (revents & POLLHUP) != 0 || (revents & POLLNVAL) != 0)) { bool skip = false; if ((revents & (POLLERR | POLLHUP | POLLNVAL)) != 0) { lock (just_deleted_mutex) { if (just_deleted.Contains(Socket.Get(ufds[i].sock))) { skip = true; } } } if (!skip) { func(revents & (events | POLLERR | POLLHUP | POLLNVAL)); } } ufds[i].revents = 0; } lock (just_deleted_mutex) { just_deleted.Clear(); } }
public bool connect(string host, int port) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connected_host = host; connected_port = port; if (!setNonBlocking()) throw new Exception("Failed to make socket nonblocking"); setNoDelay(true); IPAddress IPA = null; if (!IPAddress.TryParse(host, out IPA)) { foreach (IPAddress ipa in Dns.GetHostAddresses(host).Where(ipa => !ipa.ToString().Contains(":"))) { IPA = ipa; break; } if (IPA == null) { close(); EDB.WriteLine("Couldn't resolve host name [{0}]", host); return false; } } if (IPA == null) return false; IPEndPoint ipep = new IPEndPoint(IPA, port); LocalEndPoint = ipep; DateTime connectionAttempted = DateTime.Now; IAsyncResult asyncres; lock (this) asyncres = sock.BeginConnect(ipep, iar => { lock(this) if (sock != null) try { sock.EndConnect(iar); } catch (Exception e) { EDB.WriteLine(e); } }, null); bool completed = false; while (ROS.ok && !ROS.shutting_down) { #pragma warning disable 665 if ((completed = asyncres.AsyncWaitHandle.WaitOne(10,false))) #pragma warning restore 665 break; if (DateTime.Now.Subtract(connectionAttempted).TotalSeconds >= 3) { EDB.WriteLine("TRYING TO CONNECT FOR " + DateTime.Now.Subtract(connectionAttempted).TotalSeconds + "s\t: " + this); if (!asyncres.AsyncWaitHandle.WaitOne(100,true)) { sock.Close(); sock = null; } } } if (!completed || sock == null || !sock.Connected) return false; return ROS.ok && initializeSocket(); }
public bool delSocket(Socket s) { lock (socket_info_mutex) { uint fd = s.FD; if (!socket_info.ContainsKey(fd)) return false; socket_info.Remove(fd); lock (just_deleted_mutex) { just_deleted.Add(s); } sockets_changed = true; } signal(); return true; }
public bool connect(string host, int port) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connected_host = host; connected_port = port; setNonBlocking(); IPAddress IPA = null; if (!IPAddress.TryParse(host, out IPA)) { foreach (IPAddress ipa in Dns.GetHostAddresses(host).Where(ipa => !ipa.ToString().Contains(":"))) { IPA = ipa; break; } if (IPA == null) { close(); EDB.WriteLine("Couldn't resolve host name [{0}]", host); return false; } } if (IPA == null) return false; IPEndPoint ipep = new IPEndPoint(IPA, port); LocalEndPoint = ipep; ManualResetEvent connectDone = new ManualResetEvent(false); sock.BeginConnect(ipep, (iar) => { try { sock.EndConnect(iar); connectDone.Set(); } catch (Exception e) { Console.WriteLine(e); } }, null); bool completed = false; while (ROS.ok && !ROS.shutting_down) { #pragma warning disable 665 if ((completed = connectDone.WaitOne(10))) #pragma warning restore 665 break; } if (!completed || !sock.Connected) return false; return ROS.ok && initializeSocket(); }
public bool connect(string host, int port) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connected_host = host; connected_port = port; if (!setNonBlocking()) { throw new Exception("Failed to make socket nonblocking"); } setNoDelay(true); IPAddress IPA = null; if (!IPAddress.TryParse(host, out IPA)) { foreach (IPAddress ipa in Dns.GetHostAddresses(host).Where(ipa => !ipa.ToString().Contains(":"))) { IPA = ipa; break; } if (IPA == null) { close(); EDB.WriteLine("Couldn't resolve host name [{0}]", host); return(false); } } if (IPA == null) { return(false); } IPEndPoint ipep = new IPEndPoint(IPA, port); LocalEndPoint = ipep; DateTime connectionAttempted = DateTime.Now; IAsyncResult asyncres; lock (this) asyncres = sock.BeginConnect(ipep, iar => { lock (this) if (sock != null) { try { sock.EndConnect(iar); } catch (Exception e) { EDB.WriteLine(e); } } }, null); bool completed = false; while (ROS.ok && !ROS.shutting_down) { #pragma warning disable 665 if ((completed = asyncres.AsyncWaitHandle.WaitOne(10, false))) #pragma warning restore 665 { break; } if (DateTime.Now.Subtract(connectionAttempted).TotalSeconds >= 3) { EDB.WriteLine("TRYING TO CONNECT FOR " + DateTime.Now.Subtract(connectionAttempted).TotalSeconds + "s\t: " + this); if (!asyncres.AsyncWaitHandle.WaitOne(100, true)) { sock.Close(); sock = null; } } } if (!completed || sock == null || !sock.Connected) { return(false); } return(ROS.ok && initializeSocket()); }
private bool setSocket(Socket s) { sock = s; return(initializeSocket()); }
public void close() { DisconnectFunc disconnect_cb = null; lock (close_mutex) { if (!closed) { closed = true; if (poll_set != null) poll_set.delSocket(sock); if (sock.Connected) sock.Shutdown(SocketShutdown.Both); sock.Close(); sock = null; disconnect_cb = this.disconnect_cb; this.disconnect_cb = null; read_cb = null; write_cb = null; accept_cb = null; } } if (disconnect_cb != null) { disconnect_cb(this); } }
public bool addSocket(Socket s, SocketUpdateFunc update_func) { return(addSocket(s, update_func, null)); }
public bool delEvents(Socket s, int events) { if (s != null && s.Info != null) s.Info.events &= ~events; return true; }
public bool addEvents(Socket s, int events) { if (s != null && s.Info != null) s.Info.events |= events; return true; }
public bool addSocket(Socket s, SocketUpdateFunc update_func) { return addSocket(s, update_func, null); }
public bool addSocket(Socket s, SocketUpdateFunc update_func, TcpTransport trans) { SocketInfo info = new SocketInfo {sock = s.FD, func = update_func, transport = trans}; lock (socket_info_mutex) { if (socket_info.ContainsKey(info.sock)) return false; socket_info.Add(info.sock, info); sockets_changed = true; } signal(); return true; }
public bool connect(string host, int port) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connected_host = host; connected_port = port; setNonBlocking(); IPAddress IPA = null; if (!IPAddress.TryParse(host, out IPA)) { foreach (IPAddress ipa in Dns.GetHostAddresses(host).Where(ipa => !ipa.ToString().Contains(":"))) { IPA = ipa; break; } if (IPA == null) { close(); EDB.WriteLine("Couldn't resolve host name [{0}]", host); return(false); } } if (IPA == null) { return(false); } IPEndPoint ipep = new IPEndPoint(IPA, port); LocalEndPoint = ipep; ManualResetEvent connectDone = new ManualResetEvent(false); sock.BeginConnect(ipep, iar => { try { sock.EndConnect(iar); connectDone.Set(); } catch (Exception e) { Console.WriteLine(e); } }, null); bool completed = false; while (ROS.ok && !ROS.shutting_down) { #pragma warning disable 665 if ((completed = connectDone.WaitOne(10))) #pragma warning restore 665 { break; } } if (!completed || !sock.Connected) { return(false); } return(ROS.ok && initializeSocket()); }