Esempio n. 1
0
        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);
 }
Esempio n. 3
0
 /// <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);
         }
     }
 }
Esempio n. 4
0
 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);
 }