public void WriteWhenClosedAfterRead() { // Write when closed. using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "11a"); PeerSendsData(pair); PeerCloses(pair); Assert.IsTrue(sock.Connected, "11b"); ReadSomeBytes(strm, 5); Assert.IsTrue(sock.Connected, "11c"); if (!m_connectedMayBeFalseEarlier) { WriteSomeBytes(strm); Assert.IsTrue(sock.Connected, "11d"); } int count = 0; while (true) { bool didFail = WriteSomeBytesExpectError(strm); ++count; if (didFail) { break; } Assert.IsTrue(sock.Connected, "11e_" + count); } Assert.IsFalse(sock.Connected, "11f"); } }
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)); } }
public RequestHandler CreateRequestHandler(ConnectionManager manager, ISocketWrapper socket) { RequestHandler.HttpHeader header; string gdl = string.Empty; // new string(contentInput); using (var input = new StreamReader(socket.GetStream())) { header = ReadHeader(input); var buffer = new char[header.ContentLength]; int bytesRemaining = header.ContentLength; while (bytesRemaining > 0) { int read = input.Read(buffer, 0, header.ContentLength); gdl += new string(buffer).Substring(0, read); bytesRemaining -= read; } } GdlList content = GameContainer.Parser.Parse(gdl); if (content == null || !content.Any()) { Console.WriteLine("There was no content parsed"); return null; } RequestHandler handler = CreateFromList(socket, header, content); handler.SetManager(manager); return handler; }
public void ReadWhenClosedAfterWrite() { // Read when closed (no data pending etc), after state refreshed by Write. using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "10a"); PeerCloses(pair); if (!m_connectedMayBeFalseEarlier) { Assert.IsTrue(sock.Connected, "10b"); WriteSomeBytes(strm); } int count = 0; while (true) { bool didFail = WriteSomeBytesExpectError(strm); ++count; if (didFail) { break; } Assert.IsTrue(sock.Connected, "11c_" + count); } Assert.IsFalse(sock.Connected, "10d"); ReadSomeBytesExpectError(strm); Assert.IsFalse(sock.Connected, "10e"); } }
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 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 LocalShutdownBothOnSocket() { // Local shutdown_Both (Socket). if (m_spFactory.SocketSupportsShutdown) { using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "6a"); sock.Shutdown(SocketShutdown.Both); Assert.IsFalse(sock.Connected, "6b"); } } }
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 AsyncReadWhenClosed() { // Read when closed (no data pending etc). using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "10a"); PeerCloses(pair); Assert.IsTrue(sock.Connected, "10b"); // Eek, reading to EoF doesn't set Connected=false!! AsyncReadSomeBytesButExpectZeroOf(strm, sock, true); Assert.IsTrue(sock.Connected, "10c"); AsyncReadSomeBytesButExpectZeroOf(strm, sock, true); Assert.IsTrue(sock.Connected, "10d"); } }
public void AsyncWriteWhenClosed() { // Write when closed. using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "11a"); PeerCloses(pair); Assert.IsTrue(sock.Connected, "11b"); if (!m_connectedMayBeFalseEarlier) { AsyncWriteSomeBytes(strm, sock); Assert.IsTrue(sock.Connected, "11c"); } bool didFail = AsyncWriteSomeBytesExpectErrorInBegin(strm, sock); Assert.IsFalse(sock.Connected, "11d"); } }
public void AsyncReadWhenClosedAfterAsyncWrite() { // Read when closed (no data pending etc), when state refreshed by Write. using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "10a"); PeerCloses(pair); Assert.IsTrue(sock.Connected, "10b"); if (!m_connectedMayBeFalseEarlier) { AsyncWriteSomeBytes(strm, sock); Assert.IsTrue(sock.Connected, "10c"); } AsyncWriteSomeBytesExpectErrorInBegin(strm, sock); Assert.IsFalse(sock.Connected, "10d"); AsyncReadSomeBytesExpectErrorInBegin(strm, sock); Assert.IsFalse(sock.Connected, "10e"); } }
public void AsyncReadWhenClosedWhenPendingData() { // Read when closed when pending data. using (ITestSocketPair pair = m_spFactory.CreateSocketPair()) { ISocketWrapper sock = pair.SocketA; NetworkStream strm = sock.GetStream(); Assert.IsTrue(sock.Connected, "12a"); // PeerSendsData(pair); ReadSomeBytes(strm, 5); PeerCloses(pair); Assert.IsTrue(sock.Connected, "12b"); AsyncReadSomeBytes(strm, sock, 5, true); Assert.IsTrue(sock.Connected, "12c"); // Eek, reading to EoF doesn't set Connected=false!! AsyncReadSomeBytesButExpectZeroOf(strm, sock, true); Assert.IsTrue(sock.Connected, "12d"); AsyncReadSomeBytesButExpectZeroOf(strm, sock, true); Assert.IsTrue(sock.Connected, "12e"); } }