コード例 #1
0
        /// <summary>
        /// Sends command to server
        /// </summary>
        /// <param name="id">command id - use this to internally track when command finished sending</param>
        /// <param name="command">command</param>
        /// <param name="parameters">command parameters</param>
        public void SendCommand(int id, string command, params object[] parameters)
        {
            ServerConnectionEventArgs eventArgs = new ServerConnectionEventArgs(this, id, command, parameters);

            if (stream != null && stream.CanWrite)
            {
                try {
                    byte[] buffer = PrepareCommand(command, parameters);
                    stream.BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(CommandSentCallback), eventArgs);
                } catch {
                    eventArgs.Result = ServerConnectionEventArgs.ResultTypes.NetworkError;
                    if (CommandSent != null)
                    {
                        CommandSent(this, eventArgs);
                    }
                    Close();
                    return;
                }
            }
            else
            {
                eventArgs.Result = ServerConnectionEventArgs.ResultTypes.NetworkError;
                if (CommandSent != null)
                {
                    CommandSent(this, eventArgs);
                }
            }
        }
コード例 #2
0
 private void OnCommandRecieved(object sender, ServerConnectionEventArgs args)
 {
     if (sender == con)
     {
         DispatchServerCommand(args.Command, (string[])args.Parameters);
     }
 }
コード例 #3
0
        private static void CommandSentCallback(IAsyncResult res)
        {
            ServerConnectionEventArgs arg = (ServerConnectionEventArgs)res.AsyncState;

            arg.Result = ServerConnectionEventArgs.ResultTypes.Success;
            try {
                arg.ServerConnection.stream.EndWrite(res);
            } catch {
                arg.Result = ServerConnectionEventArgs.ResultTypes.NetworkError;
                arg.ServerConnection.Close();
                return;
            }
            if (arg.ServerConnection.CommandSent != null)
            {
                arg.ServerConnection.CommandSent(arg.ServerConnection, arg);
            }
        }
コード例 #4
0
		private void OnCommandRecieved(object sender, ServerConnectionEventArgs args)
		{
			if (sender == con) DispatchServerCommand(args.Command, (string[]) args.Parameters);
		}
コード例 #5
0
        private static void DataRecieveCallback(IAsyncResult res)
        {
            ServerConnection server = (ServerConnection)res.AsyncState;

            try {
                server.readPosition += server.stream.EndRead(res); // actual data read - this blocks
            } catch {
                // there was error while reading - stream is broken
                server.Close();
                return;
            }

            // check data for new line - isolating commands from it
            for (int i = server.readPosition - 1; i >= 0; --i)
            {
                if (server.readBuffer[i] == '\n')
                {
                    // new line found - convert to string and parse commands

                    String recData = Encoding.ASCII.GetString(server.readBuffer, 0, i + 1); // convert recieved bytes to string

                    // cycle through lines of data
                    foreach (string line in recData.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        // for each line = command do
                        string[] args = line.Split(' '); // split arguments

                        /*for (int j = 0; j < args.Length; ++j) { // replace \t with ' ' in every argument
                         * args[j] = args[j].Replace('\t', ' ');
                         * }*/

                        // prepare and send command recieved info
                        ServerConnectionEventArgs command = new ServerConnectionEventArgs();
                        command.ServerConnection = server;
                        command.RequestId        = server.commandNumber++;
                        command.Command          = args[0];
                        command.Result           = ServerConnectionEventArgs.ResultTypes.Success;
                        command.Parameters       = new string[args.Length - 1];
                        for (int j = 1; j < args.Length; ++j)
                        {
                            command.Parameters[j - 1] = args[j];
                        }
                        if (server.CommandRecieved != null)
                        {
                            server.CommandRecieved(server, command);
                        }
                    }

                    // copy remaining data (not ended by \n yet) to the beginning of buffer
                    for (int x = 0; x < server.readPosition - i - 1; ++x)
                    {
                        server.readBuffer[x] = server.readBuffer[x + i + 1];
                    }
                    server.readPosition = server.readPosition - i - 1;
                    break;
                }
            }

            // prepare to read more data
            int rembuf = server.readBuffer.Length - server.readPosition;

            if (rembuf <= 0)
            {
                // read buffer too small, increase it
                byte[] n = new byte[server.readBuffer.Length * 2];
                server.readBuffer.CopyTo(n, 0);
                server.readBuffer = n;
                rembuf            = server.readBuffer.Length - server.readPosition;
            }

            try {
                server.stream.BeginRead(server.readBuffer, server.readPosition, rembuf, new AsyncCallback(DataRecieveCallback), server);
            } catch {
                // there was error while reading - stream is broken
                server.Close();
                return;
            }
        }
