File: TestAsync.cs

package info (click to toggle)
gtk-sharp2 2.8.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 21,208 kB
  • ctags: 5,843
  • sloc: xml: 245,420; cs: 24,183; sh: 8,547; ansic: 2,739; makefile: 1,582; perl: 1,177
file content (72 lines) | stat: -rw-r--r-- 1,740 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using GLib;
using Gnome.Vfs;
using System;
using System.Text;
using System.Threading;

namespace TestGnomeVfs {
	public class TestAsync {
		private static MainLoop loop;
		private static Handle handle;
	
		static void Main (string[] args)
		{
			if (args.Length != 1) {
				Console.WriteLine ("Usage: TestAsync <uri>");
				return;
			}
		
			Gnome.Vfs.Vfs.Initialize ();

			Gnome.Vfs.Uri uri = new Gnome.Vfs.Uri (args[0]);
			handle = Async.Open (uri, OpenMode.Read,
					     (int)Async.Priority.Default,
					     new Gnome.Vfs.AsyncCallback (OnOpen));
			
			loop = new MainLoop ();
			loop.Run ();
			
			Gnome.Vfs.Vfs.Shutdown ();
		}


		private static void OnOpen (Handle handle, Result result)
		{
			Console.WriteLine ("Uri opened: {0}", result);
			if (result != Result.Ok) {
				loop.Quit ();
				return;
			}
			
			byte[] buffer = new byte[1024];
			Async.Read (handle, out buffer[0], (uint)buffer.Length,
				    new AsyncReadCallback (OnRead));
		}
		
		private static void OnRead (Handle handle, Result result, byte[] buffer,
					    ulong bytes_requested, ulong bytes_read)
		{
			Console.WriteLine ("Read: {0}", result);
			if (result != Result.Ok && result != Result.ErrorEof) {
				loop.Quit ();
				return;
			}

			UTF8Encoding utf8 = new UTF8Encoding ();
			Console.WriteLine ("read ({0} bytes) : '{1}'", bytes_read,
					   utf8.GetString (buffer, 0, (int)bytes_read));

			if (bytes_read != 0)
				Async.Read (handle, out buffer[0], (uint)buffer.Length,
					    new AsyncReadCallback (OnRead));
			else
				Async.Close (handle, new Gnome.Vfs.AsyncCallback (OnClose));
		}
		
		private static void OnClose (Handle handle, Result result)
		{
			Console.WriteLine ("Close: {0}", result);
			loop.Quit ();
		}
	}
}