PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/std/cpp/net/Socket.hx

http://github.com/Yoomee/clippy
Haxe | 146 lines | 98 code | 21 blank | 27 comment | 6 complexity | eeecae8fc5d9fd26e7bd0528ecc5c332 MD5 | raw file
  1/*
  2 * Copyright (c) 2005, The haXe Project Contributors
  3 * All rights reserved.
  4 * Redistribution and use in source and binary forms, with or without
  5 * modification, are permitted provided that the following conditions are met:
  6 *
  7 *   - Redistributions of source code must retain the above copyright
  8 *     notice, this list of conditions and the following disclaimer.
  9 *   - Redistributions in binary form must reproduce the above copyright
 10 *     notice, this list of conditions and the following disclaimer in the
 11 *     documentation and/or other materials provided with the distribution.
 12 *
 13 * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 16 * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
 17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 23 * DAMAGE.
 24 *
 25 * Contributor: Lee McColl Sylvester
 26 */
 27package cpp.net;
 28
 29typedef SocketHandle = Dynamic;
 30
 31class Socket {
 32
 33	private var __s : SocketHandle;
 34	public var input(default,null) : SocketInput;
 35	public var output(default,null) : SocketOutput;
 36	public var custom : Dynamic;
 37
 38	public function new( ?s ) {
 39		__s = if( s == null ) socket_new(false) else s;
 40		input = new SocketInput(__s);
 41		output = new SocketOutput(__s);
 42	}
 43
 44	public function close() : Void {
 45		socket_close(__s);
 46		untyped {
 47			input.__s = null;
 48			output.__s = null;
 49		}
 50		input.close();
 51		output.close();
 52	}
 53
 54	public function read() : String {
 55		var bytes:haxe.io.BytesData = socket_read(__s);
 56		if (bytes==null) return "";
 57		return bytes.toString();
 58	}
 59
 60	public function write( content : String ) {
 61		socket_write(__s, haxe.io.Bytes.ofString(content).getData() );
 62	}
 63
 64	public function connect(host : Host, port : Int) {
 65		try {
 66			socket_connect(__s, host.ip, port);
 67		} catch( s : String ) {
 68			if( s == "std@socket_connect" )
 69				throw "Failed to connect on "+(try host.reverse() catch( e : Dynamic ) host.toString())+":"+port;
 70			else
 71				cpp.Lib.rethrow(s);
 72		}
 73	}
 74
 75	public function listen(connections : Int) {
 76		socket_listen(__s, connections);
 77	}
 78
 79	public function shutdown( read : Bool, write : Bool ){
 80		socket_shutdown(__s,read,write);
 81	}
 82
 83	public function bind(host : Host, port : Int) {
 84		socket_bind(__s, host.ip, port);
 85	}
 86
 87	public function accept() : Socket {
 88		return new Socket(socket_accept(__s));
 89	}
 90
 91	public function peer() : { host : Host, port : Int } {
 92		var a : Dynamic = socket_peer(__s);
 93		var h = new Host("127.0.0.1");
 94		untyped h.ip = a[0];
 95		return { host : h, port : a[1] };
 96	}
 97
 98	public function host() : { host : Host, port : Int } {
 99		var a : Dynamic = socket_host(__s);
100		var h = new Host("127.0.0.1");
101		untyped h.ip = a[0];
102		return { host : h, port : a[1] };
103	}
104
105	public function setTimeout( timeout : Float ) {
106		socket_set_timeout(__s, timeout);
107	}
108
109	public function waitForRead() {
110		select([this],null,null,null);
111	}
112
113	public function setBlocking( b : Bool ) {
114		socket_set_blocking(__s,b);
115	}
116
117	public static function newUdpSocket() {
118		return new Socket(socket_new(true));
119	}
120
121	// STATICS
122	public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, timeout : Float) : {read: Array<Socket>,write: Array<Socket>,others: Array<Socket>} {
123		var neko_array = socket_select(read,write,others, timeout);
124		return {
125			read: neko_array[0],
126			write: neko_array[1],
127			others: neko_array[2]
128		};
129	}
130
131	private static var socket_new = cpp.Lib.load("std","socket_new",1);
132	private static var socket_close = cpp.Lib.load("std","socket_close",1);
133	private static var socket_write = cpp.Lib.load("std","socket_write",2);
134	private static var socket_read = cpp.Lib.load("std","socket_read",1);
135	private static var socket_connect = cpp.Lib.load("std","socket_connect",3);
136	private static var socket_listen = cpp.Lib.load("std","socket_listen",2);
137	private static var socket_select = cpp.Lib.load("std","socket_select",4);
138	private static var socket_bind = cpp.Lib.load("std","socket_bind",3);
139	private static var socket_accept = cpp.Lib.load("std","socket_accept",1);
140	private static var socket_peer = cpp.Lib.load("std","socket_peer",1);
141	private static var socket_host = cpp.Lib.load("std","socket_host",1);
142	private static var socket_set_timeout = cpp.Lib.load("std","socket_set_timeout",2);
143	private static var socket_shutdown = cpp.Lib.load("std","socket_shutdown",3);
144	private static var socket_set_blocking = cpp.Lib.load("std","socket_set_blocking",2);
145
146}