/// <summary> /// 开始接收 /// </summary> /// <param name="stream">Socket网络流</param> /// <param name="callback">回调函数</param> /// <param name="state">自定义状态</param> /// <returns>异步结果</returns> public IAsyncResult BeginReceive(Stream stream, AsyncCallback callback, object state) { //stream不能为null if (stream == null) { throw new ArgumentNullException("stream"); } //回调函数不能为null if (callback == null) { throw new ArgumentNullException("callback"); } //stream异常 if (!stream.CanRead) { throw new ArgumentException("stream不支持读取。"); } SocketAsyncResult result = new SocketAsyncResult(state); //初始化SocketHandlerState SocketHandlerState shs = new SocketHandlerState(); shs.Data = new byte[2]; shs.AsyncResult = result; shs.Stream = stream; shs.AsyncCallBack = callback; shs.Completed = true; //开始异步接收长度为2的头信息 //该头信息包含要接收的主要数据长度 stream.BeginRead(shs.Data, 0, 2, EndRead, shs); return(result); }
public void Dispose () { if (result != null) { result.Dispose (); result = null; args = null; } }
public void Dispose() { if (result != null) { result.Dispose(); result = null; args = null; } }
static void DispatcherCB(SocketAsyncResult sar) { /* SendPackets and ReceiveMessageFrom are not implemented yet */ switch (sar.operation) { case SocketOperation.Receive: case SocketOperation.ReceiveGeneric: case SocketOperation.RecvJustCallback: sar.Worker.Receive(); break; case SocketOperation.Send: case SocketOperation.SendGeneric: case SocketOperation.SendJustCallback: sar.Worker.Send(); break; case SocketOperation.ReceiveFrom: sar.Worker.ReceiveFrom(); break; case SocketOperation.SendTo: sar.Worker.SendTo(); break; case SocketOperation.Connect: sar.Worker.Connect(); break; case SocketOperation.Accept: sar.Worker.Accept(); break; case SocketOperation.AcceptReceive: sar.Worker.AcceptReceive(); break; case SocketOperation.Disconnect: sar.Worker.Disconnect(); break; // case SocketOperation.ReceiveMessageFrom // sar.Worker.ReceiveMessageFrom () // break; // case SocketOperation.SendPackets: // sar.Worker.SendPackets (); // break; default: throw new NotImplementedException(String.Format("Operation {0} is not implemented", sar.operation)); } }
static void DispatcherCB (SocketAsyncResult sar) { /* SendPackets and ReceiveMessageFrom are not implemented yet */ switch (sar.operation) { case SocketOperation.Receive: case SocketOperation.ReceiveGeneric: case SocketOperation.RecvJustCallback: sar.Worker.Receive (); break; case SocketOperation.Send: case SocketOperation.SendGeneric: case SocketOperation.SendJustCallback: sar.Worker.Send (); break; case SocketOperation.ReceiveFrom: sar.Worker.ReceiveFrom (); break; case SocketOperation.SendTo: sar.Worker.SendTo (); break; case SocketOperation.Connect: sar.Worker.Connect (); break; case SocketOperation.Accept: sar.Worker.Accept (); break; case SocketOperation.AcceptReceive: sar.Worker.AcceptReceive (); break; case SocketOperation.Disconnect: sar.Worker.Disconnect (); break; // case SocketOperation.ReceiveMessageFrom // sar.Worker.ReceiveMessageFrom () // break; // case SocketOperation.SendPackets: // sar.Worker.SendPackets (); // break; default: throw new NotImplementedException (String.Format ("Operation {0} is not implemented", sar.operation)); } }
/// <summary> /// Send data success /// </summary> /// <param name="result"></param> protected virtual void OnSendCompleted(SocketAsyncResult result) { }
internal bool TryDequeueOrReset(out SocketAsyncResult result) { lock(socket) { if (sendQueue.TryDequeue(out result)) return true; else Interlocked.Exchange(ref isInSending, 0); return false; } }
/// <summary> /// 开始发送 /// </summary> /// <param name="data">要发送的数据</param> /// <param name="offset">数据偏移</param> /// <param name="count">发送长度</param> /// <param name="stream">Socket网络流</param> /// <param name="callback">回调函数</param> /// <param name="state">自定义状态</param> /// <returns>异步结果</returns> public IAsyncResult BeginSend(byte[] data, int offset, int count, Stream stream, AsyncCallback callback, object state) { //data不能为null if (data == null) { throw new ArgumentNullException("data"); } //offset不能小于0和超过data长度 if (offset > data.Length || offset < 0) { throw new ArgumentOutOfRangeException("offset"); } //count不能大于65535 if (count <= 0 || count > data.Length - offset || count > ushort.MaxValue) { throw new ArgumentOutOfRangeException("count"); } //stream不能为null if (stream == null) { throw new ArgumentNullException("stream"); } //回调函数不能为null if (callback == null) { throw new ArgumentNullException("callback"); } //stream异常 if (!stream.CanWrite) { throw new ArgumentException("stream不支持写入。"); } SocketAsyncResult result = new SocketAsyncResult(state); //初始化SocketHandlerState SocketHandlerState shs = new SocketHandlerState(); shs.Data = data; shs.AsyncResult = result; shs.Stream = stream; shs.AsyncCallBack = callback; shs.DataLength = 0; //锁定SendQueue //避免多线程同时发送数据 lock (SendQueue) { //添加状态 SendQueue.Add(shs); //如果SendQueue数量大于1,则表示有数据尚未发送完成 if (SendQueue.Count > 1) { return(result); } } //获取数据长度 //ushort的最大值为65535 //转换为byte[]长度为2 var dataLength = BitConverter.GetBytes((ushort)data.Length); //向对方发送长度为2的头信息,表示接下来要发送的数据长度 stream.Write(dataLength, 0, dataLength.Length); //开始异步发送数据 stream.BeginWrite(shs.Data, 0, shs.Data.Length, EndWrite, shs).AsyncWaitHandle.WaitOne(); return(result); }
internal bool TryDequeue(out SocketAsyncResult result) { return sendQueue.TryDequeue(out result); }
public SocketAsyncWorker(SocketAsyncResult ares) { this.result = ares; }
public SocketAsyncWorker(SocketAsyncEventArgs args) { this.args = args; result = new SocketAsyncResult(); result.Worker = this; }
public void Connect() { if (result.EndPoint == null) { result.Complete(new SocketException((int)SocketError.AddressNotAvailable)); return; } SocketAsyncResult mconnect = result.AsyncState as SocketAsyncResult; bool is_mconnect = (mconnect != null && mconnect.Addresses != null); try { int error_code; EndPoint ep = result.EndPoint; error_code = (int)result.socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error); if (error_code == 0) { if (is_mconnect) { result = mconnect; } result.socket.seed_endpoint = ep; result.socket.is_connected = true; result.socket.is_bound = true; result.socket.connect_in_progress = false; result.error = 0; result.Complete(); if (is_mconnect) { result.DoMConnectCallback(); } return; } if (!is_mconnect) { result.socket.connect_in_progress = false; result.Complete(new SocketException(error_code)); return; } if (mconnect.CurrentAddress >= mconnect.Addresses.Length) { mconnect.Complete(new SocketException(error_code)); if (is_mconnect) { mconnect.DoMConnectCallback(); } return; } mconnect.socket.BeginMConnect(mconnect); } catch (Exception e) { result.socket.connect_in_progress = false; if (is_mconnect) { result = mconnect; } result.Complete(e); if (is_mconnect) { result.DoMConnectCallback(); } return; } }
public SocketAsyncWorker (SocketAsyncResult ares) { this.result = ares; }
public SocketAsyncWorker (SocketAsyncEventArgs args) { this.args = args; result = new SocketAsyncResult (); result.Worker = this; }
public void Connect () { if (result.EndPoint == null) { result.Complete (new SocketException ((int)SocketError.AddressNotAvailable)); return; } SocketAsyncResult mconnect = result.AsyncState as SocketAsyncResult; bool is_mconnect = (mconnect != null && mconnect.Addresses != null); try { int error_code; EndPoint ep = result.EndPoint; error_code = (int) result.socket.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error); if (error_code == 0) { if (is_mconnect) result = mconnect; result.socket.seed_endpoint = ep; result.socket.is_connected = true; result.socket.is_bound = true; result.socket.connect_in_progress = false; result.error = 0; result.Complete (); if (is_mconnect) result.DoMConnectCallback (); return; } if (!is_mconnect) { result.socket.connect_in_progress = false; result.Complete (new SocketException (error_code)); return; } if (mconnect.CurrentAddress >= mconnect.Addresses.Length) { mconnect.Complete (new SocketException (error_code)); if (is_mconnect) mconnect.DoMConnectCallback (); return; } mconnect.socket.BeginMConnect (mconnect); } catch (Exception e) { result.socket.connect_in_progress = false; if (is_mconnect) result = mconnect; result.Complete (e); if (is_mconnect) result.DoMConnectCallback (); return; } }