protected override void HandleRead() { int size, error; IPEndPoint source; if (parent.IsConnected) { size = SocketFunctions.manos_socket_receive(Handle.ToInt32(), buffer, buffer.Length, out error); source = parent.RemoteEndpoint; } else { ManosIPEndpoint ep; size = SocketFunctions.manos_socket_receivefrom_ip(Handle.ToInt32(), buffer, buffer.Length, out ep, out error); source = ep; } if (size < 0 && error != 0) { RaiseError(Errors.SocketStreamFailure("Read failure", error)); Close(); } else { RaiseData(buffer, size, source); } }
public override void Connect(IPEndPoint endpoint, Action callback, Action <Exception> error) { CheckDisposed(); if (endpoint == null) { throw new ArgumentNullException("endpoint"); } if (callback == null) { throw new ArgumentNullException("callback"); } if (error == null) { throw new ArgumentNullException("error"); } int err; ManosIPEndpoint ep = endpoint; err = SocketFunctions.manos_socket_connect_ip(fd, ref ep, out err); if (err != 0) { throw Errors.SocketFailure("Connect failure", err); } else { localname = endpoint; } IsConnected = true; callback(); }
public override void Connect(IPEndPoint endpoint, Action callback, Action <Exception> error) { CheckDisposed(); if (endpoint == null) { throw new ArgumentNullException("endpoint"); } if (callback == null) { throw new ArgumentNullException("callback"); } if (error == null) { throw new ArgumentNullException("error"); } if (localname != null && localname.AddressFamily != endpoint.AddressFamily) { throw new ArgumentException(); } if (IsConnected) { throw new InvalidOperationException(); } int err; ManosIPEndpoint ep = endpoint; err = SocketFunctions.manos_socket_connect_ip(fd, ref ep, out err); if (err != 0) { throw Errors.SocketFailure("Connect failure", err); } else { var connectWatcher = new IOWatcher(new IntPtr(fd), EventTypes.Write, Context.Loop, (watcher, revents) => { watcher.Stop(); watcher.Dispose(); var result = SocketFunctions.manos_socket_peername_ip(fd, out ep, out err); if (result < 0) { error(Errors.SocketFailure("Connect failure", err)); } else { peername = endpoint; IsConnected = true; callback(); } }); connectWatcher.Start(); } }
protected override void Dispose(bool disposing) { if (fd != 0) { int err; SocketFunctions.manos_socket_close(fd, out err); fd = 0; } base.Dispose(disposing); }
protected IPSocket(Context context, AddressFamily addressFamily, ProtocolFamily protocolFamily) : base(context, addressFamily) { int err; fd = SocketFunctions.manos_socket_create((int)addressFamily, (int)protocolFamily, out err); if (fd < 0) { throw Errors.SocketFailure("Could not create socket", err); } }
protected override WriteResult WriteSingleFragment(ByteBuffer buffer) { int err; int sent = SocketFunctions.manos_socket_send(Handle.ToInt32(), buffer.Bytes, buffer.Position, buffer.Length, out err); if (sent < 0 && err != 0) { RaiseError(Errors.SocketStreamFailure("Write failure", err)); return(WriteResult.Error); } else { buffer.Skip(sent); return(buffer.Length == 0 ? WriteResult.Consume : WriteResult.Continue); } }
protected override void HandleRead() { int err; var received = SocketFunctions.manos_socket_receive(Handle.ToInt32(), receiveBuffer, receiveBuffer.Length, out err); if (received < 0 && err != 0 || received == 0) { if (received < 0) { RaiseError(Errors.SocketStreamFailure("Read failure", err)); } Close(); } else { byte [] newBuffer = new byte [received]; Buffer.BlockCopy(receiveBuffer, 0, newBuffer, 0, received); RaiseData(new ByteBuffer(newBuffer)); } }
public override void Bind(IPEndPoint endpoint) { CheckDisposed(); if (endpoint == null) { throw new ArgumentNullException("endpoint"); } int err; ManosIPEndpoint ep = endpoint; var result = SocketFunctions.manos_socket_bind_ip(fd, ref ep, out err); if (result < 0) { throw Errors.SocketFailure("Could not bind to address", err); } else { localname = endpoint; } IsBound = true; }
protected override WriteResult WriteSingleFragment(UdpPacket packet) { int len, error; if (parent.IsConnected) { len = SocketFunctions.manos_socket_send(Handle.ToInt32(), packet.Buffer.Bytes, packet.Buffer.Position, packet.Buffer.Length, out error); } else { ManosIPEndpoint ep = new IPEndPoint(IPAddress.Parse(packet.Address), packet.Port); len = SocketFunctions.manos_socket_sendto_ip(Handle.ToInt32(), packet.Buffer.Bytes, packet.Buffer.Position, packet.Buffer.Length, ref ep, out error); } if (len < 0) { RaiseError(Errors.SocketStreamFailure("Write failure", error)); return(WriteResult.Error); } return(WriteResult.Consume); }
public void Listen(int backlog, Action <ITcpSocket> callback) { CheckDisposed(); if (stream != null) { throw new InvalidOperationException(); } if (listener != null) { listener.Stop(); listener.Dispose(); } int error; var result = SocketFunctions.manos_socket_listen(fd, backlog, out error); if (result < 0) { throw Errors.SocketFailure("Listen failure", error); } listener = new IOWatcher(new IntPtr(fd), EventTypes.Read, Context.Loop, delegate { ManosIPEndpoint ep; var client = SocketFunctions.manos_socket_accept(fd, out ep, out error); if (client < 0 && error != 0) { throw new Exception(string.Format("Error while accepting: {0}", Errors.ErrorToString(error))); } else if (client > 0) { var socket = new TcpSocket(Context, AddressFamily, client, LocalEndpoint, ep); callback(socket); } }); listener.Start(); }