| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294 |
- // System.Net.Sockets.Socket.cs
- //
- // Authors:
- // Phillip Pearson ([email protected])
- // Dick Porter <[email protected]>
- // Gonzalo Paniagua Javier ([email protected])
- // Sridhar Kulkarni ([email protected])
- // Brian Nickel ([email protected])
- //
- // Copyright (C) 2001, 2002 Phillip Pearson and Ximian, Inc.
- // http://www.myelin.co.nz
- // (c) 2004-2006 Novell, Inc. (http://www.novell.com)
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Net;
- using System.Collections;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Reflection;
- using System.IO;
- using System.Net.Configuration;
- using System.Text;
- #if NET_2_0
- using System.Collections.Generic;
- using System.Net.NetworkInformation;
- using System.Timers;
- #endif
- namespace System.Net.Sockets
- {
- public class Socket : IDisposable
- {
- enum SocketOperation {
- Accept,
- Connect,
- Receive,
- ReceiveFrom,
- Send,
- SendTo,
- UsedInManaged1,
- UsedInManaged2,
- UsedInProcess,
- UsedInConsole2,
- Disconnect,
- AcceptReceive
- }
- [StructLayout (LayoutKind.Sequential)]
- private sealed class SocketAsyncResult: IAsyncResult
- {
- /* Same structure in the runtime */
- public Socket Sock;
- public IntPtr handle;
- object state;
- AsyncCallback callback;
- WaitHandle waithandle;
- Exception delayedException;
- public EndPoint EndPoint; // Connect,ReceiveFrom,SendTo
- public byte [] Buffer; // Receive,ReceiveFrom,Send,SendTo
- public int Offset; // Receive,ReceiveFrom,Send,SendTo
- public int Size; // Receive,ReceiveFrom,Send,SendTo
- public SocketFlags SockFlags; // Receive,ReceiveFrom,Send,SendTo
- public Socket AcceptSocket; // AcceptReceive
- public IPAddress[] Addresses; // Connect
- public int Port; // Connect
- #if NET_2_0
- public IList<ArraySegment<byte>> Buffers; // Receive, Send
- #else
- public object Buffers; // Reserve this slot in older profiles
- #endif
- public bool ReuseSocket; // Disconnect
- // Return values
- Socket acc_socket;
- int total;
- bool completed_sync;
- bool completed;
- public bool blocking;
- internal int error;
- SocketOperation operation;
- public object ares;
- public SocketAsyncResult (Socket sock, object state, AsyncCallback callback, SocketOperation operation)
- {
- this.Sock = sock;
- this.blocking = sock.blocking;
- this.handle = sock.socket;
- this.state = state;
- this.callback = callback;
- this.operation = operation;
- SockFlags = SocketFlags.None;
- }
- public void CheckIfThrowDelayedException ()
- {
- if (delayedException != null) {
- throw delayedException;
- }
- if (error != 0)
- throw new SocketException (error);
- }
- void CompleteAllOnDispose (Queue queue)
- {
- object [] pending = queue.ToArray ();
- queue.Clear ();
- WaitCallback cb;
- for (int i = 0; i < pending.Length; i++) {
- SocketAsyncResult ares = (SocketAsyncResult) pending [i];
- cb = new WaitCallback (ares.CompleteDisposed);
- ThreadPool.QueueUserWorkItem (cb, null);
- }
- }
- void CompleteDisposed (object unused)
- {
- Complete ();
- }
- public void Complete ()
- {
- if (operation != SocketOperation.Receive && Sock.disposed)
- delayedException = new ObjectDisposedException (Sock.GetType ().ToString ());
- IsCompleted = true;
- Queue queue = null;
- if (operation == SocketOperation.Receive || operation == SocketOperation.ReceiveFrom) {
- queue = Sock.readQ;
- } else if (operation == SocketOperation.Send || operation == SocketOperation.SendTo) {
- queue = Sock.writeQ;
- }
- if (queue != null) {
- SocketAsyncCall sac = null;
- SocketAsyncResult req = null;
- lock (queue) {
- queue.Dequeue (); // remove ourselves
- if (queue.Count > 0) {
- req = (SocketAsyncResult) queue.Peek ();
- if (!Sock.disposed) {
- Worker worker = new Worker (req);
- sac = GetDelegate (worker, req.operation);
- } else {
- CompleteAllOnDispose (queue);
- }
- }
- }
- if (sac != null)
- sac.BeginInvoke (null, req);
- }
- if (callback != null)
- callback (this);
- }
- SocketAsyncCall GetDelegate (Worker worker, SocketOperation op)
- {
- switch (op) {
- case SocketOperation.Receive:
- return new SocketAsyncCall (worker.Receive);
- case SocketOperation.ReceiveFrom:
- return new SocketAsyncCall (worker.ReceiveFrom);
- case SocketOperation.Send:
- return new SocketAsyncCall (worker.Send);
- case SocketOperation.SendTo:
- return new SocketAsyncCall (worker.SendTo);
- default:
- return null; // never happens
- }
- }
- public void Complete (bool synch)
- {
- completed_sync = synch;
- Complete ();
- }
- public void Complete (int total)
- {
- this.total = total;
- Complete ();
- }
- public void Complete (Exception e, bool synch)
- {
- completed_sync = synch;
- delayedException = e;
- Complete ();
- }
- public void Complete (Exception e)
- {
- delayedException = e;
- Complete ();
- }
- public void Complete (Socket s)
- {
- acc_socket = s;
- Complete ();
- }
- public void Complete (Socket s, int total)
- {
- acc_socket = s;
- this.total = total;
- Complete ();
- }
- public object AsyncState {
- get {
- return state;
- }
- }
- public WaitHandle AsyncWaitHandle {
- get {
- lock (this) {
- if (waithandle == null)
- waithandle = new ManualResetEvent (completed);
- }
- return waithandle;
- }
- set {
- waithandle=value;
- }
- }
- public bool CompletedSynchronously {
- get {
- return(completed_sync);
- }
- }
- public bool IsCompleted {
- get {
- return(completed);
- }
- set {
- completed=value;
- lock (this) {
- if (waithandle != null && value) {
- ((ManualResetEvent) waithandle).Set ();
- }
- }
- }
- }
-
- public Socket Socket {
- get {
- return acc_socket;
- }
- }
- public int Total {
- get { return total; }
- set { total = value; }
- }
- public SocketError ErrorCode
- {
- get {
- #if NET_2_0
- SocketException ex = delayedException as SocketException;
-
- if (ex != null)
- return(ex.SocketErrorCode);
- if (error != 0)
- return((SocketError)error);
- #endif
- return(SocketError.Success);
- }
- }
- }
- private sealed class Worker
- {
- SocketAsyncResult result;
- public Worker (SocketAsyncResult ares)
- {
- this.result = ares;
- }
- public void Accept ()
- {
- Socket acc_socket = null;
- try {
- acc_socket = result.Sock.Accept ();
- } catch (Exception e) {
- result.Complete (e);
- return;
- }
- result.Complete (acc_socket);
- }
- /* only used in 2.0 profile and newer, but
- * leave in older profiles to keep interface
- * to runtime consistent
- */
- public void AcceptReceive ()
- {
- Socket acc_socket = null;
-
- try {
- if (result.AcceptSocket == null) {
- acc_socket = result.Sock.Accept ();
- } else {
- acc_socket = result.AcceptSocket;
- result.Sock.Accept (acc_socket);
- }
- } catch (Exception e) {
- result.Complete (e);
- return;
- }
- int total = 0;
- try {
- SocketError error;
-
- total = acc_socket.Receive_nochecks (result.Buffer,
- result.Offset,
- result.Size,
- result.SockFlags,
- out error);
- } catch (Exception e) {
- result.Complete (e);
- return;
- }
- result.Complete (acc_socket, total);
- }
- public void Connect ()
- {
- /* If result.EndPoint is non-null,
- * this is the standard one-address
- * connect attempt. Otherwise
- * Addresses must be non-null and
- * contain a list of addresses to try
- * to connect to; the first one to
- * succeed causes the rest of the list
- * to be ignored.
- */
- if (result.EndPoint != null) {
- try {
- if (!result.Sock.Blocking) {
- int success;
- result.Sock.Poll (-1, SelectMode.SelectWrite, out success);
- if (success == 0) {
- result.Sock.connected = true;
- } else {
- result.Complete (new SocketException (success));
- return;
- }
- } else {
- result.Sock.seed_endpoint = result.EndPoint;
- result.Sock.Connect (result.EndPoint);
- result.Sock.connected = true;
- }
- } catch (Exception e) {
- result.Complete (e);
- return;
- }
- result.Complete ();
- } else if (result.Addresses != null) {
- foreach(IPAddress address in result.Addresses) {
- IPEndPoint iep = new IPEndPoint (address, result.Port);
- SocketAddress serial = iep.Serialize ();
- int error = 0;
-
- Socket.Connect_internal (result.Sock.socket, serial, out error);
- if (error == 0) {
- result.Sock.connected = true;
- result.Sock.seed_endpoint = iep;
- result.Complete ();
- return;
- } else if (error != (int)SocketError.InProgress &&
- error != (int)SocketError.WouldBlock) {
- continue;
- }
- if (!result.Sock.Blocking) {
- int success;
- result.Sock.Poll (-1, SelectMode.SelectWrite, out success);
- if (success == 0) {
- result.Sock.connected = true;
- result.Sock.seed_endpoint = iep;
- result.Complete ();
- return;
- }
- }
- }
-
- result.Complete (new SocketException ((int)SocketError.InProgress));
- } else {
- result.Complete (new SocketException ((int)SocketError.AddressNotAvailable));
- }
- }
- /* Also only used in 2.0 profile and newer */
- public void Disconnect ()
- {
- #if NET_2_0
- try {
- result.Sock.Disconnect (result.ReuseSocket);
- } catch (Exception e) {
- result.Complete (e);
- return;
- }
- result.Complete ();
- #else
- result.Complete (new SocketException ((int)SocketError.Fault));
- #endif
- }
- public void Receive ()
- {
- // Actual recv() done in the runtime
- result.Complete ();
- }
- public void ReceiveFrom ()
- {
- int total = 0;
- try {
- total = result.Sock.ReceiveFrom_nochecks (result.Buffer,
- result.Offset,
- result.Size,
- result.SockFlags,
- ref result.EndPoint);
- } catch (Exception e) {
- result.Complete (e);
- return;
- }
- result.Complete (total);
- }
- int send_so_far;
- void UpdateSendValues (int last_sent)
- {
- if (result.error == 0) {
- send_so_far += last_sent;
- result.Offset += last_sent;
- result.Size -= last_sent;
- }
- }
- public void Send ()
- {
- // Actual send() done in the runtime
- if (result.error == 0) {
- UpdateSendValues (result.Total);
- if (result.Sock.disposed) {
- result.Complete ();
- return;
- }
- if (result.Size > 0) {
- SocketAsyncCall sac = new SocketAsyncCall (this.Send);
- sac.BeginInvoke (null, result);
- return; // Have to finish writing everything. See bug #74475.
- }
- result.Total = send_so_far;
- }
- result.Complete ();
- }
- public void SendTo ()
- {
- int total = 0;
- try {
- total = result.Sock.SendTo_nochecks (result.Buffer,
- result.Offset,
- result.Size,
- result.SockFlags,
- result.EndPoint);
- UpdateSendValues (total);
- if (result.Size > 0) {
- SocketAsyncCall sac = new SocketAsyncCall (this.SendTo);
- sac.BeginInvoke (null, result);
- return; // Have to finish writing everything. See bug #74475.
- }
- result.Total = send_so_far;
- } catch (Exception e) {
- result.Complete (e);
- return;
- }
- result.Complete ();
- }
- }
-
- static Socket () {
- // initialize ipv4Supported and ipv6Supported
- CheckProtocolSupport ();
- }
- /* the field "socket" is looked up by name by the runtime */
- private IntPtr socket;
- private AddressFamily address_family;
- private SocketType socket_type;
- private ProtocolType protocol_type;
- internal bool blocking=true;
- private Queue readQ = new Queue (2);
- private Queue writeQ = new Queue (2);
- delegate void SocketAsyncCall ();
- /*
- * These two fields are looked up by name by the runtime, don't change
- * their name without also updating the runtime code.
- */
- private static int ipv4Supported = -1, ipv6Supported = -1;
- /* When true, the socket was connected at the time of
- * the last IO operation
- */
- private bool connected;
- /* true if we called Close_internal */
- private bool closed;
- internal bool disposed;
-
- /*
- * This EndPoint is used when creating new endpoints. Because
- * there are many types of EndPoints possible,
- * seed_endpoint.Create(addr) is used for creating new ones.
- * As such, this value is set on Bind, SentTo, ReceiveFrom,
- * Connect, etc.
- */
- private EndPoint seed_endpoint = null;
- #if NET_2_0
- private bool isbound;
- private bool islistening;
- private bool useoverlappedIO;
- #endif
-
- static void AddSockets (ArrayList sockets, IList list, string name)
- {
- if (list != null) {
- foreach (Socket sock in list) {
- if (sock == null) // MS throws a NullRef
- throw new ArgumentNullException ("name", "Contains a null element");
- sockets.Add (sock);
- }
- }
- sockets.Add (null);
- }
- #if !TARGET_JVM
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Select_internal (ref Socket [] sockets,
- int microSeconds,
- out int error);
- #endif
- public static void Select (IList checkRead, IList checkWrite, IList checkError, int microSeconds)
- {
- ArrayList list = new ArrayList ();
- AddSockets (list, checkRead, "checkRead");
- AddSockets (list, checkWrite, "checkWrite");
- AddSockets (list, checkError, "checkError");
- if (list.Count == 3) {
- throw new ArgumentNullException ("checkRead, checkWrite, checkError",
- "All the lists are null or empty.");
- }
- int error;
- /*
- * The 'sockets' array contains: READ socket 0-n, null,
- * WRITE socket 0-n, null,
- * ERROR socket 0-n, null
- */
- Socket [] sockets = (Socket []) list.ToArray (typeof (Socket));
- Select_internal (ref sockets, microSeconds, out error);
- if (error != 0)
- throw new SocketException (error);
- if (checkRead != null)
- checkRead.Clear ();
- if (checkWrite != null)
- checkWrite.Clear ();
- if (checkError != null)
- checkError.Clear ();
- if (sockets == null)
- return;
- int mode = 0;
- int count = sockets.Length;
- IList currentList = checkRead;
- for (int i = 0; i < count; i++) {
- Socket sock = sockets [i];
- if (sock == null) { // separator
- currentList = (mode == 0) ? checkWrite : checkError;
- mode++;
- continue;
- }
- if (currentList != null) {
- if (currentList == checkWrite && !sock.connected) {
- if ((int) sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error) == 0) {
- sock.connected = true;
- }
- }
-
- currentList.Add (sock);
- }
- }
- }
- // private constructor used by Accept, which already
- // has a socket handle to use
- private Socket(AddressFamily family, SocketType type,
- ProtocolType proto, IntPtr sock)
- {
- address_family=family;
- socket_type=type;
- protocol_type=proto;
-
- socket=sock;
- connected=true;
- }
- #if !TARGET_JVM
- // Creates a new system socket, returning the handle
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern IntPtr Socket_internal(AddressFamily family,
- SocketType type,
- ProtocolType proto,
- out int error);
- #endif
- private void SocketDefaults ()
- {
- #if NET_2_0
- try {
- if (address_family == AddressFamily.InterNetwork /* Need to test IPv6 further ||
- address_family == AddressFamily.InterNetworkV6 */) {
- /* This is the default, but it
- * probably has nasty side
- * effects on Linux, as the
- * socket option is kludged by
- * turning on or off PMTU
- * discovery...
- */
- this.DontFragment = false;
- }
- //
- // Microsoft sets these to 8192, but we are going to keep them
- // both to the OS defaults as these have a big performance impact.
- // on WebClient performance.
- //
- //this.ReceiveBufferSize = 8192;
- //this.SendBufferSize = 8192;
- } catch (SocketException) {
- }
- #endif
- }
-
- public Socket(AddressFamily family, SocketType type, ProtocolType proto)
- {
- address_family=family;
- socket_type=type;
- protocol_type=proto;
-
- int error;
-
- socket = Socket_internal (family, type, proto, out error);
- if (error != 0)
- throw new SocketException (error);
- SocketDefaults ();
- }
- #if NET_2_0
- [MonoTODO]
- public Socket (SocketInformation socketInformation)
- {
- throw new NotImplementedException ("SocketInformation not figured out yet");
- // ifdef to avoid the warnings.
- #if false
- //address_family = socketInformation.address_family;
- //socket_type = socketInformation.socket_type;
- //protocol_type = socketInformation.protocol_type;
- address_family = AddressFamily.InterNetwork;
- socket_type = SocketType.Stream;
- protocol_type = ProtocolType.IP;
-
- int error;
- socket = Socket_internal (address_family, socket_type, protocol_type, out error);
- if (error != 0)
- throw new SocketException (error);
- SocketDefaults ();
- #endif
- }
- #endif
- public AddressFamily AddressFamily {
- get {
- return(address_family);
- }
- }
- #if !TARGET_JVM
- // Returns the amount of data waiting to be read on socket
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static int Available_internal(IntPtr socket, out int error);
- #endif
- public int Available {
- get {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int ret, error;
-
- ret = Available_internal(socket, out error);
- if (error != 0)
- throw new SocketException (error);
- return(ret);
- }
- }
- #if !TARGET_JVM
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Blocking_internal(IntPtr socket,
- bool block,
- out int error);
- #endif
- public bool Blocking {
- get {
- return(blocking);
- }
- set {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
-
- Blocking_internal (socket, value, out error);
- if (error != 0)
- throw new SocketException (error);
-
- blocking=value;
- }
- }
- public bool Connected {
- get {
- return(connected);
- }
- }
- #if NET_2_0
- public bool DontFragment {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- bool dontfragment;
-
- if (address_family == AddressFamily.InterNetwork) {
- dontfragment = (int)(GetSocketOption (SocketOptionLevel.IP, SocketOptionName.DontFragment)) != 0;
- } else if (address_family == AddressFamily.InterNetworkV6) {
- dontfragment = (int)(GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DontFragment)) != 0;
- } else {
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
-
- return(dontfragment);
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- if (address_family == AddressFamily.InterNetwork) {
- SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DontFragment, value?1:0);
- } else if (address_family == AddressFamily.InterNetworkV6) {
- SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DontFragment, value?1:0);
- } else {
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
- }
- }
- public bool EnableBroadcast {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- if (protocol_type != ProtocolType.Udp) {
- throw new SocketException ((int)SocketError.ProtocolOption);
- }
-
- return((int)(GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast)) != 0);
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- if (protocol_type != ProtocolType.Udp) {
- throw new SocketException ((int)SocketError.ProtocolOption);
- }
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, value?1:0);
- }
- }
-
- public bool ExclusiveAddressUse {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- return((int)(GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse)) != 0);
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- if (isbound) {
- throw new InvalidOperationException ("Bind has already been called for this socket");
- }
-
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, value?1:0);
- }
- }
-
- public bool IsBound {
- get {
- return(isbound);
- }
- }
-
- public LingerOption LingerState {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- return((LingerOption)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger));
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
-
- SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.Linger,
- value);
- }
- }
-
- public bool MulticastLoopback {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- /* Even though this option can be set
- * for TCP sockets on Linux, throw
- * this exception anyway to be
- * compatible (the MSDN docs say
- * "Setting this property on a
- * Transmission Control Protocol (TCP)
- * socket will have no effect." but
- * the MS runtime throws the
- * exception...)
- */
- if (protocol_type == ProtocolType.Tcp) {
- throw new SocketException ((int)SocketError.ProtocolOption);
- }
-
- bool multicastloopback;
-
- if (address_family == AddressFamily.InterNetwork) {
- multicastloopback = (int)(GetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback)) != 0;
- } else if (address_family == AddressFamily.InterNetworkV6) {
- multicastloopback = (int)(GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback)) != 0;
- } else {
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
-
- return(multicastloopback);
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- /* Even though this option can be set
- * for TCP sockets on Linux, throw
- * this exception anyway to be
- * compatible (the MSDN docs say
- * "Setting this property on a
- * Transmission Control Protocol (TCP)
- * socket will have no effect." but
- * the MS runtime throws the
- * exception...)
- */
- if (protocol_type == ProtocolType.Tcp) {
- throw new SocketException ((int)SocketError.ProtocolOption);
- }
-
- if (address_family == AddressFamily.InterNetwork) {
- SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, value?1:0);
- } else if (address_family == AddressFamily.InterNetworkV6) {
- SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback, value?1:0);
- } else {
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
- }
- }
-
- public static bool OSSupportsIPv6 {
- get {
- NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces ();
-
- foreach(NetworkInterface adapter in nics) {
- if (adapter.Supports (NetworkInterfaceComponent.IPv6) == true) {
- return(true);
- } else {
- continue;
- }
- }
- return(false);
- }
- }
-
- public int ReceiveBufferSize {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- return((int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer));
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- if (value < 0) {
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
- }
-
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, value);
- }
- }
- public int SendBufferSize {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- return((int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer));
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
- if (value < 0) {
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
- }
-
- SetSocketOption (SocketOptionLevel.Socket,
- SocketOptionName.SendBuffer,
- value);
- }
- }
-
- public short Ttl {
- get {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
-
- short ttl_val;
-
- if (address_family == AddressFamily.InterNetwork) {
- ttl_val = (short)((int)GetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive));
- } else if (address_family == AddressFamily.InterNetworkV6) {
- ttl_val = (short)((int)GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit));
- } else {
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
-
- return(ttl_val);
- }
- set {
- if (disposed && closed) {
- throw new ObjectDisposedException (GetType ().ToString ());
- }
-
- if (address_family == AddressFamily.InterNetwork) {
- SetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, value);
- } else if (address_family == AddressFamily.InterNetworkV6) {
- SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit, value);
- } else {
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
- }
- }
-
- [MonoTODO ("This doesn't do anything on Mono yet")]
- public bool UseOnlyOverlappedIO {
- get {
- return(useoverlappedIO);
- }
- set {
- useoverlappedIO = value;
- }
- }
- #endif
- public IntPtr Handle {
- get {
- return(socket);
- }
- }
- #if !TARGET_JVM
- // Returns the local endpoint details in addr and port
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static SocketAddress LocalEndPoint_internal(IntPtr socket, out int error);
- #endif
- // Wish: support non-IP endpoints.
- public EndPoint LocalEndPoint {
- get {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
-
- /*
- * If the seed EndPoint is null, Connect, Bind,
- * etc has not yet been called. MS returns null
- * in this case.
- */
- if (seed_endpoint == null)
- return null;
-
- SocketAddress sa;
- int error;
-
- sa=LocalEndPoint_internal(socket, out error);
- if (error != 0)
- throw new SocketException (error);
- return seed_endpoint.Create (sa);
- }
- }
- public ProtocolType ProtocolType {
- get {
- return(protocol_type);
- }
- }
- // Returns the remote endpoint details in addr and port
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static SocketAddress RemoteEndPoint_internal(IntPtr socket, out int error);
- public EndPoint RemoteEndPoint {
- get {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
-
- /*
- * If the seed EndPoint is null, Connect, Bind,
- * etc has not yet been called. MS returns null
- * in this case.
- */
- if (seed_endpoint == null)
- return null;
-
- SocketAddress sa;
- int error;
-
- sa=RemoteEndPoint_internal(socket, out error);
- if (error != 0)
- throw new SocketException (error);
- return seed_endpoint.Create (sa);
- }
- }
- public SocketType SocketType {
- get {
- return(socket_type);
- }
- }
- public static bool SupportsIPv4 {
- get {
- CheckProtocolSupport();
- return ipv4Supported == 1;
- }
- }
- #if NET_2_0
- [ObsoleteAttribute ("Use OSSupportsIPv6 instead")]
- #endif
- public static bool SupportsIPv6 {
- get {
- CheckProtocolSupport();
- return ipv6Supported == 1;
- }
- }
- #if NET_2_0
- public int SendTimeout {
- get {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- return (int)GetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.SendTimeout);
- }
- set {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (value < -1)
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than -1");
- /* According to the MSDN docs we
- * should adjust values between 1 and
- * 499 to 500, but the MS runtime
- * doesn't do this.
- */
- if (value == -1)
- value = 0;
- SetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.SendTimeout, value);
- }
- }
- public int ReceiveTimeout {
- get {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- return (int)GetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.ReceiveTimeout);
- }
- set {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (value < -1)
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than -1");
- if (value == -1) {
- value = 0;
- }
-
- SetSocketOption(
- SocketOptionLevel.Socket,
- SocketOptionName.ReceiveTimeout, value);
- }
- }
- public bool NoDelay {
- get {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (protocol_type == ProtocolType.Udp)
- throw new SocketException ((int)SocketError.ProtocolOption);
- return (int)(GetSocketOption (
- SocketOptionLevel.Tcp,
- SocketOptionName.NoDelay)) != 0;
- }
- set {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (protocol_type == ProtocolType.Udp)
- throw new SocketException ((int)SocketError.ProtocolOption);
- SetSocketOption (
- SocketOptionLevel.Tcp,
- SocketOptionName.NoDelay, value ? 1 : 0);
- }
- }
- #endif
- internal static void CheckProtocolSupport ()
- {
- if(ipv4Supported == -1) {
- try {
- Socket tmp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- tmp.Close();
- ipv4Supported = 1;
- } catch {
- ipv4Supported = 0;
- }
- }
- if (ipv6Supported == -1) {
- #if NET_2_0 && CONFIGURATION_DEP
- SettingsSection config;
- config = (SettingsSection) System.Configuration.ConfigurationManager.GetSection ("system.net/settings");
- if (config != null)
- ipv6Supported = config.Ipv6.Enabled ? -1 : 0;
- #else
- NetConfig config = (NetConfig)System.Configuration.ConfigurationSettings.GetConfig("system.net/settings");
- if (config != null)
- ipv6Supported = config.ipv6Enabled ? -1 : 0;
- #endif
- if (ipv6Supported != 0) {
- try {
- Socket tmp = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
- tmp.Close();
- ipv6Supported = 1;
- } catch { }
- }
- }
- }
- // Creates a new system socket, returning the handle
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static IntPtr Accept_internal(IntPtr sock, out int error);
- Thread blocking_thread;
- public Socket Accept() {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error = 0;
- IntPtr sock = (IntPtr) (-1);
- blocking_thread = Thread.CurrentThread;
- try {
- sock = Accept_internal(socket, out error);
- } catch (ThreadAbortException) {
- if (disposed) {
- Thread.ResetAbort ();
- error = (int) SocketError.Interrupted;
- }
- } finally {
- blocking_thread = null;
- }
- if (error != 0)
- throw new SocketException (error);
-
- Socket accepted = new Socket(this.AddressFamily, this.SocketType,
- this.ProtocolType, sock);
- accepted.seed_endpoint = this.seed_endpoint;
- accepted.Blocking = this.Blocking;
- return(accepted);
- }
- private void Accept (Socket acceptSocket)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
-
- int error = 0;
- IntPtr sock = (IntPtr)(-1);
- blocking_thread = Thread.CurrentThread;
-
- try {
- sock = Accept_internal (socket, out error);
- } catch (ThreadAbortException) {
- if (disposed) {
- Thread.ResetAbort ();
- error = (int)SocketError.Interrupted;
- }
- } finally {
- blocking_thread = null;
- }
-
- if (error != 0)
- throw new SocketException (error);
-
- acceptSocket.address_family = this.AddressFamily;
- acceptSocket.socket_type = this.SocketType;
- acceptSocket.protocol_type = this.ProtocolType;
- acceptSocket.socket = sock;
- acceptSocket.connected = true;
- acceptSocket.seed_endpoint = this.seed_endpoint;
- acceptSocket.Blocking = this.Blocking;
- /* FIXME: figure out what if anything else
- * needs to be reset
- */
- }
- public IAsyncResult BeginAccept(AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- #if NET_2_0
- /* FIXME: check the 1.1 docs for this too */
- if (!isbound || !islistening)
- throw new InvalidOperationException ();
- #endif
- SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Accept);
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.Accept);
- sac.BeginInvoke (null, req);
- return(req);
- }
- #if NET_2_0
- public IAsyncResult BeginAccept (int receiveSize,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (receiveSize < 0)
- throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
- SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.AcceptReceive);
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.AcceptReceive);
-
- req.Buffer = new byte[receiveSize];
- req.Offset = 0;
- req.Size = receiveSize;
- req.SockFlags = SocketFlags.None;
- sac.BeginInvoke (null, req);
- return(req);
- }
- public IAsyncResult BeginAccept (Socket acceptSocket,
- int receiveSize,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (receiveSize < 0)
- throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
- if (acceptSocket != null) {
- if (acceptSocket.disposed && acceptSocket.closed)
- throw new ObjectDisposedException (acceptSocket.GetType ().ToString ());
- if (acceptSocket.IsBound)
- throw new InvalidOperationException ();
- /* For some reason the MS runtime
- * barfs if the new socket is not TCP,
- * even though it's just about to blow
- * away all those parameters
- */
- if (acceptSocket.ProtocolType != ProtocolType.Tcp)
- throw new SocketException ((int)SocketError.InvalidArgument);
- }
-
- SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.AcceptReceive);
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.AcceptReceive);
-
- req.Buffer = new byte[receiveSize];
- req.Offset = 0;
- req.Size = receiveSize;
- req.SockFlags = SocketFlags.None;
- req.AcceptSocket = acceptSocket;
- sac.BeginInvoke (null, req);
- return(req);
- }
- #endif
- public IAsyncResult BeginConnect(EndPoint end_point,
- AsyncCallback callback,
- object state) {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (end_point == null)
- throw new ArgumentNullException ("end_point");
- SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Connect);
- req.EndPoint = end_point;
- // Bug #75154: Connect() should not succeed for .Any addresses.
- if (end_point is IPEndPoint) {
- IPEndPoint ep = (IPEndPoint) end_point;
- if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) {
- req.Complete (new SocketException ((int) SocketError.AddressNotAvailable), true);
- return req;
- }
- }
- int error = 0;
- if (!blocking) {
- SocketAddress serial = end_point.Serialize ();
- Connect_internal (socket, serial, out error);
- if (error == 0) {
- // succeeded synch
- connected = true;
- req.Complete (true);
- } else if (error != (int) SocketError.InProgress && error != (int) SocketError.WouldBlock) {
- // error synch
- connected = false;
- req.Complete (new SocketException (error), true);
- }
- }
- if (blocking || error == (int) SocketError.InProgress || error == (int) SocketError.WouldBlock) {
- // continue asynch
- connected = false;
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.Connect);
- sac.BeginInvoke (null, req);
- }
- return(req);
- }
- #if NET_2_0
- public IAsyncResult BeginConnect (IPAddress address, int port,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (address == null)
- throw new ArgumentNullException ("address");
- if (address.ToString ().Length == 0)
- throw new ArgumentException ("The length of the IP address is zero");
- if (islistening)
- throw new InvalidOperationException ();
- IPEndPoint iep = new IPEndPoint (address, port);
- return(BeginConnect (iep, callback, state));
- }
- public IAsyncResult BeginConnect (IPAddress[] addresses,
- int port,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (addresses == null)
- throw new ArgumentNullException ("addresses");
- if (this.AddressFamily != AddressFamily.InterNetwork &&
- this.AddressFamily != AddressFamily.InterNetworkV6)
- throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
- if (islistening)
- throw new InvalidOperationException ();
- SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Connect);
- req.Addresses = addresses;
- req.Port = port;
-
- connected = false;
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.Connect);
- sac.BeginInvoke (null, req);
-
- return(req);
- }
- public IAsyncResult BeginConnect (string host, int port,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (host == null)
- throw new ArgumentNullException ("host");
- if (address_family != AddressFamily.InterNetwork &&
- address_family != AddressFamily.InterNetworkV6)
- throw new NotSupportedException ("This method is valid only for sockets in the InterNetwork and InterNetworkV6 families");
- if (islistening)
- throw new InvalidOperationException ();
- IPHostEntry hostent = Dns.GetHostEntry (host);
- return (BeginConnect (hostent.AddressList, port, callback, state));
- }
- public IAsyncResult BeginDisconnect (bool reuseSocket,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Disconnect);
- req.ReuseSocket = reuseSocket;
-
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.Disconnect);
- sac.BeginInvoke (null, req);
-
- return(req);
- }
- #endif
-
- public IAsyncResult BeginReceive(byte[] buffer, int offset,
- int size,
- SocketFlags socket_flags,
- AsyncCallback callback,
- object state) {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (offset < 0 || offset > buffer.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buffer.Length)
- throw new ArgumentOutOfRangeException ("size");
- SocketAsyncResult req;
- lock (readQ) {
- req = new SocketAsyncResult (this, state, callback, SocketOperation.Receive);
- req.Buffer = buffer;
- req.Offset = offset;
- req.Size = size;
- req.SockFlags = socket_flags;
- readQ.Enqueue (req);
- if (readQ.Count == 1) {
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.Receive);
- sac.BeginInvoke (null, req);
- }
- }
- return req;
- }
- #if NET_2_0
- public IAsyncResult BeginReceive (byte[] buffer, int offset,
- int size, SocketFlags flags,
- out SocketError error,
- AsyncCallback callback,
- object state)
- {
- /* As far as I can tell from the docs and from
- * experimentation, a pointer to the
- * SocketError parameter is not supposed to be
- * saved for the async parts. And as we don't
- * set any socket errors in the setup code, we
- * just have to set it to Success.
- */
- error = SocketError.Success;
- return (BeginReceive (buffer, offset, size, flags, callback, state));
- }
- [CLSCompliant (false)]
- public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers,
- SocketFlags socketFlags,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffers == null)
- throw new ArgumentNullException ("buffers");
- SocketAsyncResult req;
- lock(readQ) {
- req = new SocketAsyncResult (this, state, callback, SocketOperation.Receive);
- req.Buffers = buffers;
- req.SockFlags = socketFlags;
- readQ.Enqueue (req);
- if (readQ.Count == 1) {
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.Receive);
- sac.BeginInvoke (null, req);
- }
- }
-
- return(req);
- }
-
- [CLSCompliant (false)]
- public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers,
- SocketFlags socketFlags,
- out SocketError errorCode,
- AsyncCallback callback,
- object state)
- {
- /* I assume the same SocketError semantics as
- * above
- */
- errorCode = SocketError.Success;
- return (BeginReceive (buffers, socketFlags, callback, state));
- }
- #endif
- public IAsyncResult BeginReceiveFrom(byte[] buffer, int offset,
- int size,
- SocketFlags socket_flags,
- ref EndPoint remote_end,
- AsyncCallback callback,
- object state) {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (offset < 0)
- throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
- if (size < 0)
- throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
- if (offset + size > buffer.Length)
- throw new ArgumentOutOfRangeException ("offset, size", "offset + size exceeds the buffer length");
- SocketAsyncResult req;
- lock (readQ) {
- req = new SocketAsyncResult (this, state, callback, SocketOperation.ReceiveFrom);
- req.Buffer = buffer;
- req.Offset = offset;
- req.Size = size;
- req.SockFlags = socket_flags;
- req.EndPoint = remote_end;
- readQ.Enqueue (req);
- if (readQ.Count == 1) {
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.ReceiveFrom);
- sac.BeginInvoke (null, req);
- }
- }
- return req;
- }
- #if NET_2_0
- [MonoTODO]
- public IAsyncResult BeginReceiveMessageFrom (
- byte[] buffer, int offset, int size,
- SocketFlags socketFlags, ref EndPoint remoteEP,
- AsyncCallback callback, object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (remoteEP == null)
- throw new ArgumentNullException ("remoteEP");
- if (offset < 0 || offset > buffer.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buffer.Length)
- throw new ArgumentOutOfRangeException ("size");
- throw new NotImplementedException ();
- }
- #endif
- public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socket_flags,
- AsyncCallback callback, object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (offset < 0)
- throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
- if (size < 0)
- throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
- if (offset + size > buffer.Length)
- throw new ArgumentOutOfRangeException ("offset, size", "offset + size exceeds the buffer length");
- #if NET_2_0
- /* TODO: Check this exception in the 1.1 profile */
- if (!connected)
- throw new SocketException ((int)SocketError.NotConnected);
- #endif
- SocketAsyncResult req;
- lock (writeQ) {
- req = new SocketAsyncResult (this, state, callback, SocketOperation.Send);
- req.Buffer = buffer;
- req.Offset = offset;
- req.Size = size;
- req.SockFlags = socket_flags;
- writeQ.Enqueue (req);
- if (writeQ.Count == 1) {
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.Send);
- sac.BeginInvoke (null, req);
- }
- }
- return req;
- }
- #if NET_2_0
- public IAsyncResult BeginSend (byte[] buffer, int offset,
- int size,
- SocketFlags socketFlags,
- out SocketError errorCode,
- AsyncCallback callback,
- object state)
- {
- if (!connected) {
- errorCode = SocketError.NotConnected;
- throw new SocketException ((int)errorCode);
- }
-
- errorCode = SocketError.Success;
-
- return (BeginSend (buffer, offset, size, socketFlags, callback,
- state));
- }
- public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers,
- SocketFlags socketFlags,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffers == null)
- throw new ArgumentNullException ("buffers");
- if (!connected)
- throw new SocketException ((int)SocketError.NotConnected);
- SocketAsyncResult req;
- lock (writeQ) {
- req = new SocketAsyncResult (this, state, callback, SocketOperation.Send);
- req.Buffers = buffers;
- req.SockFlags = socketFlags;
- writeQ.Enqueue (req);
- if (writeQ.Count == 1) {
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.Send);
- sac.BeginInvoke (null, req);
- }
- }
-
- return(req);
- }
- [CLSCompliant (false)]
- public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers,
- SocketFlags socketFlags,
- out SocketError errorCode,
- AsyncCallback callback,
- object state)
- {
- if (!connected) {
- errorCode = SocketError.NotConnected;
- throw new SocketException ((int)errorCode);
- }
-
- errorCode = SocketError.Success;
- return (BeginSend (buffers, socketFlags, callback, state));
- }
- [MonoTODO ("Not implemented")]
- public IAsyncResult BeginSendFile (string fileName,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (!connected)
- throw new NotSupportedException ();
- if (!File.Exists (fileName))
- throw new FileNotFoundException ();
- throw new NotImplementedException ();
- }
- [MonoTODO ("Not implemented")]
- public IAsyncResult BeginSendFile (string fileName,
- byte[] preBuffer,
- byte[] postBuffer,
- TransmitFileOptions flags,
- AsyncCallback callback,
- object state)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (!connected)
- throw new NotSupportedException ();
- if (!File.Exists (fileName))
- throw new FileNotFoundException ();
- throw new NotImplementedException ();
- }
- #endif
- public IAsyncResult BeginSendTo(byte[] buffer, int offset,
- int size,
- SocketFlags socket_flags,
- EndPoint remote_end,
- AsyncCallback callback,
- object state) {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (offset < 0)
- throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
- if (size < 0)
- throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
- if (offset + size > buffer.Length)
- throw new ArgumentOutOfRangeException ("offset, size", "offset + size exceeds the buffer length");
- SocketAsyncResult req;
- lock (writeQ) {
- req = new SocketAsyncResult (this, state, callback, SocketOperation.SendTo);
- req.Buffer = buffer;
- req.Offset = offset;
- req.Size = size;
- req.SockFlags = socket_flags;
- req.EndPoint = remote_end;
- writeQ.Enqueue (req);
- if (writeQ.Count == 1) {
- Worker worker = new Worker (req);
- SocketAsyncCall sac = new SocketAsyncCall (worker.SendTo);
- sac.BeginInvoke (null, req);
- }
- }
- return req;
- }
- // Creates a new system socket, returning the handle
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Bind_internal(IntPtr sock,
- SocketAddress sa,
- out int error);
- public void Bind(EndPoint local_end) {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (local_end == null)
- throw new ArgumentNullException("local_end");
-
- int error;
-
- Bind_internal(socket, local_end.Serialize(), out error);
- if (error != 0)
- throw new SocketException (error);
- #if NET_2_0
- if (error == 0)
- isbound = true;
- #endif
-
- seed_endpoint = local_end;
- }
- // Closes the socket
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Close_internal(IntPtr socket, out int error);
-
- public void Close()
- {
- ((IDisposable) this).Dispose ();
- }
- #if NET_2_0
- public void Close (int timeout)
- {
- System.Timers.Timer close_timer = new System.Timers.Timer ();
- close_timer.Elapsed += new ElapsedEventHandler (OnTimeoutClose);
- close_timer.Interval = timeout * 1000;
- close_timer.AutoReset = false;
- close_timer.Enabled = true;
- }
- private void OnTimeoutClose (object source, ElapsedEventArgs e)
- {
- this.Close ();
- }
- #endif
- // Connects to the remote address
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Connect_internal(IntPtr sock,
- SocketAddress sa,
- out int error);
- public void Connect(EndPoint remote_end) {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (remote_end == null)
- throw new ArgumentNullException("remote_end");
- if (remote_end is IPEndPoint) {
- IPEndPoint ep = (IPEndPoint) remote_end;
- if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
- throw new SocketException ((int) SocketError.AddressNotAvailable);
- }
- #if NET_2_0
- /* TODO: check this for the 1.1 profile too */
- if (islistening)
- throw new InvalidOperationException ();
- #endif
- SocketAddress serial = remote_end.Serialize ();
- int error = 0;
- blocking_thread = Thread.CurrentThread;
- try {
- Connect_internal (socket, serial, out error);
- } catch (ThreadAbortException) {
- if (disposed) {
- Thread.ResetAbort ();
- error = (int) SocketError.Interrupted;
- }
- } finally {
- blocking_thread = null;
- }
- if (error != 0)
- throw new SocketException (error);
- connected=true;
- #if NET_2_0
- isbound = true;
- #endif
-
- seed_endpoint = remote_end;
- }
- #if NET_2_0
- public void Connect (IPAddress address, int port)
- {
- Connect (new IPEndPoint (address, port));
- }
-
- public void Connect (IPAddress[] addresses, int port)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (addresses == null)
- throw new ArgumentNullException ("addresses");
- if (this.AddressFamily != AddressFamily.InterNetwork &&
- this.AddressFamily != AddressFamily.InterNetworkV6)
- throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
- if (islistening)
- throw new InvalidOperationException ();
- /* FIXME: do non-blocking sockets Poll here? */
- foreach (IPAddress address in addresses) {
- IPEndPoint iep = new IPEndPoint (address,
- port);
- SocketAddress serial = iep.Serialize ();
- int error = 0;
-
- Connect_internal (socket, serial, out error);
- if (error == 0) {
- connected = true;
- seed_endpoint = iep;
- return;
- } else if (error != (int)SocketError.InProgress &&
- error != (int)SocketError.WouldBlock) {
- continue;
- }
-
- if (!blocking) {
- Poll (-1, SelectMode.SelectWrite);
- int success = (int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
- if (success == 0) {
- connected = true;
- seed_endpoint = iep;
- return;
- }
- }
- }
- }
- public void Connect (string host, int port)
- {
- IPHostEntry hostent = Dns.GetHostEntry (host);
- Connect (hostent.AddressList, port);
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Disconnect_internal(IntPtr sock,
- bool reuse,
- out int error);
- /* According to the docs, the MS runtime will throw
- * PlatformNotSupportedException if the platform is
- * newer than w2k. We should be able to cope...
- */
- public void Disconnect (bool reuseSocket)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error = 0;
-
- Disconnect_internal (socket, reuseSocket, out error);
- if (error != 0) {
- if (error == 50) {
- /* ERROR_NOT_SUPPORTED */
- throw new PlatformNotSupportedException ();
- } else {
- throw new SocketException (error);
- }
- }
- connected = false;
-
- if (reuseSocket) {
- /* Do managed housekeeping here... */
- }
- }
- [MonoTODO ("Not implemented")]
- public SocketInformation DuplicateAndClose (int targetProcessId)
- {
- /* Need to serialize this socket into a
- * SocketInformation struct, but must study
- * the MS implementation harder to figure out
- * behaviour as documentation is lacking
- */
- throw new NotImplementedException ();
- }
- #endif
-
- public Socket EndAccept (IAsyncResult result)
- {
- int bytes;
- byte[] buffer;
-
- return(EndAccept (out buffer, out bytes, result));
- }
- #if NET_2_0
- public Socket EndAccept (out byte[] buffer,
- IAsyncResult asyncResult)
- {
- int bytes;
-
- return(EndAccept (out buffer, out bytes, asyncResult));
- }
- #endif
- #if NET_2_0
- public
- #else
- private
- #endif
- Socket EndAccept (out byte[] buffer, out int bytesTransferred,
- IAsyncResult asyncResult)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
-
- SocketAsyncResult req = asyncResult as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
- if (!asyncResult.IsCompleted)
- asyncResult.AsyncWaitHandle.WaitOne ();
- req.CheckIfThrowDelayedException ();
-
- buffer = req.Buffer;
- bytesTransferred = req.Total;
-
- return(req.Socket);
- }
- public void EndConnect (IAsyncResult result)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (result == null)
- throw new ArgumentNullException ("result");
- SocketAsyncResult req = result as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "result");
- if (!result.IsCompleted)
- result.AsyncWaitHandle.WaitOne();
- req.CheckIfThrowDelayedException();
- }
- #if NET_2_0
- public void EndDisconnect (IAsyncResult asyncResult)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
- SocketAsyncResult req = asyncResult as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
- if (!asyncResult.IsCompleted)
- asyncResult.AsyncWaitHandle.WaitOne ();
- req.CheckIfThrowDelayedException ();
- }
- #endif
- public int EndReceive (IAsyncResult result)
- {
- SocketError error;
-
- return (EndReceive (result, out error));
- }
- #if NET_2_0
- public
- #else
- private
- #endif
- int EndReceive (IAsyncResult asyncResult, out SocketError errorCode)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
- SocketAsyncResult req = asyncResult as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
- if (!asyncResult.IsCompleted)
- asyncResult.AsyncWaitHandle.WaitOne ();
- errorCode = req.ErrorCode;
- req.CheckIfThrowDelayedException ();
-
- return(req.Total);
- }
- public int EndReceiveFrom(IAsyncResult result, ref EndPoint end_point)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (result == null)
- throw new ArgumentNullException ("result");
- SocketAsyncResult req = result as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "result");
- if (!result.IsCompleted)
- result.AsyncWaitHandle.WaitOne();
- req.CheckIfThrowDelayedException();
- end_point = req.EndPoint;
- return req.Total;
- }
- #if NET_2_0
- [MonoTODO]
- public int EndReceiveMessageFrom (IAsyncResult asyncResult,
- ref SocketFlags socketFlags,
- ref EndPoint endPoint,
- out IPPacketInformation ipPacketInformation)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
- if (endPoint == null)
- throw new ArgumentNullException ("endPoint");
- SocketAsyncResult req = asyncResult as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
- throw new NotImplementedException ();
- }
- #endif
- public int EndSend (IAsyncResult result)
- {
- SocketError error;
-
- return(EndSend (result, out error));
- }
- #if NET_2_0
- public
- #else
- private
- #endif
- int EndSend (IAsyncResult asyncResult, out SocketError errorCode)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
-
- SocketAsyncResult req = asyncResult as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "result");
- if (!asyncResult.IsCompleted)
- asyncResult.AsyncWaitHandle.WaitOne ();
- errorCode = req.ErrorCode;
- req.CheckIfThrowDelayedException ();
-
- return(req.Total);
- }
- #if NET_2_0
- [MonoTODO]
- public void EndSendFile (IAsyncResult asyncResult)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
- SocketAsyncResult req = asyncResult as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
- throw new NotImplementedException ();
- }
- #endif
- public int EndSendTo (IAsyncResult result)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (result == null)
- throw new ArgumentNullException ("result");
- SocketAsyncResult req = result as SocketAsyncResult;
- if (req == null)
- throw new ArgumentException ("Invalid IAsyncResult", "result");
- if (!result.IsCompleted)
- result.AsyncWaitHandle.WaitOne();
- req.CheckIfThrowDelayedException();
- return req.Total;
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void GetSocketOption_obj_internal(IntPtr socket,
- SocketOptionLevel level, SocketOptionName name, out object obj_val,
- out int error);
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void GetSocketOption_arr_internal(IntPtr socket,
- SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val,
- out int error);
- public object GetSocketOption (SocketOptionLevel level, SocketOptionName name)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- object obj_val;
- int error;
-
- GetSocketOption_obj_internal(socket, level, name, out obj_val,
- out error);
- if (error != 0)
- throw new SocketException (error);
-
- if (name == SocketOptionName.Linger) {
- return((LingerOption)obj_val);
- } else if (name==SocketOptionName.AddMembership ||
- name==SocketOptionName.DropMembership) {
- return((MulticastOption)obj_val);
- } else if (obj_val is int) {
- return((int)obj_val);
- } else {
- return(obj_val);
- }
- }
- public void GetSocketOption (SocketOptionLevel level, SocketOptionName name, byte [] opt_value)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
-
- GetSocketOption_arr_internal(socket, level, name, ref opt_value,
- out error);
- if (error != 0)
- throw new SocketException (error);
- }
- public byte [] GetSocketOption (SocketOptionLevel level, SocketOptionName name, int length)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- byte[] byte_val=new byte[length];
- int error;
-
- GetSocketOption_arr_internal(socket, level, name, ref byte_val,
- out error);
- if (error != 0)
- throw new SocketException (error);
- return(byte_val);
- }
- // See Socket.IOControl, WSAIoctl documentation in MSDN. The
- // common options between UNIX and Winsock are FIONREAD,
- // FIONBIO and SIOCATMARK. Anything else will depend on the
- // system.
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- extern static int WSAIoctl (IntPtr sock, int ioctl_code, byte [] input,
- byte [] output, out int error);
- public int IOControl (int ioctl_code, byte [] in_value, byte [] out_value)
- {
- if (disposed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
- int result = WSAIoctl (socket, ioctl_code, in_value, out_value,
- out error);
- if (error != 0)
- throw new SocketException (error);
-
- if (result == -1)
- throw new InvalidOperationException ("Must use Blocking property instead.");
- return result;
- }
- #if NET_2_0
- [MonoTODO]
- public int IOControl (IOControlCode ioControlCode,
- byte[] optionInValue,
- byte[] optionOutValue)
- {
- /* Probably just needs to mirror the int
- * overload, but more investigation needed.
- */
- throw new NotImplementedException ();
- }
- #endif
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Listen_internal(IntPtr sock, int backlog,
- out int error);
- public void Listen (int backlog)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- #if NET_2_0
- /* TODO: check if this should be thrown in the
- * 1.1 profile too
- */
- if (!isbound)
- throw new SocketException ((int)SocketError.InvalidArgument);
- #endif
- int error;
-
- Listen_internal(socket, backlog, out error);
- if (error != 0)
- throw new SocketException (error);
- #if NET_2_0
- islistening = true;
- #endif
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- extern static bool Poll_internal (IntPtr socket, SelectMode mode, int timeout, out int error);
- public bool Poll (int time_us, SelectMode mode)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (mode != SelectMode.SelectRead &&
- mode != SelectMode.SelectWrite &&
- mode != SelectMode.SelectError)
- throw new NotSupportedException ("'mode' parameter is not valid.");
- int error;
- bool result = Poll_internal (socket, mode, time_us, out error);
- if (error != 0)
- throw new SocketException (error);
- if (mode == SelectMode.SelectWrite && result && !connected) {
- /* Update the connected state; for
- * non-blocking Connect()s this is
- * when we can find out that the
- * connect succeeded.
- */
- if ((int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error) == 0) {
- connected = true;
- }
- }
-
- return result;
- }
- /* This overload is needed as the async Connect method
- * also needs to check the socket error status, but
- * getsockopt(..., SO_ERROR) clears the error.
- */
- private bool Poll (int time_us, SelectMode mode, out int socket_error)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (mode != SelectMode.SelectRead &&
- mode != SelectMode.SelectWrite &&
- mode != SelectMode.SelectError)
- throw new NotSupportedException ("'mode' parameter is not valid.");
- int error;
- bool result = Poll_internal (socket, mode, time_us, out error);
- if (error != 0)
- throw new SocketException (error);
- socket_error = (int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
-
- if (mode == SelectMode.SelectWrite && result) {
- /* Update the connected state; for
- * non-blocking Connect()s this is
- * when we can find out that the
- * connect succeeded.
- */
- if (socket_error == 0) {
- connected = true;
- }
- }
-
- return result;
- }
-
- public int Receive (byte [] buf)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- SocketError error;
- int ret = Receive_nochecks (buf, 0, buf.Length, SocketFlags.None, out error);
-
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
- return ret;
- }
- public int Receive (byte [] buf, SocketFlags flags)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- SocketError error;
- int ret = Receive_nochecks (buf, 0, buf.Length, flags, out error);
-
- if (error != SocketError.Success) {
- if (error == SocketError.WouldBlock && blocking) // This might happen when ReceiveTimeout is set
- throw new SocketException ((int) error, "Operation timed out.");
- throw new SocketException ((int) error);
- }
- return ret;
- }
- public int Receive (byte [] buf, int size, SocketFlags flags)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- if (size < 0 || size > buf.Length)
- throw new ArgumentOutOfRangeException ("size");
- SocketError error;
- int ret = Receive_nochecks (buf, 0, size, flags, out error);
-
- if (error != SocketError.Success) {
- if (error == SocketError.WouldBlock && blocking) // This might happen when ReceiveTimeout is set
- throw new SocketException ((int) error, "Operation timed out.");
- throw new SocketException ((int) error);
- }
- return ret;
- }
- public int Receive (byte [] buf, int offset, int size, SocketFlags flags)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- if (offset < 0 || offset > buf.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buf.Length)
- throw new ArgumentOutOfRangeException ("size");
-
- SocketError error;
- int ret = Receive_nochecks (buf, offset, size, flags, out error);
-
- if (error != SocketError.Success) {
- if (error == SocketError.WouldBlock && blocking) // This might happen when ReceiveTimeout is set
- throw new SocketException ((int) error, "Operation timed out.");
- throw new SocketException ((int) error);
- }
- return ret;
- }
- #if NET_2_0
- public int Receive (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- if (offset < 0 || offset > buf.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buf.Length)
- throw new ArgumentOutOfRangeException ("size");
-
- return Receive_nochecks (buf, offset, size, flags, out error);
- }
- [MonoTODO]
- public int Receive (IList<ArraySegment<byte>> buffers)
- {
- /* For these generic IList overloads I need to
- * implement WSARecv in the runtime
- */
- throw new NotImplementedException ();
- }
-
- [CLSCompliant (false)]
- [MonoTODO]
- public int Receive (IList<ArraySegment<byte>> buffers,
- SocketFlags socketFlags)
- {
- throw new NotImplementedException ();
- }
- [CLSCompliant (false)]
- [MonoTODO]
- public int Receive (IList<ArraySegment<byte>> buffers,
- SocketFlags socketFlags,
- out SocketError errorCode)
- {
- throw new NotImplementedException ();
- }
- #endif
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static int Receive_internal(IntPtr sock,
- byte[] buffer,
- int offset,
- int count,
- SocketFlags flags,
- out int error);
- int Receive_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
- {
- int nativeError;
- int ret = Receive_internal (socket, buf, offset, size, flags, out nativeError);
- error = (SocketError) nativeError;
- if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress)
- connected = false;
- else
- connected = true;
-
- return ret;
- }
-
- public int ReceiveFrom (byte [] buf, ref EndPoint remote_end)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- if (remote_end == null)
- throw new ArgumentNullException ("remote_end");
- return ReceiveFrom_nochecks (buf, 0, buf.Length, SocketFlags.None, ref remote_end);
- }
- public int ReceiveFrom (byte [] buf, SocketFlags flags, ref EndPoint remote_end)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- if (remote_end == null)
- throw new ArgumentNullException ("remote_end");
- return ReceiveFrom_nochecks (buf, 0, buf.Length, flags, ref remote_end);
- }
- public int ReceiveFrom (byte [] buf, int size, SocketFlags flags,
- ref EndPoint remote_end)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- if (remote_end == null)
- throw new ArgumentNullException ("remote_end");
- if (size < 0 || size > buf.Length)
- throw new ArgumentOutOfRangeException ("size");
- return ReceiveFrom_nochecks (buf, 0, size, flags, ref remote_end);
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static int RecvFrom_internal(IntPtr sock,
- byte[] buffer,
- int offset,
- int count,
- SocketFlags flags,
- ref SocketAddress sockaddr,
- out int error);
- public int ReceiveFrom (byte [] buf, int offset, int size, SocketFlags flags,
- ref EndPoint remote_end)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- if (remote_end == null)
- throw new ArgumentNullException ("remote_end");
- if (offset < 0 || offset > buf.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buf.Length)
- throw new ArgumentOutOfRangeException ("size");
- return ReceiveFrom_nochecks (buf, offset, size, flags, ref remote_end);
- }
- int ReceiveFrom_nochecks (byte [] buf, int offset, int size, SocketFlags flags,
- ref EndPoint remote_end)
- {
- SocketAddress sockaddr = remote_end.Serialize();
- int cnt, error;
- cnt = RecvFrom_internal (socket, buf, offset, size, flags, ref sockaddr, out error);
- SocketError err = (SocketError) error;
- if (err != 0) {
- if (err != SocketError.WouldBlock && err != SocketError.InProgress)
- connected = false;
- else if (err == SocketError.WouldBlock && blocking) // This might happen when ReceiveTimeout is set
- throw new SocketException (error, "Operation timed out.");
- throw new SocketException (error);
- }
- connected = true;
- #if NET_2_0
- isbound = true;
- #endif
- // If sockaddr is null then we're a connection
- // oriented protocol and should ignore the
- // remote_end parameter (see MSDN
- // documentation for Socket.ReceiveFrom(...) )
-
- if ( sockaddr != null ) {
- // Stupidly, EndPoint.Create() is an
- // instance method
- remote_end = remote_end.Create (sockaddr);
- }
-
- seed_endpoint = remote_end;
-
- return cnt;
- }
- #if NET_2_0
- [MonoTODO ("Not implemented")]
- public int ReceiveMessageFrom (byte[] buffer, int offset,
- int size,
- ref SocketFlags socketFlags,
- ref EndPoint remoteEP,
- out IPPacketInformation ipPacketInformation)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (remoteEP == null)
- throw new ArgumentNullException ("remoteEP");
- if (offset < 0 || offset > buffer.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buffer.Length)
- throw new ArgumentOutOfRangeException ("size");
- /* FIXME: figure out how we get hold of the
- * IPPacketInformation
- */
- throw new NotImplementedException ();
- }
- #endif
- public int Send (byte [] buf)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- SocketError error;
- int ret = Send_nochecks (buf, 0, buf.Length, SocketFlags.None, out error);
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
- return ret;
- }
- public int Send (byte [] buf, SocketFlags flags)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- SocketError error;
- int ret = Send_nochecks (buf, 0, buf.Length, flags, out error);
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
- return ret;
- }
- public int Send (byte [] buf, int size, SocketFlags flags)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buf");
- if (size < 0 || size > buf.Length)
- throw new ArgumentOutOfRangeException ("size");
- SocketError error;
- int ret = Send_nochecks (buf, 0, size, flags, out error);
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
- return ret;
- }
- public int Send (byte [] buf, int offset, int size, SocketFlags flags)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buffer");
- if (offset < 0 || offset > buf.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buf.Length)
- throw new ArgumentOutOfRangeException ("size");
- SocketError error;
- int ret = Send_nochecks (buf, offset, size, flags, out error);
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
- return ret;
- }
- #if NET_2_0
- public int Send (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buf == null)
- throw new ArgumentNullException ("buffer");
- if (offset < 0 || offset > buf.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buf.Length)
- throw new ArgumentOutOfRangeException ("size");
- return Send_nochecks (buf, offset, size, flags, out error);
- }
- [MonoTODO]
- public int Send (IList<ArraySegment<byte>> buffers)
- {
- /* For these generic IList overloads I need to
- * implement WSASend in the runtime
- */
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public int Send (IList<ArraySegment<byte>> buffers,
- SocketFlags socketFlags)
- {
- throw new NotImplementedException ();
- }
- [CLSCompliant (false)]
- [MonoTODO]
- public int Send (IList<ArraySegment<byte>> buffers,
- SocketFlags socketFlags,
- out SocketError errorCode)
- {
- throw new NotImplementedException ();
- }
- #endif
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static int Send_internal(IntPtr sock,
- byte[] buf, int offset,
- int count,
- SocketFlags flags,
- out int error);
- int Send_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
- {
- if (size == 0) {
- error = SocketError.Success;
- return 0;
- }
- int nativeError;
- int ret = Send_internal (socket, buf, offset, size, flags, out nativeError);
- error = (SocketError)nativeError;
- if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress)
- connected = false;
- else
- connected = true;
- return ret;
- }
- #if NET_2_0
- [MonoTODO ("Not implemented")]
- public void SendFile (string fileName)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (!connected)
- throw new NotSupportedException ();
- if (!blocking)
- throw new InvalidOperationException ();
- if (!File.Exists (fileName))
- throw new FileNotFoundException ();
- /* FIXME: Implement TransmitFile */
- throw new NotImplementedException ();
- }
- [MonoTODO ("Not implemented")]
- public void SendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (!connected)
- throw new NotSupportedException ();
- if (!blocking)
- throw new InvalidOperationException ();
- if (!File.Exists (fileName))
- throw new FileNotFoundException ();
- /* FIXME: Implement TransmitFile */
- throw new NotImplementedException ();
- }
- #endif
- public int SendTo (byte [] buffer, EndPoint remote_end)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (remote_end == null)
- throw new ArgumentNullException ("remote_end");
- return SendTo_nochecks (buffer, 0, buffer.Length, SocketFlags.None, remote_end);
- }
- public int SendTo (byte [] buffer, SocketFlags flags, EndPoint remote_end)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (remote_end == null)
- throw new ArgumentNullException ("remote_end");
-
- return SendTo_nochecks (buffer, 0, buffer.Length, flags, remote_end);
- }
- public int SendTo (byte [] buffer, int size, SocketFlags flags, EndPoint remote_end)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (remote_end == null)
- throw new ArgumentNullException ("remote_end");
- if (size < 0 || size > buffer.Length)
- throw new ArgumentOutOfRangeException ("size");
- return SendTo_nochecks (buffer, 0, size, flags, remote_end);
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static int SendTo_internal(IntPtr sock,
- byte[] buffer,
- int offset,
- int count,
- SocketFlags flags,
- SocketAddress sa,
- out int error);
- public int SendTo (byte [] buffer, int offset, int size, SocketFlags flags,
- EndPoint remote_end)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (buffer == null)
- throw new ArgumentNullException ("buffer");
- if (remote_end == null)
- throw new ArgumentNullException("remote_end");
- if (offset < 0 || offset > buffer.Length)
- throw new ArgumentOutOfRangeException ("offset");
- if (size < 0 || offset + size > buffer.Length)
- throw new ArgumentOutOfRangeException ("size");
- return SendTo_nochecks (buffer, offset, size, flags, remote_end);
- }
- int SendTo_nochecks (byte [] buffer, int offset, int size, SocketFlags flags,
- EndPoint remote_end)
- {
- SocketAddress sockaddr = remote_end.Serialize ();
- int ret, error;
- ret = SendTo_internal (socket, buffer, offset, size, flags, sockaddr, out error);
- SocketError err = (SocketError) error;
- if (err != 0) {
- if (err != SocketError.WouldBlock && err != SocketError.InProgress)
- connected = false;
- throw new SocketException (error);
- }
- connected = true;
- #if NET_2_0
- isbound = true;
- #endif
-
- seed_endpoint = remote_end;
-
- return ret;
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void SetSocketOption_internal (IntPtr socket, SocketOptionLevel level,
- SocketOptionName name, object obj_val,
- byte [] byte_val, int int_val,
- out int error);
- public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, byte[] opt_value)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
-
- SetSocketOption_internal(socket, level, name, null,
- opt_value, 0, out error);
- if (error != 0)
- throw new SocketException (error);
- }
- public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, int opt_value)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
-
- SetSocketOption_internal(socket, level, name, null,
- null, opt_value, out error);
- if (error != 0)
- throw new SocketException (error);
- }
- public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, object opt_value)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- if (opt_value == null)
- throw new ArgumentNullException("opt_value");
-
- int error;
- /* From MS documentation on SetSocketOption: "For an
- * option with a Boolean data type, specify a nonzero
- * value to enable the option, and a zero value to
- * disable the option."
- * Booleans are only handled in 2.0
- */
- if (opt_value is System.Boolean) {
- #if NET_2_0
- bool bool_val = (bool) opt_value;
- int int_val = (bool_val) ? 1 : 0;
- SetSocketOption_internal (socket, level, name, null, null, int_val, out error);
- #else
- throw new ArgumentException ("Use an integer 1 (true) or 0 (false) instead of a boolean.", "opt_value");
- #endif
- } else {
- SetSocketOption_internal (socket, level, name, opt_value, null, 0, out error);
- }
- if (error != 0)
- throw new SocketException (error);
- }
- #if NET_2_0
- public void SetSocketOption (SocketOptionLevel level, SocketOptionName name, bool optionValue)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
- int int_val = (optionValue) ? 1 : 0;
- SetSocketOption_internal (socket, level, name, null, null, int_val, out error);
- if (error != 0)
- throw new SocketException (error);
- }
- #endif
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Shutdown_internal(IntPtr socket, SocketShutdown how, out int error);
-
- public void Shutdown (SocketShutdown how)
- {
- if (disposed && closed)
- throw new ObjectDisposedException (GetType ().ToString ());
- int error;
-
- Shutdown_internal (socket, how, out error);
- if (error != 0)
- throw new SocketException (error);
- }
- #if ONLY_1_1
- public override int GetHashCode ()
- {
- // LAMESPEC:
- // The socket is not suitable to serve as a hash code,
- // because it will change during its lifetime, but
- // this is how MS.NET 1.1 implemented this method.
- return (int) socket;
- }
- #endif
- protected virtual void Dispose (bool explicitDisposing)
- {
- if (disposed)
- return;
- disposed = true;
- connected = false;
- if ((int) socket != -1) {
- int error;
- closed = true;
- IntPtr x = socket;
- socket = (IntPtr) (-1);
- Close_internal (x, out error);
- if (blocking_thread != null) {
- blocking_thread.Abort ();
- blocking_thread = null;
- }
- if (error != 0)
- throw new SocketException (error);
- }
- }
- void IDisposable.Dispose ()
- {
- Dispose (true);
- GC.SuppressFinalize (this);
- }
-
- ~Socket () {
- Dispose (false);
- }
- }
- }
|