PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Applications/Network/TcpGulp/TcpGulp.cs

#
C# | 193 lines | 146 code | 34 blank | 13 comment | 8 complexity | 841b386cbe61f211bf4959889153a4c6 MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: Simple Singularity test program.
  8. //
  9. using System;
  10. using System.Diagnostics;
  11. using System.Net.IP;
  12. using Microsoft.Singularity;
  13. using Microsoft.Singularity.Channels;
  14. using Microsoft.Singularity.Directory;
  15. using NetStack.Contracts;
  16. using NetStack.Channels.Public;
  17. using Microsoft.Contracts;
  18. using Microsoft.SingSharp.Reflection;
  19. using Microsoft.Singularity.Applications;
  20. using Microsoft.Singularity.Io;
  21. using Microsoft.Singularity.Configuration;
  22. [assembly: Transform(typeof(ApplicationResourceTransform))]
  23. namespace Microsoft.Singularity.Applications.Network
  24. {
  25. [ConsoleCategory(HelpMessage="Gulp TCP data from an ipAddress, port", DefaultAction=true)]
  26. internal class Parameters {
  27. [InputEndpoint("data")]
  28. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  29. [OutputEndpoint("data")]
  30. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  31. [Endpoint]
  32. public readonly TRef<TcpContract.Imp:Start> tcpRef;
  33. [StringParameter( "ipAddress", Mandatory=true, Position=0, HelpMessage="IP Address to send to")]
  34. internal string ipAddress;
  35. [StringParameter( "port", Mandatory=true, Position=1, HelpMessage="port to blats")]
  36. internal string port;
  37. reflective internal Parameters();
  38. internal int AppMain() {
  39. return TcpGulp.AppMain(this);
  40. }
  41. }
  42. public class TcpGulp
  43. {
  44. public static int Gulp(TcpConnectionContract.Imp:ReadyState! tcpConn, IPv4 address, ushort port)
  45. {
  46. Console.WriteLine("TcpGulp({0}, {1})", address, port);
  47. try {
  48. // Try to connect to the remote host
  49. tcpConn.SendConnect((uint)address, port);
  50. switch receive {
  51. case tcpConn.CouldNotConnect(TcpError error):
  52. {
  53. Console.WriteLine("Failed to connect: TcpError = " + System.Net.Sockets.TcpException.GetMessageForTcpError(error));
  54. return -1;
  55. }
  56. break;
  57. case tcpConn.OK() :
  58. // success;
  59. break;
  60. case tcpConn.ChannelClosed() :
  61. // how rude
  62. Console.WriteLine("Netstack channel closed unexpectedly");
  63. return -1;
  64. break;
  65. }
  66. Console.WriteLine("Connected");
  67. bool connected = true;
  68. int messages = 0;
  69. int bytesRead = 0;
  70. while (connected) {
  71. tcpConn.SendRead();
  72. switch receive {
  73. case tcpConn.Data(byte[]! in ExHeap data) :
  74. Console.Write('.');
  75. messages++;
  76. bytesRead += data.Length;
  77. #if false
  78. Console.WriteLine("Read {0} bytes :", data.Length);
  79. for (int i = 0; i < data.Length; ++i) {
  80. if (data[i] < 32) {
  81. Console.Write("<" + (int)data[i] + ">");
  82. }
  83. else {
  84. Console.Write((char)data[i]);
  85. }
  86. }
  87. #endif
  88. delete data;
  89. break;
  90. case tcpConn.ConnectionClosed() :
  91. tcpConn.SendClose(); // Zombie -> Closed
  92. connected = false;
  93. break;
  94. case tcpConn.NoMoreData() :
  95. // We're never going to send anything
  96. tcpConn.SendClose(); // SendOnly -> Closed
  97. connected = false;
  98. break;
  99. case tcpConn.ChannelClosed() :
  100. // How rude
  101. connected = false;
  102. break;
  103. }
  104. }
  105. Console.WriteLine("\nConnection closed");
  106. Console.WriteLine("got {0} messages {1} bytes ",
  107. messages, bytesRead);
  108. }
  109. catch (Exception e) {
  110. Console.WriteLine("Unexpected exception: {0}", e);
  111. }
  112. finally {
  113. }
  114. return 0;
  115. }
  116. public static void Usage()
  117. {
  118. Console.WriteLine("tcpgulp <ipAddress> <port>");
  119. }
  120. internal static int AppMain(Parameters! config)
  121. {
  122. IPv4 host;
  123. try {
  124. host = IPv4.Parse(config.ipAddress);
  125. }
  126. catch (FormatException e) {
  127. Console.WriteLine("{0}: {1}", e, config.ipAddress);
  128. return -1;
  129. }
  130. ushort port;
  131. try {
  132. port = UInt16.Parse(config.port);
  133. }
  134. catch (FormatException) {
  135. Console.WriteLine("Malformed port number: {0}", config.port);
  136. return -1;
  137. }
  138. catch (OverflowException) {
  139. Console.WriteLine("Port number out of range: {0}", config.port);
  140. return -1;
  141. }
  142. TcpContract.Imp tcpConn = ((!)config.tcpRef).Acquire();
  143. if (tcpConn == null) {
  144. Console.WriteLine("Could not initialize TCP endpoint.");
  145. return 1;
  146. }
  147. tcpConn.RecvReady();
  148. TcpConnectionContract.Imp! connImp;
  149. TcpConnectionContract.Exp! connExp;
  150. TcpConnectionContract.NewChannel(out connImp, out connExp);
  151. tcpConn.SendCreateTcpSession(connExp);
  152. connImp.RecvReady();
  153. delete tcpConn;
  154. int result = Gulp(connImp, host, port);
  155. delete connImp;
  156. return result;
  157. }
  158. } // end class TcpGulp
  159. }