bool ProcessSend() { bool ret = false; try { if (this.clientSocket.Blocking) { Debug.Log("this.clientSocket.Blocking = true\n"); } bool error = this.clientSocket.Poll(0, SelectMode.SelectError); if (error) { Debug.Log("ProcessSend Poll SelectError\n"); this.CloseConnection(NET_ERROR_SEND_EXCEPTION); return(false); } ret = this.clientSocket.Poll(0, SelectMode.SelectWrite); if (ret) { //sendStream exist data if (this.sendBuffer.Position > this.sendOffset) { int bufsize = (int)(this.sendBuffer.Position - this.sendOffset); int n = this.clientSocket.Send(this.sendBuffer.GetBuffer(), this.sendOffset, bufsize, SocketFlags.None); if (n <= 0) { this.CloseConnection(NET_ERROR_ZERO_BYTE); return(false); } this.sendOffset += n; if (this.sendOffset >= this.sendBuffer.Position) { this.sendOffset = 0; this.sendBuffer.Position = 0; this.sendQueue.Dequeue();//remove message when send complete } } else { //fetch package from sendQueue if (this.sendQueue.Count > 0) { NetMessage message = this.sendQueue.Peek(); byte[] package = PackageHandler.PackMessage(message); this.sendBuffer.Write(package, 0, package.Length); } } } } catch (Exception e) { Debug.Log("ProcessSend exception:" + e.ToString() + "\n"); this.CloseConnection(NET_ERROR_SEND_EXCEPTION); return(false); } return(true); }
public byte[] GetResponse() { if (response != null) { if (PostResponser != null) { this.PostResponser.PostProcess(Response); } byte[] data = PackageHandler.PackMessage(response);//打包 response = null; return(data); } return(null); }
/// <summary> /// Sends data to the client. /// </summary> /// <param name="data">The data to send.</param> /// <param name="offset">The offset into the data.</param> /// <param name="count">The ammount of data to send.</param> public void SendData(SkillBridge.Message.NetMessage message) { byte[] data = PackageHandler.PackMessage(message); lock (this) { State state = eventArgs.UserToken as State; Socket socket = state.socket; if (socket.Connected) { //socket.Send(data, offset, count, SocketFlags.None); socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket); } } }
public byte[] GetResponse() { if (response != null) { if (this.Character != null && this.Character.StatusManager.HasStatus) { this.Character.StatusManager.ApplyResponse(Response); } byte[] data = PackageHandler.PackMessage(response); response = null; return(data); } return(null); }
/// <summary> /// A connection to our server, always listening asynchronously. /// </summary> /// <param name="socket">The Socket for the connection.</param> /// <param name="args">The SocketAsyncEventArgs for asyncronous recieves.</param> /// <param name="dataReceived">A callback invoked when data is recieved.</param> /// <param name="disconnectedCallback">A callback invoked on disconnection.</param> public NetConnection(Socket socket) { lock (this) { this.packageHandler = new PackageHandler <NetConnection <T> >(this); State state = new State() { socket = socket, //dataReceived = dataReceived, //disconnectedCallback = disconnectedCallback }; eventArgs = new SocketAsyncEventArgs(); eventArgs.AcceptSocket = socket; eventArgs.Completed += ReceivedCompleted; eventArgs.UserToken = state; eventArgs.SetBuffer(new byte[64 * 1024], 0, 64 * 1024); BeginReceive(eventArgs); this.session = new T(); } }