Socket.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // System.Net.Sockets.Socket.cs
  2. //
  3. // Authors:
  4. // Phillip Pearson ([email protected])
  5. // Dick Porter <[email protected]>
  6. //
  7. // Copyright (C) 2001, 2002 Phillip Pearson and Ximian, Inc.
  8. // http://www.myelin.co.nz
  9. //
  10. using System;
  11. using System.Net;
  12. using System.Collections;
  13. using System.Runtime.CompilerServices;
  14. using System.Threading;
  15. namespace System.Net.Sockets
  16. {
  17. public class Socket : IDisposable
  18. {
  19. private sealed class SocketAsyncResult: IAsyncResult
  20. {
  21. private object state;
  22. private WaitHandle waithandle;
  23. private bool completed_sync, completed;
  24. private Worker worker;
  25. public SocketAsyncResult(object state) {
  26. this.state=state;
  27. waithandle=new ManualResetEvent(false);
  28. completed_sync=completed=false;
  29. }
  30. public object AsyncState {
  31. get {
  32. return(state);
  33. }
  34. }
  35. public WaitHandle AsyncWaitHandle {
  36. get {
  37. return(waithandle);
  38. }
  39. set {
  40. waithandle=value;
  41. }
  42. }
  43. public bool CompletedSynchronously {
  44. get {
  45. return(completed_sync);
  46. }
  47. }
  48. public bool IsCompleted {
  49. get {
  50. return(completed);
  51. }
  52. set {
  53. completed=value;
  54. }
  55. }
  56. public Worker Worker {
  57. get {
  58. return(worker);
  59. }
  60. set {
  61. worker=value;
  62. }
  63. }
  64. }
  65. private sealed class Worker
  66. {
  67. private AsyncCallback callback;
  68. private SocketAsyncResult result;
  69. private Socket socket;
  70. // Parameters
  71. private EndPoint endpoint; // Connect,ReceiveFrom,SendTo
  72. private byte[] buffer; // Receive,ReceiveFrom,Send,SendTo
  73. private int offset; // Receive,ReceiveFrom,Send,SendTo
  74. private int size; // Receive,ReceiveFrom,Send,SendTo
  75. private SocketFlags sockflags; // Receive,ReceiveFrom,Send,SendTo
  76. // Return values
  77. private Socket acc_socket;
  78. private int total;
  79. // For Accept
  80. public Worker(Socket req_sock,
  81. AsyncCallback req_callback,
  82. SocketAsyncResult req_result)
  83. : this(req_sock, null, 0, 0, SocketFlags.None,
  84. null, req_callback, req_result) {}
  85. // For Connect
  86. public Worker(Socket req_sock, EndPoint req_endpoint,
  87. AsyncCallback req_callback,
  88. SocketAsyncResult req_result)
  89. : this(req_sock, null, 0, 0, SocketFlags.None,
  90. req_endpoint, req_callback,
  91. req_result) {}
  92. // For Receive and Send
  93. public Worker(Socket req_sock, byte[] req_buffer,
  94. int req_offset, int req_size,
  95. SocketFlags req_sockflags,
  96. AsyncCallback req_callback,
  97. SocketAsyncResult req_result)
  98. : this(req_sock, req_buffer, req_offset,
  99. req_size, req_sockflags, null,
  100. req_callback, req_result) {}
  101. // For ReceiveFrom and SendTo
  102. public Worker(Socket req_sock, byte[] req_buffer,
  103. int req_offset, int req_size,
  104. SocketFlags req_sockflags,
  105. EndPoint req_endpoint,
  106. AsyncCallback req_callback,
  107. SocketAsyncResult req_result) {
  108. socket=req_sock;
  109. buffer=req_buffer;
  110. offset=req_offset;
  111. size=req_size;
  112. sockflags=req_sockflags;
  113. endpoint=req_endpoint;
  114. callback=req_callback;
  115. result=req_result;
  116. }
  117. private void End() {
  118. callback(result);
  119. ((ManualResetEvent)result.AsyncWaitHandle).Set();
  120. result.IsCompleted=true;
  121. }
  122. public void Accept() {
  123. lock(result) {
  124. acc_socket=socket.Accept();
  125. End();
  126. }
  127. }
  128. public void Connect() {
  129. lock(result) {
  130. socket.Connect(endpoint);
  131. End();
  132. }
  133. }
  134. public void Receive() {
  135. lock(result) {
  136. total=socket.Receive(buffer, offset,
  137. size, sockflags);
  138. End();
  139. }
  140. }
  141. public void ReceiveFrom() {
  142. lock(result) {
  143. total=socket.ReceiveFrom(buffer,
  144. offset, size,
  145. sockflags,
  146. ref endpoint);
  147. End();
  148. }
  149. }
  150. public void Send() {
  151. lock(result) {
  152. total=socket.Send(buffer, offset, size,
  153. sockflags);
  154. End();
  155. }
  156. }
  157. public void SendTo() {
  158. lock(result) {
  159. total=socket.SendTo(buffer, offset,
  160. size, sockflags,
  161. endpoint);
  162. End();
  163. }
  164. }
  165. public EndPoint EndPoint {
  166. get {
  167. return(endpoint);
  168. }
  169. }
  170. public Socket Socket {
  171. get {
  172. return(acc_socket);
  173. }
  174. }
  175. public int Total {
  176. get {
  177. return(total);
  178. }
  179. }
  180. }
  181. /* the field "socket" is looked up by name by the runtime */
  182. private IntPtr socket;
  183. private AddressFamily address_family;
  184. private SocketType socket_type;
  185. private ProtocolType protocol_type;
  186. private bool blocking=true;
  187. /* When true, the socket was connected at the time of
  188. * the last IO operation
  189. */
  190. private bool connected=false;
  191. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  192. private extern static void Select_internal(ref Socket[] read,
  193. ref Socket[] write,
  194. ref Socket[] err,
  195. int timeout);
  196. public static void Select(IList read_list, IList write_list,
  197. IList err_list, int time_us) {
  198. if(read_list==null &&
  199. write_list==null &&
  200. err_list==null) {
  201. throw new ArgumentNullException();
  202. }
  203. int read_count, write_count, err_count;
  204. if(read_list!=null) {
  205. read_count=read_list.Count;
  206. } else {
  207. read_count=0;
  208. }
  209. if(write_list!=null) {
  210. write_count=write_list.Count;
  211. } else {
  212. write_count=0;
  213. }
  214. if(err_list!=null) {
  215. err_count=err_list.Count;
  216. } else {
  217. err_count=0;
  218. }
  219. Socket[] read_arr=new Socket[read_count];
  220. Socket[] write_arr=new Socket[write_count];
  221. Socket[] err_arr=new Socket[err_count];
  222. int i;
  223. if(read_list!=null) {
  224. i=0;
  225. foreach (Socket s in read_list) {
  226. read_arr[i]=s;
  227. i++;
  228. }
  229. }
  230. if(write_list!=null) {
  231. i=0;
  232. foreach (Socket s in write_list) {
  233. write_arr[i]=s;
  234. i++;
  235. }
  236. }
  237. if(err_list!=null) {
  238. i=0;
  239. foreach (Socket s in err_list) {
  240. err_arr[i]=s;
  241. i++;
  242. }
  243. }
  244. Select_internal(ref read_arr, ref write_arr,
  245. ref err_arr, time_us);
  246. if(read_list!=null) {
  247. read_list.Clear();
  248. for(i=0; i<read_arr.Length; i++) {
  249. read_list.Add(read_arr[i]);
  250. }
  251. }
  252. if(write_list!=null) {
  253. write_list.Clear();
  254. for(i=0; i<write_arr.Length; i++) {
  255. write_list.Add(write_arr[i]);
  256. }
  257. }
  258. if(err_list!=null) {
  259. err_list.Clear();
  260. for(i=0; i<err_arr.Length; i++) {
  261. err_list.Add(err_arr[i]);
  262. }
  263. }
  264. }
  265. // private constructor used by Accept, which already
  266. // has a socket handle to use
  267. private Socket(AddressFamily family, SocketType type,
  268. ProtocolType proto, IntPtr sock) {
  269. address_family=family;
  270. socket_type=type;
  271. protocol_type=proto;
  272. socket=sock;
  273. connected=true;
  274. }
  275. // Creates a new system socket, returning the handle
  276. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  277. private extern IntPtr Socket_internal(AddressFamily family,
  278. SocketType type,
  279. ProtocolType proto);
  280. public Socket(AddressFamily family, SocketType type,
  281. ProtocolType proto) {
  282. address_family=family;
  283. socket_type=type;
  284. protocol_type=proto;
  285. socket=Socket_internal(family, type, proto);
  286. }
  287. public AddressFamily AddressFamily {
  288. get {
  289. return(address_family);
  290. }
  291. }
  292. // Returns the amount of data waiting to be read on socket
  293. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  294. private extern static int Available_internal(IntPtr socket);
  295. public int Available {
  296. get {
  297. return(Available_internal(socket));
  298. }
  299. }
  300. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  301. private extern static void Blocking_internal(IntPtr socket,
  302. bool block);
  303. public bool Blocking {
  304. get {
  305. return(blocking);
  306. }
  307. set {
  308. Blocking_internal(socket, value);
  309. blocking=value;
  310. }
  311. }
  312. public bool Connected {
  313. get {
  314. return(connected);
  315. }
  316. }
  317. public IntPtr Handle {
  318. get {
  319. return(socket);
  320. }
  321. }
  322. // Returns the local endpoint details in addr and port
  323. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  324. private extern static SocketAddress LocalEndPoint_internal(IntPtr socket);
  325. [MonoTODO("Support non-IP endpoints")]
  326. public EndPoint LocalEndPoint {
  327. get {
  328. SocketAddress sa;
  329. sa=LocalEndPoint_internal(socket);
  330. if(sa.Family==AddressFamily.InterNetwork) {
  331. // Stupidly, EndPoint.Create() is an
  332. // instance method
  333. return new IPEndPoint(0, 0).Create(sa);
  334. } else {
  335. throw new NotImplementedException();
  336. }
  337. }
  338. }
  339. public ProtocolType ProtocolType {
  340. get {
  341. return(protocol_type);
  342. }
  343. }
  344. // Returns the remote endpoint details in addr and port
  345. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  346. private extern static SocketAddress RemoteEndPoint_internal(IntPtr socket);
  347. [MonoTODO("Support non-IP endpoints")]
  348. public EndPoint RemoteEndPoint {
  349. get {
  350. SocketAddress sa;
  351. sa=RemoteEndPoint_internal(socket);
  352. if(sa.Family==AddressFamily.InterNetwork) {
  353. // Stupidly, EndPoint.Create() is an
  354. // instance method
  355. return new IPEndPoint(0, 0).Create(sa);
  356. } else {
  357. throw new NotImplementedException();
  358. }
  359. }
  360. }
  361. public SocketType SocketType {
  362. get {
  363. return(socket_type);
  364. }
  365. }
  366. // Creates a new system socket, returning the handle
  367. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  368. private extern static IntPtr Accept_internal(IntPtr sock);
  369. public Socket Accept() {
  370. IntPtr sock=Accept_internal(socket);
  371. return(new Socket(this.AddressFamily, this.SocketType,
  372. this.ProtocolType, sock));
  373. }
  374. public IAsyncResult BeginAccept(AsyncCallback callback,
  375. object state) {
  376. SocketAsyncResult req=new SocketAsyncResult(state);
  377. Worker worker=new Worker(this, callback, req);
  378. Thread child=new Thread(new ThreadStart(worker.Accept));
  379. child.Start();
  380. return(req);
  381. }
  382. public IAsyncResult BeginConnect(EndPoint end_point,
  383. AsyncCallback callback,
  384. object state) {
  385. SocketAsyncResult req=new SocketAsyncResult(state);
  386. Worker worker=new Worker(this, end_point, callback,
  387. req);
  388. Thread child=new Thread(new ThreadStart(worker.Connect));
  389. child.Start();
  390. return(req);
  391. }
  392. public IAsyncResult BeginReceive(byte[] buffer, int offset,
  393. int size,
  394. SocketFlags socket_flags,
  395. AsyncCallback callback,
  396. object state) {
  397. SocketAsyncResult req=new SocketAsyncResult(state);
  398. Worker worker=new Worker(this, buffer, offset, size,
  399. socket_flags, callback, req);
  400. Thread child=new Thread(new ThreadStart(worker.Receive));
  401. child.Start();
  402. return(req);
  403. }
  404. public IAsyncResult BeginReceiveFrom(byte[] buffer, int offset,
  405. int size,
  406. SocketFlags socket_flags,
  407. ref EndPoint remote_end,
  408. AsyncCallback callback,
  409. object state) {
  410. SocketAsyncResult req=new SocketAsyncResult(state);
  411. Worker worker=new Worker(this, buffer, offset, size,
  412. socket_flags, remote_end,
  413. callback, req);
  414. Thread child=new Thread(new ThreadStart(worker.ReceiveFrom));
  415. child.Start();
  416. return(req);
  417. }
  418. public IAsyncResult BeginSend(byte[] buffer, int offset,
  419. int size,
  420. SocketFlags socket_flags,
  421. AsyncCallback callback,
  422. object state) {
  423. SocketAsyncResult req=new SocketAsyncResult(state);
  424. Worker worker=new Worker(this, buffer, offset, size,
  425. socket_flags, callback, req);
  426. Thread child=new Thread(new ThreadStart(worker.Send));
  427. child.Start();
  428. return(req);
  429. }
  430. public IAsyncResult BeginSendTo(byte[] buffer, int offset,
  431. int size,
  432. SocketFlags socket_flags,
  433. EndPoint remote_end,
  434. AsyncCallback callback,
  435. object state) {
  436. SocketAsyncResult req=new SocketAsyncResult(state);
  437. Worker worker=new Worker(this, buffer, offset, size,
  438. socket_flags, remote_end,
  439. callback, req);
  440. Thread child=new Thread(new ThreadStart(worker.SendTo));
  441. child.Start();
  442. return(req);
  443. }
  444. // Creates a new system socket, returning the handle
  445. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  446. private extern static void Bind_internal(IntPtr sock,
  447. SocketAddress sa);
  448. public void Bind(EndPoint local_end) {
  449. if(local_end==null) {
  450. throw new ArgumentNullException();
  451. }
  452. Bind_internal(socket, local_end.Serialize());
  453. }
  454. // Closes the socket
  455. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  456. private extern static void Close_internal(IntPtr socket);
  457. public void Close() {
  458. connected=false;
  459. Close_internal(socket);
  460. }
  461. // Connects to the remote address
  462. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  463. private extern static void Connect_internal(IntPtr sock,
  464. SocketAddress sa);
  465. public void Connect(EndPoint remote_end) {
  466. if(remote_end==null) {
  467. throw new ArgumentNullException();
  468. }
  469. Connect_internal(socket, remote_end.Serialize());
  470. connected=true;
  471. }
  472. public Socket EndAccept(IAsyncResult result) {
  473. SocketAsyncResult req=(SocketAsyncResult)result;
  474. result.AsyncWaitHandle.WaitOne();
  475. return(req.Worker.Socket);
  476. }
  477. public void EndConnect(IAsyncResult result) {
  478. SocketAsyncResult req=(SocketAsyncResult)result;
  479. result.AsyncWaitHandle.WaitOne();
  480. }
  481. public int EndReceive(IAsyncResult result) {
  482. SocketAsyncResult req=(SocketAsyncResult)result;
  483. result.AsyncWaitHandle.WaitOne();
  484. return(req.Worker.Total);
  485. }
  486. public int EndReceiveFrom(IAsyncResult result,
  487. ref EndPoint end_point) {
  488. SocketAsyncResult req=(SocketAsyncResult)result;
  489. result.AsyncWaitHandle.WaitOne();
  490. end_point=req.Worker.EndPoint;
  491. return(req.Worker.Total);
  492. }
  493. public int EndSend(IAsyncResult result) {
  494. SocketAsyncResult req=(SocketAsyncResult)result;
  495. result.AsyncWaitHandle.WaitOne();
  496. return(req.Worker.Total);
  497. }
  498. public int EndSendTo(IAsyncResult result) {
  499. SocketAsyncResult req=(SocketAsyncResult)result;
  500. result.AsyncWaitHandle.WaitOne();
  501. return(req.Worker.Total);
  502. }
  503. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  504. private extern static void GetSocketOption_obj_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, out object obj_val);
  505. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  506. private extern static void GetSocketOption_arr_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val);
  507. public object GetSocketOption(SocketOptionLevel level,
  508. SocketOptionName name) {
  509. object obj_val;
  510. GetSocketOption_obj_internal(socket, level, name,
  511. out obj_val);
  512. if(name==SocketOptionName.Linger) {
  513. return((LingerOption)obj_val);
  514. } else if (name==SocketOptionName.AddMembership ||
  515. name==SocketOptionName.DropMembership) {
  516. return((MulticastOption)obj_val);
  517. } else {
  518. return((int)obj_val);
  519. }
  520. }
  521. public void GetSocketOption(SocketOptionLevel level,
  522. SocketOptionName name,
  523. byte[] opt_value) {
  524. int opt_value_len=opt_value.Length;
  525. GetSocketOption_arr_internal(socket, level, name,
  526. ref opt_value);
  527. }
  528. public byte[] GetSocketOption(SocketOptionLevel level,
  529. SocketOptionName name,
  530. int length) {
  531. byte[] byte_val=new byte[length];
  532. GetSocketOption_arr_internal(socket, level, name,
  533. ref byte_val);
  534. return(byte_val);
  535. }
  536. [MonoTODO("Totally undocumented")]
  537. public int IOControl(int ioctl_code, byte[] in_value,
  538. byte[] out_value) {
  539. throw new NotImplementedException();
  540. }
  541. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  542. private extern static void Listen_internal(IntPtr sock,
  543. int backlog);
  544. public void Listen(int backlog) {
  545. Listen_internal(socket, backlog);
  546. }
  547. /* The docs for Poll() are a bit lightweight too, but
  548. * it seems to be just a simple wrapper around Select.
  549. */
  550. public bool Poll(int time_us, SelectMode mode) {
  551. ArrayList socketlist=new ArrayList(1);
  552. socketlist.Add(this);
  553. switch(mode) {
  554. case SelectMode.SelectError:
  555. Select(null, null, socketlist, time_us);
  556. break;
  557. case SelectMode.SelectRead:
  558. Select(socketlist, null, null, time_us);
  559. break;
  560. case SelectMode.SelectWrite:
  561. Select(null, socketlist, null, time_us);
  562. break;
  563. default:
  564. throw new NotSupportedException();
  565. }
  566. if(socketlist.Contains(this)) {
  567. return(true);
  568. } else {
  569. return(false);
  570. }
  571. }
  572. public int Receive(byte[] buf) {
  573. return(Receive(buf, 0, buf.Length, SocketFlags.None));
  574. }
  575. public int Receive(byte[] buf, SocketFlags flags) {
  576. return(Receive(buf, 0, buf.Length, flags));
  577. }
  578. public int Receive(byte[] buf, int size, SocketFlags flags) {
  579. return(Receive(buf, 0, size, flags));
  580. }
  581. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  582. private extern static int Receive_internal(IntPtr sock,
  583. byte[] buffer,
  584. int offset,
  585. int count,
  586. SocketFlags flags);
  587. public int Receive(byte[] buf, int offset, int size,
  588. SocketFlags flags) {
  589. if(buf==null) {
  590. throw new ArgumentNullException();
  591. }
  592. if(offset+size>buf.Length) {
  593. throw new ArgumentException();
  594. }
  595. int ret;
  596. try {
  597. ret=Receive_internal(socket, buf, offset,
  598. size, flags);
  599. } catch(SocketException) {
  600. connected=false;
  601. throw;
  602. }
  603. connected=true;
  604. return(ret);
  605. }
  606. public int ReceiveFrom(byte[] buf, ref EndPoint remote_end) {
  607. return(ReceiveFrom(buf, 0, buf.Length,
  608. SocketFlags.None, ref remote_end));
  609. }
  610. public int ReceiveFrom(byte[] buf, SocketFlags flags,
  611. ref EndPoint remote_end) {
  612. return(ReceiveFrom(buf, 0, buf.Length, flags,
  613. ref remote_end));
  614. }
  615. public int ReceiveFrom(byte[] buf, int size, SocketFlags flags,
  616. ref EndPoint remote_end) {
  617. return(ReceiveFrom(buf, 0, size, flags,
  618. ref remote_end));
  619. }
  620. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  621. private extern static int RecvFrom_internal(IntPtr sock,
  622. byte[] buffer,
  623. int offset,
  624. int count,
  625. SocketFlags flags,
  626. ref SocketAddress sockaddr);
  627. public int ReceiveFrom(byte[] buf, int offset, int size,
  628. SocketFlags flags,
  629. ref EndPoint remote_end) {
  630. if(buf==null || remote_end==null) {
  631. throw new ArgumentNullException();
  632. }
  633. if(offset+size>buf.Length) {
  634. throw new ArgumentException();
  635. }
  636. SocketAddress sockaddr=remote_end.Serialize();
  637. int count;
  638. try {
  639. count=RecvFrom_internal(socket, buf, offset,
  640. size, flags,
  641. ref sockaddr);
  642. } catch(SocketException) {
  643. connected=false;
  644. throw;
  645. }
  646. connected=true;
  647. // Stupidly, EndPoint.Create() is an
  648. // instance method
  649. remote_end=remote_end.Create(sockaddr);
  650. return(count);
  651. }
  652. public int Send(byte[] buf) {
  653. return(Send(buf, 0, buf.Length, SocketFlags.None));
  654. }
  655. public int Send(byte[] buf, SocketFlags flags) {
  656. return(Send(buf, 0, buf.Length, flags));
  657. }
  658. public int Send(byte[] buf, int size, SocketFlags flags) {
  659. return(Send(buf, 0, size, flags));
  660. }
  661. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  662. private extern static int Send_internal(IntPtr sock,
  663. byte[] buf, int offset,
  664. int count,
  665. SocketFlags flags);
  666. public int Send(byte[] buf, int offset, int size,
  667. SocketFlags flags) {
  668. if(buf==null) {
  669. throw new ArgumentNullException();
  670. }
  671. if(offset+size>buf.Length) {
  672. throw new ArgumentException();
  673. }
  674. int ret;
  675. try {
  676. ret=Send_internal(socket, buf, offset, size,
  677. flags);
  678. } catch(SocketException) {
  679. connected=false;
  680. throw;
  681. }
  682. connected=true;
  683. return(ret);
  684. }
  685. public int SendTo(byte[] buffer, EndPoint remote_end) {
  686. return(SendTo(buffer, 0, buffer.Length,
  687. SocketFlags.None, remote_end));
  688. }
  689. public int SendTo(byte[] buffer, SocketFlags flags,
  690. EndPoint remote_end) {
  691. return(SendTo(buffer, 0, buffer.Length, flags,
  692. remote_end));
  693. }
  694. public int SendTo(byte[] buffer, int size, SocketFlags flags,
  695. EndPoint remote_end) {
  696. return(SendTo(buffer, size, buffer.Length, flags,
  697. remote_end));
  698. }
  699. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  700. private extern static int SendTo_internal(IntPtr sock,
  701. byte[] buffer,
  702. int offset,
  703. int count,
  704. SocketFlags flags,
  705. SocketAddress sa);
  706. public int SendTo(byte[] buffer, int offset, int size,
  707. SocketFlags flags, EndPoint remote_end) {
  708. if(buffer==null || remote_end==null) {
  709. throw new ArgumentNullException();
  710. }
  711. if(offset+size>buffer.Length) {
  712. throw new ArgumentException();
  713. }
  714. SocketAddress sockaddr=remote_end.Serialize();
  715. int ret;
  716. try {
  717. ret=SendTo_internal(socket, buffer, offset,
  718. size, flags, sockaddr);
  719. }
  720. catch(SocketException) {
  721. connected=false;
  722. throw;
  723. }
  724. connected=true;
  725. return(ret);
  726. }
  727. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  728. private extern static void SetSocketOption_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, object obj_val, byte[] byte_val, int int_val);
  729. public void SetSocketOption(SocketOptionLevel level,
  730. SocketOptionName name,
  731. byte[] opt_value) {
  732. SetSocketOption_internal(socket, level, name, null,
  733. opt_value, 0);
  734. }
  735. public void SetSocketOption(SocketOptionLevel level,
  736. SocketOptionName name,
  737. int opt_value) {
  738. SetSocketOption_internal(socket, level, name, null,
  739. null, opt_value);
  740. }
  741. public void SetSocketOption(SocketOptionLevel level,
  742. SocketOptionName name,
  743. object opt_value) {
  744. if(opt_value==null) {
  745. throw new ArgumentNullException();
  746. }
  747. SetSocketOption_internal(socket, level, name,
  748. opt_value, null, 0);
  749. }
  750. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  751. private extern static void Shutdown_internal(IntPtr socket, SocketShutdown how);
  752. public void Shutdown(SocketShutdown how) {
  753. Shutdown_internal(socket, how);
  754. }
  755. private bool disposed = false;
  756. protected virtual void Dispose(bool explicitDisposing) {
  757. // Check to see if Dispose has already been called
  758. if(!this.disposed) {
  759. // If this is a call to Dispose,
  760. // dispose all managed resources.
  761. if(explicitDisposing) {
  762. // Free up stuff here
  763. }
  764. // Release unmanaged resources
  765. this.disposed=true;
  766. this.Close();
  767. }
  768. }
  769. public void Dispose() {
  770. Dispose(true);
  771. // Take yourself off the Finalization queue
  772. GC.SuppressFinalize(this);
  773. }
  774. ~Socket () {
  775. Dispose(false);
  776. }
  777. }
  778. }