public void ConnectAsync(string uri, Action <bool> callback) { if (m_socket == null) { CreateSocket(uri); } string[] splits = uri.Split(':'); IPHostEntry ipHostInfo = Dns.GetHostEntry(splits[0]); IPAddress ipAddress = null; foreach (var address in ipHostInfo.AddressList) { if (address.AddressFamily == AddressFamily.InterNetwork) { ipAddress = address; break; } } IPEndPoint remoteEP = new IPEndPoint(ipAddress, int.Parse(splits[1])); var state = new SocketCallbackObject { WorkSocket = m_socket, Callback = callback }; m_socket.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), state); }
public void SendAsync(byte[] data, Action <bool> completed) { if (m_socket != null) { int bodyLength = data.Length; byte[] buf = new byte[HeaderLength + bodyLength]; byte[] lengthBytes = BitConverter.GetBytes((ushort)bodyLength); if (BitConverter.IsLittleEndian) { Array.Reverse(lengthBytes, 0, lengthBytes.Length); } Buffer.BlockCopy(lengthBytes, 0, buf, 0, lengthBytes.Length); Buffer.BlockCopy(data, 0, buf, HeaderLength, data.Length); var state = new SocketCallbackObject { WorkSocket = m_socket, Callback = completed }; m_socket.BeginSend(buf, 0, buf.Length, SocketFlags.None, new AsyncCallback(SendCallback), state); } else { if (completed != null) { completed(false); } } }