コード例 #6
0
    private static void DataRecieveCallback(IAsyncResult res)
    {
      ServerConnection server = (ServerConnection)res.AsyncState;
      try {
        int cnt = server.stream.EndRead(res); // actual data read - this blocks
		server.readPosition += cnt;
		if (cnt == 0)
		{
			server.Close();
			return;
		}
      } catch {
        // there was error while reading - stream is broken
        server.Close();
        return;
      }

      // check data for new line - isolating commands from it
      for (int i = server.readPosition - 1; i >= 0; --i) {
        if (server.readBuffer[i] == '\n') {
          // new line found - convert to string and parse commands

          String recData = Encoding.ASCII.GetString(server.readBuffer, 0, i + 1); // convert recieved bytes to string

          // cycle through lines of data
          foreach (string line in recData.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries)) {
            // for each line = command do
            string[] args = line.Split(' '); // split arguments
            /*for (int j = 0; j < args.Length; ++j) { // replace \t with ' ' in every argument
              args[j] = args[j].Replace('\t', ' ');
            }*/

            // prepare and send command recieved info
            ServerConnectionEventArgs command = new ServerConnectionEventArgs();
            command.ServerConnection = server;
            command.RequestId = server.commandNumber++;
            command.Command = args[0];
            command.Result = ServerConnectionEventArgs.ResultTypes.Success;
            command.Parameters = new string[args.Length - 1];
            for (int j = 1; j < args.Length; ++j) command.Parameters[j - 1] = args[j];
            if (server.CommandRecieved != null) server.CommandRecieved(server, command);
          }

          // copy remaining data (not ended by \n yet) to the beginning of buffer
          for (int x = 0; x < server.readPosition - i - 1; ++x) server.readBuffer[x] = server.readBuffer[x + i + 1];
          server.readPosition = server.readPosition - i - 1;
          break;
        }
      }

      // prepare to read more data
      int rembuf = server.readBuffer.Length - server.readPosition;
      if (rembuf <= 0) {
        // read buffer too small, increase it
        byte[] n = new byte[server.readBuffer.Length*2];
        server.readBuffer.CopyTo(n, 0);
        server.readBuffer = n;
        rembuf = server.readBuffer.Length - server.readPosition;
      }

      try {
        server.stream.BeginRead(server.readBuffer, server.readPosition, rembuf, new AsyncCallback(DataRecieveCallback), server);
      } catch {
        // there was error while reading - stream is broken
        server.Close();
        return;
      }
    }
コード例 #7
0
 /// <summary>
 /// Sends command to server
 /// </summary>
 /// <param name="id">command id - use this to internally track when command finished sending</param>
 /// <param name="command">command</param>
 /// <param name="parameters">command parameters</param>
 public void SendCommand(int id, string command, params object[] parameters)
 {
   ServerConnectionEventArgs eventArgs = new ServerConnectionEventArgs(this, id, command, parameters);
   if (stream != null && stream.CanWrite) {
     try {
       byte[] buffer = PrepareCommand(command, parameters);
       stream.BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(CommandSentCallback), eventArgs);
     } catch {
       eventArgs.Result = ServerConnectionEventArgs.ResultTypes.NetworkError;
       if (CommandSent != null) CommandSent(this, eventArgs);
       Close();
       return;
     }
   } else {
     eventArgs.Result = ServerConnectionEventArgs.ResultTypes.NetworkError;
     if (CommandSent != null) CommandSent(this, eventArgs);
   }
 }