public void Run() { Logger.LogInfo(string.Format("TaskRunner [{0}] is running ...", trId)); try { while (!stop) { using (var networkStream = socket.GetStream()) { byte[] bytes = SerDe.ReadBytes(networkStream, sizeof(int)); if (bytes != null) { int splitIndex = SerDe.ToInt(bytes); bool readComplete = Worker.ProcessStream(networkStream, splitIndex); if (!readComplete) // if the socket is not read through completely, then it can't be reused { stop = true; // wait for server to complete, otherwise server may get 'connection reset' exception Logger.LogInfo("Sleep 500 millisecond to close socket ..."); Thread.Sleep(500); } else if (!socketReuse) { stop = true; // wait for server to complete, otherwise server gets 'connection reset' exception // Use SerDe.ReadBytes() to detect java side has closed socket properly // ReadBytes() will block until the socket is closed Logger.LogInfo("waiting JVM side to close socket..."); SerDe.ReadBytes(networkStream); Logger.LogInfo("JVM side has closed socket"); } } else { stop = true; Logger.LogWarn("read null splitIndex, socket is closed by JVM"); } } } } catch (Exception e) { stop = true; Logger.LogError(string.Format("TaskRunner [{0}] exeption, will dispose this TaskRunner", trId)); Logger.LogException(e); } finally { try { socket.Close(); } catch (Exception ex) { Logger.LogWarn(string.Format("close socket exception: ex", ex)); } Logger.LogInfo(string.Format("TaskRunner [{0}] finished", trId)); } }
private void Dispose(bool disposing) { if (disposing) { m_cli.Close(); m_svr.Close(); } }
public void LocalCloseOnSocket() { // Local close (Socket). using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "2a"); sock.Close(); Assert.IsFalse(sock.Connected, "2b"); } }
public void LocalCloseOnSocketAndWrite() { // Local close (Socket). using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "2a"); sock.Close(); Assert.IsFalse(sock.Connected, "2b"); WriteSomeBytesExpectError(strm, typeof(ObjectDisposedException)); } }
public void LocalCloseIntOnSocket() { // Local close(int) (Socket). if (m_spFactory.SocketSupportsCloseInt32) { using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "3a"); sock.Close(1); Thread.Sleep(1200); Assert.IsFalse(sock.Connected, "3b"); } } }
public void TestCleanUp() { sc.Stop(); try { using (var s = sock.GetStream()) { int numUpdates = 0; SerDe.Write(s, numUpdates); } } catch { // do nothing here } finally { sock.Close(); } }
public void Run() { Logger.LogInfo("TaskRunner [{0}] is running ...", TaskId); try { while (!stop) { using (var inputStream = socket.GetInputStream()) using (var outputStream = socket.GetOutputStream()) { if (!string.IsNullOrEmpty(secret)) { SerDe.Write(outputStream, secret); outputStream.Flush(); var reply = SerDe.ReadString(inputStream); Logger.LogDebug("Connect back to JVM: " + reply); secret = null; } byte[] bytes = SerDe.ReadBytes(inputStream, sizeof(int)); if (bytes != null) { int splitIndex = SerDe.ToInt(bytes); bool readComplete = Worker.ProcessStream(inputStream, outputStream, splitIndex); outputStream.Flush(); if (!readComplete) // if the socket is not read through completely, then it can't be reused { stop = true; // wait for server to complete, otherwise server may get 'connection reset' exception Logger.LogInfo("Sleep 500 millisecond to close socket ..."); Thread.Sleep(500); } else if (!socketReuse) { stop = true; // wait for server to complete, otherwise server gets 'connection reset' exception // Use SerDe.ReadBytes() to detect java side has closed socket properly // ReadBytes() will block until the socket is closed Logger.LogInfo("waiting JVM side to close socket..."); SerDe.ReadBytes(inputStream); Logger.LogInfo("JVM side has closed socket"); } } else { stop = true; Logger.LogWarn("read null splitIndex, socket is closed by JVM"); } } } } catch (Exception e) { stop = true; Logger.LogError("TaskRunner [{0}] exeption, will dispose this TaskRunner", TaskId); Logger.LogException(e); } finally { try { socket.Close(); } catch (Exception ex) { Logger.LogWarn("close socket exception: {0}", ex); } Logger.LogInfo("TaskRunner [{0}] finished", TaskId); } }
public void Dispose() { m_cliA.Close(); }
internal void Shutdown() { serverShutdown = true; innerSocket.Close(); }
private void SocketTest(ISocketWrapper serverSocket) { serverSocket.Listen(); if (serverSocket is RioSocketWrapper) { // Do nothing for second listen operation. Assert.DoesNotThrow(() => serverSocket.Listen(int.MaxValue)); } var port = ((IPEndPoint)serverSocket.LocalEndPoint).Port; var clientMsg = "Hello Message from client"; var clientMsgBytes = Encoding.UTF8.GetBytes(clientMsg); Task.Run(() => { var bytes = new byte[1024]; using (var socket = serverSocket.Accept()) { using (var s = socket.GetStream()) { // Receive data var bytesRec = s.Read(bytes, 0, bytes.Length); // send echo message. s.Write(bytes, 0, bytesRec); s.Flush(); // Receive one byte var oneByte = s.ReadByte(); // Send echo one byte byte[] oneBytes = { (byte)oneByte }; s.Write(oneBytes, 0, oneBytes.Length); Thread.SpinWait(0); // Keep sending to ensure no memory leak var longBytes = Encoding.UTF8.GetBytes(new string('x', 8192)); for (int i = 0; i < 1000; i++) { s.Write(longBytes, 0, longBytes.Length); } byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>"); s.Write(msg, 0, msg.Length); // Receive echo byte. s.ReadByte(); } } }); var clientSock = SocketFactory.CreateSocket(); // Valid invalid operation Assert.Throws<InvalidOperationException>(() => clientSock.GetStream()); Assert.Throws<InvalidOperationException>(() => clientSock.Receive()); Assert.Throws<InvalidOperationException>(() => clientSock.Send(null)); Assert.Throws<SocketException>(() => clientSock.Connect(IPAddress.Any, 1024)); clientSock.Connect(IPAddress.Loopback, port); // Valid invalid operation var byteBuf = ByteBufPool.Default.Allocate(); Assert.Throws<ArgumentException>(() => clientSock.Send(byteBuf)); byteBuf.Release(); Assert.Throws<SocketException>(() => clientSock.Listen()); if (clientSock is RioSocketWrapper) { Assert.Throws<InvalidOperationException>(() => clientSock.Accept()); } using (var s = clientSock.GetStream()) { // Send message s.Write(clientMsgBytes, 0, clientMsgBytes.Length); // Receive echo message var bytes = new byte[1024]; var bytesRec = s.Read(bytes, 0, bytes.Length); Assert.AreEqual(clientMsgBytes.Length, bytesRec); var recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec); Assert.AreEqual(clientMsg, recvStr); // Send one byte byte[] oneBytes = { 1 }; s.Write(oneBytes, 0, oneBytes.Length); // Receive echo message var oneByte = s.ReadByte(); Assert.AreEqual((byte)1, oneByte); // Keep receiving to ensure no memory leak. while (true) { bytesRec = s.Read(bytes, 0, bytes.Length); recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec); if (recvStr.IndexOf("<EOF>", StringComparison.OrdinalIgnoreCase) > -1) { break; } } // send echo bytes s.Write(oneBytes, 0, oneBytes.Length); } clientSock.Close(); // Verify invalid operation Assert.Throws<ObjectDisposedException>(() => clientSock.Receive()); serverSocket.Close(); }
private void SocketTest(ISocketWrapper serverSocket) { serverSocket.Listen(); if (serverSocket is RioSocketWrapper) { // Do nothing for second listen operation. Assert.DoesNotThrow(() => serverSocket.Listen(int.MaxValue)); } var port = ((IPEndPoint)serverSocket.LocalEndPoint).Port; var clientMsg = "Hello Message from client"; var clientMsgBytes = Encoding.UTF8.GetBytes(clientMsg); Task.Run(() => { var bytes = new byte[1024]; using (var socket = serverSocket.Accept()) { using (var s = socket.GetStream()) { // Receive data var bytesRec = s.Read(bytes, 0, bytes.Length); // send echo message. s.Write(bytes, 0, bytesRec); s.Flush(); // Receive one byte var oneByte = s.ReadByte(); // Send echo one byte byte[] oneBytes = { (byte)oneByte }; s.Write(oneBytes, 0, oneBytes.Length); Thread.SpinWait(0); // Keep sending to ensure no memory leak var longBytes = Encoding.UTF8.GetBytes(new string('x', 8192)); for (int i = 0; i < 1000; i++) { s.Write(longBytes, 0, longBytes.Length); } byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>"); s.Write(msg, 0, msg.Length); // Receive echo byte. s.ReadByte(); } } }); var clientSock = SocketFactory.CreateSocket(); // Valid invalid operation Assert.Throws <InvalidOperationException>(() => clientSock.GetStream()); Assert.Throws <InvalidOperationException>(() => clientSock.Receive()); Assert.Throws <InvalidOperationException>(() => clientSock.Send(null)); Assert.Throws <SocketException>(() => clientSock.Connect(IPAddress.Any, 1024)); clientSock.Connect(IPAddress.Loopback, port); // Valid invalid operation var byteBuf = ByteBufPool.Default.Allocate(); Assert.Throws <ArgumentException>(() => clientSock.Send(byteBuf)); byteBuf.Release(); Assert.Throws <SocketException>(() => clientSock.Listen()); if (clientSock is RioSocketWrapper) { Assert.Throws <InvalidOperationException>(() => clientSock.Accept()); } using (var s = clientSock.GetStream()) { // Send message s.Write(clientMsgBytes, 0, clientMsgBytes.Length); // Receive echo message var bytes = new byte[1024]; var bytesRec = s.Read(bytes, 0, bytes.Length); Assert.AreEqual(clientMsgBytes.Length, bytesRec); var recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec); Assert.AreEqual(clientMsg, recvStr); // Send one byte byte[] oneBytes = { 1 }; s.Write(oneBytes, 0, oneBytes.Length); // Receive echo message var oneByte = s.ReadByte(); Assert.AreEqual((byte)1, oneByte); // Keep receiving to ensure no memory leak. while (true) { bytesRec = s.Read(bytes, 0, bytes.Length); recvStr = Encoding.UTF8.GetString(bytes, 0, bytesRec); if (recvStr.IndexOf("<EOF>", StringComparison.OrdinalIgnoreCase) > -1) { break; } } // send echo bytes s.Write(oneBytes, 0, oneBytes.Length); } clientSock.Close(); // Verify invalid operation Assert.Throws <ObjectDisposedException>(() => clientSock.Receive()); serverSocket.Close(); }