Socket.jvm.cs 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617
  1. // System.Net.Sockets.Socket.cs
  2. //
  3. // Authors:
  4. // Phillip Pearson ([email protected])
  5. // Dick Porter <[email protected]>
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // Copyright (C) 2001, 2002 Phillip Pearson and Ximian, Inc.
  9. // http://www.myelin.co.nz
  10. // (c) 2004 Novell, Inc. (http://www.novell.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using System.Net.Configuration;
  35. using System.Runtime.InteropServices;
  36. using System.Threading;
  37. namespace System.Net.Sockets
  38. {
  39. public class Socket : IDisposable
  40. {
  41. enum SocketOperation
  42. {
  43. Accept,
  44. Connect,
  45. Receive,
  46. ReceiveFrom,
  47. Send,
  48. SendTo
  49. }
  50. [StructLayout (LayoutKind.Sequential)]
  51. private sealed class SocketAsyncResult: IAsyncResult
  52. {
  53. /* Same structure in the runtime */
  54. public Socket Sock;
  55. #if !TARGET_JVM
  56. public IntPtr handle;
  57. #else
  58. public GHSocket handle;
  59. #endif
  60. object state;
  61. AsyncCallback callback;
  62. WaitHandle waithandle;
  63. Exception delayedException;
  64. public EndPoint EndPoint; // Connect,ReceiveFrom,SendTo
  65. public byte [] Buffer; // Receive,ReceiveFrom,Send,SendTo
  66. public int Offset; // Receive,ReceiveFrom,Send,SendTo
  67. public int Size; // Receive,ReceiveFrom,Send,SendTo
  68. public SocketFlags SockFlags; // Receive,ReceiveFrom,Send,SendTo
  69. // Return values
  70. Socket acc_socket;
  71. int total;
  72. bool completed_sync;
  73. bool completed;
  74. public bool blocking;
  75. internal int error;
  76. SocketOperation operation;
  77. public object ares;
  78. public SocketAsyncResult (Socket sock, object state, AsyncCallback callback, SocketOperation operation)
  79. {
  80. this.Sock = sock;
  81. this.blocking = sock.blocking;
  82. this.handle = sock.socket;
  83. this.state = state;
  84. this.callback = callback;
  85. this.operation = operation;
  86. SockFlags = SocketFlags.None;
  87. }
  88. public void CheckIfThrowDelayedException ()
  89. {
  90. if (delayedException != null)
  91. throw delayedException;
  92. if (error != 0)
  93. throw new SocketException (error);
  94. }
  95. void CompleteAllOnDispose (Queue queue)
  96. {
  97. object [] pending = queue.ToArray ();
  98. queue.Clear ();
  99. WaitCallback cb;
  100. for (int i = 0; i < pending.Length; i++)
  101. {
  102. SocketAsyncResult ares = (SocketAsyncResult) pending [i];
  103. cb = new WaitCallback (ares.CompleteDisposed);
  104. ThreadPool.QueueUserWorkItem (cb, null);
  105. }
  106. }
  107. void CompleteDisposed (object unused)
  108. {
  109. Complete ();
  110. }
  111. public void Complete ()
  112. {
  113. if (operation != SocketOperation.Receive && Sock.disposed)
  114. delayedException = new ObjectDisposedException (Sock.GetType ().ToString ());
  115. IsCompleted = true;
  116. Queue queue = null;
  117. if (operation == SocketOperation.Receive || operation == SocketOperation.ReceiveFrom)
  118. {
  119. queue = Sock.readQ;
  120. }
  121. else if (operation == SocketOperation.Send || operation == SocketOperation.SendTo)
  122. {
  123. queue = Sock.writeQ;
  124. }
  125. if (queue != null)
  126. {
  127. SocketAsyncCall sac = null;
  128. SocketAsyncResult req = null;
  129. lock (queue)
  130. {
  131. queue.Dequeue (); // remove ourselves
  132. if (queue.Count > 0)
  133. {
  134. req = (SocketAsyncResult) queue.Peek ();
  135. if (!Sock.disposed)
  136. {
  137. Worker worker = new Worker (req);
  138. sac = GetDelegate (worker, req.operation);
  139. }
  140. else
  141. {
  142. CompleteAllOnDispose (queue);
  143. }
  144. }
  145. }
  146. if (sac != null)
  147. sac.BeginInvoke (null, req);
  148. }
  149. if (callback != null)
  150. callback (this);
  151. }
  152. SocketAsyncCall GetDelegate (Worker worker, SocketOperation op)
  153. {
  154. switch (op)
  155. {
  156. case SocketOperation.Receive:
  157. return new SocketAsyncCall (worker.Receive);
  158. case SocketOperation.ReceiveFrom:
  159. return new SocketAsyncCall (worker.ReceiveFrom);
  160. case SocketOperation.Send:
  161. return new SocketAsyncCall (worker.Send);
  162. case SocketOperation.SendTo:
  163. return new SocketAsyncCall (worker.SendTo);
  164. default:
  165. return null; // never happens
  166. }
  167. }
  168. public void Complete (bool synch)
  169. {
  170. completed_sync = synch;
  171. Complete ();
  172. }
  173. public void Complete (int total)
  174. {
  175. this.total = total;
  176. Complete ();
  177. }
  178. public void Complete (Exception e, bool synch)
  179. {
  180. completed_sync = synch;
  181. delayedException = e;
  182. Complete ();
  183. }
  184. public void Complete (Exception e)
  185. {
  186. delayedException = e;
  187. Complete ();
  188. }
  189. public void Complete (Socket s)
  190. {
  191. acc_socket = s;
  192. Complete ();
  193. }
  194. public object AsyncState
  195. {
  196. get
  197. {
  198. return state;
  199. }
  200. }
  201. public WaitHandle AsyncWaitHandle
  202. {
  203. get
  204. {
  205. lock (this)
  206. {
  207. if (waithandle == null)
  208. waithandle = new ManualResetEvent (completed);
  209. }
  210. return waithandle;
  211. }
  212. set
  213. {
  214. waithandle=value;
  215. }
  216. }
  217. public bool CompletedSynchronously
  218. {
  219. get
  220. {
  221. return(completed_sync);
  222. }
  223. }
  224. public bool IsCompleted
  225. {
  226. get
  227. {
  228. return(completed);
  229. }
  230. set
  231. {
  232. completed=value;
  233. lock (this)
  234. {
  235. if (waithandle != null && value)
  236. {
  237. ((ManualResetEvent) waithandle).Set ();
  238. }
  239. }
  240. }
  241. }
  242. public Socket Socket
  243. {
  244. get
  245. {
  246. return acc_socket;
  247. }
  248. }
  249. public int Total
  250. {
  251. get
  252. {
  253. return total;
  254. }
  255. set
  256. {
  257. total = value;
  258. }
  259. }
  260. }
  261. private sealed class Worker
  262. {
  263. SocketAsyncResult result;
  264. public Worker (SocketAsyncResult ares)
  265. {
  266. this.result = ares;
  267. }
  268. public void Accept ()
  269. {
  270. Socket acc_socket = null;
  271. try
  272. {
  273. acc_socket = result.Sock.Accept ();
  274. }
  275. catch (Exception e)
  276. {
  277. result.Complete (e);
  278. return;
  279. }
  280. result.Complete (acc_socket);
  281. }
  282. public void Connect ()
  283. {
  284. try
  285. {
  286. result.Sock.Connect (result.EndPoint);
  287. result.Sock.connected = true;
  288. }
  289. catch (Exception e)
  290. {
  291. result.Complete (e);
  292. return;
  293. }
  294. result.Complete ();
  295. }
  296. #if !TARGET_JVM
  297. public void Receive ()
  298. {
  299. // Actual recv() done in the runtime
  300. result.Complete ();
  301. }
  302. #else
  303. public void Receive ()
  304. {
  305. int total = 0;
  306. try
  307. {
  308. total = result.Sock.Receive_nochecks (result.Buffer,
  309. result.Offset,
  310. result.Size,
  311. result.SockFlags);
  312. }
  313. catch (Exception e)
  314. {
  315. result.Complete (e);
  316. return;
  317. }
  318. result.Complete (total);
  319. }
  320. #endif
  321. public void ReceiveFrom ()
  322. {
  323. int total = 0;
  324. try
  325. {
  326. total = result.Sock.ReceiveFrom_nochecks (result.Buffer,
  327. result.Offset,
  328. result.Size,
  329. result.SockFlags,
  330. ref result.EndPoint);
  331. }
  332. catch (Exception e)
  333. {
  334. result.Complete (e);
  335. return;
  336. }
  337. result.Complete (total);
  338. }
  339. int send_so_far;
  340. void UpdateSendValues (int last_sent)
  341. {
  342. if (result.error == 0)
  343. {
  344. send_so_far += last_sent;
  345. result.Offset += last_sent;
  346. result.Size -= last_sent;
  347. }
  348. }
  349. #if !TARGET_JVM
  350. public void Send ()
  351. {
  352. // Actual send() done in the runtime
  353. if (result.error == 0)
  354. {
  355. UpdateSendValues (result.Total);
  356. if (result.Sock.disposed)
  357. {
  358. result.Complete ();
  359. return;
  360. }
  361. if (result.Size > 0)
  362. {
  363. SocketAsyncCall sac = new SocketAsyncCall (this.Send);
  364. sac.BeginInvoke (null, result);
  365. return; // Have to finish writing everything. See bug #74475.
  366. }
  367. result.Total = send_so_far;
  368. }
  369. result.Complete ();
  370. }
  371. #else
  372. public void Send ()
  373. {
  374. int total = 0;
  375. try
  376. {
  377. total = result.Sock.Send_nochecks (result.Buffer,
  378. result.Offset,
  379. result.Size,
  380. result.SockFlags);
  381. UpdateSendValues (total);
  382. if (result.Size > 0)
  383. {
  384. SocketAsyncCall sac = new SocketAsyncCall (this.SendTo);
  385. sac.BeginInvoke (null, result);
  386. return; // Have to finish writing everything. See bug #74475.
  387. }
  388. result.Total = send_so_far;
  389. }
  390. catch (Exception e)
  391. {
  392. result.Complete (e);
  393. return;
  394. }
  395. result.Complete ();
  396. }
  397. #endif
  398. public void SendTo ()
  399. {
  400. int total = 0;
  401. try
  402. {
  403. total = result.Sock.SendTo_nochecks (result.Buffer,
  404. result.Offset,
  405. result.Size,
  406. result.SockFlags,
  407. result.EndPoint);
  408. UpdateSendValues (total);
  409. if (result.Size > 0)
  410. {
  411. SocketAsyncCall sac = new SocketAsyncCall (this.SendTo);
  412. sac.BeginInvoke (null, result);
  413. return; // Have to finish writing everything. See bug #74475.
  414. }
  415. result.Total = send_so_far;
  416. }
  417. catch (Exception e)
  418. {
  419. result.Complete (e);
  420. return;
  421. }
  422. result.Complete ();
  423. }
  424. }
  425. /* the field "socket" is looked up by name by the runtime */
  426. #if !TARGET_JVM
  427. private IntPtr socket;
  428. #else
  429. private GHSocket socket;
  430. #endif
  431. private AddressFamily address_family;
  432. private SocketType socket_type;
  433. private ProtocolType protocol_type;
  434. internal bool blocking=true;
  435. private Queue readQ = new Queue (2);
  436. private Queue writeQ = new Queue (2);
  437. delegate void SocketAsyncCall ();
  438. /*
  439. * These two fields are looked up by name by the runtime, don't change
  440. * their name without also updating the runtime code.
  441. */
  442. private static int ipv4Supported = -1, ipv6Supported = -1;
  443. /* When true, the socket was connected at the time of
  444. * the last IO operation
  445. */
  446. private bool connected=false;
  447. /* true if we called Close_internal */
  448. private bool closed;
  449. internal bool disposed;
  450. /* Used in LocalEndPoint and RemoteEndPoint if the
  451. * Mono.Posix assembly is available
  452. */
  453. private static object unixendpoint=null;
  454. private static Type unixendpointtype=null;
  455. static void AddSockets (ArrayList sockets, IList list, string name)
  456. {
  457. if (list != null)
  458. {
  459. foreach (Socket sock in list)
  460. {
  461. if (sock == null) // MS throws a NullRef
  462. throw new ArgumentNullException (name, "Contains a null element");
  463. sockets.Add (sock);
  464. }
  465. }
  466. sockets.Add (null);
  467. }
  468. #if !TARGET_JVM
  469. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  470. private extern static void Select_internal (ref Socket [] sockets,
  471. int microSeconds,
  472. out int error);
  473. #else
  474. private static void Select_internal (ref Socket [] sockets, int microSeconds, out int error)
  475. {
  476. GHSocketFactory.Select_internal(ref sockets, microSeconds, out error);
  477. }
  478. #endif
  479. public static void Select (IList checkRead, IList checkWrite, IList checkError, int microSeconds)
  480. {
  481. ArrayList list = new ArrayList ();
  482. AddSockets (list, checkRead, "checkRead");
  483. AddSockets (list, checkWrite, "checkWrite");
  484. AddSockets (list, checkError, "checkError");
  485. if (list.Count == 3)
  486. {
  487. throw new ArgumentNullException ("checkRead, checkWrite, checkError",
  488. "All the lists are null or empty.");
  489. }
  490. int error;
  491. /*
  492. * The 'sockets' array contains: READ socket 0-n, null,
  493. * WRITE socket 0-n, null,
  494. * ERROR socket 0-n, null
  495. */
  496. Socket [] sockets = (Socket []) list.ToArray (typeof (Socket));
  497. Select_internal (ref sockets, microSeconds, out error);
  498. if (error != 0)
  499. throw new SocketException (error);
  500. if (checkRead != null)
  501. checkRead.Clear ();
  502. if (checkWrite != null)
  503. checkWrite.Clear ();
  504. if (checkError != null)
  505. checkError.Clear ();
  506. if (sockets == null)
  507. return;
  508. int mode = 0;
  509. int count = sockets.Length;
  510. IList currentList = checkRead;
  511. for (int i = 0; i < count; i++)
  512. {
  513. Socket sock = sockets [i];
  514. if (sock == null)
  515. { // separator
  516. currentList = (mode == 0) ? checkWrite : checkError;
  517. mode++;
  518. continue;
  519. }
  520. if (currentList != null)
  521. {
  522. sock.connected = true;
  523. currentList.Add (sock);
  524. }
  525. }
  526. }
  527. #if !TARGET_JVM
  528. static Socket()
  529. {
  530. Assembly ass;
  531. try {
  532. ass = Assembly.Load (Consts.AssemblyMono_Posix);
  533. } catch (FileNotFoundException) {
  534. return;
  535. }
  536. unixendpointtype=ass.GetType("Mono.Posix.UnixEndPoint");
  537. /* The endpoint Create() method is an instance
  538. * method :-(
  539. */
  540. Type[] arg_types=new Type[1];
  541. arg_types[0]=typeof(string);
  542. ConstructorInfo cons=unixendpointtype.GetConstructor(arg_types);
  543. object[] args=new object[1];
  544. args[0]="";
  545. unixendpoint=cons.Invoke(args);
  546. }
  547. #endif
  548. #if !TARGET_JVM
  549. // private constructor used by Accept, which already
  550. // has a socket handle to use
  551. private Socket(AddressFamily family, SocketType type,
  552. ProtocolType proto, IntPtr sock)
  553. #else
  554. // private constructor used by Accept, which already
  555. // has a socket handle to use
  556. private Socket(AddressFamily family, SocketType type,
  557. ProtocolType proto, GHSocket sock)
  558. {
  559. #endif
  560. address_family=family;
  561. socket_type=type;
  562. protocol_type=proto;
  563. socket=sock;
  564. connected=true;
  565. }
  566. #if !TARGET_JVM
  567. // Creates a new system socket, returning the handle
  568. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  569. private extern IntPtr Socket_internal(AddressFamily family,
  570. SocketType type,
  571. ProtocolType proto,
  572. out int error);
  573. #else
  574. private GHSocket Socket_internal(AddressFamily family,
  575. SocketType type,
  576. ProtocolType proto,
  577. out int error)
  578. {
  579. return GHSocketFactory.Socket_internal(family, type, proto, out error);
  580. }
  581. #endif
  582. public Socket(AddressFamily family, SocketType type,
  583. ProtocolType proto)
  584. {
  585. address_family=family;
  586. socket_type=type;
  587. protocol_type=proto;
  588. int error;
  589. socket=Socket_internal(family, type, proto, out error);
  590. if (error != 0) {
  591. throw new SocketException (error);
  592. }
  593. }
  594. public AddressFamily AddressFamily
  595. {
  596. get
  597. {
  598. return(address_family);
  599. }
  600. }
  601. #if !TARGET_JVM
  602. // Returns the amount of data waiting to be read on socket
  603. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  604. private extern static int Available_internal(IntPtr socket,
  605. out int error);
  606. #else
  607. private int Available_internal(GHSocket socket, out int error)
  608. {
  609. return socket.Available_internal(out error);
  610. }
  611. #endif
  612. public int Available
  613. {
  614. get
  615. {
  616. EnsureStillUsable();
  617. int ret, error;
  618. ret = Available_internal(socket, out error);
  619. if (error != 0)
  620. {
  621. throw new SocketException (error);
  622. }
  623. return(ret);
  624. }
  625. }
  626. #if !TARGET_JVM
  627. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  628. private extern static void Blocking_internal(IntPtr socket,
  629. bool block,
  630. out int error);
  631. #else
  632. private void Blocking_internal(GHSocket socket, bool block, out int error)
  633. {
  634. socket.Blocking_internal(block, out error);
  635. }
  636. #endif
  637. public bool Blocking
  638. {
  639. get
  640. {
  641. return(blocking);
  642. }
  643. set
  644. {
  645. EnsureStillUsable();
  646. int error;
  647. Blocking_internal(socket, value, out error);
  648. if (error != 0) {
  649. throw new SocketException (error);
  650. }
  651. blocking=value;
  652. }
  653. }
  654. public bool Connected
  655. {
  656. get
  657. {
  658. return(connected);
  659. }
  660. }
  661. #if !TARGET_JVM
  662. public IntPtr Handle
  663. {
  664. get
  665. {
  666. return(socket);
  667. }
  668. }
  669. #else
  670. public IntPtr Handle
  671. {
  672. get
  673. {
  674. throw new NotImplementedException ();
  675. }
  676. }
  677. internal GHSocket GHHandle
  678. {
  679. get
  680. {
  681. return socket;
  682. }
  683. }
  684. #endif
  685. #if !TARGET_JVM
  686. // Returns the local endpoint details in addr and port
  687. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  688. private extern static SocketAddress LocalEndPoint_internal(IntPtr socket, out int error);
  689. [MonoTODO("Support non-IP endpoints")]
  690. public EndPoint LocalEndPoint
  691. {
  692. get
  693. {
  694. if (disposed && closed)
  695. throw new ObjectDisposedException (GetType ().ToString ());
  696. SocketAddress sa;
  697. int error;
  698. sa=LocalEndPoint_internal(socket, out error);
  699. if (error != 0) {
  700. throw new SocketException (error);
  701. }
  702. if(sa.Family==AddressFamily.InterNetwork || sa.Family==AddressFamily.InterNetworkV6) {
  703. // Stupidly, EndPoint.Create() is an
  704. // instance method
  705. return new IPEndPoint(0, 0).Create(sa);
  706. } else if (sa.Family==AddressFamily.Unix &&
  707. unixendpoint!=null) {
  708. return((EndPoint)unixendpointtype.InvokeMember("Create", BindingFlags.InvokeMethod|BindingFlags.Instance|BindingFlags.Public, null, unixendpoint, new object[] {sa}));
  709. } else {
  710. throw new NotImplementedException();
  711. }
  712. }
  713. }
  714. #else
  715. private EndPoint LocalEndPoint_internal(GHSocket socket, out int error)
  716. {
  717. return socket.LocalEndPoint_internal(out error);
  718. }
  719. public EndPoint LocalEndPoint
  720. {
  721. get
  722. {
  723. EnsureStillUsable();
  724. int error;
  725. EndPoint ret = LocalEndPoint_internal(socket, out error);
  726. if (error != 0)
  727. {
  728. throw new SocketException (error);
  729. }
  730. return ret;
  731. }
  732. }
  733. #endif
  734. public ProtocolType ProtocolType
  735. {
  736. get
  737. {
  738. return(protocol_type);
  739. }
  740. }
  741. #if !TARGET_JVM
  742. // Returns the remote endpoint details in addr and port
  743. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  744. private extern static SocketAddress RemoteEndPoint_internal(IntPtr socket, out int error);
  745. [MonoTODO("Support non-IP endpoints")]
  746. public EndPoint RemoteEndPoint
  747. {
  748. get
  749. {
  750. if (disposed && closed)
  751. throw new ObjectDisposedException (GetType ().ToString ());
  752. SocketAddress sa;
  753. int error;
  754. sa=RemoteEndPoint_internal(socket, out error);
  755. if (error != 0) {
  756. throw new SocketException (error);
  757. }
  758. if(sa.Family==AddressFamily.InterNetwork || sa.Family==AddressFamily.InterNetworkV6 ) {
  759. // Stupidly, EndPoint.Create() is an
  760. // instance method
  761. return new IPEndPoint(0, 0).Create(sa);
  762. } else if (sa.Family==AddressFamily.Unix &&
  763. unixendpoint!=null) {
  764. return((EndPoint)unixendpointtype.InvokeMember("Create", BindingFlags.InvokeMethod|BindingFlags.Instance|BindingFlags.Public, null, unixendpoint, new object[] {sa}));
  765. } else {
  766. throw new NotImplementedException();
  767. }
  768. }
  769. }
  770. #else
  771. private EndPoint RemoteEndPoint_internal(GHSocket socket, out int error)
  772. {
  773. return socket.RemoteEndPoint_internal(out error);
  774. }
  775. public EndPoint RemoteEndPoint
  776. {
  777. get
  778. {
  779. EnsureStillUsable();
  780. int error;
  781. EndPoint ret = RemoteEndPoint_internal(socket, out error);
  782. if (error != 0)
  783. {
  784. throw new SocketException (error);
  785. }
  786. return ret;
  787. }
  788. }
  789. #endif
  790. public SocketType SocketType
  791. {
  792. get
  793. {
  794. return(socket_type);
  795. }
  796. }
  797. #if NET_1_1
  798. public static bool SupportsIPv4
  799. {
  800. get
  801. {
  802. CheckProtocolSupport();
  803. return ipv4Supported == 1;
  804. }
  805. }
  806. public static bool SupportsIPv6
  807. {
  808. get
  809. {
  810. CheckProtocolSupport();
  811. return ipv6Supported == 1;
  812. }
  813. }
  814. #else
  815. internal static bool SupportsIPv4
  816. {
  817. get
  818. {
  819. return true;
  820. }
  821. }
  822. internal static bool SupportsIPv6
  823. {
  824. get
  825. {
  826. return false;
  827. }
  828. }
  829. #endif
  830. internal static void CheckProtocolSupport()
  831. {
  832. if(ipv4Supported == -1)
  833. {
  834. try
  835. {
  836. Socket tmp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  837. tmp.Close();
  838. ipv4Supported = 1;
  839. }
  840. catch
  841. {
  842. ipv4Supported = 0;
  843. }
  844. }
  845. if(ipv6Supported == -1)
  846. {
  847. #if NET_2_0 && CONFIGURATION_DEP
  848. SettingsSection config;
  849. config = (SettingsSection) System.Configuration.ConfigurationManager.GetSection ("system.net/settings");
  850. if (config != null)
  851. ipv6Supported = config.Ipv6.Enabled ? -1 : 0;
  852. #else
  853. NetConfig config = (NetConfig)System.Configuration.ConfigurationSettings.GetConfig("system.net/settings");
  854. if(config != null)
  855. ipv6Supported = config.ipv6Enabled?-1:0;
  856. #endif
  857. if(ipv6Supported != 0)
  858. {
  859. try
  860. {
  861. Socket tmp = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
  862. tmp.Close();
  863. ipv6Supported = 1;
  864. }
  865. catch { }
  866. }
  867. }
  868. }
  869. #if !TARGET_JVM
  870. // Creates a new system socket, returning the handle
  871. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  872. private extern static IntPtr Accept_internal(IntPtr sock,
  873. out int error);
  874. #else
  875. private GHSocket Accept_internal(GHSocket sock, out int error)
  876. {
  877. return sock.Accept_internal(out error);
  878. }
  879. #endif
  880. public Socket Accept()
  881. {
  882. EnsureStillUsable();
  883. int error = 0;
  884. #if !TARGET_JVM
  885. IntPtr sock = (IntPtr) (-1);
  886. #else
  887. GHSocket sock = null;
  888. #endif
  889. sock = Accept_internal(socket, out error);
  890. if (error != 0) {
  891. throw new SocketException (error);
  892. }
  893. Socket accepted = new Socket(this.AddressFamily,
  894. this.SocketType,
  895. this.ProtocolType, sock);
  896. accepted.Blocking = this.Blocking;
  897. return(accepted);
  898. }
  899. public IAsyncResult BeginAccept(AsyncCallback callback,
  900. object state)
  901. {
  902. EnsureStillUsable();
  903. SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Accept);
  904. Worker worker = new Worker (req);
  905. SocketAsyncCall sac = new SocketAsyncCall (worker.Accept);
  906. sac.BeginInvoke (null, req);
  907. return(req);
  908. }
  909. public IAsyncResult BeginConnect(EndPoint end_point,
  910. AsyncCallback callback,
  911. object state)
  912. {
  913. EnsureStillUsable();
  914. if (end_point == null)
  915. throw new ArgumentNullException ("end_point");
  916. SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Connect);
  917. req.EndPoint = end_point;
  918. // Bug #75154: Connect() should not succeed for .Any addresses.
  919. if (end_point is IPEndPoint)
  920. {
  921. IPEndPoint ep = (IPEndPoint) end_point;
  922. if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
  923. {
  924. req.Complete (new SocketException (10049), true);
  925. return req;
  926. }
  927. }
  928. int error = 0;
  929. if (!blocking)
  930. {
  931. #if !TARGET_JVM
  932. SocketAddress serial = end_point.Serialize ();
  933. Connect_internal (socket, serial, out error);
  934. #else
  935. Connect_internal (socket, end_point, out error);
  936. #endif
  937. if (error == 0)
  938. {
  939. // succeeded synch
  940. connected = true;
  941. req.Complete (true);
  942. }
  943. else if (error != 10036 && error != 10035)
  944. {
  945. // error synch
  946. connected = false;
  947. req.Complete (new SocketException (error), true);
  948. }
  949. }
  950. if (blocking || error == 10036 || error == 10035)
  951. {
  952. // continue asynch
  953. connected = false;
  954. Worker worker = new Worker (req);
  955. SocketAsyncCall sac = new SocketAsyncCall (worker.Connect);
  956. sac.BeginInvoke (null, req);
  957. }
  958. return(req);
  959. }
  960. public IAsyncResult BeginReceive(byte[] buffer, int offset,
  961. int size,
  962. SocketFlags socket_flags,
  963. AsyncCallback callback,
  964. object state)
  965. {
  966. EnsureStillUsable();
  967. if (buffer == null)
  968. throw new ArgumentNullException ("buffer");
  969. if (offset < 0 || offset > buffer.Length)
  970. throw new ArgumentOutOfRangeException ("offset");
  971. if (size < 0 || offset + size > buffer.Length)
  972. throw new ArgumentOutOfRangeException ("size");
  973. SocketAsyncResult req;
  974. lock (readQ)
  975. {
  976. req = new SocketAsyncResult (this, state, callback, SocketOperation.Receive);
  977. req.Buffer = buffer;
  978. req.Offset = offset;
  979. req.Size = size;
  980. req.SockFlags = socket_flags;
  981. readQ.Enqueue (req);
  982. if (readQ.Count == 1)
  983. {
  984. Worker worker = new Worker (req);
  985. SocketAsyncCall sac = new SocketAsyncCall (worker.Receive);
  986. sac.BeginInvoke (null, req);
  987. }
  988. }
  989. return req;
  990. }
  991. public IAsyncResult BeginReceiveFrom(byte[] buffer, int offset,
  992. int size,
  993. SocketFlags socket_flags,
  994. ref EndPoint remote_end,
  995. AsyncCallback callback,
  996. object state)
  997. {
  998. EnsureStillUsable();
  999. if (buffer == null)
  1000. throw new ArgumentNullException ("buffer");
  1001. if (offset < 0)
  1002. throw new ArgumentOutOfRangeException ("offset must be >= 0");
  1003. if (size < 0)
  1004. throw new ArgumentOutOfRangeException ("size must be >= 0");
  1005. if (offset + size > buffer.Length)
  1006. throw new ArgumentOutOfRangeException ("offset + size exceeds the buffer length");
  1007. SocketAsyncResult req;
  1008. lock (readQ)
  1009. {
  1010. req = new SocketAsyncResult (this, state, callback, SocketOperation.ReceiveFrom);
  1011. req.Buffer = buffer;
  1012. req.Offset = offset;
  1013. req.Size = size;
  1014. req.SockFlags = socket_flags;
  1015. req.EndPoint = remote_end;
  1016. readQ.Enqueue (req);
  1017. if (readQ.Count == 1)
  1018. {
  1019. Worker worker = new Worker (req);
  1020. SocketAsyncCall sac = new SocketAsyncCall (worker.ReceiveFrom);
  1021. sac.BeginInvoke (null, req);
  1022. }
  1023. }
  1024. return req;
  1025. }
  1026. public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socket_flags,
  1027. AsyncCallback callback, object state)
  1028. {
  1029. EnsureStillUsable();
  1030. if (buffer == null)
  1031. throw new ArgumentNullException ("buffer");
  1032. if (offset < 0)
  1033. throw new ArgumentOutOfRangeException ("offset must be >= 0");
  1034. if (size < 0)
  1035. throw new ArgumentOutOfRangeException ("size must be >= 0");
  1036. if (offset + size > buffer.Length)
  1037. throw new ArgumentOutOfRangeException ("offset + size exceeds the buffer length");
  1038. SocketAsyncResult req;
  1039. lock (writeQ)
  1040. {
  1041. req = new SocketAsyncResult (this, state, callback, SocketOperation.Send);
  1042. req.Buffer = buffer;
  1043. req.Offset = offset;
  1044. req.Size = size;
  1045. req.SockFlags = socket_flags;
  1046. writeQ.Enqueue (req);
  1047. if (writeQ.Count == 1)
  1048. {
  1049. Worker worker = new Worker (req);
  1050. SocketAsyncCall sac = new SocketAsyncCall (worker.Send);
  1051. sac.BeginInvoke (null, req);
  1052. }
  1053. }
  1054. return req;
  1055. }
  1056. public IAsyncResult BeginSendTo(byte[] buffer, int offset,
  1057. int size,
  1058. SocketFlags socket_flags,
  1059. EndPoint remote_end,
  1060. AsyncCallback callback,
  1061. object state)
  1062. {
  1063. EnsureStillUsable();
  1064. if (buffer == null)
  1065. throw new ArgumentNullException ("buffer");
  1066. if (offset < 0)
  1067. throw new ArgumentOutOfRangeException ("offset must be >= 0");
  1068. if (size < 0)
  1069. throw new ArgumentOutOfRangeException ("size must be >= 0");
  1070. if (offset + size > buffer.Length)
  1071. throw new ArgumentOutOfRangeException ("offset + size exceeds the buffer length");
  1072. SocketAsyncResult req;
  1073. lock (writeQ)
  1074. {
  1075. req = new SocketAsyncResult (this, state, callback, SocketOperation.SendTo);
  1076. req.Buffer = buffer;
  1077. req.Offset = offset;
  1078. req.Size = size;
  1079. req.SockFlags = socket_flags;
  1080. req.EndPoint = remote_end;
  1081. writeQ.Enqueue (req);
  1082. if (writeQ.Count == 1)
  1083. {
  1084. Worker worker = new Worker (req);
  1085. SocketAsyncCall sac = new SocketAsyncCall (worker.SendTo);
  1086. sac.BeginInvoke (null, req);
  1087. }
  1088. }
  1089. return req;
  1090. }
  1091. #if !TARGET_JVM
  1092. // Creates a new system socket, returning the handle
  1093. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1094. private extern static void Bind_internal(IntPtr sock,
  1095. SocketAddress sa,
  1096. out int error);
  1097. public void Bind(EndPoint local_end)
  1098. {
  1099. if (disposed && closed)
  1100. throw new ObjectDisposedException (GetType ().ToString ());
  1101. if(local_end==null) {
  1102. throw new ArgumentNullException("local_end");
  1103. }
  1104. int error;
  1105. Bind_internal(socket, local_end.Serialize(),
  1106. out error);
  1107. if (error != 0) {
  1108. throw new SocketException (error);
  1109. }
  1110. }
  1111. #else
  1112. private void Bind_internal(GHSocket sock,
  1113. EndPoint sa,
  1114. out int error)
  1115. {
  1116. sock.Bind_internal(sa, out error);
  1117. }
  1118. public void Bind(EndPoint local_end)
  1119. {
  1120. EnsureStillUsable();
  1121. if(local_end==null) {
  1122. throw new ArgumentNullException("local_end");
  1123. }
  1124. int error;
  1125. Bind_internal(socket, local_end,
  1126. out error);
  1127. if (error != 0) {
  1128. throw new SocketException (error);
  1129. }
  1130. }
  1131. #endif
  1132. #if !TARGET_JVM
  1133. // Closes the socket
  1134. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1135. private extern static void Close_internal(IntPtr socket,
  1136. out int error);
  1137. #else
  1138. private void Close_internal(GHSocket socket, out int error)
  1139. {
  1140. socket.Close_internal(out error);
  1141. }
  1142. #endif
  1143. public void Close()
  1144. {
  1145. ((IDisposable) this).Dispose ();
  1146. }
  1147. #if !TARGET_JVM
  1148. // Connects to the remote address
  1149. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1150. private extern static void Connect_internal(IntPtr sock,
  1151. SocketAddress sa,
  1152. out int error);
  1153. #else
  1154. private void Connect_internal(GHSocket sock,
  1155. EndPoint sa,
  1156. out int error)
  1157. {
  1158. sock.Connect_internal(sa, out error);
  1159. }
  1160. #endif
  1161. public void Connect(EndPoint remote_end)
  1162. {
  1163. EnsureStillUsable();
  1164. if(remote_end==null) {
  1165. throw new ArgumentNullException("remote_end");
  1166. }
  1167. if (remote_end is IPEndPoint) {
  1168. IPEndPoint ep = (IPEndPoint) remote_end;
  1169. if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
  1170. throw new SocketException (10049);
  1171. }
  1172. int error = 0;
  1173. #if !TARGET_JVM
  1174. Connect_internal (socket, remote_end.Serialize(), out error);
  1175. #else
  1176. Connect_internal (socket, remote_end, out error);
  1177. #endif
  1178. if (error != 0) {
  1179. throw new SocketException (error);
  1180. }
  1181. connected=true;
  1182. }
  1183. #if TARGET_JVM
  1184. public void ChangeToSSL()
  1185. {
  1186. try
  1187. {
  1188. GHSocket tmp = socket.ChangeToSSL(null);
  1189. if (tmp != null)
  1190. {
  1191. socket = tmp;
  1192. }
  1193. }
  1194. catch (Exception e)
  1195. {
  1196. #if DEBUG
  1197. Console.WriteLine("Caught exception during ChangeToSSL: {0}, {1}", e.GetType(), e.Message);
  1198. #endif
  1199. throw new SocketException(10045);
  1200. }
  1201. }
  1202. #endif
  1203. public Socket EndAccept(IAsyncResult result)
  1204. {
  1205. EnsureStillUsable();
  1206. if (result == null)
  1207. throw new ArgumentNullException ("result");
  1208. SocketAsyncResult req = result as SocketAsyncResult;
  1209. if (req == null)
  1210. throw new ArgumentException ("Invalid IAsyncResult", "result");
  1211. if (!result.IsCompleted)
  1212. result.AsyncWaitHandle.WaitOne();
  1213. req.CheckIfThrowDelayedException();
  1214. return req.Socket;
  1215. }
  1216. public void EndConnect(IAsyncResult result) {
  1217. EnsureStillUsable();
  1218. if (result == null)
  1219. throw new ArgumentNullException ("result");
  1220. SocketAsyncResult req = result as SocketAsyncResult;
  1221. if (req == null)
  1222. throw new ArgumentException ("Invalid IAsyncResult", "result");
  1223. if (!result.IsCompleted)
  1224. result.AsyncWaitHandle.WaitOne();
  1225. req.CheckIfThrowDelayedException();
  1226. }
  1227. #if NET_2_0
  1228. [MonoNotSupported ("")]
  1229. public void EndDisconnect (IAsyncResult asyncResult)
  1230. {
  1231. throw new NotImplementedException ();
  1232. }
  1233. #endif
  1234. public int EndReceive(IAsyncResult result) {
  1235. EnsureStillUsable();
  1236. if (result == null)
  1237. throw new ArgumentNullException ("result");
  1238. SocketAsyncResult req = result as SocketAsyncResult;
  1239. if (req == null)
  1240. throw new ArgumentException ("Invalid IAsyncResult", "result");
  1241. if (!result.IsCompleted)
  1242. result.AsyncWaitHandle.WaitOne();
  1243. req.CheckIfThrowDelayedException();
  1244. return req.Total;
  1245. }
  1246. public int EndReceiveFrom(IAsyncResult result,
  1247. ref EndPoint end_point)
  1248. {
  1249. EnsureStillUsable();
  1250. if (result == null)
  1251. throw new ArgumentNullException ("result");
  1252. SocketAsyncResult req = result as SocketAsyncResult;
  1253. if (req == null)
  1254. throw new ArgumentException ("Invalid IAsyncResult", "result");
  1255. if (!result.IsCompleted)
  1256. result.AsyncWaitHandle.WaitOne();
  1257. req.CheckIfThrowDelayedException();
  1258. end_point = req.EndPoint;
  1259. return req.Total;
  1260. }
  1261. public int EndSend(IAsyncResult result) {
  1262. EnsureStillUsable();
  1263. if (result == null)
  1264. throw new ArgumentNullException ("result");
  1265. SocketAsyncResult req = result as SocketAsyncResult;
  1266. if (req == null)
  1267. throw new ArgumentException ("Invalid IAsyncResult", "result");
  1268. if (!result.IsCompleted)
  1269. result.AsyncWaitHandle.WaitOne();
  1270. req.CheckIfThrowDelayedException();
  1271. return req.Total;
  1272. }
  1273. public int EndSendTo(IAsyncResult result) {
  1274. EnsureStillUsable();
  1275. if (result == null)
  1276. throw new ArgumentNullException ("result");
  1277. SocketAsyncResult req = result as SocketAsyncResult;
  1278. if (req == null)
  1279. throw new ArgumentException ("Invalid IAsyncResult", "result");
  1280. if (!result.IsCompleted)
  1281. result.AsyncWaitHandle.WaitOne();
  1282. req.CheckIfThrowDelayedException();
  1283. return req.Total;
  1284. }
  1285. #if !TARGET_JVM
  1286. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1287. private extern static void GetSocketOption_obj_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, out object obj_val, out int error);
  1288. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1289. private extern static void GetSocketOption_arr_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val, out int error);
  1290. #else
  1291. private void GetSocketOption_obj_internal(GHSocket socket, SocketOptionLevel level,
  1292. SocketOptionName name, out object obj_val, out int error)
  1293. {
  1294. EnsureStillUsable();
  1295. socket.GetSocketOption_obj_internal(level, name, out obj_val, out error);
  1296. }
  1297. private void GetSocketOption_arr_internal(GHSocket socket, SocketOptionLevel level,
  1298. SocketOptionName name, ref byte[] byte_val, out int error)
  1299. {
  1300. EnsureStillUsable();
  1301. socket.GetSocketOption_arr_internal(level, name, ref byte_val, out error);
  1302. }
  1303. #endif
  1304. public object GetSocketOption(SocketOptionLevel level,
  1305. SocketOptionName name) {
  1306. object obj_val;
  1307. int error;
  1308. GetSocketOption_obj_internal(socket, level, name,
  1309. out obj_val, out error);
  1310. if (error != 0) {
  1311. throw new SocketException (error);
  1312. }
  1313. if(name==SocketOptionName.Linger) {
  1314. return((LingerOption)obj_val);
  1315. } else if (name==SocketOptionName.AddMembership ||
  1316. name==SocketOptionName.DropMembership) {
  1317. return((MulticastOption)obj_val);
  1318. } else if (obj_val is int) {
  1319. return((int)obj_val);
  1320. } else {
  1321. return(obj_val);
  1322. }
  1323. }
  1324. public void GetSocketOption(SocketOptionLevel level,
  1325. SocketOptionName name,
  1326. byte[] opt_value) {
  1327. int error;
  1328. GetSocketOption_arr_internal(socket, level, name,
  1329. ref opt_value, out error);
  1330. if (error != 0) {
  1331. throw new SocketException (error);
  1332. }
  1333. }
  1334. public byte[] GetSocketOption(SocketOptionLevel level,
  1335. SocketOptionName name,
  1336. int length) {
  1337. byte[] byte_val=new byte[length];
  1338. int error;
  1339. GetSocketOption_arr_internal(socket, level, name,
  1340. ref byte_val, out error);
  1341. if (error != 0) {
  1342. throw new SocketException (error);
  1343. }
  1344. return(byte_val);
  1345. }
  1346. #if !TARGET_JVM
  1347. // See Socket.IOControl, WSAIoctl documentation in MSDN. The
  1348. // common options between UNIX and Winsock are FIONREAD,
  1349. // FIONBIO and SIOCATMARK. Anything else will depend on the
  1350. // system.
  1351. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1352. extern static int WSAIoctl (IntPtr sock, int ioctl_code,
  1353. byte [] input, byte [] output,
  1354. out int error);
  1355. #else
  1356. int WSAIoctl (GHSocket sock, int ioctl_code,
  1357. byte [] input, byte [] output,
  1358. out int error)
  1359. {
  1360. return sock.WSAIoctl(ioctl_code, input, output, out error);
  1361. }
  1362. #endif
  1363. public int IOControl (int ioctl_code, byte [] in_value, byte [] out_value)
  1364. {
  1365. if (disposed)
  1366. throw new ObjectDisposedException (GetType ().ToString ());
  1367. int error;
  1368. int result = WSAIoctl (socket, ioctl_code, in_value,
  1369. out_value, out error);
  1370. if (error != 0) {
  1371. throw new SocketException (error);
  1372. }
  1373. if (result == -1)
  1374. throw new InvalidOperationException ("Must use Blocking property instead.");
  1375. return result;
  1376. }
  1377. #if NET_2_0
  1378. [MonoNotSupported ("")]
  1379. public int IOControl (IOControlCode ioControlCode, byte [] optionInValue, byte [] optionOutValue)
  1380. {
  1381. throw new NotImplementedException ();
  1382. }
  1383. #endif
  1384. #if !TARGET_JVM
  1385. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1386. private extern static void Listen_internal(IntPtr sock,
  1387. int backlog,
  1388. out int error);
  1389. #else
  1390. private void Listen_internal(GHSocket sock,
  1391. int backlog,
  1392. out int error)
  1393. {
  1394. EnsureStillUsable();
  1395. sock.Listen_internal(backlog, out error);
  1396. }
  1397. #endif
  1398. public void Listen(int backlog)
  1399. {
  1400. int error;
  1401. Listen_internal(socket, backlog, out error);
  1402. if (error != 0) {
  1403. throw new SocketException (error);
  1404. }
  1405. }
  1406. #if !TARGET_JVM
  1407. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1408. extern static bool Poll_internal (IntPtr socket, SelectMode mode, int timeout, out int error);
  1409. #else
  1410. bool Poll_internal (GHSocket socket, SelectMode mode, int timeout, out int error)
  1411. {
  1412. return socket.Poll_internal(mode, timeout, this, out error);
  1413. }
  1414. #endif
  1415. public bool Poll(int time_us, SelectMode mode)
  1416. {
  1417. EnsureStillUsable();
  1418. if (mode != SelectMode.SelectRead &&
  1419. mode != SelectMode.SelectWrite &&
  1420. mode != SelectMode.SelectError)
  1421. throw new NotSupportedException ("'mode' parameter is not valid.");
  1422. int error;
  1423. bool result = Poll_internal (socket, mode, time_us, out error);
  1424. if (error != 0)
  1425. throw new SocketException (error);
  1426. if (result == true) {
  1427. /* Update the connected state; for
  1428. * non-blocking Connect()s this is
  1429. * when we can find out that the
  1430. * connect succeeded.
  1431. */
  1432. connected = true;
  1433. }
  1434. return result;
  1435. }
  1436. public int Receive (byte [] buf)
  1437. {
  1438. EnsureStillUsable();
  1439. if (buf == null)
  1440. throw new ArgumentNullException ("buf");
  1441. return Receive_nochecks (buf, 0, buf.Length, SocketFlags.None);
  1442. }
  1443. public int Receive (byte [] buf, SocketFlags flags)
  1444. {
  1445. EnsureStillUsable();
  1446. if (buf == null)
  1447. throw new ArgumentNullException ("buf");
  1448. return Receive_nochecks (buf, 0, buf.Length, flags);
  1449. }
  1450. public int Receive (byte [] buf, int size, SocketFlags flags)
  1451. {
  1452. EnsureStillUsable();
  1453. if (buf == null)
  1454. throw new ArgumentNullException ("buf");
  1455. if (size < 0 || size > buf.Length)
  1456. throw new ArgumentOutOfRangeException ("size");
  1457. return Receive_nochecks (buf, 0, size, flags);
  1458. }
  1459. #if !TARGET_JVM
  1460. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1461. private extern static int Receive_internal(IntPtr sock,
  1462. byte[] buffer,
  1463. int offset,
  1464. int count,
  1465. SocketFlags flags,
  1466. out int error);
  1467. #else
  1468. private int Receive_internal(GHSocket sock,
  1469. byte[] buffer,
  1470. int offset,
  1471. int count,
  1472. SocketFlags flags,
  1473. out int error)
  1474. {
  1475. return sock.Receive_internal(buffer, offset, count, flags, out error);
  1476. }
  1477. #endif
  1478. public int Receive (byte [] buf, int offset, int size, SocketFlags flags)
  1479. {
  1480. EnsureStillUsable();
  1481. if(buf==null)
  1482. throw new ArgumentNullException ("buf");
  1483. if (offset < 0 || offset > buf.Length)
  1484. throw new ArgumentOutOfRangeException ("offset");
  1485. if (size < 0 || offset + size > buf.Length)
  1486. throw new ArgumentOutOfRangeException ("size");
  1487. return Receive_nochecks (buf, offset, size, flags);
  1488. }
  1489. int Receive_nochecks (byte [] buf, int offset, int size, SocketFlags flags)
  1490. {
  1491. int ret, error;
  1492. ret = Receive_internal (socket, buf, offset, size, flags, out error);
  1493. if(error != 0) {
  1494. if (error != 10035 && error != 10036) // WSAEWOULDBLOCK && WSAEINPROGRESS
  1495. connected=false;
  1496. throw new SocketException (error);
  1497. }
  1498. connected=true;
  1499. return ret;
  1500. }
  1501. public int ReceiveFrom (byte [] buf, ref EndPoint remote_end)
  1502. {
  1503. if (buf == null)
  1504. throw new ArgumentNullException ("buf");
  1505. if (remote_end == null)
  1506. throw new ArgumentNullException ("remote_end");
  1507. return ReceiveFrom_nochecks (buf, 0, buf.Length, SocketFlags.None, ref remote_end);
  1508. }
  1509. public int ReceiveFrom (byte [] buf, SocketFlags flags, ref EndPoint remote_end)
  1510. {
  1511. if (buf == null)
  1512. throw new ArgumentNullException ("buf");
  1513. if (remote_end == null)
  1514. throw new ArgumentNullException ("remote_end");
  1515. return ReceiveFrom_nochecks (buf, 0, buf.Length, flags, ref remote_end);
  1516. }
  1517. public int ReceiveFrom(byte[] buf, int size, SocketFlags flags,
  1518. ref EndPoint remote_end)
  1519. {
  1520. if (buf == null)
  1521. throw new ArgumentNullException ("buf");
  1522. if (remote_end == null)
  1523. throw new ArgumentNullException ("remote_end");
  1524. if (size < 0 || size > buf.Length)
  1525. throw new ArgumentOutOfRangeException ("size");
  1526. return ReceiveFrom_nochecks (buf, 0, size, flags, ref remote_end);
  1527. }
  1528. #if !TARGET_JVM
  1529. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1530. private extern static int RecvFrom_internal(IntPtr sock,
  1531. byte[] buffer,
  1532. int offset,
  1533. int count,
  1534. SocketFlags flags,
  1535. ref SocketAddress sockaddr,
  1536. out int error);
  1537. #else
  1538. private int RecvFrom_internal(GHSocket sock,
  1539. byte[] buffer,
  1540. int offset,
  1541. int count,
  1542. SocketFlags flags,
  1543. ref SocketAddress sockaddr,
  1544. out int error)
  1545. {
  1546. return sock.RecvFrom_internal(buffer, offset, count, flags, ref sockaddr, out error);
  1547. }
  1548. #endif
  1549. public int ReceiveFrom(byte[] buf, int offset, int size, SocketFlags flags,
  1550. ref EndPoint remote_end)
  1551. {
  1552. EnsureStillUsable();
  1553. if (buf == null)
  1554. throw new ArgumentNullException ("buf");
  1555. if (remote_end == null)
  1556. throw new ArgumentNullException ("remote_end");
  1557. if (offset < 0 || offset > buf.Length)
  1558. throw new ArgumentOutOfRangeException ("offset");
  1559. if (size < 0 || offset + size > buf.Length)
  1560. throw new ArgumentOutOfRangeException ("size");
  1561. return ReceiveFrom_nochecks (buf, offset, size, flags, ref remote_end);
  1562. }
  1563. int ReceiveFrom_nochecks (byte [] buf, int offset, int size, SocketFlags flags,
  1564. ref EndPoint remote_end)
  1565. {
  1566. EnsureStillUsable();
  1567. SocketAddress sockaddr=remote_end.Serialize();
  1568. int cnt, error;
  1569. cnt = RecvFrom_internal (socket, buf, offset, size, flags, ref sockaddr, out error);
  1570. if (error != 0) {
  1571. if (error != 10035 && error != 10036) // WSAEWOULDBLOCK && WSAEINPROGRESS
  1572. connected=false;
  1573. throw new SocketException (error);
  1574. }
  1575. connected=true;
  1576. // If sockaddr is null then we're a connection
  1577. // oriented protocol and should ignore the
  1578. // remote_end parameter (see MSDN
  1579. // documentation for Socket.ReceiveFrom(...) )
  1580. if ( sockaddr != null ) {
  1581. // Stupidly, EndPoint.Create() is an
  1582. // instance method
  1583. remote_end=remote_end.Create(sockaddr);
  1584. }
  1585. return cnt;
  1586. }
  1587. public int Send (byte [] buf)
  1588. {
  1589. EnsureStillUsable();
  1590. if (buf == null)
  1591. throw new ArgumentNullException ("buf");
  1592. return Send_nochecks (buf, 0, buf.Length, SocketFlags.None);
  1593. }
  1594. public int Send (byte [] buf, SocketFlags flags)
  1595. {
  1596. EnsureStillUsable();
  1597. if (buf == null)
  1598. throw new ArgumentNullException ("buf");
  1599. return Send_nochecks (buf, 0, buf.Length, flags);
  1600. }
  1601. public int Send (byte [] buf, int size, SocketFlags flags)
  1602. {
  1603. EnsureStillUsable();
  1604. if (buf == null)
  1605. throw new ArgumentNullException ("buf");
  1606. if (size < 0 || size > buf.Length)
  1607. throw new ArgumentOutOfRangeException ("size");
  1608. return Send_nochecks (buf, 0, size, flags);
  1609. }
  1610. #if NET_2_0
  1611. [MonoNotSupported ("")]
  1612. public int Send (byte [] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
  1613. {
  1614. throw new NotImplementedException ();
  1615. }
  1616. [MonoNotSupported ("")]
  1617. public int Send (IList<ArraySegment<byte>> buffers)
  1618. {
  1619. throw new NotImplementedException ();
  1620. }
  1621. [MonoNotSupported ("")]
  1622. public int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
  1623. {
  1624. throw new NotImplementedException ();
  1625. }
  1626. //[CLSCompliantAttribute (false)]
  1627. [MonoNotSupported ("")]
  1628. public int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
  1629. {
  1630. throw new NotImplementedException ();
  1631. }
  1632. [MonoNotSupported ("")]
  1633. public int Receive (byte [] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
  1634. {
  1635. throw new NotImplementedException ();
  1636. }
  1637. [MonoNotSupported ("")]
  1638. public int Receive (IList<ArraySegment<byte>> buffers)
  1639. {
  1640. throw new NotImplementedException ();
  1641. }
  1642. //[CLSCompliantAttribute (false)]
  1643. [MonoNotSupported ("")]
  1644. public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
  1645. {
  1646. throw new NotImplementedException ();
  1647. }
  1648. //[CLSCompliantAttribute (false)]
  1649. [MonoNotSupported ("")]
  1650. public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
  1651. {
  1652. throw new NotImplementedException ();
  1653. }
  1654. [MonoNotSupported ("")]
  1655. public int ReceiveMessageFrom (byte [] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
  1656. {
  1657. throw new NotImplementedException ();
  1658. }
  1659. [MonoNotSupported ("")]
  1660. public IAsyncResult BeginReceiveMessageFrom (byte [] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, Object state)
  1661. {
  1662. throw new NotImplementedException ();
  1663. }
  1664. [MonoNotSupported ("")]
  1665. public int EndReceiveMessageFrom (IAsyncResult asyncResult, ref SocketFlags socketFlags, ref EndPoint endPoint, out IPPacketInformation ipPacketInformation)
  1666. {
  1667. throw new NotImplementedException ();
  1668. }
  1669. #endif
  1670. #if !TARGET_JVM
  1671. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1672. private extern static int Send_internal(IntPtr sock,
  1673. byte[] buf, int offset,
  1674. int count,
  1675. SocketFlags flags,
  1676. out int error);
  1677. #else
  1678. private int Send_internal(GHSocket sock,
  1679. byte[] buf, int offset,
  1680. int count,
  1681. SocketFlags flags,
  1682. out int error)
  1683. {
  1684. return sock.Send_internal(buf, offset, count, flags, out error);
  1685. }
  1686. #endif
  1687. public int Send (byte[] buf, int offset, int size, SocketFlags flags)
  1688. {
  1689. EnsureStillUsable();
  1690. if (buf == null)
  1691. throw new ArgumentNullException ("buffer");
  1692. if (offset < 0 || offset > buf.Length)
  1693. throw new ArgumentOutOfRangeException ("offset");
  1694. if (size < 0 || offset + size > buf.Length)
  1695. throw new ArgumentOutOfRangeException ("size");
  1696. return Send_nochecks (buf, offset, size, flags);
  1697. }
  1698. int Send_nochecks (byte [] buf, int offset, int size, SocketFlags flags)
  1699. {
  1700. if (size == 0)
  1701. return 0;
  1702. int ret, error;
  1703. ret = Send_internal (socket, buf, offset, size, flags, out error);
  1704. if (error != 0) {
  1705. if (error != 10035 && error != 10036) // WSAEWOULDBLOCK && WSAEINPROGRESS
  1706. connected = false;
  1707. throw new SocketException (error);
  1708. }
  1709. connected = true;
  1710. return ret;
  1711. }
  1712. public int SendTo (byte [] buffer, EndPoint remote_end)
  1713. {
  1714. EnsureStillUsable();
  1715. if (buffer == null)
  1716. throw new ArgumentNullException ("buffer");
  1717. if (remote_end == null)
  1718. throw new ArgumentNullException ("remote_end");
  1719. return SendTo_nochecks (buffer, 0, buffer.Length, SocketFlags.None, remote_end);
  1720. }
  1721. public int SendTo (byte [] buffer, SocketFlags flags, EndPoint remote_end)
  1722. {
  1723. EnsureStillUsable();
  1724. if (buffer == null)
  1725. throw new ArgumentNullException ("buffer");
  1726. if (remote_end == null)
  1727. throw new ArgumentNullException ("remote_end");
  1728. return SendTo_nochecks (buffer, 0, buffer.Length, flags, remote_end);
  1729. }
  1730. public int SendTo (byte [] buffer, int size, SocketFlags flags, EndPoint remote_end)
  1731. {
  1732. EnsureStillUsable();
  1733. if (buffer == null)
  1734. throw new ArgumentNullException ("buffer");
  1735. if (remote_end == null)
  1736. throw new ArgumentNullException ("remote_end");
  1737. if (size < 0 || size > buffer.Length)
  1738. throw new ArgumentOutOfRangeException ("size");
  1739. return SendTo_nochecks (buffer, 0, size, flags, remote_end);
  1740. }
  1741. #if !TARGET_JVM
  1742. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1743. private extern static int SendTo_internal(IntPtr sock,
  1744. byte[] buffer,
  1745. int offset,
  1746. int count,
  1747. SocketFlags flags,
  1748. SocketAddress sa,
  1749. out int error);
  1750. #else
  1751. private int SendTo_internal(GHSocket sock,
  1752. byte[] buffer,
  1753. int offset,
  1754. int count,
  1755. SocketFlags flags,
  1756. SocketAddress sa,
  1757. out int error)
  1758. {
  1759. return sock.SendTo_internal(buffer, offset, count, flags, sa, out error);
  1760. }
  1761. #endif
  1762. public int SendTo(byte[] buffer, int offset, int size, SocketFlags flags,
  1763. EndPoint remote_end)
  1764. {
  1765. EnsureStillUsable();
  1766. if (buffer == null)
  1767. throw new ArgumentNullException ("buffer");
  1768. if (remote_end == null)
  1769. throw new ArgumentNullException("remote_end");
  1770. if (offset < 0 || offset > buffer.Length)
  1771. throw new ArgumentOutOfRangeException ("offset");
  1772. if (size < 0 || offset + size > buffer.Length)
  1773. throw new ArgumentOutOfRangeException ("size");
  1774. return SendTo_nochecks (buffer, offset, size, flags, remote_end);
  1775. }
  1776. int SendTo_nochecks (byte [] buffer, int offset, int size, SocketFlags flags,
  1777. EndPoint remote_end)
  1778. {
  1779. SocketAddress sockaddr=remote_end.Serialize();
  1780. int ret, error;
  1781. ret = SendTo_internal (socket, buffer, offset, size, flags, sockaddr, out error);
  1782. if (error != 0) {
  1783. if (error != 10035 && error != 10036) // WSAEWOULDBLOCK && WSAEINPROGRESS
  1784. connected=false;
  1785. throw new SocketException (error);
  1786. }
  1787. connected=true;
  1788. return ret;
  1789. }
  1790. #if !TARGET_JVM
  1791. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1792. private extern static void SetSocketOption_internal (IntPtr socket, SocketOptionLevel level,
  1793. SocketOptionName name, object obj_val,
  1794. byte [] byte_val, int int_val,
  1795. out int error);
  1796. #else
  1797. private void SetSocketOption_internal (GHSocket socket, SocketOptionLevel level,
  1798. SocketOptionName name, object obj_val,
  1799. byte [] byte_val, int int_val,
  1800. out int error)
  1801. {
  1802. socket.SetSocketOption_internal(level, name, obj_val, byte_val, int_val, out error);
  1803. }
  1804. #endif
  1805. public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, byte[] opt_value)
  1806. {
  1807. EnsureStillUsable();
  1808. int error;
  1809. SetSocketOption_internal(socket, level, name, null,
  1810. opt_value, 0, out error);
  1811. if (error != 0) {
  1812. throw new SocketException (error);
  1813. }
  1814. }
  1815. public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, int opt_value)
  1816. {
  1817. EnsureStillUsable();
  1818. int error;
  1819. SetSocketOption_internal(socket, level, name, null,
  1820. null, opt_value, out error);
  1821. if (error != 0) {
  1822. throw new SocketException (error);
  1823. }
  1824. }
  1825. public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, object opt_value)
  1826. {
  1827. EnsureStillUsable();
  1828. if(opt_value==null) {
  1829. throw new ArgumentNullException();
  1830. }
  1831. int error;
  1832. /* From MS documentation on SetSocketOption: "For an
  1833. * option with a Boolean data type, specify a nonzero
  1834. * value to enable the option, and a zero value to
  1835. * disable the option."
  1836. * Booleans are only handled in 2.0
  1837. */
  1838. if (opt_value is System.Boolean) {
  1839. #if NET_2_0
  1840. bool bool_val = (bool) opt_value;
  1841. int int_val = (bool_val) ? 1 : 0;
  1842. SetSocketOption_internal (socket, level, name, null, null, int_val, out error);
  1843. #else
  1844. throw new ArgumentException ("Use an integer 1 (true) or 0 (false) instead of a boolean.", "opt_value");
  1845. #endif
  1846. } else {
  1847. SetSocketOption_internal (socket, level, name, opt_value, null, 0, out error);
  1848. }
  1849. if (error != 0)
  1850. throw new SocketException (error);
  1851. }
  1852. #if NET_2_0
  1853. public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, bool optionValue) {
  1854. EnsureStillUsable();
  1855. int error;
  1856. int int_val = (optionValue) ? 1 : 0;
  1857. SetSocketOption_internal (socket, level, name, null, null, int_val, out error);
  1858. if (error != 0)
  1859. throw new SocketException (error);
  1860. }
  1861. #endif
  1862. #if !TARGET_JVM
  1863. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  1864. private extern static void Shutdown_internal(IntPtr socket, SocketShutdown how, out int error);
  1865. #else
  1866. private void Shutdown_internal(GHSocket socket, SocketShutdown how, out int error)
  1867. {
  1868. socket.Shutdown_internal(how, out error);
  1869. }
  1870. #endif
  1871. public void Shutdown(SocketShutdown how)
  1872. {
  1873. EnsureStillUsable();
  1874. int error;
  1875. Shutdown_internal(socket, how, out error);
  1876. if (error != 0) {
  1877. throw new SocketException (error);
  1878. }
  1879. }
  1880. #if !TARGET_JVM
  1881. public override int GetHashCode ()
  1882. {
  1883. return (int) socket;
  1884. }
  1885. #else
  1886. public override int GetHashCode ()
  1887. {
  1888. if (socket == null)
  1889. return -1;
  1890. return socket.GetHashCode();
  1891. }
  1892. #endif
  1893. #if !TARGET_JVM
  1894. protected virtual void Dispose (bool explicitDisposing)
  1895. {
  1896. if (disposed)
  1897. return;
  1898. disposed = true;
  1899. connected = false;
  1900. if ((int) socket != -1)
  1901. {
  1902. int error;
  1903. closed = true;
  1904. IntPtr x = socket;
  1905. socket = (IntPtr) (-1);
  1906. Close_internal (x, out error);
  1907. if (blocking_thread != null)
  1908. {
  1909. blocking_thread.Abort ();
  1910. blocking_thread = null;
  1911. }
  1912. if (error != 0)
  1913. throw new SocketException (error);
  1914. }
  1915. }
  1916. #else
  1917. private void EnsureStillUsable()
  1918. {
  1919. if (disposed && closed)
  1920. throw new ObjectDisposedException(this.GetType().ToString());
  1921. }
  1922. protected virtual void Dispose (bool explicitDisposing)
  1923. {
  1924. if (disposed)
  1925. return;
  1926. disposed = true;
  1927. connected = false;
  1928. if (socket != null)
  1929. {
  1930. int error;
  1931. closed = true;
  1932. GHSocket x = socket;
  1933. socket = null;
  1934. Close_internal (x, out error);
  1935. if (error != 0)
  1936. throw new SocketException (error);
  1937. }
  1938. }
  1939. #endif
  1940. #region .Net 2.0 properties and methods
  1941. #if NET_2_0
  1942. #region Properties
  1943. [MonoTODO]
  1944. public int ReceiveBufferSize
  1945. {
  1946. get { throw new NotImplementedException(); }
  1947. set { throw new NotImplementedException(); }
  1948. }
  1949. [MonoTODO]
  1950. public int SendBufferSize
  1951. {
  1952. get { throw new NotImplementedException(); }
  1953. set { throw new NotImplementedException(); }
  1954. }
  1955. [MonoTODO]
  1956. public bool UseOnlyOverlappedIO
  1957. {
  1958. get { throw new NotImplementedException(); }
  1959. set { throw new NotImplementedException(); }
  1960. }
  1961. [MonoTODO]
  1962. public bool NoDelay
  1963. {
  1964. get { throw new NotImplementedException(); }
  1965. set { throw new NotImplementedException(); }
  1966. }
  1967. [MonoTODO]
  1968. public bool IsBound
  1969. {
  1970. get { throw new NotImplementedException(); }
  1971. }
  1972. [MonoTODO]
  1973. public bool ExclusiveAddressUse
  1974. {
  1975. get { throw new NotImplementedException(); }
  1976. set { throw new NotImplementedException(); }
  1977. }
  1978. [MonoTODO("udp sockets are not supported")]
  1979. public bool DontFragment
  1980. {
  1981. get { throw new NotImplementedException(); }
  1982. set { throw new NotImplementedException(); }
  1983. }
  1984. [MonoTODO]
  1985. public bool EnableBroadcast
  1986. {
  1987. get { throw new NotImplementedException(); }
  1988. set { throw new NotImplementedException(); }
  1989. }
  1990. [MonoTODO]
  1991. public bool MulticastLoopback
  1992. {
  1993. get { throw new NotImplementedException(); }
  1994. set { throw new NotImplementedException(); }
  1995. }
  1996. [MonoTODO]
  1997. public short Ttl
  1998. {
  1999. get { throw new NotImplementedException(); }
  2000. set { throw new NotImplementedException(); }
  2001. }
  2002. [MonoTODO]
  2003. public int ReceiveTimeout
  2004. {
  2005. get { throw new NotImplementedException(); }
  2006. set { throw new NotImplementedException(); }
  2007. }
  2008. [MonoTODO]
  2009. public int SendTimeout
  2010. {
  2011. get { throw new NotImplementedException(); }
  2012. set { throw new NotImplementedException(); }
  2013. }
  2014. #endregion //Properties
  2015. #region Methods
  2016. [MonoTODO]
  2017. public IAsyncResult BeginConnect(IPAddress address, int port,
  2018. AsyncCallback requestCallback,
  2019. object state)
  2020. {
  2021. throw new NotImplementedException();
  2022. }
  2023. [MonoTODO]
  2024. public IAsyncResult BeginConnect(IPAddress[] addresses, int port,
  2025. AsyncCallback requestCallback,
  2026. object state)
  2027. {
  2028. throw new NotImplementedException();
  2029. }
  2030. [MonoTODO]
  2031. public IAsyncResult BeginConnect(string host, int port,
  2032. AsyncCallback requestCallback,
  2033. object state)
  2034. {
  2035. throw new NotImplementedException();
  2036. }
  2037. [MonoTODO]
  2038. public IAsyncResult BeginAccept(int receiveSize, AsyncCallback callback, object state)
  2039. {
  2040. throw new NotImplementedException();
  2041. }
  2042. [MonoTODO]
  2043. public IAsyncResult BeginAccept( Socket acceptSocket,int receiveSize,
  2044. AsyncCallback callback, object state)
  2045. {
  2046. throw new NotImplementedException();
  2047. }
  2048. [MonoNotSupported ("")]
  2049. public Socket EndAccept (out byte [] buffer, IAsyncResult asyncResult)
  2050. {
  2051. throw new NotImplementedException ();
  2052. }
  2053. [MonoNotSupported ("")]
  2054. public Socket EndAccept (out byte [] buffer, out int bytesTransferred, IAsyncResult asyncResult)
  2055. {
  2056. throw new NotImplementedException ();
  2057. }
  2058. [MonoTODO]
  2059. public IAsyncResult BeginDisconnect(bool reuseSocket, AsyncCallback callback, object state)
  2060. {
  2061. throw new NotImplementedException();
  2062. }
  2063. [MonoNotSupported ("")]
  2064. public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
  2065. {
  2066. throw new NotImplementedException ();
  2067. }
  2068. [MonoNotSupported ("")]
  2069. [CLSCompliantAttribute (false)]
  2070. public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags,
  2071. out SocketError errorCode, AsyncCallback callback,
  2072. object state)
  2073. {
  2074. throw new NotImplementedException ();
  2075. }
  2076. [MonoTODO]
  2077. public IAsyncResult BeginSend(byte[] buffer, int offset, int size,
  2078. SocketFlags socketFlags, out SocketError errorCode,
  2079. AsyncCallback callback, object state)
  2080. {
  2081. throw new NotImplementedException();
  2082. }
  2083. [MonoNotSupported ("")]
  2084. public int EndSend (IAsyncResult asyncResult, out SocketError errorCode)
  2085. {
  2086. throw new NotImplementedException ();
  2087. }
  2088. [MonoNotSupported ("")]
  2089. public IAsyncResult BeginReceive (byte [] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, Object state)
  2090. {
  2091. throw new NotImplementedException ();
  2092. }
  2093. [CLSCompliantAttribute (false)]
  2094. [MonoNotSupported ("")]
  2095. public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, Object state)
  2096. {
  2097. throw new NotImplementedException ();
  2098. }
  2099. [CLSCompliantAttribute (false)]
  2100. [MonoNotSupported ("")]
  2101. public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, Object state)
  2102. {
  2103. throw new NotImplementedException ();
  2104. }
  2105. [MonoNotSupported ("")]
  2106. public int EndReceive (IAsyncResult asyncResult, out SocketError errorCode)
  2107. {
  2108. throw new NotImplementedException ();
  2109. }
  2110. [MonoNotSupported ("")]
  2111. public void SendFile (string fileName)
  2112. {
  2113. throw new NotImplementedException ();
  2114. }
  2115. [MonoNotSupported ("")]
  2116. public void SendFile (string fileName, byte [] preBuffer, byte [] postBuffer, TransmitFileOptions flags)
  2117. {
  2118. throw new NotImplementedException ();
  2119. }
  2120. [MonoNotSupported ("")]
  2121. public IAsyncResult BeginSendFile (string fileName, AsyncCallback callback, object state)
  2122. {
  2123. throw new NotImplementedException ();
  2124. }
  2125. [MonoNotSupported ("")]
  2126. public IAsyncResult BeginSendFile (string fileName, byte [] preBuffer, byte [] postBuffer, TransmitFileOptions flags, AsyncCallback callback, object state)
  2127. {
  2128. throw new NotImplementedException ();
  2129. }
  2130. [MonoNotSupported ("")]
  2131. public void EndSendFile (IAsyncResult asyncResult)
  2132. {
  2133. throw new NotImplementedException ();
  2134. }
  2135. [MonoTODO]
  2136. public void Close(int timeout)
  2137. {
  2138. throw new NotImplementedException();
  2139. }
  2140. [MonoTODO]
  2141. public void Connect(IPAddress address, int port)
  2142. {
  2143. throw new NotImplementedException();
  2144. }
  2145. [MonoTODO]
  2146. public void Connect(IPAddress[] address, int port)
  2147. {
  2148. throw new NotImplementedException();
  2149. }
  2150. [MonoTODO]
  2151. public void Connect(string host, int port)
  2152. {
  2153. throw new NotImplementedException();
  2154. }
  2155. [MonoNotSupported ("")]
  2156. public void Disconnect (bool reuseSocket)
  2157. {
  2158. throw new NotImplementedException ();
  2159. }
  2160. [MonoNotSupported ("")]
  2161. public SocketInformation DuplicateAndClose (int targetProcessId)
  2162. {
  2163. throw new NotImplementedException ();
  2164. }
  2165. #endregion //Methods
  2166. #endif
  2167. #endregion
  2168. void IDisposable.Dispose ()
  2169. {
  2170. Dispose (true);
  2171. GC.SuppressFinalize (this);
  2172. }
  2173. ~Socket () {
  2174. Dispose(false);
  2175. }
  2176. }
  2177. }