protected virtual void SendData(byte[] data, int len) { if (connected) { uint new_conv = 0; KCP.ikcp_decode32u(data, 0, ref new_conv); Log.Error("send_cov=" + new_conv + ",time=" + GetCurrent()); //查找有没有空闲的发送MySocketEventArgs,有就直接拿来用,没有就创建新的.So easy! MySocketEventArgs sendArgs = listArgs.Find(a => a.IsUsing == false); if (sendArgs == null) { sendArgs = initSendArgs(); } lock (sendArgs) //要锁定,不锁定让别的线程抢走了就不妙了. { sendArgs.IsUsing = true; sendArgs.SetBuffer(data, 0, len); } if (!clientSocket.SendToAsync(sendArgs)) { sendArgs.IsUsing = false; ProcessSend(sendArgs); } } else { throw new SocketException((Int32)SocketError.NotConnected); } }
/// <summary> /// 初始化收发参数 /// </summary> /// <param name="e"></param> private void initArgs(SocketAsyncEventArgs e) { //发送参数 initSendArgs(); MySocketEventArgs readArgs = this.receiveEventArgsPool.Rent <MySocketEventArgs>(); this.clientSocket.ReceiveFromAsync(readArgs); }
private MySocketEventArgs ConfigureSocketEventArgs() { var eventArg = new MySocketEventArgs(); eventArg.Completed += new EventHandler <SocketAsyncEventArgs>(this.IO_Completed); m_bufferManager.SetBuffer(eventArg); eventArg.RemoteEndPoint = localEndPoint; eventArg.ArgsTag = 0; return(eventArg); }
// This method is invoked when an asynchronous receive operation completes. // If the remote host closed the connection, then the socket is closed. // If data was received then the data is echoed back to the client. // private void ProcessReceive(SocketAsyncEventArgs e) { try { Log.Error("EEE=" + e.BytesTransferred + "," + e.SocketError + "," + e.Count); if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { MySocketEventArgs eventArgs = this.receiveEventArgsPool.Rent <MySocketEventArgs>(); this.clientSocket.ReceiveFromAsync(eventArgs); //读取数据 byte[] data = new byte[e.BytesTransferred]; Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred); if (!(data.Length < KCP.IKCP_OVERHEAD)) { lock (kcpManager) { uint new_conv = 0; KCP.ikcp_decode32u(data, 0, ref new_conv); if (kcpManager.ContainsKey(new_conv)) { kcpManager[new_conv].Input(data); } } } this.receiveEventArgsPool.Return((MySocketEventArgs)e); /* * if (!token.ReceiveFromAsync(e)) * this.ProcessReceive(e); */ } else { ProcessError(e); } } catch (Exception xe) { Console.WriteLine(xe.Message); } }
/// <summary> /// 初始化发送参数MySocketEventArgs /// </summary> /// <returns></returns> MySocketEventArgs initSendArgs() { MySocketEventArgs sendArg = new MySocketEventArgs(); sendArg.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); sendArg.UserToken = clientSocket; sendArg.RemoteEndPoint = hostEndPoint; sendArg.DisconnectReuseSocket = true; sendArg.IsUsing = false; Interlocked.Increment(ref tagCount); sendArg.ArgsTag = tagCount; lock (listArgs) { listArgs.Add(sendArg); } return(sendArg); }
void IO_Completed(object sender, SocketAsyncEventArgs e) { MySocketEventArgs mys = (MySocketEventArgs)e; Log.Error("111111111=" + e.LastOperation); // determine which type of operation just completed and call the associated handler switch (e.LastOperation) { case SocketAsyncOperation.ReceiveFrom: ProcessReceive(e); break; case SocketAsyncOperation.SendTo: mys.IsUsing = false; //数据发送已完成.状态设为False ProcessSend(e); break; default: throw new ArgumentException("The last operation completed on the socket was not a receive or send"); } }