wasCSharpSQLite – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
3 *
4 * SocketChannel.java
5 *
6 * Implements a socket channel.
7 */
8 using System;
9 using System.IO;
10 namespace tcl.lang
11 {
12  
13 /// <summary> The SocketChannel class implements a channel object for Socket
14 /// connections, created using the socket command.
15 ///
16 /// </summary>
17  
18 public class SocketChannel : Channel
19 {
20 public override string ChanType
21 {
22 get
23 {
24 return "tcp";
25 }
26  
27 }
28 override protected internal Stream InputStream
29 {
30 get
31 {
32 return (Stream)sock.GetStream();
33 }
34  
35 }
36 override protected internal Stream OutputStream
37 {
38 get
39 {
40 return (Stream)sock.GetStream();
41 }
42  
43 }
44  
45 /// <summary> The java Socket object associated with this Channel
46 ///
47 /// </summary>
48  
49 private System.Net.Sockets.TcpClient sock;
50  
51 /// <summary> Constructor - creates a new SocketChannel object with the given
52 /// options. Also creates an underlying Socket object, and Input and
53 /// Output Streams.
54 ///
55 /// </summary>
56  
57 public SocketChannel( Interp interp, int mode, string localAddr, int localPort, bool async, string address, int port )
58 {
59 System.Net.IPAddress localAddress = null;
60 System.Net.IPAddress addr = null;
61  
62 if ( async )
63 throw new TclException( interp, "Asynchronous socket connection not " + "currently implemented" );
64  
65 // Resolve addresses
66 if ( !localAddr.Equals( "" ) )
67 {
68 try
69 {
70 localAddress = System.Net.Dns.GetHostByName( localAddr ).AddressList[0];
71 }
72 catch ( System.Exception e )
73 {
74 throw new TclException( interp, "host unknown: " + localAddr );
75 }
76 }
77  
78 try
79 {
80 addr = System.Net.Dns.GetHostByName( address ).AddressList[0];
81 }
82 catch ( System.Exception e )
83 {
84 throw new TclException( interp, "host unknown: " + address );
85 }
86  
87  
88 // Set the mode of this socket.
89 this.mode = mode;
90  
91 // Create the Socket object
92  
93 // if ((localAddress != null) && (localPort != 0))
94 // {
95 //
96 // sock = new Socket(addr, port, localAddress, localPort);
97 // }
98 // else
99 sock = new System.Net.Sockets.TcpClient( addr.ToString(), port );
100  
101 // If we got this far, then the socket has been created.
102 // Create the channel name
103 ChanName = TclIO.getNextDescriptor( interp, "sock" );
104 }
105  
106 /// <summary> Constructor for making SocketChannel objects from connections
107 /// made to a ServerSocket.
108 ///
109 /// </summary>
110  
111 public SocketChannel( Interp interp, System.Net.Sockets.TcpClient s )
112 {
113 this.mode = TclIO.RDWR;
114 this.sock = s;
115  
116 ChanName = TclIO.getNextDescriptor( interp, "sock" );
117 }
118  
119 /// <summary> Close the SocketChannel.</summary>
120  
121 internal override void close()
122 {
123 // Invoke super.close() first since it might write an eof char
124 try
125 {
126 base.close();
127 }
128 finally
129 {
130 sock.Close();
131 }
132 }
133 }
134 }